public virtual MeshData Blockdata(Chunk chunk, int x, int y, int z, float scale, float worldScale, MeshData meshData) { if (BlockType == VoxBlockType.Empty) return meshData; meshData.UseRenderDataForCol = true; if (!chunk.GetBlock(x, y + 1, z).IsSolid(Direction.Down)) { meshData = FaceDataUp(chunk, x, y, z, scale, worldScale, meshData); } if (!chunk.GetBlock(x, y - 1, z).IsSolid(Direction.Up)) { meshData = FaceDataDown(chunk, x, y, z, scale, worldScale, meshData); } if (!chunk.GetBlock(x, y, z + 1).IsSolid(Direction.South)) { meshData = FaceDataNorth(chunk, x, y, z, scale, worldScale, meshData); } if (!chunk.GetBlock(x, y, z - 1).IsSolid(Direction.North)) { meshData = FaceDataSouth(chunk, x, y, z, scale, worldScale, meshData); } if (!chunk.GetBlock(x + 1, y, z).IsSolid(Direction.West)) { meshData = FaceDataEast(chunk, x, y, z, scale, worldScale, meshData); } if (!chunk.GetBlock(x - 1, y, z).IsSolid(Direction.East)) { meshData = FaceDataWest(chunk, x, y, z, scale, worldScale, meshData); } return meshData; }
//Sends the calculated mesh information //to the mesh and collision components protected void RenderMesh(MeshData meshData) { Filter.mesh.Clear(); Filter.mesh.vertices = meshData.vertices.ToArray(); Filter.mesh.triangles = meshData.triangles.ToArray(); Filter.mesh.colors = meshData.colors.ToArray(); Filter.mesh.uv = meshData.uv.ToArray(); Filter.mesh.RecalculateNormals(); Coll.sharedMesh = null; var mesh = new Mesh { vertices = meshData.colVertices.ToArray(), triangles = meshData.colTriangles.ToArray() }; mesh.RecalculateNormals(); Coll.sharedMesh = mesh; }
//Updates the chunk based on its contents protected void Reconstruct() { Rendered = true; var meshData = new MeshData(); for (var x = 0; x < World.ChunkSize; x++) { for (var y = 0; y < World.ChunkSize; y++) { for (var z = 0; z < World.ChunkSize; z++) { Blocks[x, y, z].JustCreated = false; meshData = Blocks[x, y, z].Blockdata(this, x, y, z, BlockScale * World.UnitScale, World.UnitScale, meshData); //if (Blocks[x, y, z] != null) //{ //} //else //{ // Blocks[x, y, z] = new Block(Color.white) {BlockType = VoxBlockType.Empty}; //} } } } RenderMesh(meshData); }
protected virtual MeshData FaceDataWest(Chunk chunk, int x, int y, int z, float scale, float worldScale, MeshData meshData) { meshData.AddVertex(new Vector3(x * worldScale - scale, y * worldScale - scale, z * worldScale + scale), BlockColor); meshData.AddVertex(new Vector3(x * worldScale - scale, y * worldScale + scale, z * worldScale + scale), BlockColor); meshData.AddVertex(new Vector3(x * worldScale - scale, y * worldScale + scale, z * worldScale - scale), BlockColor); meshData.AddVertex(new Vector3(x * worldScale - scale, y * worldScale - scale, z * worldScale - scale), BlockColor); meshData.AddQuadTriangles(); meshData.uv.AddRange(FaceUVs(Direction.West)); return meshData; }