-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathTerrainUtility.cs
351 lines (303 loc) · 14 KB
/
TerrainUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine.Bindings;
using UnityEngine.Scripting;
using UnityEngine.Scripting.APIUpdating;
namespace UnityEngine.TerrainUtils
{
internal enum TerrainMapStatusCode
{
OK = 0,
Overlapping = 1 << 0,
SizeMismatch = 1 << 2,
EdgeAlignmentMismatch = 1 << 3,
}
public readonly struct TerrainTileCoord
{
public readonly int tileX;
public readonly int tileZ;
public TerrainTileCoord(int tileX, int tileZ)
{
this.tileX = tileX;
this.tileZ = tileZ;
}
}
public class TerrainMap
{
public Terrain GetTerrain(int tileX, int tileZ)
{
Terrain result = null;
m_terrainTiles.TryGetValue(new TerrainTileCoord(tileX, tileZ), out result);
return result;
}
private struct QueueElement
{
public readonly int tileX;
public readonly int tileZ;
public readonly Terrain terrain;
public QueueElement(int tileX, int tileZ, Terrain terrain)
{
this.tileX = tileX;
this.tileZ = tileZ;
this.terrain = terrain;
}
}
static public TerrainMap CreateFromConnectedNeighbors(Terrain originTerrain, System.Predicate<Terrain> filter = null, bool fullValidation = true)
{
if (originTerrain == null)
return null;
if (originTerrain.terrainData == null)
return null;
TerrainMap terrainMap = new TerrainMap();
Queue<QueueElement> todoQueue = new Queue<QueueElement>();
todoQueue.Enqueue(new QueueElement(0, 0, originTerrain));
int maxTerrains = Terrain.activeTerrains.Length;
while (todoQueue.Count > 0)
{
QueueElement cur = todoQueue.Dequeue();
if ((filter == null) || filter(cur.terrain))
{
if (terrainMap.TryToAddTerrain(cur.tileX, cur.tileZ, cur.terrain))
{
// sanity check to stop bad neighbors causing infinite iteration
if (terrainMap.m_terrainTiles.Count > maxTerrains)
break;
if (cur.terrain.leftNeighbor != null)
todoQueue.Enqueue(new QueueElement(cur.tileX - 1, cur.tileZ, cur.terrain.leftNeighbor));
if (cur.terrain.bottomNeighbor != null)
todoQueue.Enqueue(new QueueElement(cur.tileX, cur.tileZ - 1, cur.terrain.bottomNeighbor));
if (cur.terrain.rightNeighbor != null)
todoQueue.Enqueue(new QueueElement(cur.tileX + 1, cur.tileZ, cur.terrain.rightNeighbor));
if (cur.terrain.topNeighbor != null)
todoQueue.Enqueue(new QueueElement(cur.tileX, cur.tileZ + 1, cur.terrain.topNeighbor));
}
}
}
// run validation to check alignment status
if (fullValidation)
terrainMap.Validate();
return terrainMap;
}
// create a terrain map of ALL terrains, by using only their placement to fit them to a grid
// the position and size of originTerrain defines the grid alignment and origin. if NULL, we use the first active terrain
static public TerrainMap CreateFromPlacement(Terrain originTerrain, System.Predicate<Terrain> filter = null, bool fullValidation = true)
{
if ((Terrain.activeTerrains == null) || (Terrain.activeTerrains.Length == 0) || (originTerrain == null))
return null;
if (originTerrain.terrainData == null)
return null;
int groupID = originTerrain.groupingID;
float gridOriginX = originTerrain.transform.position.x;
float gridOriginZ = originTerrain.transform.position.z;
float gridSizeX = originTerrain.terrainData.size.x;
float gridSizeZ = originTerrain.terrainData.size.z;
if (filter == null)
filter = (x => (x.groupingID == groupID));
return CreateFromPlacement(new Vector2(gridOriginX, gridOriginZ), new Vector2(gridSizeX, gridSizeZ), filter, fullValidation);
}
// create a terrain map of ALL terrains, by using only their placement to fit them to a grid
// the position and size of originTerrain defines the grid alignment and origin. if NULL, we use the first active terrain
static public TerrainMap CreateFromPlacement(Vector2 gridOrigin, Vector2 gridSize, System.Predicate<Terrain> filter = null, bool fullValidation = true)
{
if ((Terrain.activeTerrains == null) || (Terrain.activeTerrains.Length == 0))
return null;
TerrainMap terrainMap = new TerrainMap();
float gridScaleX = 1.0f / gridSize.x;
float gridScaleZ = 1.0f / gridSize.y;
// iterate all active terrains
foreach (Terrain terrain in Terrain.activeTerrains)
{
// some integration tests just create a terrain component without terrain data
if (terrain.terrainData == null)
continue;
if ((filter == null) || filter(terrain))
{
// convert position to a grid index, with proper rounding
Vector3 pos = terrain.transform.position;
int tileX = Mathf.RoundToInt((pos.x - gridOrigin.x) * gridScaleX);
int tileZ = Mathf.RoundToInt((pos.z - gridOrigin.y) * gridScaleZ);
// attempt to add the terrain at that grid position
terrainMap.TryToAddTerrain(tileX, tileZ, terrain);
}
}
// run validation to check alignment status
if (fullValidation)
terrainMap.Validate();
return (terrainMap.m_terrainTiles.Count > 0) ? terrainMap : null;
}
Vector3 m_patchSize; // size of the first terrain, used for consistency checks
private TerrainMapStatusCode m_errorCode;
private Dictionary<TerrainTileCoord, Terrain> m_terrainTiles;
public Dictionary<TerrainTileCoord, Terrain> terrainTiles => m_terrainTiles;
public TerrainMap()
{
m_errorCode = TerrainMapStatusCode.OK;
m_terrainTiles = new Dictionary<TerrainTileCoord, Terrain>();
}
void AddTerrainInternal(int x, int z, Terrain terrain)
{
if (m_terrainTiles.Count == 0)
m_patchSize = terrain.terrainData.size;
else
{
// check consistency with existing terrains
if (terrain.terrainData.size != m_patchSize)
{
// ERROR - terrain is not the same size as other terrains
m_errorCode |= TerrainMapStatusCode.SizeMismatch;
}
}
m_terrainTiles.Add(new TerrainTileCoord(x, z), terrain);
}
// attempt to place the specified terrain tile at the specified (x,z) position, with consistency checks
bool TryToAddTerrain(int tileX, int tileZ, Terrain terrain)
{
bool added = false;
if (terrain != null)
{
Terrain existing = GetTerrain(tileX, tileZ);
if (existing != null)
{
// already a terrain in the location -- check it is the same tile
if (existing != terrain)
{
// ERROR - multiple different terrains at the same coordinate!
m_errorCode |= TerrainMapStatusCode.Overlapping;
}
}
else
{
// add terrain to the terrain map
AddTerrainInternal(tileX, tileZ, terrain);
added = true;
}
}
return added;
}
void ValidateTerrain(int tileX, int tileZ)
{
Terrain terrain = GetTerrain(tileX, tileZ);
if (terrain != null)
{
// grab neighbors (according to grid)
Terrain left = GetTerrain(tileX - 1, tileZ);
Terrain right = GetTerrain(tileX + 1, tileZ);
Terrain top = GetTerrain(tileX, tileZ + 1);
Terrain bottom = GetTerrain(tileX, tileZ - 1);
// check edge alignment
{
if (left)
{
if (!Mathf.Approximately(terrain.transform.position.x, left.transform.position.x + left.terrainData.size.x) ||
!Mathf.Approximately(terrain.transform.position.z, left.transform.position.z))
{
// unaligned edge, tile doesn't match expected location
m_errorCode |= TerrainMapStatusCode.EdgeAlignmentMismatch;
}
}
if (right)
{
if (!Mathf.Approximately(terrain.transform.position.x + terrain.terrainData.size.x, right.transform.position.x) ||
!Mathf.Approximately(terrain.transform.position.z, right.transform.position.z))
{
// unaligned edge, tile doesn't match expected location
m_errorCode |= TerrainMapStatusCode.EdgeAlignmentMismatch;
}
}
if (top)
{
if (!Mathf.Approximately(terrain.transform.position.x, top.transform.position.x) ||
!Mathf.Approximately(terrain.transform.position.z + terrain.terrainData.size.z, top.transform.position.z))
{
// unaligned edge, tile doesn't match expected location
m_errorCode |= TerrainMapStatusCode.EdgeAlignmentMismatch;
}
}
if (bottom)
{
if (!Mathf.Approximately(terrain.transform.position.x, bottom.transform.position.x) ||
!Mathf.Approximately(terrain.transform.position.z, bottom.transform.position.z + bottom.terrainData.size.z))
{
// unaligned edge, tile doesn't match expected location
m_errorCode |= TerrainMapStatusCode.EdgeAlignmentMismatch;
}
}
}
}
}
// perform all validation checks on the terrain map
TerrainMapStatusCode Validate()
{
// iterate all tiles and validate them
foreach (TerrainTileCoord coord in m_terrainTiles.Keys)
{
ValidateTerrain(coord.tileX, coord.tileZ);
}
return m_errorCode;
}
}
[MovedFrom("UnityEngine.Experimental.TerrainAPI")]
public static class TerrainUtility
{
internal static bool ValidTerrainsExist() { return Terrain.activeTerrains != null && Terrain.activeTerrains.Length > 0; }
internal static void ClearConnectivity()
{
foreach (Terrain terrain in Terrain.activeTerrains)
{
//it should clear only allowAutoConnect flag is true.
//Otherwise, the setting value will be cleared on first render frame if allowAutoConnect is set to false.
//case 1241302
if (terrain.allowAutoConnect)
terrain.SetNeighbors(null, null, null, null);
}
}
internal static Dictionary<int, TerrainMap> CollectTerrains(bool onlyAutoConnectedTerrains = true)
{
if (!ValidTerrainsExist())
return null;
// Collect by groups
Dictionary<int, TerrainMap> groups = new Dictionary<int, TerrainMap>();
foreach (Terrain t in Terrain.activeTerrains)
{
if (onlyAutoConnectedTerrains && !t.allowAutoConnect)
continue;
if (!groups.ContainsKey(t.groupingID))
{
TerrainMap map = TerrainMap.CreateFromPlacement(t, (x => (x.groupingID == t.groupingID) && !(onlyAutoConnectedTerrains && !x.allowAutoConnect)));
if (map != null)
groups.Add(t.groupingID, map);
}
}
return (groups.Count != 0) ? groups : null;
}
[RequiredByNativeCode]
public static void AutoConnect()
{
if (!ValidTerrainsExist())
return;
ClearConnectivity();
Dictionary<int, TerrainMap> terrainGroups = CollectTerrains();
if (terrainGroups == null)
return;
foreach (var group in terrainGroups)
{
TerrainMap terrains = group.Value;
foreach (var tile in terrains.terrainTiles)
{
TerrainTileCoord coords = tile.Key;
Terrain center = terrains.GetTerrain(coords.tileX, coords.tileZ);
Terrain left = terrains.GetTerrain(coords.tileX - 1, coords.tileZ);
Terrain right = terrains.GetTerrain(coords.tileX + 1, coords.tileZ);
Terrain top = terrains.GetTerrain(coords.tileX, coords.tileZ + 1);
Terrain bottom = terrains.GetTerrain(coords.tileX, coords.tileZ - 1);
center.SetNeighbors(left, top, right, bottom);
}
}
}
}
}