public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve heightCurve, int levelOfDetail) { int width = heightMap.GetLength(0); int height = heightMap.GetLength(1); float topLeftX = (width - 1) / -2f; float topLeftZ = (height - 1) / 2f; int meshSimplificationIncrement = (levelOfDetail == 0)?1:levelOfDetail * 2; int verticesPerLine = (width - 1) / meshSimplificationIncrement + 1; MeshData meshData = new MeshData(verticesPerLine, verticesPerLine); int vertexIndex = 0; for (int y = 0; y < height; y += meshSimplificationIncrement) { for (int x = 0; x < width; x += meshSimplificationIncrement) { meshData.vertices [vertexIndex] = new Vector3(topLeftX + x, heightCurve.Evaluate(heightMap [x, y]) * heightMultiplier, topLeftZ - y); meshData.uvs [vertexIndex] = new Vector2(x / (float)width, y / (float)height); if (x < width - 1 && y < height - 1) { meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine); meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1); } vertexIndex++; } } meshData.FlatShading(); return(meshData); }
public MeshData GenerateTerrainMesh(float[,] noiseMap, float heightMultiplier, AnimationCurve heightCurve) { int width = noiseMap.GetLength(0); int height = noiseMap.GetLength(1); float topLeftX = (width - 1) / -2f; float topLeftZ = (width - 1) / 2f; MeshData meshData = new MeshData(width, height); int vertexIndex = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { meshData.vertices[vertexIndex] = new Vector3(topLeftX + x, heightCurve.Evaluate(noiseMap[x, y]) * heightMultiplier, topLeftZ - y); meshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)height); if (x < width - 1 && y < height - 1) { meshData.AddTriangle(vertexIndex, vertexIndex + width + 1, vertexIndex + width); meshData.AddTriangle(vertexIndex + width + 1, vertexIndex, vertexIndex + 1); } vertexIndex++; } } meshData.FlatShading(); return(meshData); }
public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve _heightCurve, Gradient _colorGradient, int levelOfDetail, bool flatshading) { AnimationCurve heightCurve = new AnimationCurve(_heightCurve.keys); Gradient colorGradient = new Gradient(); colorGradient.SetKeys(_colorGradient.colorKeys, _colorGradient.alphaKeys); int width = heightMap.GetLength(0); int height = heightMap.GetLength(1); float topLeftX = (width - 1) / -2f; float topLeftZ = (height - 1) / 2f; int meshSimplificationIncrement = (levelOfDetail == 0) ? 1 : levelOfDetail * 2; if (levelOfDetail == 5) { meshSimplificationIncrement = (levelOfDetail - 1) * 2; } int verticesPerLine = (width - 1) / meshSimplificationIncrement + 1; MeshData meshData = new MeshData(verticesPerLine, false); int vertexIndex = 0; for (int y = 0; y < height; y += meshSimplificationIncrement) { for (int x = 0; x < width; x += meshSimplificationIncrement) { meshData.vertices[vertexIndex] = new Vector3(topLeftX + x, (heightCurve.Evaluate(heightMap[x, y]) * 2 - 1) * heightMultiplier, topLeftZ - y); meshData.colors[vertexIndex] = colorGradient.Evaluate(heightCurve.Evaluate(heightMap[x, y])); meshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)height); if (x < width - 1 && y < height - 1) { meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine); meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1); } vertexIndex++; } } if (flatshading) { meshData.FlatShading(); } return(meshData); }
/// <summary> /// Generierung des Meshes auf Basis der übergebenen Werte /// </summary> /// <param name="heightMap">DIe in Noise entstandene HeightMap </param> /// <param name="heightMultiplier"> Skalierungswert der Höhe der Hügel </param> /// <param name="_heightCurve"> Lässt eine nicht-lineare Umsetzung der Höhenwerte zu </param> /// <param name="levelOfDetail"> Detailgrad des zu generierenden Meshes </param> /// <param name="flatShaded"> True, wenn die Landschaft per FlatShading erschaffen werden soll </param> /// <param name="yOffset"> Verschiebt die Map auf der Y-Achse </param> /// <param name="valleyWidth"> Breite des Tals für den Lauftrack </param> /// <param name="valleyheight"> Höhe des Tals für den Lauftrack </param> /// <param name="gradient"> Der Farbverlauf auf der Landschaft </param> public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve _heightCurve, int levelOfDetail, bool flatShaded, float yOffset, float valleyWidth, float valleyHeight, Gradient gradient) { AnimationCurve heightCurve = new AnimationCurve(_heightCurve.keys); int width = heightMap.GetLength(0); int height = heightMap.GetLength(1); float topLeftX = (width - 1) / -2f; float topLeftZ = (height - 1) / 2f; // Das MeshSimplificationIncrement steuert den Detailgrad der Vertices im Mesh int meshSimplificationIncrement = (levelOfDetail == 0) ? 1 : levelOfDetail * 2; int verticesPerLine = (width - 1) / meshSimplificationIncrement + 1; MeshData meshData = new MeshData(verticesPerLine, verticesPerLine, flatShaded); int vertexIndex = 0; for (int z = 0; z < height; z += meshSimplificationIncrement) { for (int x = 0; x < width; x += meshSimplificationIncrement) { float tmpYOffset = yOffset; if (x < (width / 2.0 + valleyWidth / 2.0) && x > (width / 2.0 - valleyWidth / 2.0)) { tmpYOffset += Mathf.Abs(x - width / 2) - valleyHeight; } meshData.vertices[vertexIndex] = new Vector3(topLeftX + x, heightCurve.Evaluate(heightMap[x, z]) * heightMultiplier + tmpYOffset, topLeftZ - z); meshData.uvs[vertexIndex] = new Vector2(x / (float)width, z / (float)height); // Mit Evaluate wird eine verzerrbare Skalierung der HeightMap umgesetzt meshData.colors[vertexIndex] = gradient.Evaluate(meshData.vertices[vertexIndex].y); if (x < width - 1 && z < height - 1) { meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine); meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1); } vertexIndex++; } } if (flatShaded) { meshData.FlatShading(); } return(meshData); }
public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMutiplier, AnimationCurve heightCurve, int levelOfDetail) { int width = heightMap.GetLength(0); int heigth = heightMap.GetLength(1); // Get the top left coordinate of mesh float topleftX = (width - 1) / -2f; float topLeftZ = (heigth - 1) / 2f; // Set the simplification of mesh, if this value = 0 set 1 int meshSimplificationIncrement = (levelOfDetail == 0)?1:levelOfDetail * 2; int verticesPerLine = (width - 1) / meshSimplificationIncrement + 1; // Initialize mesh data MeshData meshData = new MeshData(verticesPerLine); // Initialize index to keep track the vertex int vertexIndex = 0; // loop through map for (int y = 0; y < heigth; y += meshSimplificationIncrement) { for (int x = 0; x < width; x += meshSimplificationIncrement) { // Initialize vertices at the vertex index meshData.vertices[vertexIndex] = new Vector3(topleftX + x, heightCurve.Evaluate(heightMap[x, y]) * heightMutiplier, topLeftZ - y); meshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)heigth); // Add triangle if the coordinate is not at final right or final bottom if (x < width - 1 && y < heigth - 1) { meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine); meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1); } vertexIndex++; } } meshData.FlatShading(); return(meshData); }
//Perlin Noise Mesh Generation public static MeshData GenerateMesh(float[,] heightMap, float heightMultiplier, AnimationCurve heightCurve) { int width = heightMap.GetLength(0); int height = heightMap.GetLength(1); float topLeftX = (width - 1) / -2f; float topLeftZ = (height - 1) / 2f; GameObject mapGen = GameObject.Find("MapGenerator"); MapGenerator mapGenerator = mapGen.GetComponent <MapGenerator>(); bool useFlatShading = mapGenerator.useFlatShading; MeshData meshData = new MeshData(width, height, useFlatShading); int vertexIndex = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { meshData.vertices[vertexIndex] = new Vector3(topLeftX + x, heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier, topLeftZ - y); meshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)height); if (x < width - 1 && y < height - 1) { //triangles added in a formulaic way, imagine numbers 0 1 2 3 with 2 3 under 0 1 //they would be joined as 0 3 2, 3 0 1 with 0 being the index or i // we know that 1 would be i + 1 and 2 would be i + width and therefore 3 would be i + width + 1 //So we can add these two triangles from a square using this formulae meshData.AddTriangle(vertexIndex, vertexIndex + width + 1, vertexIndex + width); meshData.AddTriangle(vertexIndex + width + 1, vertexIndex, vertexIndex + 1); } vertexIndex++; } } if (useFlatShading) { meshData.FlatShading(); } return(meshData); }