/// <summary> /// Rebuilds a chunk coordinate for all layers. /// </summary> /// <param name="renderer">The renderer doing the rebuilding.</param> /// <param name="x">The chunk x coordinate to rebuild.</param> /// <param name="y">The chunk y coordinate to rebuild.</param> public void Rebuild(TileMeshRenderer renderer, int x, int y) { for (int i = 0; i < N_LAYERS; i++) { infos[i]?.Rebuild(renderer, x, y); } }
/// <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(); } }
/// <summary> /// Applied after OnPrefabInit runs. /// </summary> private static void OnPrefabInit_Postfix(World __instance) { TileMeshRenderer.CreateInstance(__instance.blockTileRenderer); }
/// <summary> /// Destroys the singleton instance of this class. /// </summary> internal static void DestroyInstance() { Instance?.Dispose(); Instance = null; }
/// <summary> /// Creates the singleton instance of this class. /// </summary> /// <param name="colorSource">The tile renderer to use for tile colors.</param> internal static void CreateInstance(BlockTileRenderer colorSource) { DestroyInstance(); Instance = new TileMeshRenderer(colorSource); }