ChunkTerrainGenerator.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using KairoEngine.Chunks;
  6. using KairoEngine.NoiseUtilities;
  7. using Utilities = KairoEngine.Utility.Utilities;
  8. using Sirenix.OdinInspector;
  9. using UniRx;
  10. namespace KairoEngine.TerrainEngine
  11. {
  12. [HideMonoScript]
  13. public class ChunkTerrainGenerator : MonoBehaviour
  14. {
  15. [BoxGroup("Configurations")] public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
  16. [BoxGroup("Configurations")] public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
  17. [BoxGroup("Configurations")] public Vector3Int chunkLimit = new Vector3Int(2, 1, 2);
  18. [BoxGroup("Configurations")] public int dataProcessLimit = 4;
  19. [BoxGroup("Configurations")] public int parallelMeshJobs = 4;
  20. [BoxGroup("Configurations")] public bool showChunkData = false;
  21. [InlineProperty, HideLabel, BoxGroup("Marching Cubes")] public MarchingCubes4 marchingCubes = new();
  22. [BoxGroup("Map Data Generator"), InlineProperty, HideLabel, SerializeReference] public IMapDataGenerator mapDataGenerator;
  23. [ShowIf("@showChunkData"), BoxGroup("Chunk System"), InlineProperty, HideLabel] public ChunkSystem<BlockBase> chunkSystem;
  24. public Dictionary<Vector3, GameObject> terrainMeshes = new Dictionary<Vector3, GameObject>();
  25. private List<Vector3> chunkDataBuffer = new List<Vector3>(); // Store list of chunks for populating with data
  26. private List<Vector3> chunkMeshBuffer = new List<Vector3>(); // Store list of chunks for generating the mesh
  27. private bool initialized = false;
  28. public class VoxelUpdateData
  29. {
  30. public Vector3 position;
  31. public BlockBase block;
  32. public VoxelUpdateData(Vector3 position, BlockBase block)
  33. {
  34. this.position = position;
  35. this.block = block;
  36. }
  37. }
  38. private Dictionary<Vector3,List<VoxelUpdateData>> chunkUpdates = new Dictionary<Vector3, List<VoxelUpdateData>>();
  39. private void Update()
  40. {
  41. marchingCubes.UpdateFinishedJobs();
  42. }
  43. private void OnDestroy()
  44. {
  45. marchingCubes.OnDestroy();
  46. }
  47. public void Initialize ()
  48. {
  49. chunkSystem = new ChunkSystem<BlockBase>(chunkSize,voxelSize, true);
  50. initialized = true;
  51. }
  52. public bool IsInitialized() => initialized;
  53. [Button("Generate World"), ButtonGroup("Buttons")]
  54. void GenerateWorld()
  55. {
  56. if(mapDataGenerator == null)
  57. {
  58. Debug.LogError("Missing planet template", this.gameObject);
  59. return;
  60. }
  61. DestroyTerrain();
  62. Debug.Log("Starting chunk generation");
  63. Initialize();
  64. for (int x = 0; x < chunkLimit.x; x++) {
  65. for (int z = 0; z < chunkLimit.z; z++) {
  66. for (int y = 0; y < chunkLimit.y; y++) {
  67. Vector3 chunkGlobalPos = new Vector3(x * chunkSize.x * voxelSize.x, y * chunkSize.y * voxelSize.y, z * chunkSize.z * voxelSize.z);
  68. Vector3Int chunkPos = new Vector3Int(x * chunkSize.x, y * chunkSize.y, z * chunkSize.z);
  69. chunkSystem.CreateChunk(chunkGlobalPos, chunkPos, (Chunk<BlockBase> c, Vector3Int pos) => new BlockBase(0, 0));
  70. PopulateChunkData(chunkGlobalPos);
  71. }
  72. }
  73. }
  74. for (int x = 0; x < chunkLimit.x; x++) {
  75. for (int z = 0; z < chunkLimit.z; z++) {
  76. for (int y = 0; y < chunkLimit.y; y++) {
  77. //Vector3Int chunkPos = new Vector3Int(x * chunkSize.x, y * chunkSize.y, z * chunkSize.z);
  78. Vector3 chunkPos = new Vector3(x * chunkSize.x * voxelSize.x, y * chunkSize.y * voxelSize.y, z * chunkSize.z * voxelSize.z);
  79. GenerateTerrainMesh(chunkPos);
  80. //else if(marchingCubes2 != null) marchingCubes2.Generate(chunkSystem, chunkPos);
  81. }
  82. }
  83. }
  84. #if UNITY_EDITOR
  85. UpdateEditor ();
  86. #endif
  87. }
  88. public void GenerateChunk(Vector3 position)
  89. {
  90. position = chunkSystem.GetChunkGlobalPositionFromGlobalPosition(position);
  91. bool buffered = false;
  92. for (int i = 0; i < chunkDataBuffer.Count; i++)
  93. {
  94. if(chunkDataBuffer[i] == position) buffered = true;
  95. }
  96. for (int i = 0; i < chunkMeshBuffer.Count; i++)
  97. {
  98. if(chunkMeshBuffer[i] == position) buffered = true;
  99. }
  100. //if(chunkSystem.ChunkExists(position)) buffered = true;
  101. //bool meshGenerated = terrainMeshes.TryGetValue(position, out GameObject obj);
  102. //if(meshGenerated) buffered = true;
  103. if(buffered) return;
  104. chunkDataBuffer.Add(position);
  105. }
  106. void PopulateChunkData(Vector3 initialPos)
  107. {
  108. float width = (float)(chunkLimit.x * chunkSize.x * voxelSize.x);
  109. float height = (float)(chunkLimit.y * chunkSize.y * voxelSize.y);
  110. float length = (float)(chunkLimit.z * chunkSize.z * voxelSize.z);
  111. for (int x = 0; x < chunkSize.x; x++) {
  112. for (int z = 0; z < chunkSize.z; z++) {
  113. for (int y = 0; y < chunkSize.y; y++) {
  114. // Get a terrain height using regular old Perlin noise.
  115. Vector3 pos = initialPos + new Vector3(x * voxelSize.x, y * voxelSize.y, z * voxelSize.z);
  116. // Old example noise:
  117. //float thisHeight = height * Mathf.PerlinNoise((float)pos.x / width * noiseMultiplier + 0.001f, (float)pos.z / length * noiseMultiplier + 0.001f);
  118. float elevation = mapDataGenerator.SamplePoint((float)pos.x / width, (float)pos.z / length);
  119. float thisHeight = height * elevation;
  120. uint code = mapDataGenerator.SamplePointColor((float)pos.x / width, (float)pos.z / length, elevation);
  121. BlockBase block = new BlockBase(code, (uint)Mathf.FloorToInt(thisHeight * 1000));
  122. chunkSystem.SetBlock(pos, block);
  123. }
  124. }
  125. }
  126. chunkUpdates.TryGetValue(initialPos, out List<VoxelUpdateData> updates);
  127. if(updates == null) return;
  128. for (int i = 0; i < updates.Count; i++)
  129. {
  130. chunkSystem.SetBlock(updates[i].position, updates[i].block);
  131. }
  132. }
  133. public void GenerateChunkData(Vector3 position)
  134. {
  135. position = chunkSystem.GetChunkGlobalPositionFromGlobalPosition(position);
  136. Vector3Int chunkPos = chunkSystem.GetChunkPositionFromGlobalPosition(position);
  137. if(!chunkSystem.ChunkExists(position))
  138. {
  139. //Debug.Log($"Generating Terrain Data ({position} | {chunkPos})");
  140. chunkSystem.CreateChunk(position, chunkPos, (Chunk<BlockBase> c, Vector3Int pos) => new BlockBase(0, 0));
  141. PopulateChunkData(position);
  142. }
  143. }
  144. public void GenerateTerrainMesh(Vector3 position, bool overwrite = false)
  145. {
  146. bool meshExists = terrainMeshes.TryGetValue(position, out GameObject obj);
  147. if(meshExists && !overwrite) return;
  148. if(!overwrite) terrainMeshes.Add(position, null);
  149. if(marchingCubes != null) marchingCubes.Generate(chunkSystem, this, position);
  150. }
  151. public void UpdateGenerator()
  152. {
  153. if(marchingCubes.GetJobCount() == 0) ProcessChunkDataBuffer();
  154. ProcessChunkMeshBuffer();
  155. }
  156. public void UpdateEditor ()
  157. {
  158. Timer.ExecuteRealTime(50, () => {
  159. if(marchingCubes != null)
  160. {
  161. UpdateGenerator();
  162. if(!marchingCubes.IsGeneratorDone()) UpdateEditor();
  163. }
  164. });
  165. }
  166. private void ProcessChunkDataBuffer()
  167. {
  168. int limit = chunkDataBuffer.Count < dataProcessLimit ? chunkDataBuffer.Count : dataProcessLimit;
  169. for (int i = 0; i < limit; i++)
  170. {
  171. Vector3 position = chunkDataBuffer[i];
  172. List<Vector3> positions = new List<Vector3>{
  173. new Vector3( 0, 0, 0),
  174. new Vector3( 1, 0, 0),
  175. new Vector3( 1, 0, -1),
  176. new Vector3( 0, 0, -1),
  177. new Vector3(-1, 0, -1),
  178. new Vector3(-1, 0, 0),
  179. new Vector3(-1, 0, 1),
  180. new Vector3( 0, 0, 1),
  181. new Vector3( 1, 0, 1),
  182. new Vector3( 0, 1, 0),
  183. new Vector3( 1, 1, 0),
  184. new Vector3( 1, 1, -1),
  185. new Vector3( 0, 1, -1),
  186. new Vector3(-1, 1, -1),
  187. new Vector3(-1, 1, 0),
  188. new Vector3(-1, 1, 1),
  189. new Vector3( 0, 1, 1),
  190. new Vector3( 1, 1, 1),
  191. new Vector3( 0, -1, 0),
  192. new Vector3( 1, -1, 0),
  193. new Vector3( 1, -1, -1),
  194. new Vector3( 0, -1, -1),
  195. new Vector3(-1, -1, -1),
  196. new Vector3(-1, -1, 0),
  197. new Vector3(-1, -1, 1),
  198. new Vector3( 0, -1, 1),
  199. new Vector3( 1, -1, 1),
  200. };
  201. for (int a = 0; a < positions.Count; a++)
  202. {
  203. //position = new Vector3(position.x + chunkSize.x * voxelSize.x, position.y, position.z);
  204. Vector3 chunkPos = new Vector3(
  205. position.x + (positions[a].x * chunkSize.x * voxelSize.x),
  206. position.y + (positions[a].y * chunkSize.y * voxelSize.y),
  207. position.z + (positions[a].z * chunkSize.z * voxelSize.z)
  208. );
  209. GenerateChunkData(chunkPos);
  210. }
  211. chunkMeshBuffer.Add(chunkDataBuffer[i]);
  212. }
  213. chunkDataBuffer.RemoveRange(0, limit);
  214. }
  215. private void ProcessChunkMeshBuffer()
  216. {
  217. marchingCubes.UpdateFinishedJobs();
  218. int limit = marchingCubes.GetJobCount() < parallelMeshJobs ? parallelMeshJobs - marchingCubes.GetJobCount() : 0;
  219. limit = limit < chunkMeshBuffer.Count ? limit : chunkMeshBuffer.Count;
  220. for (int i = 0; i < limit; i++)
  221. {
  222. GenerateTerrainMesh(chunkMeshBuffer[i]);
  223. }
  224. chunkMeshBuffer.RemoveRange(0, limit);
  225. }
  226. public void DestroyChunk(Vector3 position)
  227. {
  228. DestroyChunkData(position);
  229. DestroyChunkMesh(position);
  230. }
  231. public void DestroyChunkData(Vector3 position)
  232. {
  233. chunkSystem.DestroyChunk(position);
  234. }
  235. public void DestroyChunkMesh(Vector3 position)
  236. {
  237. terrainMeshes.TryGetValue(position, out GameObject obj);
  238. if(obj != null)
  239. {
  240. Destroy(obj);
  241. #if UNITY_EDITOR
  242. DestroyImmediate(obj);
  243. #endif
  244. }
  245. terrainMeshes.Remove(position);
  246. }
  247. public void ClearChunkDataBuffer() => chunkDataBuffer.Clear();
  248. [Button("Destroy"), ButtonGroup("Buttons")]
  249. public void DestroyTerrain()
  250. {
  251. var childTransforms = this.gameObject.transform.GetComponentsInChildren<Transform>();
  252. if(childTransforms.Length > 0) Debug.Log($"Destroying {childTransforms.Length - 1} terrain objects");
  253. for (int i = 0; i < childTransforms.Length; i++)
  254. {
  255. if(childTransforms[i] == this.transform) continue;
  256. #if UNITY_EDITOR
  257. DestroyImmediate(childTransforms[i].gameObject);
  258. #else
  259. Destroy(childTransforms[i].gameObject);
  260. #endif
  261. }
  262. terrainMeshes = new Dictionary<Vector3, GameObject>();
  263. chunkDataBuffer = new List<Vector3>();
  264. chunkMeshBuffer = new List<Vector3>();
  265. chunkSystem.chunks = new Dictionary<Vector3Int, Chunk<BlockBase>>();
  266. chunkSystem.chunkList = new List<Vector3>();
  267. }
  268. public enum VoxelOperation
  269. {
  270. Add,
  271. Remove,
  272. Level
  273. }
  274. public void ChangeVoxels(Vector3 pos, uint code, VoxelOperation operation, int radius = 3)
  275. {
  276. int startHeigth = 0, endHeigth = 0, targetHeigth = 0, updatedVoxels = 0;
  277. switch (operation)
  278. {
  279. case VoxelOperation.Add:
  280. startHeigth = -1;
  281. endHeigth = radius;
  282. targetHeigth = (int)(((chunkSystem.GetBlock(pos).value/1000) + (radius * voxelSize.y)) * 1000);
  283. break;
  284. case VoxelOperation.Remove:
  285. startHeigth = -1 * (radius + 1);
  286. endHeigth = radius + 1;
  287. targetHeigth = (int)(Mathf.Clamp((chunkSystem.GetBlock(pos).value/1000) - (radius * voxelSize.y), 0, 10000) * 1000);
  288. break;
  289. case VoxelOperation.Level:
  290. startHeigth = -1 * radius;
  291. endHeigth = radius;
  292. targetHeigth = (int)chunkSystem.GetBlock(pos).value;
  293. break;
  294. default:
  295. return;
  296. }
  297. Vector3 chunkPosition = chunkSystem.GetChunkGlobalPositionFromGlobalPosition(pos);
  298. List<Vector2Int> points = Utilities.GetPointsInRadius(radius, true);
  299. List<Vector3> chunkPositions = new List<Vector3>();
  300. for (int j = 0; j < points.Count; j++)
  301. {
  302. for (int y = startHeigth; y < endHeigth + 1; y++)
  303. {
  304. updatedVoxels += 1;
  305. Vector3 voxelPos = new Vector3(pos.x + points[j].x * voxelSize.x, pos.y + y * voxelSize.y, pos.z + points[j].y * voxelSize.z);
  306. BlockBase block = chunkSystem.GetBlock(voxelPos);
  307. block.code = code;
  308. block.value = (uint)targetHeigth;
  309. chunkSystem.SetBlock(voxelPos, block);
  310. // Get positions
  311. Vector3Int localVoxelPos = chunkSystem.GetVoxelFromGlobalPosition(voxelPos);
  312. Vector3 chunkPos = chunkSystem.GetChunkGlobalPositionFromGlobalPosition(voxelPos);
  313. // Save data update
  314. VoxelUpdateData updateData = new VoxelUpdateData(voxelPos, block);
  315. chunkUpdates.TryGetValue(chunkPos, out List<VoxelUpdateData> chunkUpdate);
  316. if(chunkUpdate != null)
  317. {
  318. bool exists = false;
  319. for (int h = 0; h < chunkUpdate.Count; h++)
  320. {
  321. if(chunkUpdate[h].position.Equals(updateData.position))
  322. {
  323. exists = true;
  324. chunkUpdate[h].block = updateData.block;
  325. }
  326. }
  327. if(!exists) chunkUpdate.Add(updateData);
  328. }
  329. else chunkUpdates.Add(chunkPos, new List<VoxelUpdateData>{updateData});
  330. // Check if changed voxels are in the edge of the chunk and if true add the neightboring chunk to the update list
  331. List<Vector3> chunks = new List<Vector3>();
  332. chunks.Add(chunkPos);
  333. if(localVoxelPos.x == 0) chunks.Add(new Vector3(chunkPos.x - chunkSize.x * voxelSize.x, chunkPos.y, chunkPos.z));
  334. if(localVoxelPos.x == 16) chunks.Add(new Vector3(chunkPos.x + chunkSize.x * voxelSize.x, chunkPos.y, chunkPos.z));
  335. if(localVoxelPos.y == 0) chunks.Add(new Vector3(chunkPos.x, chunkPos.y - chunkSize.y * voxelSize.y, chunkPos.z));
  336. if(localVoxelPos.y == 16) chunks.Add(new Vector3(chunkPos.x, chunkPos.y + chunkSize.y * voxelSize.y, chunkPos.z));
  337. if(localVoxelPos.z == 0) chunks.Add(new Vector3(chunkPos.x, chunkPos.y, chunkPos.z - chunkSize.z * voxelSize.z));
  338. if(localVoxelPos.z == 16) chunks.Add(new Vector3(chunkPos.x , chunkPos.y, chunkPos.z + chunkSize.z * voxelSize.z));
  339. for (int i = 0; i < chunks.Count; i++)
  340. {
  341. bool duplicate = false;
  342. for (int k = 0; k < chunkPositions.Count; k++)
  343. {
  344. if(chunkPositions[k].Equals(chunks[i]))
  345. {
  346. duplicate = true;
  347. break;
  348. }
  349. }
  350. if(!duplicate) chunkPositions.Add(chunks[i]);
  351. }
  352. }
  353. }
  354. for (int c = 0; c < chunkPositions.Count; c++) GenerateTerrainMesh(chunkPositions[c], true);
  355. Debug.Log($"Changing voxel at {pos}, updated {updatedVoxels} voxels and {chunkPositions.Count} chunks");
  356. }
  357. public void AddGeneratedTerrainMesh(Vector3 chunkPosition, GameObject meshObj)
  358. {
  359. bool meshExists = terrainMeshes.TryGetValue(chunkPosition, out GameObject currentMesh);
  360. if(currentMesh != null)
  361. {
  362. //Debug.Log($"Updating chunk mesh at {chunkPosition}");
  363. DestroyChunkMesh(chunkPosition);
  364. terrainMeshes[chunkPosition] = meshObj;
  365. }
  366. else
  367. {
  368. //Debug.Log($"Adding chunk mesh at {chunkPosition}");
  369. //terrainMeshes.Add(chunkPosition, meshObj);
  370. terrainMeshes[chunkPosition] = meshObj;
  371. }
  372. }
  373. [Button("Save"), ButtonGroup("Buttons")]
  374. public void SaveChunkUpdateData()
  375. {
  376. }
  377. }
  378. }