public TerrainChunk Initialize(TerrainManager terrainManager, int cellsPerChunk, ChunkLocation location, TerrainGenerator terrainGenerator, Material chunkMaterial) { this.terrainManager = terrainManager; transform.position = new Vector3(location.GetX() * cellsPerChunk, 0, location.GetZ() * cellsPerChunk); transform.name = "Chunk"; gameObject.layer = 8; this.location = location; this.cellsPerChunk = cellsPerChunk; cellHeights = new float[cellsPerChunk + 1, cellsPerChunk + 1]; for (int i = 0; i < cellsPerChunk + 1; i++) { for (int j = 0; j < cellsPerChunk + 1; j++) { cellHeights[i, j] = terrainGenerator.GetTerrainHeight(i + location.GetAbsX(), j + location.GetAbsZ()); } } chunkCells = new TerrainCell[cellsPerChunk, cellsPerChunk]; for (int i = 0; i < cellsPerChunk; i++) { for (int j = 0; j < cellsPerChunk; j++) { CellLocation loc = new CellLocation(i, j, location); chunkCells[i, j] = new TerrainCell(loc, terrainGenerator); } } m_material = chunkMaterial; GetComponent <MeshRenderer>().material = m_material; return(this); }
Mesh GenChunkByVertex(int numCells, string name) { int numVerts = (numCells + 1) * (numCells + 1) * 6; int numTriangles = (numCells + 1) * (numCells + 1) * 6; Vector3[] verts = new Vector3[numVerts]; int[] triangles = new int[numTriangles]; Vector3[] normals = new Vector3[numVerts]; Vector2[] uv = new Vector2[numVerts]; Color[] colors = new Color[numVerts]; int counter = 0; for (int i = 0; i < numCells; i++) { for (int j = 0; j < numCells; j++) { TerrainCell curCell = chunkCells[i, j]; float regolith = curCell.GetRegolithAmount(); Vector3 uL = new Vector3(i, cellHeights[i, j], j); Vector3 uR = new Vector3(i, cellHeights[i, j + 1], j + 1); Vector3 lL = new Vector3(i + 1, cellHeights[i + 1, j], j); Vector3 lR = new Vector3(i + 1, cellHeights[i + 1, j + 1], j + 1); curCell.UpdateIsFlat(cellHeights[i, j] == cellHeights[i, j + 1] && cellHeights[i, j + 1] == cellHeights[i + 1, j] && cellHeights[i + 1, j] == cellHeights[i + 1, j + 1]); if (Mathf.Abs(uL.y - lR.y) < Mathf.Abs(uR.y - lL.y)) { verts[counter] = uL; verts[counter + 1] = uR; verts[counter + 2] = lR; verts[counter + 3] = uL; verts[counter + 4] = lR; verts[counter + 5] = lL; Vector3 a = uL - uR; Vector3 b = lR - uR; Vector3 abCross = Vector3.Cross(b, a).normalized; Vector3 c = uL - lL; Vector3 d = lR - lL; Vector3 cdCross = Vector3.Cross(c, d).normalized; normals[counter] = normals[counter + 1] = normals[counter + 2] = abCross; normals[counter + 3] = normals[counter + 4] = normals[counter + 5] = cdCross; uv[counter] = new Vector2(0, 0); uv[counter + 1] = new Vector2(0, 1); uv[counter + 2] = new Vector2(1, 1); uv[counter + 3] = new Vector2(0, 0); uv[counter + 4] = new Vector2(1, 1); uv[counter + 5] = new Vector2(1, 0); } else { verts[counter] = uL; verts[counter + 1] = uR; verts[counter + 2] = lL; verts[counter + 3] = uR; verts[counter + 4] = lR; verts[counter + 5] = lL; Vector3 a = uL - uR; Vector3 b = lL - uR; Vector3 abCross = Vector3.Cross(b, a).normalized; Vector3 c = uR - lR; Vector3 d = lL - lR; Vector3 cdCross = Vector3.Cross(d, c).normalized; normals[counter] = normals[counter + 1] = normals[counter + 2] = abCross; normals[counter + 3] = normals[counter + 4] = normals[counter + 5] = cdCross; uv[counter] = new Vector2(0, 0); uv[counter + 1] = new Vector2(0, 1); uv[counter + 2] = new Vector2(1, 0); uv[counter + 3] = new Vector2(0, 1); uv[counter + 4] = new Vector2(1, 1); uv[counter + 5] = new Vector2(1, 0); } /*if (curCell.iron > curCell.ice && curCell.iron > curCell.helium) * for (int a = counter; a < 6; a++) * colors[a] = iron; * else if (curCell.ice > curCell.iron && curCell.ice > curCell.helium) * for (int a = counter; a < 6; a++) * colors[a] = ice; * else if (curCell.helium > curCell.iron && curCell.helium > curCell.ice) * for (int a = counter; a < 6; a++) * colors[a] = helium; * else*/ for (int a = counter; a < 6; a++) { colors[a] = ice; } counter += 6; } } for (int i = 0; i < numCells * numCells * 6; i++) { triangles[i] = i; //no vertex saving, each triangle gets its own three vertices, in order } Mesh mesh = new Mesh(); mesh.name = name; mesh.vertices = verts; mesh.triangles = triangles; mesh.normals = normals; mesh.uv = uv; mesh.colors = colors; return(mesh); }