Advertisement
Ultimga

Untitled

Nov 12th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Concurrent;
  4. using UnityEngine;
  5.  
  6. namespace Swordfish
  7. {
  8. public class ChunkRenderer : ThreadedJob
  9. {
  10. // Keep this object alive
  11. private static ChunkRenderer _instance;
  12. public static ChunkRenderer instance
  13. {
  14. get
  15. {
  16. if (_instance == null)
  17. {
  18. _instance = UnityEngine.ScriptableObject.CreateInstance<ChunkRenderer>();
  19. }
  20.  
  21. return _instance;
  22. }
  23. }
  24.  
  25. private RenderState state = RenderState.Ready;
  26.  
  27. private ConcurrentQueue<Coord3D> queue = new ConcurrentQueue<Coord3D>();
  28.  
  29. private List<Vector3> vertices = new List<Vector3>();
  30. private List<int> triangles = new List<int>();
  31. private List<Vector3> normals = new List<Vector3>();
  32. private List<Vector2> uvs = new List<Vector2>();
  33. private List<Color> colors = new List<Color>();
  34.  
  35. public RenderState State()
  36. {
  37. return state;
  38. }
  39.  
  40. public void setState(RenderState _state)
  41. {
  42. state = _state;
  43. }
  44.  
  45. public void Queue(Coord3D _chunk)
  46. {
  47. // Make certain this chunk isn't already queued
  48. /*for (int i = 0; i < queue.Count; i++)
  49. {
  50. if (ReferenceEquals(queue[i], _chunk) == true)
  51. {
  52. return;
  53. }
  54. }*/
  55.  
  56. queue.Enqueue(_chunk);
  57.  
  58. // if (_chunk != null)// && _chunk.isVoid() == false && _chunk.isCulled() == false)
  59. // {
  60. // queue.Enqueue(_chunk);
  61. // }
  62. }
  63.  
  64. public void ClearQueue()
  65. {
  66. Coord3D entry;
  67. while (!queue.IsEmpty)
  68. {
  69. queue.TryDequeue(out entry);
  70. }
  71. }
  72.  
  73. public int QueueSize()
  74. {
  75. return queue.Count;
  76. }
  77.  
  78. protected override void ThreadFunction()
  79. {
  80. while (stop != true)
  81. {
  82. if (state == RenderState.Stopped)
  83. {
  84. continue;
  85. }
  86. else if (state == RenderState.Stopping)
  87. {
  88. state = RenderState.Stopped;
  89. continue;
  90. }
  91.  
  92. if (queue.Count == 0)
  93. {
  94. state = RenderState.Waiting;
  95. }
  96. else
  97. {
  98. state = RenderState.Rendering;
  99.  
  100. Coord3D thisPosition;
  101. bool dequeued = queue.TryDequeue(out thisPosition);
  102.  
  103. if (dequeued == false || thisPosition == null || thisPosition.voxelObject == null) { continue; }
  104.  
  105. Chunk thisChunk = thisPosition.voxelObject.getChunk(thisPosition.x, thisPosition.y, thisPosition.z);
  106.  
  107. if (thisChunk == null)
  108. {
  109. continue;
  110. }
  111.  
  112. thisChunk.setRenderState(RenderState.Rendering);
  113.  
  114. vertices.Clear();
  115. triangles.Clear();
  116. normals.Clear();
  117. uvs.Clear();
  118. colors.Clear();
  119.  
  120. // Loop all blocks in this chunk
  121. for (int n = 0; n < thisChunk.getSize(); n++)
  122. {
  123. if (state == RenderState.Stopped)
  124. {
  125. thisChunk.setRenderState(RenderState.Stopped);
  126. break;
  127. }
  128.  
  129. Block thisBlock = thisChunk.raw(n);
  130. if (thisBlock == null)
  131. {
  132. continue;
  133. }
  134. else
  135. {
  136. thisBlock.OnPreRender();
  137.  
  138. Vector3 scale;
  139. switch (thisBlock.getModelType())
  140. {
  141. case ModelType.CUBE:
  142. // Top face
  143. if (thisBlock.isFaceCulled(Direction.ABOVE) == false)
  144. {
  145. ModelBuilder.Cube.Top(vertices, triangles, normals, uvs, colors, thisBlock);
  146. }
  147.  
  148. // Bottom face
  149. if (thisBlock.isFaceCulled(Direction.BELOW) == false)
  150. {
  151. ModelBuilder.Cube.Bottom(vertices, triangles, normals, uvs, colors, thisBlock);
  152. }
  153.  
  154. // North face
  155. if (thisBlock.isFaceCulled(Direction.NORTH) == false)
  156. {
  157. ModelBuilder.Cube.North(vertices, triangles, normals, uvs, colors, thisBlock);
  158. }
  159.  
  160. // South face
  161. if (thisBlock.isFaceCulled(Direction.SOUTH) == false)
  162. {
  163. ModelBuilder.Cube.South(vertices, triangles, normals, uvs, colors, thisBlock);
  164. }
  165.  
  166. // East face
  167. if (thisBlock.isFaceCulled(Direction.EAST) == false)
  168. {
  169. ModelBuilder.Cube.East(vertices, triangles, normals, uvs, colors, thisBlock);
  170. }
  171.  
  172. // West face
  173. if (thisBlock.isFaceCulled(Direction.WEST) == false)
  174. {
  175. ModelBuilder.Cube.West(vertices, triangles, normals, uvs, colors, thisBlock);
  176. }
  177. break;
  178.  
  179. case ModelType.SLOPE:
  180. if (thisBlock.isCulled() == false)
  181. {
  182. ModelBuilder.Slope.Face(vertices, triangles, normals, uvs, colors, thisBlock);
  183. ModelBuilder.Slope.Bottom(vertices, triangles, normals, uvs, colors, thisBlock);
  184. ModelBuilder.Slope.North(vertices, triangles, normals, uvs, colors, thisBlock);
  185. ModelBuilder.Slope.East(vertices, triangles, normals, uvs, colors, thisBlock);
  186. ModelBuilder.Slope.West(vertices, triangles, normals, uvs, colors, thisBlock);
  187. }
  188. break;
  189.  
  190. case ModelType.CROSS_SECTION_SMALL:
  191. if (thisBlock.isCulled() == false)
  192. {
  193. ModelBuilder.CrossSection.Small.Build(vertices, triangles, normals, uvs, colors, thisBlock);
  194. }
  195. break;
  196.  
  197. case ModelType.CUSTOM:
  198. if (thisBlock.isCulled() == false)
  199. {
  200. ModelBuilder.Custom.Build(vertices, triangles, normals, uvs, colors, thisBlock.getModelData(), thisBlock);
  201. }
  202. break;
  203.  
  204. case ModelType.CUSTOM_CUBE:
  205. if (thisBlock.isCulled() == false)
  206. {
  207. ModelBuilder.Cube.Top(vertices, triangles, normals, uvs, colors, thisBlock);
  208. ModelBuilder.Cube.Bottom(vertices, triangles, normals, uvs, colors, thisBlock);
  209. ModelBuilder.Cube.North(vertices, triangles, normals, uvs, colors, thisBlock);
  210. ModelBuilder.Cube.South(vertices, triangles, normals, uvs, colors, thisBlock);
  211. ModelBuilder.Cube.East(vertices, triangles, normals, uvs, colors, thisBlock);
  212. ModelBuilder.Cube.West(vertices, triangles, normals, uvs, colors, thisBlock);
  213. }
  214. break;
  215.  
  216. case ModelType.CUBE_HALF:
  217. if (thisBlock.isCulled() == false)
  218. {
  219. scale = new Vector3(1.0f, 1.0f, 0.5f);
  220. ModelBuilder.Cube.Top(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  221. ModelBuilder.Cube.Bottom(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  222. ModelBuilder.Cube.North(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  223. ModelBuilder.Cube.South(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  224. ModelBuilder.Cube.East(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  225. ModelBuilder.Cube.West(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  226. }
  227. break;
  228.  
  229. case ModelType.CUBE_PLATE:
  230. if (thisBlock.isCulled() == false)
  231. {
  232. scale = new Vector3(1.0f, 1.0f, 0.25f);
  233. ModelBuilder.Cube.Top(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  234. ModelBuilder.Cube.Bottom(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  235. ModelBuilder.Cube.North(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  236. ModelBuilder.Cube.South(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  237. ModelBuilder.Cube.East(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  238. ModelBuilder.Cube.West(vertices, triangles, normals, uvs, colors, thisBlock, scale);
  239. }
  240. break;
  241. }
  242. }
  243. }
  244.  
  245. if (state != RenderState.Stopped)
  246. {
  247. thisChunk.PrepareRenderBuffer(new RenderData(vertices, triangles, normals, uvs, colors));
  248. state = RenderState.Waiting;
  249. }
  250. }
  251.  
  252. updates++; // Increment update count
  253. }
  254.  
  255. IsDone = true;
  256. }
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement