1
0

MapGenerator.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 const int mapChunkSize = 239;
  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. float[,] falloffMap;
  27. Queue<MapThreadInfo<MapData>> mapDataThreadInfoQueue = new Queue<MapThreadInfo<MapData>>();
  28. Queue<MapThreadInfo<MeshData>> meshDataThreadInfoQueue = new Queue<MapThreadInfo<MeshData>>();
  29. void Awake()
  30. {
  31. falloffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize);
  32. }
  33. public void DrawMapInEditor()
  34. {
  35. MapData mapData = GenerateMapData(Vector2.zero);
  36. MapDisplay display = FindFirstObjectByType<MapDisplay>();
  37. if (drawMode == DrawMode.NoiseMap)
  38. {
  39. display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
  40. }
  41. else if (drawMode == DrawMode.ColourMap)
  42. {
  43. display.DrawTexture(TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
  44. }
  45. else if (drawMode == DrawMode.Mesh)
  46. {
  47. display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, editorPreviewLOD), TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
  48. }
  49. else if (drawMode == DrawMode.FalloffMap)
  50. {
  51. display.DrawTexture(TextureGenerator.TextureFromHeightMap(FallOffGenerator.GenerateFallOffMap(mapChunkSize)));
  52. }
  53. }
  54. public void RequestMapData(Vector2 centre, Action<MapData> callback)
  55. {
  56. ThreadStart threadStart = delegate {
  57. MapDataThread(centre, callback);
  58. };
  59. new Thread(threadStart).Start();
  60. }
  61. void MapDataThread(Vector2 centre, Action<MapData> callback)
  62. {
  63. MapData mapData = GenerateMapData(centre);
  64. lock (mapDataThreadInfoQueue)
  65. {
  66. mapDataThreadInfoQueue.Enqueue(new MapThreadInfo<MapData>(callback, mapData));
  67. }
  68. }
  69. public void RequestMeshData(MapData mapData, int lod, Action<MeshData> callback)
  70. {
  71. ThreadStart threadStart = delegate {
  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. 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 + 2, mapChunkSize + 2, 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.Clamp01(noiseMap[x, y] - falloffMap[x, y]);
  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. 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. }