MapGenerator.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography;
  5. using System.Threading;
  6. using UnityEditor.AI;
  7. using UnityEngine;
  8. public class MapGenerator : MonoBehaviour
  9. {
  10. public enum DrawMode { NoiseMap, Mesh, FalloffMap };
  11. public DrawMode drawMode;
  12. public TerrainData terrainData;
  13. public NoiseData noiseData;
  14. public TextureData textureData;
  15. public Material terrainMaterial;
  16. [Range(0, 6)]
  17. public int editorPreviewLOD;
  18. public bool autoUpdate;
  19. float[,] falloffMap;
  20. Queue<MapThreadInfo<MapData>> mapDataThreadInfoQueue = new Queue<MapThreadInfo<MapData>>();
  21. Queue<MapThreadInfo<MeshData>> meshDataThreadInfoQueue = new Queue<MapThreadInfo<MeshData>>();
  22. void OnValuesUpdated()
  23. {
  24. if (!Application.isPlaying)
  25. {
  26. DrawMapInEditor();
  27. }
  28. }
  29. void OnTextureValuesUpdated()
  30. {
  31. textureData.ApplyToMaterial(terrainMaterial);
  32. }
  33. public int mapChunkSize
  34. {
  35. get
  36. {
  37. if (terrainData.useFlatShading)
  38. {
  39. return 95;
  40. }
  41. else
  42. {
  43. return 239;
  44. }
  45. }
  46. }
  47. private void Awake()
  48. {
  49. textureData.UpdateMeshHeights(terrainMaterial, terrainData.minHeight, terrainData.maxHeight);
  50. }
  51. public void DrawMapInEditor()
  52. {
  53. textureData.UpdateMeshHeights(terrainMaterial, terrainData.minHeight, terrainData.maxHeight);
  54. MapData mapData = GenerateMapData(Vector2.zero);
  55. MapDisplay display = FindFirstObjectByType<MapDisplay>();
  56. if (drawMode == DrawMode.NoiseMap)
  57. {
  58. display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
  59. }
  60. else if (drawMode == DrawMode.Mesh)
  61. {
  62. display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, terrainData.meshHeightMultiplier, terrainData.meshHeightCurve, editorPreviewLOD, terrainData.useFlatShading));
  63. }
  64. else if (drawMode == DrawMode.FalloffMap)
  65. {
  66. display.DrawTexture(TextureGenerator.TextureFromHeightMap(FallOffGenerator.GenerateFallOffMap(mapChunkSize)));
  67. }
  68. }
  69. public void RequestMapData(Vector2 centre, Action<MapData> callback)
  70. {
  71. ThreadStart threadStart = delegate {
  72. MapDataThread(centre, callback);
  73. };
  74. new Thread(threadStart).Start();
  75. }
  76. void MapDataThread(Vector2 centre, Action<MapData> callback)
  77. {
  78. MapData mapData = GenerateMapData(centre);
  79. lock (mapDataThreadInfoQueue)
  80. {
  81. mapDataThreadInfoQueue.Enqueue(new MapThreadInfo<MapData>(callback, mapData));
  82. }
  83. }
  84. public void RequestMeshData(MapData mapData, int lod, Action<MeshData> callback)
  85. {
  86. ThreadStart threadStart = delegate {
  87. MeshDataThread(mapData, lod, callback);
  88. };
  89. new Thread(threadStart).Start();
  90. }
  91. void MeshDataThread(MapData mapData, int lod, Action<MeshData> callback)
  92. {
  93. MeshData meshData = MeshGenerator.GenerateTerrainMesh(mapData.heightMap, terrainData.meshHeightMultiplier, terrainData.meshHeightCurve, lod, terrainData.useFlatShading);
  94. lock (meshDataThreadInfoQueue)
  95. {
  96. meshDataThreadInfoQueue.Enqueue(new MapThreadInfo<MeshData>(callback, meshData));
  97. }
  98. }
  99. void Update()
  100. {
  101. if (mapDataThreadInfoQueue.Count > 0)
  102. {
  103. for (int i = 0; i < mapDataThreadInfoQueue.Count; i++)
  104. {
  105. MapThreadInfo<MapData> threadInfo = mapDataThreadInfoQueue.Dequeue();
  106. threadInfo.callback(threadInfo.parameter);
  107. }
  108. }
  109. if (meshDataThreadInfoQueue.Count > 0)
  110. {
  111. for (int i = 0; i < meshDataThreadInfoQueue.Count; i++)
  112. {
  113. MapThreadInfo<MeshData> threadInfo = meshDataThreadInfoQueue.Dequeue();
  114. threadInfo.callback(threadInfo.parameter);
  115. }
  116. }
  117. if (ForestData.spawnable.Count > 0)
  118. {
  119. for (int i = 0; i < ForestData.spawnable.Count; i++)
  120. {
  121. Vector3 position = ForestData.spawnable.Dequeue();
  122. spawnTree(position);
  123. }
  124. }
  125. }
  126. public void spawnTree(Vector3 position)
  127. {
  128. float rnd = UnityEngine.Random.Range(0f, 1f);
  129. if(rnd < ForestData.spawnRate)
  130. {
  131. Vector3 rayStart = position + Vector3.up * 300;
  132. if (Physics.Raycast(rayStart, Vector3.down, out RaycastHit hit, 600f))
  133. {
  134. if(hit.point.y < ForestData.maxHeight && hit.point.y > ForestData.minHeight)
  135. {
  136. int rndTree = UnityEngine.Random.Range(0, ForestData.Trees.Length);
  137. UnityEngine.Object.Instantiate(ForestData.Trees[rndTree], hit.point, Quaternion.identity);
  138. }
  139. }
  140. }
  141. }
  142. private MapData GenerateMapData(Vector2 centre)
  143. {
  144. float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, noiseData.seed, noiseData.noiseScale, noiseData.octaves, noiseData.persistance, noiseData.lacunarity, centre + noiseData.offset, noiseData.normalizeMode);
  145. if (terrainData.useFalloff)
  146. {
  147. if(falloffMap == null)
  148. {
  149. falloffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize + 2);
  150. }
  151. for (int y = 0; y < mapChunkSize + 2; y++)
  152. {
  153. for (int x = 0; x < mapChunkSize + 2; x++)
  154. {
  155. if (terrainData.useFalloff)
  156. {
  157. noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x, y]);
  158. }
  159. }
  160. }
  161. }
  162. return new MapData(noiseMap);
  163. }
  164. void OnValidate()
  165. {
  166. if(terrainData != null)
  167. {
  168. terrainData.OnValuesUpdated -= OnValuesUpdated;
  169. terrainData.OnValuesUpdated += OnValuesUpdated;
  170. }
  171. if (noiseData != null)
  172. {
  173. noiseData.OnValuesUpdated -= OnValuesUpdated;
  174. noiseData.OnValuesUpdated += OnValuesUpdated;
  175. }
  176. if(textureData != null)
  177. {
  178. textureData.OnValuesUpdated -= OnTextureValuesUpdated;
  179. textureData.OnValuesUpdated += OnTextureValuesUpdated;
  180. }
  181. }
  182. struct MapThreadInfo<T>
  183. {
  184. public readonly Action<T> callback;
  185. public readonly T parameter;
  186. public MapThreadInfo(Action<T> callback, T parameter)
  187. {
  188. this.callback = callback;
  189. this.parameter = parameter;
  190. }
  191. }
  192. }
  193. public struct MapData
  194. {
  195. public readonly float[,] heightMap;
  196. public MapData(float[,] heightMap)
  197. {
  198. this.heightMap = heightMap;
  199. }
  200. }