1
0

MapGenerator.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using UnityEngine;
  2. using System;
  3. using System.Threading;
  4. using System.Collections.Generic;
  5. public class MapGenerator : MonoBehaviour
  6. {
  7. public enum DrawMode { NoiseMap, ColourMap, Mesh, FallOff};
  8. public DrawMode drawMode;
  9. public Noise.NormalizeMode normalizeMode;
  10. public const int mapChunkSize = 241;
  11. [Range(0,6)]
  12. public int editorPreviewLOD;
  13. public float noiseScale;
  14. public int octaves;
  15. [Range(0,1)]
  16. public float persistance;
  17. public float lacunarity;
  18. public int seed;
  19. public Vector2 offset;
  20. public bool useFallOff;
  21. public float meshHeightMultiplier;
  22. public AnimationCurve meshHeightCurve;
  23. public bool autoUpdate;
  24. public TerrainType[] regions;
  25. float[,] fallOffMap;
  26. Queue<MapThreadInfo<MapData>> mapDataThreadInfoQueue = new Queue<MapThreadInfo<MapData>>();
  27. Queue<MapThreadInfo<MeshData>> meshDataThreadInfoQueue = new Queue<MapThreadInfo<MeshData>>();
  28. private void Awake()
  29. {
  30. fallOffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize);
  31. }
  32. public void DrawMapInEditor()
  33. {
  34. MapData mapData = GenerateMapData(Vector2.zero);
  35. MapDisplay display = FindFirstObjectByType<MapDisplay>();
  36. if (drawMode == DrawMode.NoiseMap)
  37. {
  38. display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
  39. }
  40. else if (drawMode == DrawMode.ColourMap)
  41. {
  42. display.DrawTexture(TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
  43. }
  44. else if (drawMode == DrawMode.Mesh)
  45. {
  46. display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, editorPreviewLOD), TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
  47. }else if (drawMode ==DrawMode.FallOff)
  48. {
  49. display.DrawTexture(TextureGenerator.TextureFromHeightMap(FallOffGenerator.GenerateFallOffMap(mapChunkSize)));
  50. }
  51. }
  52. public void RequestMapData(Vector2 centre, Action<MapData> callback)
  53. {
  54. ThreadStart threadStart = delegate
  55. {
  56. MapDataThread(centre, callback);
  57. };
  58. new Thread(threadStart).Start();
  59. }
  60. void MapDataThread(Vector2 centre, Action<MapData> callback)
  61. {
  62. MapData mapData = GenerateMapData(centre);
  63. lock (mapDataThreadInfoQueue)
  64. {
  65. mapDataThreadInfoQueue.Enqueue(new MapThreadInfo<MapData>(callback, mapData));
  66. }
  67. }
  68. public void RequestMeshData(MapData mapData, int lod, Action<MeshData> callback)
  69. {
  70. ThreadStart threadStart = delegate
  71. {
  72. MeshDataThread(mapData, lod, callback);
  73. };
  74. new Thread(threadStart).Start();
  75. }
  76. void MeshDataThread(MapData mapData, int lod, Action<MeshData> callback)
  77. {
  78. MeshData meshData = MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, lod);
  79. lock (meshDataThreadInfoQueue)
  80. {
  81. meshDataThreadInfoQueue.Enqueue(new MapThreadInfo<MeshData>(callback, meshData));
  82. }
  83. }
  84. private void Update()
  85. {
  86. if (mapDataThreadInfoQueue.Count > 0)
  87. {
  88. for (int i = 0; i < mapDataThreadInfoQueue.Count; i++)
  89. {
  90. MapThreadInfo<MapData> threadInfo = mapDataThreadInfoQueue.Dequeue();
  91. threadInfo.callback(threadInfo.parameter);
  92. }
  93. }
  94. if (meshDataThreadInfoQueue.Count > 0)
  95. {
  96. for(int i = 0; i < meshDataThreadInfoQueue.Count; i++)
  97. {
  98. MapThreadInfo<MeshData> threadInfo = meshDataThreadInfoQueue.Dequeue();
  99. threadInfo.callback(threadInfo.parameter);
  100. }
  101. }
  102. }
  103. MapData GenerateMapData(Vector2 centre)
  104. {
  105. float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, centre + offset, normalizeMode);
  106. Color[] colourMap = new Color[mapChunkSize * mapChunkSize];
  107. for(int y=0; y<mapChunkSize; y++)
  108. {
  109. for (int x=0; x<mapChunkSize; x++)
  110. {
  111. if (useFallOff)
  112. {
  113. noiseMap[x, y] = Mathf.Clamp(noiseMap[x, y] - fallOffMap[x, y], 0, 1);
  114. }
  115. float currentHeight = noiseMap[x, y];
  116. for (int i = 0; i < regions.Length; i++)
  117. {
  118. if(currentHeight >= regions[i].height)
  119. {
  120. colourMap[y*mapChunkSize + x] = regions[i].colour;
  121. }
  122. else
  123. {
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. return new MapData(noiseMap, colourMap);
  130. }
  131. private void OnValidate()
  132. {
  133. if (lacunarity < 1)
  134. {
  135. lacunarity = 1;
  136. }
  137. if(octaves < 0)
  138. {
  139. octaves = 0;
  140. }
  141. fallOffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize);
  142. }
  143. struct MapThreadInfo<T>
  144. {
  145. public readonly Action<T> callback;
  146. public readonly T parameter;
  147. public MapThreadInfo(Action<T> callback, T parameter)
  148. {
  149. this.callback = callback;
  150. this.parameter = parameter;
  151. }
  152. }
  153. }
  154. [System.Serializable]
  155. public struct TerrainType
  156. {
  157. public string name;
  158. public float height;
  159. public Color colour;
  160. }
  161. public struct MapData
  162. {
  163. public readonly float[,] heightMap;
  164. public readonly Color[] colourMap;
  165. public MapData(float[,] heightMap, Color[] colourMap)
  166. {
  167. this.heightMap = heightMap;
  168. this.colourMap = colourMap;
  169. }
  170. }