Advertisement
Guest User

Monogame Chunking Issue

a guest
Nov 15th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.17 KB | None | 0 0
  1. public static void DrawChunk(ref Chunk chunk)
  2. {
  3.  
  4. // Pre-defining the buffers so they can be referenced later
  5. VertexBuffer vb;
  6. IndexBuffer ib;
  7.  
  8.  
  9. if (chunk.changed == true || chunk.vb == null || chunk.ib == null)
  10. {
  11. // Vertex lists
  12. List<VertexPositionTexture> floorVerts = new List<VertexPositionTexture>();
  13. List<short> indicies = new List<short>();
  14.  
  15. // Resize the lists beforehand so that they do not need to be resized (ChunkSizeCubed * 4 * range(0-6)) times
  16. indicies.Capacity = ChunkVerts;
  17. floorVerts.Capacity = ChunkVerts;
  18.  
  19. // The current vertex count (so that the list can become infinitely large)
  20. short indexCheck = 0;
  21.  
  22. // Loop through blocks
  23. for (int x = 0; x < ChunkSize; x++)
  24. {
  25. for (int y = 0; y < ChunkSize; y++)
  26. {
  27. for (int z = 0; z < ChunkSize; z++)
  28. {
  29. // If the block is not air
  30. if (chunk[x, y, z] >= 1)
  31. {
  32. // Check if the adjacent block is air and if so, create a quad.
  33. // For each face of the cube
  34.  
  35. //Top
  36. if (chunk[x , y, z + 1] == 0)
  37. {
  38. // Add 4 vertices
  39. 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)));
  40. 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)));
  41. 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)));
  42. 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)));
  43.  
  44. // Index the vertices in the correct order so that normals do not need to be set
  45. indicies.Add((short)(indexCheck+1));
  46. indicies.Add(indexCheck);
  47. indicies.Add((short)(indexCheck+3));
  48. indicies.Add((short)(indexCheck+2));
  49. indicies.Add((short)(indexCheck+1));
  50. indicies.Add((short)(indexCheck+3));
  51.  
  52. // Make sure the next quad can generically access the next 4 vertices
  53. indexCheck += 4;
  54. }
  55. //Bottom
  56. if (chunk[x, y, z - 1] == 0)
  57. {
  58. 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)));
  59. 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)));
  60. 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)));
  61. 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)));
  62. indicies.Add((short)(indexCheck+3));
  63. indicies.Add(indexCheck);
  64. indicies.Add((short)(indexCheck+1));
  65. indicies.Add((short)(indexCheck+3));
  66. indicies.Add((short)(indexCheck+1));
  67. indicies.Add((short)(indexCheck+2));
  68. indexCheck += 4;
  69. }
  70. //Left
  71. if (chunk[x, y + 1, z] == 0)
  72. {
  73. 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)));
  74. 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)));
  75. 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)));
  76. 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)));
  77. indicies.Add((short)(indexCheck+2));
  78. indicies.Add((short)(indexCheck+1));
  79. indicies.Add(indexCheck);
  80. indicies.Add((short)(indexCheck+2));
  81. indicies.Add(indexCheck);
  82. indicies.Add((short)(indexCheck+3));
  83.  
  84. indexCheck += 4;
  85. }
  86. //Right
  87. if (chunk[x, y - 1, z] == 0)
  88. {
  89. 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)));
  90. 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)));
  91. 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)));
  92. 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)));
  93. indicies.Add(indexCheck);
  94. indicies.Add((short)(indexCheck+1));
  95. indicies.Add((short)(indexCheck+2));
  96. indicies.Add((short)(indexCheck+3));
  97. indicies.Add(indexCheck);
  98. indicies.Add((short)(indexCheck+2));
  99. indexCheck += 4;
  100. }
  101. //Foward
  102. if (chunk[x + 1, y, z] == 0)
  103. {
  104. 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)));
  105. 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)));
  106. 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)));
  107. 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)));
  108.  
  109. indicies.Add(indexCheck);
  110. indicies.Add((short)((short)(indexCheck+1)));
  111. indicies.Add((short)((short)(indexCheck+2)));
  112. indicies.Add((short)((short)(indexCheck+3)));
  113. indicies.Add(indexCheck);
  114. indicies.Add((short)((short)(indexCheck+2)));
  115. indexCheck += 4;
  116. }
  117. //Back
  118. if (chunk[x - 1, y, z] == 0)
  119. {
  120. 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)));
  121. 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)));
  122. 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)));
  123. 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)));
  124.  
  125. indicies.Add((short)((short)(indexCheck+2)));
  126. indicies.Add((short)((short)(indexCheck+1)));
  127. indicies.Add(indexCheck);
  128. indicies.Add((short)((short)(indexCheck+2)));
  129. indicies.Add(indexCheck);
  130. indicies.Add((short)((short)(indexCheck+3)));
  131.  
  132. indexCheck += 4;
  133. }
  134. }
  135. }
  136. }
  137. }
  138.  
  139. // If there is any vertices
  140. if (floorVerts.Count > 0)
  141. {
  142. // Initialize new buffers
  143. vb = new VertexBuffer(Core.Instance.GraphicsDevice, VertexPositionTexture.VertexDeclaration, floorVerts.Count, BufferUsage.WriteOnly);
  144. ib = new IndexBuffer(Core.Instance.GraphicsDevice, IndexElementSize.SixteenBits, indicies.Count, BufferUsage.WriteOnly);
  145.  
  146. // Add the data to the buffers
  147. vb.SetData(floorVerts.ToArray());
  148. ib.SetData(indicies.ToArray());
  149.  
  150. // Store the buffers for later use
  151. chunk.vb = vb;
  152. chunk.ib = ib;
  153.  
  154. // Make sure the chunk does not generate another mesh unless it is edited again
  155. chunk.changed = false;
  156. }
  157. else
  158. {
  159. // Else set the buffers to null
  160. vb = null;
  161. ib = null;
  162. }
  163. }
  164. else
  165. {
  166. // Else the chunk has not been edited since the last mesh generation and it has a vertex buffer stored then re-use that
  167. vb = chunk.vb;
  168. ib = chunk.ib;
  169. }
  170. // If the index buffer (contextually both buffers but two checks is less efficient)
  171. if(ib != null && ib.IndexCount > 0)
  172. {
  173. // Set the buffers
  174. Core.Instance.GraphicsDevice.SetVertexBuffer(vb);
  175. Core.Instance.GraphicsDevice.Indices = ib;
  176.  
  177. // Create a shader/effect
  178. BasicEffect effect = new BasicEffect(Core.Instance.GraphicsDevice);
  179.  
  180. // Set the view and projection matrices
  181. effect.View = Core.cam.view;
  182. effect.Projection = Core.cam.projection;
  183.  
  184. // Enable textures and set the block texture
  185. effect.Texture = Core.Texture;
  186. effect.TextureEnabled = true;
  187.  
  188. // For each rendering pass needed for the effect
  189. foreach (var pass in effect.CurrentTechnique.Passes)
  190. {
  191. // Apply the pass
  192. pass.Apply();
  193.  
  194. // Render the buffered model on the graphics device (PrimitiveType, BaseVertex, StartIndex, PrimitiveCount)
  195. Core.Instance.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, chunk.ib.IndexCount / 6);
  196. }
  197. }
  198.  
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement