Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Chunk : MonoBehaviour {
  6.  
  7. public MeshRenderer meshRenderer;
  8. public MeshFilter meshFilter;
  9.  
  10. int vertexIndex = 0;
  11. List<Vector3> vertices = new List<Vector3> ();
  12. List<int> triangles = new List<int> ();
  13. List<Vector2> uvs = new List<Vector2> ();
  14.  
  15. byte[,,] voxelMap = new byte[VoxelData.ChunkWidth, VoxelData.ChunkHeight, VoxelData.ChunkWidth];
  16.  
  17. World world;
  18.  
  19. void Start () {
  20.  
  21. world = GameObject.Find("World").GetComponent<World>();
  22.  
  23. PopulateVoxelMap ();
  24. CreateMeshData ();
  25. CreateMesh ();
  26.  
  27. }
  28.  
  29. void PopulateVoxelMap () {
  30.  
  31. for (int y = 0; y < VoxelData.ChunkHeight; y++) {
  32. for (int x = 0; x < VoxelData.ChunkWidth; x++) {
  33. for (int z = 0; z < VoxelData.ChunkWidth; z++) {
  34.  
  35. voxelMap [x, y, z] = 0;
  36.  
  37. }
  38. }
  39. }
  40. }
  41.  
  42. void CreateMeshData() {
  43.  
  44. for (int y = 0; y < VoxelData.ChunkHeight; y++) {
  45. for (int x = 0; x < VoxelData.ChunkWidth; x++) {
  46. for (int z = 0; z < VoxelData.ChunkWidth; z++) {
  47.  
  48. AddVoxelDataToChunk (new Vector3(x, y, z));
  49.  
  50. }
  51. }
  52. }
  53. }
  54.  
  55. bool CheckVoxel (Vector3 pos) {
  56.  
  57. int x = Mathf.FloorToInt (pos.x);
  58. int y = Mathf.FloorToInt (pos.y);
  59. int z = Mathf.FloorToInt (pos.z);
  60.  
  61. if (x < 0 || x > VoxelData.ChunkWidth - 1 || y < 0 || y > VoxelData.ChunkHeight - 1 || z < 0 || z > VoxelData.ChunkWidth - 1)
  62. return false;
  63.  
  64. return world.blocktypes[voxelMap [x, y, z]].isSolid;
  65.  
  66. }
  67.  
  68. void AddVoxelDataToChunk (Vector3 pos) {
  69.  
  70. for (int p = 0; p < 6; p++) {
  71.  
  72. if (!CheckVoxel(pos + VoxelData.faceChecks[p])) {
  73.  
  74. vertices.Add (pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 0]]);
  75. vertices.Add (pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 1]]);
  76. vertices.Add (pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 2]]);
  77. vertices.Add (pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 3]]);
  78.  
  79. AddTexture(0);
  80.  
  81. triangles.Add (vertexIndex);
  82. triangles.Add (vertexIndex + 1);
  83. triangles.Add (vertexIndex + 2);
  84. triangles.Add (vertexIndex + 2);
  85. triangles.Add (vertexIndex + 1);
  86. triangles.Add (vertexIndex + 3);
  87. vertexIndex += 4;
  88.  
  89. }
  90. }
  91.  
  92. }
  93.  
  94. void CreateMesh () {
  95.  
  96. Mesh mesh = new Mesh ();
  97. mesh.vertices = vertices.ToArray ();
  98. mesh.triangles = triangles.ToArray ();
  99. mesh.uv = uvs.ToArray ();
  100.  
  101. mesh.RecalculateNormals ();
  102.  
  103. meshFilter.mesh = mesh;
  104. }
  105.  
  106. void AddTexture (int textureID) {
  107.  
  108. float y = textureID / VoxelData.TextureAtlasSizeInBlocks;
  109. float x = textureID - (y * VoxelData.TextureAtlasSizeInBlocks);
  110.  
  111. x *= VoxelData.NormalizedBlockTextureSize;
  112. y *= VoxelData.NormalizedBlockTextureSize;
  113.  
  114. y = 1f - y - VoxelData.NormalizedBlockTextureSize;
  115.  
  116. uvs.Add(new Vector2(x, y));
  117. uvs.Add(new Vector2(x, y + VoxelData.NormalizedBlockTextureSize));
  118. uvs.Add(new Vector2(x + VoxelData.NormalizedBlockTextureSize, y));
  119. uvs.Add(new Vector2(x + VoxelData.NormalizedBlockTextureSize, y + VoxelData.NormalizedBlockTextureSize));
  120.  
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement