1
- using UnityEngine ;
1
+ using System ;
2
+ using UnityEditor . SceneManagement ;
3
+ using UnityEngine ;
2
4
3
5
namespace Gameframe . Procgen
4
6
{
@@ -7,7 +9,7 @@ public static class TerrainMeshUtility
7
9
{
8
10
private const int mapChunkSize = 241 ;
9
11
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 )
11
13
{
12
14
float topLeftX = ( width - 1 ) / - 2f ;
13
15
float topLeftZ = ( height - 1 ) / 2f ;
@@ -16,7 +18,7 @@ public static MeshData GenerateMesh(float[] heightMap, int width, int height, in
16
18
int vertsPerLine = ( width - 1 ) / lodIncrement + 1 ;
17
19
int vertsPerColumn = ( height - 1 ) / lodIncrement + 1 ;
18
20
19
- var meshData = new MeshData ( vertsPerLine , vertsPerColumn ) ;
21
+ var meshData = new MeshData ( vertsPerLine , vertsPerColumn , colorFunction != null ) ;
20
22
int vertIndex = 0 ;
21
23
int triangleIndex = 0 ;
22
24
for ( int i = 0 ; i < vertsPerColumn ; i ++ )
@@ -26,9 +28,14 @@ public static MeshData GenerateMesh(float[] heightMap, int width, int height, in
26
28
int x = j * lodIncrement ;
27
29
int y = i * lodIncrement ;
28
30
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 ) ;
30
32
meshData . uv [ vertIndex ] = new Vector2 ( x / ( float ) width , y / ( float ) height ) ;
31
33
34
+ if ( colorFunction != null )
35
+ {
36
+ meshData . colors [ vertIndex ] = colorFunction . Invoke ( heightMap [ y * width + x ] ) ;
37
+ } //*/
38
+
32
39
if ( j < ( vertsPerLine - 1 ) && i < ( vertsPerColumn - 1 ) )
33
40
{
34
41
triangleIndex = meshData . AddTriangle ( triangleIndex , vertIndex , vertIndex + vertsPerLine + 1 ,
@@ -39,30 +46,47 @@ public static MeshData GenerateMesh(float[] heightMap, int width, int height, in
39
46
vertIndex ++ ;
40
47
}
41
48
}
42
-
49
+
43
50
return meshData ;
44
51
}
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
+ }
45
57
}
46
58
47
59
public class MeshData
48
60
{
49
61
public readonly Vector3 [ ] vertices ;
50
62
public readonly int [ ] triangles ;
51
63
public readonly Vector2 [ ] uv ;
64
+ public readonly Color [ ] colors ;
52
65
53
66
public MeshData ( Vector3 [ ] vertices , int [ ] triangles , Vector2 [ ] uv )
54
67
{
55
68
this . vertices = vertices ;
56
69
this . triangles = triangles ;
57
70
this . uv = uv ;
58
71
}
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
+ }
59
80
60
- public MeshData ( int vertsWide , int vertsHigh )
81
+ public MeshData ( int vertsWide , int vertsHigh , bool vertexColors = false )
61
82
{
62
83
vertices = new Vector3 [ vertsWide * vertsHigh ] ;
63
84
uv = new Vector2 [ vertsWide * vertsHigh ] ;
64
85
triangles = new int [ ( vertsWide - 1 ) * ( vertsHigh - 1 ) * 6 ] ;
65
-
86
+ if ( vertexColors )
87
+ {
88
+ colors = new Color [ vertsWide * vertsHigh ] ;
89
+ }
66
90
}
67
91
68
92
//Add the triangle and return the next index
@@ -82,6 +106,12 @@ public Mesh CreateMesh()
82
106
triangles = triangles ,
83
107
uv = uv
84
108
} ;
109
+
110
+ if ( colors != null )
111
+ {
112
+ mesh . colors = colors ;
113
+ }
114
+
85
115
mesh . RecalculateNormals ( ) ;
86
116
return mesh ;
87
117
}
0 commit comments