MapGenerator.cs 6.3 KB

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