Skip to content

Commit 36810fe

Browse files
committed
Script and demo updates
1 parent ab2c2d8 commit 36810fe

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

Demo/Demo-URP.unitypackage

314 KB
Binary file not shown.

Runtime/Utility/TerrainMeshUtility.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEditor.SceneManagement;
3+
using UnityEngine;
24

35
namespace Gameframe.Procgen
46
{
@@ -7,7 +9,7 @@ public static class TerrainMeshUtility
79
{
810
private const int mapChunkSize = 241;
911

10-
public static MeshData GenerateMesh(float[] heightMap, int width, int height, int levelOfDetail)
12+
public static MeshData GenerateMesh(float[] heightMap, int width, int height, int levelOfDetail, Func<float,float> stepFunction, Func<float,Color> colorFunction = null)
1113
{
1214
float topLeftX = (width - 1) / -2f;
1315
float topLeftZ = (height - 1) / 2f;
@@ -16,7 +18,7 @@ public static MeshData GenerateMesh(float[] heightMap, int width, int height, in
1618
int vertsPerLine = (width - 1) / lodIncrement + 1;
1719
int vertsPerColumn = (height - 1) / lodIncrement + 1;
1820

19-
var meshData = new MeshData(vertsPerLine, vertsPerColumn);
21+
var meshData = new MeshData(vertsPerLine, vertsPerColumn, colorFunction != null);
2022
int vertIndex = 0;
2123
int triangleIndex = 0;
2224
for (int i = 0; i < vertsPerColumn; i++)
@@ -26,9 +28,14 @@ public static MeshData GenerateMesh(float[] heightMap, int width, int height, in
2628
int x = j * lodIncrement;
2729
int y = i * lodIncrement;
2830

29-
meshData.vertices[vertIndex] = new Vector3(topLeftX + x, heightMap[y * width + x], topLeftZ - y);
31+
meshData.vertices[vertIndex] = new Vector3(topLeftX + x, stepFunction.Invoke(heightMap[y * width + x]), topLeftZ - y);
3032
meshData.uv[vertIndex] = new Vector2(x / (float) width, y / (float) height);
3133

34+
if (colorFunction != null)
35+
{
36+
meshData.colors[vertIndex] = colorFunction.Invoke(heightMap[y * width + x]);
37+
}//*/
38+
3239
if (j < (vertsPerLine - 1) && i < (vertsPerColumn - 1))
3340
{
3441
triangleIndex = meshData.AddTriangle(triangleIndex, vertIndex, vertIndex + vertsPerLine + 1,
@@ -39,30 +46,47 @@ public static MeshData GenerateMesh(float[] heightMap, int width, int height, in
3946
vertIndex++;
4047
}
4148
}
42-
49+
4350
return meshData;
4451
}
52+
53+
public static MeshData GenerateMesh(float[] heightMap, int width, int height, float heightScale, int levelOfDetail)
54+
{
55+
return GenerateMesh(heightMap, width, height, levelOfDetail, x => heightScale * x);
56+
}
4557
}
4658

4759
public class MeshData
4860
{
4961
public readonly Vector3[] vertices;
5062
public readonly int[] triangles;
5163
public readonly Vector2[] uv;
64+
public readonly Color[] colors;
5265

5366
public MeshData(Vector3[] vertices, int[] triangles, Vector2[] uv)
5467
{
5568
this.vertices = vertices;
5669
this.triangles = triangles;
5770
this.uv = uv;
5871
}
72+
73+
public MeshData(Vector3[] vertices, int[] triangles, Vector2[] uv, Color[] colors)
74+
{
75+
this.vertices = vertices;
76+
this.triangles = triangles;
77+
this.uv = uv;
78+
this.colors = colors;
79+
}
5980

60-
public MeshData(int vertsWide, int vertsHigh)
81+
public MeshData(int vertsWide, int vertsHigh, bool vertexColors = false)
6182
{
6283
vertices = new Vector3[vertsWide * vertsHigh];
6384
uv = new Vector2[vertsWide * vertsHigh];
6485
triangles = new int[(vertsWide - 1) * (vertsHigh - 1) * 6];
65-
86+
if (vertexColors)
87+
{
88+
colors = new Color[vertsWide * vertsHigh];
89+
}
6690
}
6791

6892
//Add the triangle and return the next index
@@ -82,6 +106,12 @@ public Mesh CreateMesh()
82106
triangles = triangles,
83107
uv = uv
84108
};
109+
110+
if (colors != null)
111+
{
112+
mesh.colors = colors;
113+
}
114+
85115
mesh.RecalculateNormals();
86116
return mesh;
87117
}

Runtime/WorldMap/Terrain/TerrainTable.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public Color GetColor(float value)
1616
{
1717
if (value <= regions[i].Threshold)
1818
{
19-
return regions[i].HighColor;
19+
return regions[i].ColorGradient.Evaluate(1);
2020
}
2121
}
2222

@@ -40,8 +40,8 @@ private Color GetGradiatedColor(float value)
4040

4141
public static Color GetGradiatedColor(TerrainType region, float value)
4242
{
43-
var intensity = Mathf.InverseLerp(region.Threshold, region.Floor, value);
44-
return Color.Lerp(region.HighColor, region.LowColor, intensity);
43+
var intensity = Mathf.InverseLerp(region.Floor, region.Threshold, value);
44+
return region.ColorGradient.Evaluate(intensity);
4545
}
4646

4747
public TerrainType GetTerrainType(float value)
@@ -86,7 +86,7 @@ public static Color[] GetColorMap(float[,] noiseMap, TerrainType[,] terrainMap,
8686
for (int x = 0; x < width; x++)
8787
{
8888
var index = y * width + x;
89-
colorMap[index] = gradiate ? GetGradiatedColor(terrainMap[x, y], noiseMap[x, y]) : terrainMap[x, y].HighColor;
89+
colorMap[index] = gradiate ? GetGradiatedColor(terrainMap[x, y], noiseMap[x, y]) : terrainMap[x, y].ColorGradient.Evaluate(1);
9090
}
9191
}
9292

@@ -98,7 +98,7 @@ public static Color[] GetColorMap(float[] noiseMap, TerrainType[] terrainMap, bo
9898
var colorMap = new Color[noiseMap.Length];
9999
for (int i = 0; i < colorMap.Length; i++)
100100
{
101-
colorMap[i] = gradiate ? GetGradiatedColor(terrainMap[i], noiseMap[i]) : terrainMap[i].HighColor;
101+
colorMap[i] = gradiate ? GetGradiatedColor(terrainMap[i], noiseMap[i]) : terrainMap[i].ColorGradient.Evaluate(1);
102102
}
103103

104104
return colorMap;
@@ -116,7 +116,7 @@ public static Color[] GetColorMap(float[,] noiseMap, TerrainType[] terrainMap, b
116116
{
117117
var index = y * width + x;
118118
colorMap[index] =
119-
gradiate ? GetGradiatedColor(terrainMap[index], noiseMap[x, y]) : terrainMap[index].HighColor;
119+
gradiate ? GetGradiatedColor(terrainMap[index], noiseMap[x, y]) : terrainMap[index].ColorGradient.Evaluate(1);
120120
}
121121
}
122122

Runtime/WorldMap/Terrain/TerrainType.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ public float Threshold
3838
private float _elevation;
3939
public float Elevation => _elevation;
4040

41-
[SerializeField] private Color _lowColor = Color.black;
41+
/*[SerializeField] private Color _lowColor = Color.black;
4242
public Color LowColor => _lowColor;
4343
4444
[FormerlySerializedAs("_color")] [SerializeField]
4545
private Color _highColor = Color.white;
46-
public Color HighColor => _highColor;
46+
public Color HighColor => _highColor;*/
47+
48+
[SerializeField]
49+
private Gradient gradient;
50+
public Gradient ColorGradient => gradient;
4751
}
4852
}

Runtime/WorldMap/Views/WoldMapTerrainMeshView.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public class WoldMapTerrainMeshView : MonoBehaviour, IWorldMapView
1010
[SerializeField] private MeshFilter _meshFilter = null;
1111

1212
[SerializeField, Range(0,6)] private int levelOfDetail;
13+
14+
[SerializeField] private float heightScale = 1;
15+
16+
[SerializeField] private TerrainTable terrainTable = null;
1317

1418
//This is here just to get the enabled checkbox in editor
1519
private void Start()
@@ -23,8 +27,20 @@ public void DisplayMap(WorldMapData worldMapData)
2327
return;
2428
}
2529
var heightMap = worldMapData.GetLayer<HeightMapLayerData>().heightMap;
26-
var meshData = TerrainMeshUtility.GenerateMesh(heightMap,worldMapData.width,worldMapData.height,levelOfDetail);
27-
_meshFilter.mesh = meshData.CreateMesh();
30+
31+
if (terrainTable == null)
32+
{
33+
var meshData = TerrainMeshUtility.GenerateMesh(heightMap,worldMapData.width,worldMapData.height,heightScale,levelOfDetail);
34+
_meshFilter.mesh = meshData.CreateMesh();
35+
}
36+
else
37+
{
38+
var meshData = TerrainMeshUtility.GenerateMesh(heightMap,worldMapData.width,worldMapData.height,levelOfDetail,
39+
x => terrainTable.GetTerrainType(x).Elevation * heightScale,
40+
x => terrainTable.GetTerrainType(x).ColorGradient.Evaluate(1));
41+
_meshFilter.mesh = meshData.CreateMesh();
42+
}
43+
2844
}
2945
}
3046
}

0 commit comments

Comments
 (0)