MapGenerator.cs 5.0 KB

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