| 18 | #include "FlaxEngine.Gen.h" |
| 19 | |
| 20 | bool TerrainTools::TryGetPatchCoordToAdd(Terrain* terrain, const Ray& ray, Int2& result) |
| 21 | { |
| 22 | CHECK_RETURN(terrain, true); |
| 23 | result = Int2::Zero; |
| 24 | const float patchSize = terrain->GetChunkSize() * TERRAIN_UNITS_PER_VERTEX * Terrain::ChunksCountEdge; |
| 25 | |
| 26 | // Try to pick any of the patch edges |
| 27 | for (int32 patchIndex = 0; patchIndex < terrain->GetPatchesCount(); patchIndex++) |
| 28 | { |
| 29 | const auto patch = terrain->GetPatch(patchIndex); |
| 30 | const auto x = patch->GetX(); |
| 31 | const auto z = patch->GetZ(); |
| 32 | const auto bounds = patch->GetBounds(); |
| 33 | |
| 34 | // TODO: use chunk neighbors to reduce algorithm complexity |
| 35 | |
| 36 | #define CHECK_EDGE(dx, dz) \ |
| 37 | if (terrain->GetPatch(x + dx, z + dz) == nullptr) \ |
| 38 | { \ |
| 39 | if (bounds.MakeOffsetted(Vector3(patchSize * dx, 0, patchSize * dz)).Intersects(ray)) \ |
| 40 | { \ |
| 41 | result = Int2(x + dx, z + dz); \ |
| 42 | return true; \ |
| 43 | } \ |
| 44 | } |
| 45 | |
| 46 | CHECK_EDGE(1, 0); |
| 47 | CHECK_EDGE(-1, 0); |
| 48 | CHECK_EDGE(0, 1); |
| 49 | CHECK_EDGE(0, -1); |
| 50 | CHECK_EDGE(1, 1); |
| 51 | CHECK_EDGE(1, -1); |
| 52 | CHECK_EDGE(1, 1); |
| 53 | CHECK_EDGE(-1, -1); |
| 54 | CHECK_EDGE(-1, 1); |
| 55 | |
| 56 | #undef CHECK_EDGE |
| 57 | } |
| 58 | |
| 59 | // Use the default patch if none added |
| 60 | if (terrain->GetPatchesCount() == 0) |
| 61 | { |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | struct TextureDataResult |
| 69 | { |