MapGenerator.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using UnityEngine;
  2. using System.Threading;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Net.Http.Headers;
  6. public class MapGenerator : MonoBehaviour
  7. {
  8. public enum DrawMode {NoiseMap, ColorMap, Mesh};
  9. public DrawMode drawMode;
  10. public const int mapChunkSize = 241;
  11. [Range(0,6)]
  12. public int levelOfDetail;
  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();
  29. MapDisplay display = FindFirstObjectByType<MapDisplay>();
  30. if (drawMode == DrawMode.NoiseMap)
  31. {
  32. display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
  33. }
  34. else if (drawMode == DrawMode.ColorMap)
  35. {
  36. display.DrawTexture(TextureGenerator.TextureFromColorMap(mapData.colorMap, mapChunkSize, mapChunkSize));
  37. }
  38. else if (drawMode == DrawMode.Mesh)
  39. {
  40. display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, levelOfDetail), TextureGenerator.TextureFromColorMap(mapData.colorMap, mapChunkSize, mapChunkSize));
  41. }
  42. }
  43. public void RequestMapData(Action<MapData> callback)
  44. {
  45. ThreadStart threadStart = delegate
  46. {
  47. MapDataThread(callback);
  48. };
  49. new Thread(threadStart).Start();
  50. }
  51. void MapDataThread(Action<MapData> callback)
  52. {
  53. MapData mapData = GenerateMapData();
  54. lock (mapDataThreadInfoQueue)
  55. {
  56. mapDataThreadInfoQueue.Enqueue(new MapThreadInfo<MapData>(callback, mapData));
  57. }
  58. }
  59. public void RequestMeshData(MapData mapData, Action<MeshData> callback)
  60. {
  61. ThreadStart threadStart = delegate {
  62. MeshDataThread(mapData, callback);
  63. };
  64. new Thread(threadStart).Start();
  65. }
  66. void MeshDataThread(MapData mapData, Action<MeshData> callback)
  67. {
  68. MeshData meshData = MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, levelOfDetail);
  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. for(int i = 0; i < meshDataThreadInfoQueue.Count; i++)
  86. {
  87. MapThreadInfo<MeshData> threadInfo = meshDataThreadInfoQueue.Dequeue();
  88. threadInfo.callback(threadInfo.parameter);
  89. }
  90. }
  91. }
  92. MapData GenerateMapData()
  93. {
  94. float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, offset);
  95. Color[] colorMap = new Color[mapChunkSize * mapChunkSize];
  96. for (int y = 0; y < mapChunkSize; y++)
  97. {
  98. for (int x = 0; x < mapChunkSize; x++)
  99. {
  100. float currentHeight = noiseMap[x, y];
  101. for(int i = 0; i < regions.Length; i++)
  102. {
  103. if(currentHeight <= regions[i].height)
  104. {
  105. colorMap[y * mapChunkSize + x] = regions[i].color;
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. return new MapData(noiseMap, colorMap);
  112. }
  113. private void OnValidate()
  114. {
  115. if(lacunarity < 1)
  116. {
  117. lacunarity = 1;
  118. }
  119. if(octaves < 0)
  120. {
  121. octaves = 0;
  122. }
  123. }
  124. struct MapThreadInfo<T>
  125. {
  126. public readonly Action<T> callback;
  127. public readonly T parameter;
  128. public MapThreadInfo(Action<T> callback, T parameter)
  129. {
  130. this.callback = callback;
  131. this.parameter = parameter;
  132. }
  133. }
  134. }
  135. [System.Serializable]
  136. public struct TerrainType
  137. {
  138. public string name;
  139. public float height;
  140. public Color color;
  141. }
  142. public struct MapData
  143. {
  144. public readonly float[,] heightMap;
  145. public readonly Color[] colorMap;
  146. public MapData(float[,] heightMap, Color[] colorMap)
  147. {
  148. this.heightMap = heightMap;
  149. this.colorMap = colorMap;
  150. }
  151. }