1
0

MapGenerator.cs 6.1 KB

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