MapGenerator.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Threading;
  5. using System.Collections.Generic;
  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 editorPreviewLOD;
  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. }
  49. public void DrawMapInEditor()
  50. {
  51. textureData.UpdateMeshHeights(terrainMaterial, terrainData.minHeight, terrainData.maxHeight);
  52. MapData mapData = GenerateMapData(Vector2.zero);
  53. MapDisplay display = FindFirstObjectByType<MapDisplay>();
  54. if (drawMode == DrawMode.NoiseMap)
  55. {
  56. display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
  57. }
  58. else if (drawMode == DrawMode.Mesh)
  59. {
  60. display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, terrainData.meshHeightMultiplier, terrainData.meshHeightCurve, editorPreviewLOD, terrainData.useFlatShading));
  61. }
  62. else if (drawMode == DrawMode.FalloffMap)
  63. {
  64. display.DrawTexture(TextureGenerator.TextureFromHeightMap(FallOffGenerator.GenerateFallOffMap(mapChunkSize)));
  65. }
  66. }
  67. public void RequestMapData(Vector2 centre, Action<MapData> callback)
  68. {
  69. ThreadStart threadStart = delegate {
  70. MapDataThread(centre, callback);
  71. };
  72. new Thread(threadStart).Start();
  73. }
  74. void MapDataThread(Vector2 centre, Action<MapData> callback)
  75. {
  76. MapData mapData = GenerateMapData(centre);
  77. lock (mapDataThreadInfoQueue)
  78. {
  79. mapDataThreadInfoQueue.Enqueue(new MapThreadInfo<MapData>(callback, mapData));
  80. }
  81. }
  82. public void RequestMeshData(MapData mapData, int lod, Action<MeshData> callback)
  83. {
  84. ThreadStart threadStart = delegate {
  85. MeshDataThread(mapData, lod, callback);
  86. };
  87. new Thread(threadStart).Start();
  88. }
  89. void MeshDataThread(MapData mapData, int lod, Action<MeshData> callback)
  90. {
  91. MeshData meshData = MeshGenerator.GenerateTerrainMesh(mapData.heightMap, terrainData.meshHeightMultiplier, terrainData.meshHeightCurve, lod, terrainData.useFlatShading);
  92. lock (meshDataThreadInfoQueue)
  93. {
  94. meshDataThreadInfoQueue.Enqueue(new MapThreadInfo<MeshData>(callback, meshData));
  95. }
  96. }
  97. void Update()
  98. {
  99. if (mapDataThreadInfoQueue.Count > 0)
  100. {
  101. for (int i = 0; i < mapDataThreadInfoQueue.Count; i++)
  102. {
  103. MapThreadInfo<MapData> threadInfo = mapDataThreadInfoQueue.Dequeue();
  104. threadInfo.callback(threadInfo.parameter);
  105. }
  106. }
  107. if (meshDataThreadInfoQueue.Count > 0)
  108. {
  109. for (int i = 0; i < meshDataThreadInfoQueue.Count; i++)
  110. {
  111. MapThreadInfo<MeshData> threadInfo = meshDataThreadInfoQueue.Dequeue();
  112. threadInfo.callback(threadInfo.parameter);
  113. }
  114. }
  115. }
  116. MapData GenerateMapData(Vector2 centre)
  117. {
  118. float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, noiseData.seed, noiseData.noiseScale, noiseData.octaves, noiseData.persistance, noiseData.lacunarity, centre + noiseData.offset, noiseData.normalizeMode);
  119. if (terrainData.useFalloff)
  120. {
  121. if(falloffMap == null)
  122. {
  123. falloffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize + 2);
  124. }
  125. for (int y = 0; y < mapChunkSize + 2; y++)
  126. {
  127. for (int x = 0; x < mapChunkSize + 2; x++)
  128. {
  129. if (terrainData.useFalloff)
  130. {
  131. noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x, y]);
  132. }
  133. }
  134. }
  135. }
  136. return new MapData(noiseMap);
  137. }
  138. void OnValidate()
  139. {
  140. if(terrainData != null)
  141. {
  142. terrainData.OnValuesUpdated -= OnValuesUpdated;
  143. terrainData.OnValuesUpdated += OnValuesUpdated;
  144. }
  145. if (noiseData != null)
  146. {
  147. noiseData.OnValuesUpdated -= OnValuesUpdated;
  148. noiseData.OnValuesUpdated += OnValuesUpdated;
  149. }
  150. if(textureData != null)
  151. {
  152. textureData.OnValuesUpdated -= OnTextureValuesUpdated;
  153. textureData.OnValuesUpdated += OnTextureValuesUpdated;
  154. }
  155. }
  156. struct MapThreadInfo<T>
  157. {
  158. public readonly Action<T> callback;
  159. public readonly T parameter;
  160. public MapThreadInfo(Action<T> callback, T parameter)
  161. {
  162. this.callback = callback;
  163. this.parameter = parameter;
  164. }
  165. }
  166. }
  167. public struct MapData
  168. {
  169. public readonly float[,] heightMap;
  170. public MapData(float[,] heightMap)
  171. {
  172. this.heightMap = heightMap;
  173. }
  174. }