Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Chunk {
  6.  
  7. public ChunkCoord coord;
  8.  
  9. GameObject chunkObject;
  10. MeshRenderer meshRenderer;
  11. MeshFilter meshFilter;
  12.  
  13.  
  14.  
  15. int vertexIndex = 0;
  16. List<Vector3> vertices = new List<Vector3> ();
  17. List<int> triangles = new List<int> ();
  18. List<Vector2> uvs = new List<Vector2> ();
  19.  
  20. public byte [,,] voxelMap = new byte[VoxelData.ChunkWidth, VoxelData.ChunkHeight, VoxelData.ChunkWidth];
  21.  
  22. World world;
  23.  
  24. private bool _isActive;
  25. public bool isVoxelMapPopulated = false;
  26.  
  27. public Chunk (ChunkCoord _coord, World _world, bool generateOnLoad) {
  28.  
  29. coord = _coord;
  30. world = _world;
  31. isActive = true;
  32.  
  33. if (generateOnLoad)
  34. init();
  35.  
  36. }
  37.  
  38. public void init () {
  39.  
  40. chunkObject = new GameObject();
  41. meshFilter = chunkObject.AddComponent<MeshFilter>();
  42. meshRenderer = chunkObject.AddComponent<MeshRenderer>();
  43.  
  44. meshRenderer.material = world.material;
  45. chunkObject.transform.SetParent(world.transform);
  46. chunkObject.transform.position = new Vector3(coord.x * VoxelData.ChunkWidth, 0f, coord.z * VoxelData.ChunkWidth);
  47. chunkObject.name = "Chunk " + coord.x + "," + coord.z;
  48.  
  49.  
  50. PopulateVoxelMap();
  51. CreateMeshData();
  52. CreateMesh();
  53.  
  54. }
  55.  
  56. void PopulateVoxelMap (){
  57.  
  58. for (int y = 0; y < VoxelData.ChunkHeight; y++) {
  59. for (int x = 0; x < VoxelData.ChunkWidth; x++) {
  60. for (int z = 0; z < VoxelData.ChunkWidth; z++) {
  61.  
  62. voxelMap[x, y, z] = world.GetVoxel(new Vector3(x, y, z) + position);
  63.  
  64. }
  65. }
  66. }
  67.  
  68. isVoxelMapPopulated = true;
  69.  
  70. }
  71.  
  72. void CreateMeshData () {
  73.  
  74. for (int y = 0; y < VoxelData.ChunkHeight; y++) {
  75. for (int x = 0; x < VoxelData.ChunkWidth; x++) {
  76. for (int z = 0; z < VoxelData.ChunkWidth; z++) {
  77.  
  78. if (world.blocktypes[voxelMap[x,y,z]].isSolid)
  79. AddVoxelDataToChunk (new Vector3(x, y, z));
  80.  
  81. }
  82. }
  83. }
  84. }
  85.  
  86. public bool isActive {
  87.  
  88. get { return _isActive; }
  89. set {
  90.  
  91. _isActive = value;
  92. if (chunkObject != null)
  93. chunkObject.SetActive(value);
  94.  
  95. }
  96. }
  97.  
  98. public Vector3 position {
  99.  
  100. get { return chunkObject.transform.position; }
  101.  
  102. }
  103.  
  104. bool IsVoxelInChunk (int x, int y, int z) {
  105.  
  106. if (x < 0 || x > VoxelData.ChunkWidth - 1 || y < 0 || y > VoxelData.ChunkHeight - 1 || z < 0 || z > VoxelData.ChunkWidth - 1)
  107. return false;
  108. else
  109. return true;
  110. }
  111.  
  112. bool CheckVoxel (Vector3 pos) {
  113.  
  114. int x = Mathf.FloorToInt (pos.x);
  115. int y = Mathf.FloorToInt (pos.y);
  116. int z = Mathf.FloorToInt (pos.z);
  117.  
  118. if (!IsVoxelInChunk(x, y, z))
  119. return world.CheckForVoxel(pos + position);
  120.  
  121. return world.blocktypes[voxelMap [x, y, z]].isSolid;
  122.  
  123. }
  124.  
  125. public byte GetVoxelFromGlobalVector3 (Vector3 pos) {
  126.  
  127. int xCheck = Mathf.FloorToInt(pos.x);
  128. int yCheck = Mathf.FloorToInt(pos.y);
  129. int zCheck = Mathf.FloorToInt(pos.z);
  130.  
  131. xCheck -= Mathf.FloorToInt(chunkObject.transform.position.x);
  132. zCheck -= Mathf.FloorToInt(chunkObject.transform.position.z);
  133.  
  134. return voxelMap[xCheck, yCheck, zCheck];
  135.  
  136. }
  137.  
  138. void AddVoxelDataToChunk (Vector3 pos) {
  139.  
  140. for (int p = 0; p < 6; p++) {
  141.  
  142. if (!CheckVoxel(pos + VoxelData.faceChecks[p])) {
  143.  
  144. byte blockID = voxelMap [(int)pos.x, (int)pos.y, (int)pos.z];
  145.  
  146. vertices.Add (pos + VoxelData.voxelVerts[VoxelData.voxelTris [p, 0]]);
  147. vertices.Add (pos + VoxelData.voxelVerts[VoxelData.voxelTris [p, 1]]);
  148. vertices.Add (pos + VoxelData.voxelVerts[VoxelData.voxelTris [p, 2]]);
  149. vertices.Add (pos + VoxelData.voxelVerts[VoxelData.voxelTris [p, 3]]);
  150.  
  151. AddTexture(world.blocktypes[blockID].GetTextureID(p));
  152.  
  153. triangles.Add (vertexIndex);
  154. triangles.Add (vertexIndex + 1);
  155. triangles.Add (vertexIndex + 2);
  156. triangles.Add (vertexIndex + 2);
  157. triangles.Add (vertexIndex + 1);
  158. triangles.Add (vertexIndex + 3);
  159. vertexIndex += 4;
  160.  
  161. }
  162. }
  163. }
  164.  
  165. void CreateMesh() {
  166.  
  167. Mesh mesh = new Mesh ();
  168. mesh.vertices = vertices.ToArray ();
  169. mesh.triangles = triangles.ToArray ();
  170. mesh.uv = uvs.ToArray ();
  171.  
  172. mesh.RecalculateNormals ();
  173.  
  174. meshFilter.mesh = mesh;
  175. }
  176.  
  177. void AddTexture (int textureID) {
  178.  
  179. float y = textureID / VoxelData.TextureAtlasSizeInBlocks;
  180. float x = textureID - (y * VoxelData.TextureAtlasSizeInBlocks);
  181.  
  182. x *= VoxelData.NormalizedBlockTextureSize;
  183. y *= VoxelData.NormalizedBlockTextureSize;
  184.  
  185. y = 1f - y - VoxelData.NormalizedBlockTextureSize;
  186.  
  187. uvs.Add(new Vector3(x, y));
  188. uvs.Add(new Vector3(x, y + VoxelData.NormalizedBlockTextureSize));
  189. uvs.Add(new Vector3(x + VoxelData.NormalizedBlockTextureSize, y));
  190. uvs.Add(new Vector3(x + VoxelData.NormalizedBlockTextureSize, y + VoxelData.NormalizedBlockTextureSize));
  191.  
  192. }
  193.  
  194. }
  195.  
  196. public class ChunkCoord {
  197.  
  198. public int x;
  199. public int z;
  200.  
  201. public ChunkCoord () {
  202.  
  203. x = 0;
  204. z = 0;
  205.  
  206. }
  207.  
  208. public ChunkCoord (int _x, int _z) {
  209.  
  210. x = _x;
  211. z = _z;
  212.  
  213. }
  214.  
  215. public ChunkCoord (Vector3 pos) {
  216.  
  217. int xCheck = Mathf.FloorToInt(pos.x);
  218. int zCheck = Mathf.FloorToInt(pos.z);
  219.  
  220. x = xCheck / VoxelData.ChunkWidth;
  221. z = zCheck / VoxelData.ChunkWidth;
  222.  
  223. }
  224.  
  225. public bool Equals (ChunkCoord other) {
  226.  
  227. if (other == null)
  228. return false;
  229. else if (other.x == x && other.z == z)
  230. return true;
  231. else
  232. return false;
  233.  
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement