MapGenerator.cs 5.5 KB

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