1- using UnityEngine ;
1+ using System ;
2+ using UnityEditor . SceneManagement ;
3+ using UnityEngine ;
24
35namespace 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 }
0 commit comments