Advertisement
Teonnyn

VoxelEngine

Aug 26th, 2013
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class ChunkRenderer : MonoBehaviour {
  6.  
  7.  
  8. public Texture basicTexture;
  9. public int[,,] chunk;
  10.  
  11.  
  12. int height = 6;
  13. int width = 2;
  14. int depth = 3;
  15.  
  16. LineRenderer lineRenderer;
  17.  
  18. MeshCollider meshCollider;
  19. MeshFilter meshFilter;
  20. MeshRenderer meshRenderer;
  21. Mesh mesh;
  22.  
  23. // Use this for initialization
  24. void Start () {
  25. chunk = new int[width,height,depth];
  26.  
  27. // Set Block Type //
  28.  
  29. for (int x = 0; x < width; x++)
  30. {
  31. for (int y = 0; y < height; y++)
  32. {
  33. for (int z = 0; z < depth; z++)
  34. {
  35. chunk[x,y,z] = 1;
  36. }
  37. }
  38. }
  39.  
  40. // Add Mesh Pieces //
  41.  
  42. meshCollider = gameObject.AddComponent<MeshCollider>();
  43. meshFilter = gameObject.AddComponent<MeshFilter>();
  44. meshRenderer = gameObject.AddComponent<MeshRenderer>();
  45.  
  46. ChunkRender();
  47. }
  48.  
  49. // Update is called once per frame
  50. void Update () {
  51. //ChunkRender(chunk);
  52. }
  53.  
  54. public void ChunkRender()
  55. {
  56. List<Vector3> vertices = new List<Vector3>();
  57. List<Vector2> uvs = new List<Vector2>();
  58. List<int> triangles = new List<int>();
  59.  
  60. int vertexIndex = 0;
  61. int top = 0;
  62. int north = 0;
  63. int east = 0;
  64. int south = 0;
  65. int west = 0;
  66. int bottom = 0;
  67.  
  68. meshFilter.mesh.Clear();
  69.  
  70. for (int x = 0; x < width; x++)
  71. {
  72. for (int y = 0; y < height; y++)
  73. {
  74. for (int z = 0; z < depth; z++)
  75. {
  76. Block block = new Block();
  77. block.blockType = chunk[x,y,z];
  78.  
  79. if (x == width - 1) {
  80. east = 0;
  81. } else {
  82. east = chunk[x + 1, y, z];
  83. }
  84.  
  85. if (x == 0) {
  86. west = 0;
  87. } else {
  88. west = chunk[x - 1, y, z];
  89. }
  90.  
  91. if (y == height - 1) {
  92. top = 0;
  93. } else {
  94. top = chunk[x, y + 1, z];
  95. }
  96.  
  97. if (y == 0) {
  98. bottom = 0;
  99. } else {
  100. bottom = chunk[x, y - 1, z];
  101. }
  102.  
  103. if (z == depth - 1) {
  104. north = 0;
  105. } else {
  106. north = chunk[x, y, z + 1];
  107. }
  108.  
  109. if (z == 0) {
  110. south = 0;
  111. } else {
  112. south = chunk[x, y, z - 1];
  113. }
  114.  
  115. // Check which faces to render //
  116.  
  117. // Grassy Block - Finished //
  118.  
  119. if (block.blockType == 1 && top == 0)
  120. {
  121. vertexIndex = vertices.Count;
  122. vertices.Add(new Vector3(x, y + 1, z));
  123. vertices.Add(new Vector3(x, y + 1, z + 1));
  124. vertices.Add(new Vector3(x + 1, y + 1, z + 1));
  125. vertices.Add(new Vector3(x + 1, y + 1, z));
  126. // first triangle for the block top
  127. triangles.Add(vertexIndex);
  128. triangles.Add(vertexIndex + 1);
  129. triangles.Add(vertexIndex + 2);
  130. // second triangle for the block top
  131. triangles.Add(vertexIndex + 2);
  132. triangles.Add(vertexIndex + 3);
  133. triangles.Add(vertexIndex);
  134. // add UV
  135. uvs.Add(new Vector2 (0.125f, 0.0f));
  136. uvs.Add(new Vector2 (0.25f, 0.0f));
  137. uvs.Add(new Vector2 (0.25f, 0.125f));
  138. uvs.Add(new Vector2 (0.125f, 0.125f));
  139.  
  140. }
  141. // Bottom is Done //
  142.  
  143. if (block.blockType == 1 && bottom == 0)
  144. {
  145. vertexIndex = vertices.Count;
  146. vertices.Add(new Vector3(x, y, z));
  147. vertices.Add(new Vector3(x + 1, y, z));
  148. vertices.Add(new Vector3(x + 1, y, z + 1));
  149. vertices.Add(new Vector3(x, y , z + 1));
  150.  
  151. triangles.Add(vertexIndex);
  152. triangles.Add(vertexIndex + 1);
  153. triangles.Add(vertexIndex + 2);
  154.  
  155. triangles.Add(vertexIndex + 2);
  156. triangles.Add(vertexIndex + 3);
  157. triangles.Add(vertexIndex);
  158. // add UV
  159. uvs.Add(new Vector2 (0.25f, 0.0f));
  160. uvs.Add(new Vector2 (0.375f, 0.0f));
  161. uvs.Add(new Vector2 (0.375f, 0.125f));
  162. uvs.Add(new Vector2 (0.25f, 0.125f));
  163.  
  164. }
  165.  
  166. // North Block faces Away //
  167.  
  168. if (block.blockType == 1 && north == 0)
  169. {
  170. vertexIndex = vertices.Count;
  171. vertices.Add(new Vector3(x, y, z + 1));
  172. vertices.Add(new Vector3(x + 1, y, z + 1));
  173. vertices.Add(new Vector3(x + 1, y + 1, z + 1));
  174. vertices.Add(new Vector3(x, y + 1 , z + 1));
  175.  
  176. triangles.Add(vertexIndex);
  177. triangles.Add(vertexIndex + 1);
  178. triangles.Add(vertexIndex + 2);
  179.  
  180. triangles.Add(vertexIndex + 2);
  181. triangles.Add(vertexIndex + 3);
  182. triangles.Add(vertexIndex);
  183. // add UV
  184. uvs.Add(new Vector2 (0.75f, 0.0f));
  185. uvs.Add(new Vector2 (0.625f, 0.0f));
  186. uvs.Add(new Vector2 (0.625f, 0.125f));
  187. uvs.Add(new Vector2 (0.75f, 0.125f));
  188. }
  189.  
  190. // South block faces Player
  191.  
  192. if (block.blockType == 1 && south == 0)
  193. {
  194. vertexIndex = vertices.Count;
  195. vertices.Add(new Vector3(x, y, z));
  196. vertices.Add(new Vector3(x, y + 1, z));
  197. vertices.Add(new Vector3(x + 1, y + 1, z));
  198. vertices.Add(new Vector3(x + 1, y , z));
  199.  
  200. triangles.Add(vertexIndex);
  201. triangles.Add(vertexIndex + 1);
  202. triangles.Add(vertexIndex + 2);
  203.  
  204. triangles.Add(vertexIndex + 2);
  205. triangles.Add(vertexIndex + 3);
  206. triangles.Add(vertexIndex);
  207. // add UV
  208. uvs.Add(new Vector2 (0.0f, 0.0f));
  209. uvs.Add(new Vector2 (0.125f, 0.0f));
  210. uvs.Add(new Vector2 (0.125f, 0.125f));
  211. uvs.Add(new Vector2 (0.0f, 0.125f));
  212. }
  213.  
  214. if (block.blockType == 1 && east == 0)
  215. {
  216. vertexIndex = vertices.Count;
  217. vertices.Add(new Vector3(x + 1, y, z));
  218. vertices.Add(new Vector3(x + 1, y + 1, z));
  219. vertices.Add(new Vector3(x + 1, y + 1, z + 1));
  220. vertices.Add(new Vector3(x + 1, y , z + 1));
  221.  
  222. triangles.Add(vertexIndex);
  223. triangles.Add(vertexIndex + 1);
  224. triangles.Add(vertexIndex + 2);
  225.  
  226. triangles.Add(vertexIndex + 2);
  227. triangles.Add(vertexIndex + 3);
  228. triangles.Add(vertexIndex);
  229. // add UV
  230. uvs.Add(new Vector2 (0.25f, 0.0f));
  231. uvs.Add(new Vector2 (0.375f, 0.0f));
  232. uvs.Add(new Vector2 (0.375f, 0.125f));
  233. uvs.Add(new Vector2 (0.25f, 0.125f));
  234. }
  235.  
  236. if (block.blockType == 1 && west == 0)
  237. {
  238. vertexIndex = vertices.Count;
  239. vertices.Add(new Vector3(x, y, z + 1));
  240. vertices.Add(new Vector3(x, y + 1, z + 1));
  241. vertices.Add(new Vector3(x, y + 1, z));
  242. vertices.Add(new Vector3(x, y , z));
  243.  
  244. triangles.Add(vertexIndex);
  245. triangles.Add(vertexIndex + 1);
  246. triangles.Add(vertexIndex + 2);
  247.  
  248. triangles.Add(vertexIndex + 2);
  249. triangles.Add(vertexIndex + 3);
  250. triangles.Add(vertexIndex);
  251. // add UV
  252. uvs.Add(new Vector2 (0.375f, 0.0f));
  253. uvs.Add(new Vector2 (0.50f, 0.0f));
  254. uvs.Add(new Vector2 (0.50f, 0.125f));
  255. uvs.Add(new Vector2 (0.375f, 0.125f));
  256. }
  257. }
  258. }
  259. }
  260.  
  261. // End Loop //
  262.  
  263. mesh = meshFilter.mesh;
  264. mesh.vertices = vertices.ToArray();
  265. mesh.triangles = triangles.ToArray();
  266. mesh.uv = uvs.ToArray();
  267.  
  268. mesh.RecalculateNormals();
  269.  
  270. meshCollider.sharedMesh = null;
  271. meshCollider.sharedMesh = mesh;
  272.  
  273.  
  274. gameObject.renderer.material.mainTexture = basicTexture;
  275.  
  276. }
  277.  
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement