MapGenerator.cs 6.1 KB

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