/// <summary> /// Rebuilds the mesh at the given chunk coordinates. /// </summary> /// <param name="renderer">The renderer that is rebuilding the mesh.</param> /// <param name="chunkX">The chunk X coordinate.</param> /// <param name="chunkY">The chunk Y coordinate.</param> public void Rebuild(TileMeshRenderer renderer, int chunkX, int chunkY) { var chunk = renderChunks[chunkX, chunkY]; bool decor = decorMaterial != null; if (chunk.dirty || renderer.forceRebuild) { int w = Grid.WidthInCells, maxY = CHUNK_SIZE * (chunkY + 1), minX = CHUNK_SIZE * chunkX, maxX = CHUNK_SIZE + minX; // Create vertex arrays chunk.Clear(); for (int y = chunkY * CHUNK_SIZE; y < maxY; y++) { for (int x = minX; x < maxX; x++) { int cell = y * w + x; if (occupiedCells.ContainsKey(cell)) { var color = renderer.GetCellColor(cell, element); GenerateVertices(x, y, color); } } } if (MeshUtil.vertices.Count > 0) { chunk.CreateMesh(); chunk.BuildMesh(); chunk.SetActive(true); // Deal with the decor if (decor) { RebuildDecor(renderer, chunkX, chunkY); } } else { chunk.DestroyMesh(); if (decor) { decorChunks[chunkX, chunkY].DestroyMesh(); } } chunk.dirty = false; } else { // It still needs to be drawn if it exists chunk.SetActive(true); if (decor) { decorChunks[chunkX, chunkY].SetActive(true); } } }
/// <summary> /// Rebuilds the decor mesh at the given coordinates. /// </summary> /// <param name="renderer">The renderer that is rebuilding the mesh.</param> /// <param name="chunkX">The chunk X coordinate.</param> /// <param name="chunkY">The chunk Y coordinate.</param> private void RebuildDecor(TileMeshRenderer renderer, int chunkX, int chunkY) { int w = Grid.WidthInCells, maxY = CHUNK_SIZE * (chunkY + 1), minX = CHUNK_SIZE * chunkX, maxX = CHUNK_SIZE + minX; var chunk = decorChunks[chunkX, chunkY]; // Create vertex arrays chunk.Clear(); decorTriangles.Clear(); for (int y = chunkY * CHUNK_SIZE; y < maxY; y++) { for (int x = minX; x < maxX; x++) { int cell = y * w + x; if (occupiedCells.ContainsKey(cell)) { var color = renderer.GetCellColor(cell, element); GenerateDecorVertices(x, y, color); } } } if (MeshUtil.vertices.Count > 0) { var indices = MeshUtil.indices; int d = decorTriangles.Count; chunk.CreateMesh(); // Triangles must be in sorted order decorTriangles.Sort(TriangleComparer.Instance); for (int i = 0; i < d; i++) { var triangle = decorTriangles[i]; indices.Add(triangle.i0); indices.Add(triangle.i1); indices.Add(triangle.i2); } chunk.BuildMesh(); chunk.SetActive(true); } else { chunk.DestroyMesh(); } }