Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void DrawChunk(ref Chunk chunk)
- {
- // Pre-defining the buffers so they can be referenced later
- VertexBuffer vb;
- IndexBuffer ib;
- if (chunk.changed == true || chunk.vb == null || chunk.ib == null)
- {
- // Vertex lists
- List<VertexPositionTexture> floorVerts = new List<VertexPositionTexture>();
- List<short> indicies = new List<short>();
- // Resize the lists beforehand so that they do not need to be resized (ChunkSizeCubed * 4 * range(0-6)) times
- indicies.Capacity = ChunkVerts;
- floorVerts.Capacity = ChunkVerts;
- // The current vertex count (so that the list can become infinitely large)
- short indexCheck = 0;
- // Loop through blocks
- for (int x = 0; x < ChunkSize; x++)
- {
- for (int y = 0; y < ChunkSize; y++)
- {
- for (int z = 0; z < ChunkSize; z++)
- {
- // If the block is not air
- if (chunk[x, y, z] >= 1)
- {
- // Check if the adjacent block is air and if so, create a quad.
- // For each face of the cube
- //Top
- if (chunk[x , y, z + 1] == 0)
- {
- // Add 4 vertices
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(1, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(1, 1)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(0, 1)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(0, 0)));
- // Index the vertices in the correct order so that normals do not need to be set
- indicies.Add((short)(indexCheck+1));
- indicies.Add(indexCheck);
- indicies.Add((short)(indexCheck+3));
- indicies.Add((short)(indexCheck+2));
- indicies.Add((short)(indexCheck+1));
- indicies.Add((short)(indexCheck+3));
- // Make sure the next quad can generically access the next 4 vertices
- indexCheck += 4;
- }
- //Bottom
- if (chunk[x, y, z - 1] == 0)
- {
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(1, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(1, 1)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(0, 1)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(0, 0)));
- indicies.Add((short)(indexCheck+3));
- indicies.Add(indexCheck);
- indicies.Add((short)(indexCheck+1));
- indicies.Add((short)(indexCheck+3));
- indicies.Add((short)(indexCheck+1));
- indicies.Add((short)(indexCheck+2));
- indexCheck += 4;
- }
- //Left
- if (chunk[x, y + 1, z] == 0)
- {
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(1, 1)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(1, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(0, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(0, 1)));
- indicies.Add((short)(indexCheck+2));
- indicies.Add((short)(indexCheck+1));
- indicies.Add(indexCheck);
- indicies.Add((short)(indexCheck+2));
- indicies.Add(indexCheck);
- indicies.Add((short)(indexCheck+3));
- indexCheck += 4;
- }
- //Right
- if (chunk[x, y - 1, z] == 0)
- {
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(1, 1)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(1, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(0, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(0, 1)));
- indicies.Add(indexCheck);
- indicies.Add((short)(indexCheck+1));
- indicies.Add((short)(indexCheck+2));
- indicies.Add((short)(indexCheck+3));
- indicies.Add(indexCheck);
- indicies.Add((short)(indexCheck+2));
- indexCheck += 4;
- }
- //Foward
- if (chunk[x + 1, y, z] == 0)
- {
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(1, 1)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(1, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(0, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X + 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(0, 1)));
- indicies.Add(indexCheck);
- indicies.Add((short)((short)(indexCheck+1)));
- indicies.Add((short)((short)(indexCheck+2)));
- indicies.Add((short)((short)(indexCheck+3)));
- indicies.Add(indexCheck);
- indicies.Add((short)((short)(indexCheck+2)));
- indexCheck += 4;
- }
- //Back
- if (chunk[x - 1, y, z] == 0)
- {
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(1, 1)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y + 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(1, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z - 0.5f), new Vector2(0, 0)));
- floorVerts.Add(new VertexPositionTexture(new Vector3(x + chunk.ChunkPos.X - 0.5f, y + chunk.ChunkPos.Y - 0.5f, z + chunk.ChunkPos.Z + 0.5f), new Vector2(0, 1)));
- indicies.Add((short)((short)(indexCheck+2)));
- indicies.Add((short)((short)(indexCheck+1)));
- indicies.Add(indexCheck);
- indicies.Add((short)((short)(indexCheck+2)));
- indicies.Add(indexCheck);
- indicies.Add((short)((short)(indexCheck+3)));
- indexCheck += 4;
- }
- }
- }
- }
- }
- // If there is any vertices
- if (floorVerts.Count > 0)
- {
- // Initialize new buffers
- vb = new VertexBuffer(Core.Instance.GraphicsDevice, VertexPositionTexture.VertexDeclaration, floorVerts.Count, BufferUsage.WriteOnly);
- ib = new IndexBuffer(Core.Instance.GraphicsDevice, IndexElementSize.SixteenBits, indicies.Count, BufferUsage.WriteOnly);
- // Add the data to the buffers
- vb.SetData(floorVerts.ToArray());
- ib.SetData(indicies.ToArray());
- // Store the buffers for later use
- chunk.vb = vb;
- chunk.ib = ib;
- // Make sure the chunk does not generate another mesh unless it is edited again
- chunk.changed = false;
- }
- else
- {
- // Else set the buffers to null
- vb = null;
- ib = null;
- }
- }
- else
- {
- // Else the chunk has not been edited since the last mesh generation and it has a vertex buffer stored then re-use that
- vb = chunk.vb;
- ib = chunk.ib;
- }
- // If the index buffer (contextually both buffers but two checks is less efficient)
- if(ib != null && ib.IndexCount > 0)
- {
- // Set the buffers
- Core.Instance.GraphicsDevice.SetVertexBuffer(vb);
- Core.Instance.GraphicsDevice.Indices = ib;
- // Create a shader/effect
- BasicEffect effect = new BasicEffect(Core.Instance.GraphicsDevice);
- // Set the view and projection matrices
- effect.View = Core.cam.view;
- effect.Projection = Core.cam.projection;
- // Enable textures and set the block texture
- effect.Texture = Core.Texture;
- effect.TextureEnabled = true;
- // For each rendering pass needed for the effect
- foreach (var pass in effect.CurrentTechnique.Passes)
- {
- // Apply the pass
- pass.Apply();
- // Render the buffered model on the graphics device (PrimitiveType, BaseVertex, StartIndex, PrimitiveCount)
- Core.Instance.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, chunk.ib.IndexCount / 6);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement