1
0

MapGenerator.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Threading;
  5. using System.Collections.Generic;
  6. using UnityEditor.AI;
  7. public class MapGenerator : MonoBehaviour
  8. {
  9. public enum DrawMode { NoiseMap, Mesh, FalloffMap };
  10. public DrawMode drawMode;
  11. public TerrainData terrainData;
  12. public NoiseData noiseData;
  13. public TextureData textureData;
  14. public Material terrainMaterial;
  15. [Range(0, 6)]
  16. public int editorPreviewLOD;
  17. public bool autoUpdate;
  18. float[,] falloffMap;
  19. public ForestManager forestManager;
  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. for (int x=0; x<terrainData.)
  109. forestManager.SpawnTree(threadInfo.parameter.)
  110. */
  111. }
  112. }
  113. if (meshDataThreadInfoQueue.Count > 0)
  114. {
  115. for (int i = 0; i < meshDataThreadInfoQueue.Count; i++)
  116. {
  117. MapThreadInfo<MeshData> threadInfo = meshDataThreadInfoQueue.Dequeue();
  118. threadInfo.callback(threadInfo.parameter);
  119. }
  120. }
  121. }
  122. private MapData GenerateMapData(Vector2 centre)
  123. {
  124. float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, noiseData.seed, noiseData.noiseScale, noiseData.octaves, noiseData.persistance, noiseData.lacunarity, centre + noiseData.offset, noiseData.normalizeMode);
  125. if (terrainData.useFalloff)
  126. {
  127. if(falloffMap == null)
  128. {
  129. falloffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize + 2);
  130. }
  131. for (int y = 0; y < mapChunkSize + 2; y++)
  132. {
  133. for (int x = 0; x < mapChunkSize + 2; x++)
  134. {
  135. if (terrainData.useFalloff)
  136. {
  137. noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x, y]);
  138. }
  139. }
  140. }
  141. }
  142. return new MapData(noiseMap);
  143. }
  144. void OnValidate()
  145. {
  146. if(terrainData != null)
  147. {
  148. terrainData.OnValuesUpdated -= OnValuesUpdated;
  149. terrainData.OnValuesUpdated += OnValuesUpdated;
  150. }
  151. if (noiseData != null)
  152. {
  153. noiseData.OnValuesUpdated -= OnValuesUpdated;
  154. noiseData.OnValuesUpdated += OnValuesUpdated;
  155. }
  156. if(textureData != null)
  157. {
  158. textureData.OnValuesUpdated -= OnTextureValuesUpdated;
  159. textureData.OnValuesUpdated += OnTextureValuesUpdated;
  160. }
  161. }
  162. struct MapThreadInfo<T>
  163. {
  164. public readonly Action<T> callback;
  165. public readonly T parameter;
  166. public MapThreadInfo(Action<T> callback, T parameter)
  167. {
  168. this.callback = callback;
  169. this.parameter = parameter;
  170. }
  171. }
  172. }
  173. public struct MapData
  174. {
  175. public readonly float[,] heightMap;
  176. public MapData(float[,] heightMap)
  177. {
  178. this.heightMap = heightMap;
  179. }
  180. }