public static TerrainMesh GenerateMesh(int WidthCount, int LenghtCount, float step) { // TerrainMesh tm = new TerrainMesh(); int wverticesCount = WidthCount + 1; int lverticesCount = LenghtCount + 1; TerrainVertex[] vertices = new TerrainVertex[wverticesCount * lverticesCount]; float sizew = step * WidthCount; float sizel = step * LenghtCount; float scaletcoord = 0.5f; Vector3 startvector = new Vector3(-sizew / 2, 0, -sizel / 2); for (int i = 0; i < wverticesCount;i++ ) for (int j = 0; j < lverticesCount; j++) { int index= i * lverticesCount + j; vertices[index].position = startvector + new Vector3(i * step, 0, j * step); vertices[index].textureCoordinate = new Vector2(i * scaletcoord, j * scaletcoord); vertices[index].normal = Vector3.UnitY; } ushort[] indices = new ushort[WidthCount * LenghtCount * 6]; int indexadd = 0; for (int i = 0; i < WidthCount; i++) { for (int j = 0; j < LenghtCount; j++) { int qwadindex = i * LenghtCount + j; int firsttrindex = qwadindex; ushort index1 = Convert.ToUInt16(LenghtCount * i + j + indexadd); ushort index2 = Convert.ToUInt16(LenghtCount * i + j + 1 + indexadd); ushort index3 = Convert.ToUInt16(LenghtCount * (i + 1) + j + 1 + indexadd); indices[firsttrindex * 3] = index1; indices[firsttrindex * 3 + 1] = index2; indices[firsttrindex * 3 + 2] = index3; ushort index6 = Convert.ToUInt16(LenghtCount * (i + 1) + j + 2 + indexadd); indices[firsttrindex * 3 + indices.Length / 2] = index3; indices[firsttrindex * 3 + 1 + indices.Length / 2] = index2; indices[firsttrindex * 3 + 2 + indices.Length / 2] = index6; } indexadd++; } TerrainMesh tm = new TerrainMesh(vertices, indices); tm.Size = new Vector3(WidthCount*step,0,LenghtCount*step); return tm; }
public TerrainRenderObject(TerrainMesh mesh) { tm = mesh; }