|
@@ -4,7 +4,7 @@ using System.Collections;
|
|
|
public static class MeshGenerator
|
|
public static class MeshGenerator
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
- public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve _heightCurve, int levelOfDetail)
|
|
|
|
|
|
|
+ public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve _heightCurve, int levelOfDetail, bool useFlatShading)
|
|
|
{
|
|
{
|
|
|
AnimationCurve heightCurve = new AnimationCurve(_heightCurve.keys);
|
|
AnimationCurve heightCurve = new AnimationCurve(_heightCurve.keys);
|
|
|
|
|
|
|
@@ -20,7 +20,7 @@ public static class MeshGenerator
|
|
|
|
|
|
|
|
int verticesPerLine = (meshSize - 1) / meshSimplificationIncrement + 1;
|
|
int verticesPerLine = (meshSize - 1) / meshSimplificationIncrement + 1;
|
|
|
|
|
|
|
|
- MeshData meshData = new MeshData(verticesPerLine);
|
|
|
|
|
|
|
+ MeshData meshData = new MeshData(verticesPerLine, useFlatShading);
|
|
|
|
|
|
|
|
int[,] vertexIndicesMap = new int[borderedSize, borderedSize];
|
|
int[,] vertexIndicesMap = new int[borderedSize, borderedSize];
|
|
|
int meshVertexIndex = 0;
|
|
int meshVertexIndex = 0;
|
|
@@ -70,7 +70,7 @@ public static class MeshGenerator
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- meshData.BakeNormals();
|
|
|
|
|
|
|
+ meshData.Finalize();
|
|
|
|
|
|
|
|
return meshData;
|
|
return meshData;
|
|
|
|
|
|
|
@@ -90,8 +90,11 @@ public class MeshData
|
|
|
int triangleIndex;
|
|
int triangleIndex;
|
|
|
int borderTriangleIndex;
|
|
int borderTriangleIndex;
|
|
|
|
|
|
|
|
- public MeshData(int verticesPerLine)
|
|
|
|
|
|
|
+ bool useFlatShading;
|
|
|
|
|
+
|
|
|
|
|
+ public MeshData(int verticesPerLine, bool useFlatShading)
|
|
|
{
|
|
{
|
|
|
|
|
+ this.useFlatShading = useFlatShading;
|
|
|
vertices = new Vector3[verticesPerLine * verticesPerLine];
|
|
vertices = new Vector3[verticesPerLine * verticesPerLine];
|
|
|
uvs = new Vector2[verticesPerLine * verticesPerLine];
|
|
uvs = new Vector2[verticesPerLine * verticesPerLine];
|
|
|
triangles = new int[(verticesPerLine - 1) * (verticesPerLine - 1) * 6];
|
|
triangles = new int[(verticesPerLine - 1) * (verticesPerLine - 1) * 6];
|
|
@@ -193,18 +196,53 @@ public class MeshData
|
|
|
return Vector3.Cross(sideAB, sideAC).normalized;
|
|
return Vector3.Cross(sideAB, sideAC).normalized;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void BakeNormals()
|
|
|
|
|
|
|
+ public void Finalize()
|
|
|
|
|
+ {
|
|
|
|
|
+ if (useFlatShading)
|
|
|
|
|
+ {
|
|
|
|
|
+ FlatShading();
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ BakeNormals();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void BakeNormals()
|
|
|
{
|
|
{
|
|
|
bakedNormals = CalculateNormals();
|
|
bakedNormals = CalculateNormals();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ void FlatShading()
|
|
|
|
|
+ {
|
|
|
|
|
+ Vector3[] flatShadedVertices = new Vector3[triangles.Length];
|
|
|
|
|
+ Vector2[] flatShadedUvs = new Vector2[triangles.Length];
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < triangles.Length; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ flatShadedVertices[i] = vertices[triangles[i]];
|
|
|
|
|
+ flatShadedUvs[i] = uvs[triangles[i]];
|
|
|
|
|
+ triangles[i] = i;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ vertices = flatShadedVertices;
|
|
|
|
|
+ uvs = flatShadedUvs;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public Mesh CreateMesh()
|
|
public Mesh CreateMesh()
|
|
|
{
|
|
{
|
|
|
Mesh mesh = new Mesh();
|
|
Mesh mesh = new Mesh();
|
|
|
mesh.vertices = vertices;
|
|
mesh.vertices = vertices;
|
|
|
mesh.triangles = triangles;
|
|
mesh.triangles = triangles;
|
|
|
mesh.uv = uvs;
|
|
mesh.uv = uvs;
|
|
|
- mesh.normals = bakedNormals;
|
|
|
|
|
|
|
+ if (useFlatShading)
|
|
|
|
|
+ {
|
|
|
|
|
+ mesh.RecalculateNormals();
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ mesh.normals = bakedNormals;
|
|
|
|
|
+ }
|
|
|
return mesh;
|
|
return mesh;
|
|
|
}
|
|
}
|
|
|
|
|
|