public void Unload() { if (Right != null) { Right.IsSurrounded = false; } if (Left != null) { Left.IsSurrounded = false; } if (Front != null) { Front.IsSurrounded = false; } if (Back != null) { Back.IsSurrounded = false; } for (int i = 0; i < HEIGHT; i++) { SubChunk sc = subChunks[i]; Renderer.RemoveVertexBuffer(sc); } }
public Chunk(int x, int z) { ChunkPosition = new Vector2(x, z); Position = new Vector3(SubChunk.WIDTH * ChunkPosition.X, 0, SubChunk.DEPTH * ChunkPosition.Y); // Fill with subchunks for (int i = 0; i < HEIGHT; i++) { SubChunk sc = new SubChunk(this); subChunks[i] = sc; sc.Initialize(i); } }
public static VertexPositionColor[] Mesh(SubChunk chunk) { if (chunk.GetCount() == 0) { return new VertexPositionColor[] { } } ; List <VertexPositionColor> CurrentArray = new List <VertexPositionColor>(); Blocks type; for (int x = 0; x < SubChunk.WIDTH; x++) { for (int y = 0; y < SubChunk.HEIGHT; y++) { for (int z = 0; z < SubChunk.DEPTH; z++) { type = chunk.GetBlock(x, y, z); if (type == Blocks.Air) { continue; } CreateBlock(x, y, z, chunk, type, CurrentArray); if (z > 0) { back = type == Blocks.Air; } } } } //for (int i = 0; i < SubChunk.FULL_COUNT; i++) //{ // Blocks type = chunk.GetBlock(i); // if (type == Blocks.Air) // continue; // // // int z = (i >> 8) & 0xF; // int y = (i >> 4) & 0xF; // int x = i & 0xF; // CreateBlock(x, y, z, chunk, type, CurrentArray); //} return(CurrentArray.ToArray()); }
public void Mesh() { //Stopwatch sw = new Stopwatch(); //sw.Start(); for (int i = 0; i < HEIGHT; i++) { SubChunk sc = subChunks[i]; sc.Mesh = ChunkMesher.Mesh(sc); } //sw.Stop(); //Console.WriteLine("Chunk mesh took:" + sw.ElapsedMilliseconds); IsMeshed = true; Changed = false; QueueToRender(); }
public void Rebuild() { for (int i = 0; i < HEIGHT; i++) { SubChunk sc = subChunks[i]; if (sc.NeedRebuild) { sc.Mesh = ChunkMesher.Mesh(sc); sc.NeedRebuild = false; Renderer.UpdateVertexBuffer(sc); } } for (int i = 0; i < HEIGHT; i++) { SubChunk sc = subChunks[i]; Renderer.UpdateVertexBuffer(sc); } Changed = false; }
private static void CreateBlock(int x, int y, int z, SubChunk chunk, Blocks type, List <VertexPositionColor> CurrentArray) { int gx = (int)chunk.Parent.Position.X + x; int gy = SubChunk.HEIGHT * chunk.Index + y; int gz = (int)chunk.Parent.Position.Z + z; top = y != SubChunk.HEIGHT - 1 ? chunk.GetBlock(x, y + 1, z) == Blocks.Air : true; bottom = y != 0 ? chunk.GetBlock(x, y - 1, z) == Blocks.Air : true; left = x != 0 ? chunk.GetBlock(x - 1, y, z) == Blocks.Air : chunk.Parent.Left.GetSubChunk(chunk.Index).GetBlock(SubChunk.DEPTH - 1, y, z) == Blocks.Air; right = x != SubChunk.WIDTH - 1 ? chunk.GetBlock(x + 1, y, z) == Blocks.Air : chunk.Parent.Right.GetSubChunk(chunk.Index).GetBlock(0, y, z) == Blocks.Air; front = z != SubChunk.DEPTH - 1 ? chunk.GetBlock(x, y, z + 1) == Blocks.Air : chunk.Parent.Front.GetSubChunk(chunk.Index).GetBlock(x, y, 0) == Blocks.Air; back = z != 0 ? chunk.GetBlock(x, y, z - 1) == Blocks.Air : chunk.Parent.Back.GetSubChunk(chunk.Index).GetBlock(x, y, SubChunk.DEPTH - 1) == Blocks.Air; // Block is surrounded if (top && bottom && left && right && front && back) { return; } bool topChunk = true; bool bottomChunk = true; if (chunk.Index != Chunk.HEIGHT - 1) { SubChunk topSubChunk = chunk.GetAboveSubChunk(); if (topSubChunk.GetCount() == SubChunk.FULL_COUNT) { topChunk = false; } else { topChunk = top = topSubChunk.GetBlock(x, 0, z) == Blocks.Air; } } if (chunk.Index != 0) { SubChunk botSubChunk = chunk.GetUnderSubChunk(); if (botSubChunk.GetCount() == SubChunk.FULL_COUNT) { bottomChunk = false; } else { bottomChunk = bottom = botSubChunk.GetBlock(x, SubChunk.HEIGHT - 1, z) == Blocks.Air; } } if (!left && !right && !back && !front && !topChunk && !bottomChunk) { return; } // False if should not render chunk border faces. bool topBorder = y == SubChunk.HEIGHT - 1 ? topChunk : top; bool bottomBorder = y == 0 ? bottomChunk : bottom; Color color = BlockManager.GetBlockColor(type, gx, gz); if (topBorder) { PushQuadAO(CUBE_FACES.Top, gx, gy, gz, 4, 5, 6, 7, color, CurrentArray); } if (bottomBorder) { PushQuadAO(CUBE_FACES.Bottom, gx, gy, gz, 3, 2, 1, 0, color, CurrentArray); } if (left) { PushQuadAO(CUBE_FACES.Left, gx, gy, gz, 0, 4, 7, 3, Color.Lerp(color, Color.Black, 0.1f), CurrentArray); } if (right) { PushQuadAO(CUBE_FACES.Right, gx, gy, gz, 1, 2, 6, 5, Color.Lerp(color, Color.Black, 0.15f), CurrentArray); } if (front) { PushQuadAO(CUBE_FACES.Front, gx, gy, gz, 2, 3, 7, 6, Color.Lerp(color, Color.Black, 0.1f), CurrentArray); } if (back) { PushQuadAO(CUBE_FACES.Back, gx, gy, gz, 5, 4, 0, 1, Color.Lerp(color, Color.Black, 0.15f), CurrentArray); } }