Skip to content

Commit 1484bbd

Browse files
committed
terrain generation fixes and cleanup
1 parent e6dd7c3 commit 1484bbd

File tree

4 files changed

+48
-221
lines changed

4 files changed

+48
-221
lines changed

Runtime/Utility/TerrainMeshUtility.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@ public static MeshData GenerateMesh(float[] heightMap, int width, int height, in
1010
float topLeftZ = (height - 1) / 2f;
1111

1212
int lodIncrement = Mathf.Max(1, levelOfDetail * 2);
13-
int vertsPerLine = (width - 1) / lodIncrement + 1;
13+
int vertsPerLine = (width-1) / lodIncrement + 1;
14+
int vertsPerColumn = (height-1) / lodIncrement + 1;
1415

15-
var meshData = new MeshData(vertsPerLine,vertsPerLine);
16+
var meshData = new MeshData(vertsPerLine,vertsPerColumn);
1617
int vertIndex = 0;
17-
1818
int triangleIndex = 0;
19-
for (int y = 0; y < height; y += lodIncrement)
19+
for (int i = 0; i < vertsPerColumn; i++)
2020
{
21-
for (int x = 0; x < width; x += lodIncrement)
21+
for (int j = 0; j < vertsPerLine; j++)
2222
{
23+
int x = j * lodIncrement;
24+
int y = i * lodIncrement;
25+
2326
meshData.vertices[vertIndex] = new Vector3(topLeftX + x,heightMap[y*width + x], topLeftZ - y);
2427
meshData.uv[vertIndex] = new Vector2(x/(float)width, y/(float)height);
25-
26-
if (x < width - 1 && y < height - 1)
28+
29+
if (j < (vertsPerLine-1) && i < (vertsPerColumn-1))
2730
{
2831
triangleIndex = meshData.AddTriangle(triangleIndex, vertIndex, vertIndex + vertsPerLine + 1, vertIndex + vertsPerLine);
2932
triangleIndex = meshData.AddTriangle(triangleIndex, vertIndex + vertsPerLine + 1, vertIndex, vertIndex + 1);
@@ -32,6 +35,20 @@ public static MeshData GenerateMesh(float[] heightMap, int width, int height, in
3235
vertIndex++;
3336
}
3437
}
38+
39+
/*for (int y = 0; y < height; y += lodIncrement)
40+
{
41+
for (int x = 0; x < width; x += lodIncrement)
42+
{
43+
if (x < width - 1 && y < height - 1)
44+
{
45+
//triangleIndex = meshData.AddTriangle(triangleIndex, vertIndex, vertIndex + vertsPerLine, vertIndex + 1);
46+
//triangleIndex = meshData.AddTriangle(triangleIndex, vertIndex + 1, vertIndex + vertsPerLine + 1, vertIndex + vertsPerLine);
47+
triangleIndex = meshData.AddTriangle(triangleIndex, vertIndex, vertIndex + vertsPerLine + 1, vertIndex + vertsPerLine);
48+
triangleIndex = meshData.AddTriangle(triangleIndex, vertIndex + vertsPerLine + 1, vertIndex, vertIndex + 1);
49+
}
50+
}
51+
}*/
3552

3653
return meshData;
3754
}
@@ -50,11 +67,12 @@ public MeshData(Vector3[] vertices, int[] triangles, Vector2[] uv)
5067
this.uv = uv;
5168
}
5269

53-
public MeshData(int width, int height)
70+
public MeshData(int vertsWide, int vertsHigh)
5471
{
55-
vertices = new Vector3[width * height];
56-
uv = new Vector2[width * height];
57-
triangles = new int[(width - 1) * (height - 1) * 6];
72+
vertices = new Vector3[vertsWide * vertsHigh];
73+
uv = new Vector2[vertsWide * vertsHigh];
74+
triangles = new int[(vertsWide-1) * (vertsHigh-1) * 6];
75+
5876
}
5977

6078
//Add the triangle and return the next index

Runtime/WorldMap/Layers/RegionMapLayerGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
public class RegionMapLayerGenerator : ScriptableObject
77
{
88
public int regionCount = 5;
9+
public float waterLevel = 0.2380952f;
910

10-
public void AddToWorld(WorldMapData mapData, float waterLevel)
11+
public void AddToWorld(WorldMapData mapData)
1112
{
1213
var rng = new System.Random(mapData.seed);
1314
var regionLayer = new RegionMapLayerData();

Runtime/WorldMap/Views/WorldMapTextureView.cs

Lines changed: 16 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@
66

77
public class WorldMapTextureView : MonoBehaviour, IWorldMapView
88
{
9-
[SerializeField]
10-
private Renderer _renderer = null;
11-
12-
[SerializeField]
13-
private DrawMode _drawMode = DrawMode.Color;
14-
15-
[SerializeField]
16-
private TerrainTable _terrainTable = null;
17-
18-
[SerializeField]
19-
private bool scaleRenderer = false;
9+
[SerializeField] private Renderer _renderer = null;
10+
[SerializeField] private bool mainTexture = false;
11+
[SerializeField] private string texturePropertyName = "_BaseMap";
12+
13+
[SerializeField] private TerrainTable _terrainTable = null;
2014

21-
[SerializeField] private FilterMode filterMode = FilterMode.Point;
15+
[SerializeField] private bool scaleRenderer = false;
2216

2317
[SerializeField] private bool gradiate = false;
2418

@@ -27,63 +21,12 @@ public class WorldMapTextureView : MonoBehaviour, IWorldMapView
2721
[SerializeField] private bool drawSpawnPoints = false;
2822
[SerializeField] private bool drawPoissonPoints = false;
2923

24+
[SerializeField,Range(0f,1f)] private float borderAlpha = 0.5f;
25+
[SerializeField,Range(0,1f)] private float regionFillAlpha = 0.2f;
3026
[SerializeField] private Color[] regionColors = new Color[0];
27+
[SerializeField] private FilterMode filterMode = FilterMode.Point;
3128

32-
public enum DrawMode
33-
{
34-
Greyscale,
35-
Color
36-
}
37-
38-
public bool applyRegions = false;
39-
public int regions = 50;
40-
public int seed = 100;
41-
public int minimumRegionSize = 50;
42-
public bool remapSmallRegions = false;
43-
44-
[Range(0f,1f)]
45-
public float borderAlpha = 0.5f;
46-
47-
[Range(0,1f)]
48-
public float regionFillAlpha = 0.2f;
49-
50-
public void DisplayMap(WorldMapData mapData)
51-
{
52-
if (applyRegions)
53-
{
54-
DisplayMapWithRegions(mapData);
55-
return;
56-
}
57-
58-
var heightMap = mapData.GetLayer<HeightMapLayerData>().heightMap;
59-
60-
if (_renderer == null)
61-
{
62-
return;
63-
}
64-
65-
Texture2D texture = null;
66-
if (_drawMode == DrawMode.Greyscale || _terrainTable == null)
67-
{
68-
texture = TextureUtility.GetHeightMap(heightMap,mapData.width,mapData.height);
69-
}
70-
else
71-
{
72-
var terrainMap = _terrainTable.GetTerrainMap(heightMap);
73-
var colorMap = TerrainTable.GetColorMap(heightMap,terrainMap,gradiate);
74-
texture = TextureUtility.GetColorMap(colorMap,mapData.width,mapData.height);
75-
}
76-
77-
texture.filterMode = filterMode;
78-
SetTexture(texture);
79-
80-
if (scaleRenderer)
81-
{
82-
_renderer.transform.localScale = new Vector3(mapData.width,mapData.height,1);
83-
}
84-
}
85-
86-
public void DisplayMapWithRegions(WorldMapData worldMapData)
29+
public void DisplayMap(WorldMapData worldMapData)
8730
{
8831
var heightMapLayer = worldMapData.GetLayer<HeightMapLayerData>();
8932
var regionMapLayer = worldMapData.GetLayer<RegionMapLayerData>();
@@ -98,7 +41,7 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
9841
var terrainMap = _terrainTable.GetTerrainMap(heightMap);
9942
var colorMap = TerrainTable.GetColorMap(heightMap, terrainMap, gradiate);
10043

101-
if (fillRegions)
44+
if (fillRegions && regionColors.Length > 0)
10245
{
10346
for (int i = 0; i < colorMap.Length; i++)
10447
{
@@ -107,13 +50,13 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
10750
{
10851
continue;
10952
}
110-
var regionColor = regionColors[regionIndex - 1];
53+
var regionColor = regionColors[(regionIndex - 1) % regionColors.Length];
11154
var alpha = Mathf.Clamp01(regionColor.a * regionFillAlpha);
11255
colorMap[i] = regionColor * alpha + (1 - alpha) * colorMap[i];
11356
}
11457
}
11558

116-
if (drawBorders)
59+
if (drawBorders && regionColors.Length > 0)
11760
{
11861
foreach (var region in regions)
11962
{
@@ -153,157 +96,25 @@ public void DisplayMapWithRegions(WorldMapData worldMapData)
15396
}
15497

15598
var texture = TextureUtility.GetColorMap(colorMap,width,height);
156-
SetTexture(texture);
157-
158-
if (scaleRenderer)
159-
{
160-
_renderer.transform.localScale = new Vector3(width,height,1);
161-
}
162-
}
163-
164-
public void DisplayMapWithRegions(float[,] noiseMap)
165-
{
166-
if (_renderer == null)
167-
{
168-
return;
169-
}
170-
171-
var width = noiseMap.GetLength(0);
172-
var height = noiseMap.GetLength(1);
173-
174-
//Generate Regions
175-
var voronoiData = Voronoi.Create(width, height, regions, seed);
176-
var regionColors = Voronoi.GenerateColors(voronoiData.regionCount);
177-
178-
Texture2D texture = null;
179-
if (_drawMode == DrawMode.Greyscale || _terrainTable == null)
180-
{
181-
texture = TextureUtility.GetHeightMap(noiseMap);
182-
}
183-
else
184-
{
185-
var terrainMap = _terrainTable.GetSingleDimensionTerrainMap(noiseMap);
186-
var colorMap = TerrainTable.GetColorMap(noiseMap, terrainMap, gradiate);
187-
188-
//We only want non-water regions
189-
var regionMask = new int[terrainMap.Length];
190-
for (var i = 0; i < terrainMap.Length; i++)
191-
{
192-
regionMask[i] = terrainMap[i].Elevation > 0 ? 1 : 0;
193-
}
194-
195-
if (remapSmallRegions)
196-
{
197-
for (int i = 0; i < 20; i++)
198-
{
199-
Debug.Log($"Combine Step {i}");
200-
if (CombineSmallRegions(regionMask, ref voronoiData.regionData, minimumRegionSize, width, height))
201-
{
202-
break;
203-
}
204-
}
205-
}
206-
207-
Debug.Log("Colorizing Map");
208-
for (int i = 0; i < colorMap.Length; i++)
209-
{
210-
if (regionMask[i] != 0)
211-
{
212-
var regionIndex = voronoiData.regionData[i];
213-
colorMap[i] *= regionColors[regionIndex];
214-
}
215-
}
216-
217-
texture = TextureUtility.GetColorMap(colorMap,width,height);
218-
}
219-
22099
texture.filterMode = filterMode;
221100
SetTexture(texture);
222-
101+
223102
if (scaleRenderer)
224103
{
225104
_renderer.transform.localScale = new Vector3(width,height,1);
226105
}
227106
}
228107

229-
[SerializeField] private bool URP = false;
230-
private static readonly int BaseMap = Shader.PropertyToID("_BaseMap");
231-
232108
private void SetTexture(Texture2D texture)
233109
{
234-
if (URP)
110+
if (!mainTexture)
235111
{
236-
//_renderer.sharedMaterial.SetTexture("BaseMap",texture);
237-
_renderer.sharedMaterial.SetTexture(BaseMap,texture);
112+
_renderer.sharedMaterial.SetTexture(texturePropertyName,texture);
238113
}
239114
else
240115
{
241116
_renderer.sharedMaterial.mainTexture = texture;
242117
}
243118
}
244119

245-
private static bool CombineSmallRegions(int[] regionMask, ref int[] regionData, int minSize, int width, int height)
246-
{
247-
//Get Region Sizes
248-
var regionSizes = Voronoi.GetRegionSizes(regionMask, regionData);
249-
var adjacencies = Voronoi.GetAdjacentRegions(regionMask, regionData, width, height);
250-
251-
/*foreach (var pair in regionSizes)
252-
{
253-
Debug.Log($"*{pair.Key} = {pair.Value} < {minSize}");
254-
}*/
255-
256-
var smallList = new List<int>();
257-
foreach (var pair in regionSizes)
258-
{
259-
if (pair.Value < minSize)
260-
{
261-
smallList.Add(pair.Key);
262-
}
263-
}
264-
265-
if (smallList.Count == 0)
266-
{
267-
return true;
268-
}
269-
270-
var msg = $"{smallList.Count} small regions";
271-
foreach (var idx in smallList)
272-
{
273-
msg += $" {idx},";
274-
}
275-
Debug.Log(msg);
276-
277-
int count = 0;
278-
//Remap Small Regions to larger adjacent ones
279-
foreach (var smallRegionIndex in smallList)
280-
{
281-
if (!adjacencies.TryGetValue(smallRegionIndex, out var adjacentList))
282-
{
283-
//Debug.Log($"{smallRegionIndex} has no adjacent regions");
284-
continue;
285-
}
286-
//Pick a region to remap to
287-
var adjacentIndex = adjacentList[0];
288-
//Debug.Log($"Remapping {smallRegionIndex} to {adjacentButNotSmallIndex}");
289-
int replaceCout = 0;
290-
for (int i = 0; i < regionData.Length; i++)
291-
{
292-
if (regionData[i] == smallRegionIndex)
293-
{
294-
regionData[i] = adjacentIndex;
295-
replaceCout++;
296-
}
297-
}
298-
299-
//Debug.Log($"Replaced {smallRegionIndex} ({replaceCout} pixels) with {adjacentIndex}");
300-
301-
count++;
302-
}
303-
Debug.Log($"Remapped {count}");
304-
305-
return false;
306-
}
307-
308-
309120
}

Runtime/WorldMap/WorldMapGenerator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ public class WorldMapGenerator : MonoBehaviour
1717

1818
[SerializeField]
1919
private PoissonMapLayerGenerator poissonMapGenerator;
20-
21-
[SerializeField]
22-
private float waterLevel = 0.2380952f;
2320

2421
[SerializeField] private int seed = 100;
2522

@@ -38,7 +35,7 @@ public void GenerateMap()
3835
height = mapHeight
3936
};
4037
heightMapGenerator.AddToWorld(worldData);
41-
regionMapGenerator.AddToWorld(worldData,waterLevel);
38+
regionMapGenerator.AddToWorld(worldData);
4239
poissonMapGenerator.AddToWorld(worldData);
4340
DisplayMap(worldData);
4441
}

0 commit comments

Comments
 (0)