1
0

MapGenerator.cs 5.1 KB

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