public static WaterMeshData GenerateTerrainMesh(float[,] heightMap, float heightmultiplier) { int width = heightMap.GetLength(0); int height = heightMap.GetLength(1); float topLeftX = (width - 1) / -2f; float topLeftZ = (height - 1) / 2f; WaterMeshData meshData = new WaterMeshData(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, 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 + width + 1, vertexIndex + width); meshData.AddTriangle(vertexIndex + width + 1, vertexIndex, vertexIndex + 1); } vertexIndex++; } } return(meshData); }
public void DrawMesh(WaterMeshData meshData, Texture2D texture) { meshFilter.sharedMesh = meshData.CreateMesh(); meshRenderer.sharedMaterial.mainTexture = texture; }