| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using UnityEngine;
- using System;
- using System.Threading;
- using System.Collections.Generic;
- public class MapGenerator : MonoBehaviour
- {
- public enum DrawMode { NoiseMap, ColourMap, Mesh, FallOff};
- public DrawMode drawMode;
- public Noise.NormalizeMode normalizeMode;
- public const int mapChunkSize = 241;
- [Range(0,6)]
- public int editorPreviewLOD;
- public float noiseScale;
- public int octaves;
- [Range(0,1)]
- public float persistance;
- public float lacunarity;
- public int seed;
- public Vector2 offset;
- public bool useFallOff;
- public float meshHeightMultiplier;
- public AnimationCurve meshHeightCurve;
- public bool autoUpdate;
- public TerrainType[] regions;
- float[,] fallOffMap;
- Queue<MapThreadInfo<MapData>> mapDataThreadInfoQueue = new Queue<MapThreadInfo<MapData>>();
- Queue<MapThreadInfo<MeshData>> meshDataThreadInfoQueue = new Queue<MapThreadInfo<MeshData>>();
- private void Awake()
- {
- fallOffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize);
- }
- public void DrawMapInEditor()
- {
- MapData mapData = GenerateMapData(Vector2.zero);
- MapDisplay display = FindFirstObjectByType<MapDisplay>();
- if (drawMode == DrawMode.NoiseMap)
- {
- display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
- }
- else if (drawMode == DrawMode.ColourMap)
- {
- display.DrawTexture(TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
- }
- else if (drawMode == DrawMode.Mesh)
- {
- display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, editorPreviewLOD), TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
- }else if (drawMode ==DrawMode.FallOff)
- {
- display.DrawTexture(TextureGenerator.TextureFromHeightMap(FallOffGenerator.GenerateFallOffMap(mapChunkSize)));
- }
- }
- public void RequestMapData(Vector2 centre, Action<MapData> callback)
- {
- ThreadStart threadStart = delegate
- {
- MapDataThread(centre, callback);
- };
- new Thread(threadStart).Start();
- }
- void MapDataThread(Vector2 centre, Action<MapData> callback)
- {
- MapData mapData = GenerateMapData(centre);
- lock (mapDataThreadInfoQueue)
- {
- mapDataThreadInfoQueue.Enqueue(new MapThreadInfo<MapData>(callback, mapData));
- }
- }
- public void RequestMeshData(MapData mapData, int lod, Action<MeshData> callback)
- {
- ThreadStart threadStart = delegate
- {
- MeshDataThread(mapData, lod, callback);
- };
- new Thread(threadStart).Start();
- }
- void MeshDataThread(MapData mapData, int lod, Action<MeshData> callback)
- {
- MeshData meshData = MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, lod);
- lock (meshDataThreadInfoQueue)
- {
- meshDataThreadInfoQueue.Enqueue(new MapThreadInfo<MeshData>(callback, meshData));
- }
- }
- private void Update()
- {
- if (mapDataThreadInfoQueue.Count > 0)
- {
- for (int i = 0; i < mapDataThreadInfoQueue.Count; i++)
- {
- MapThreadInfo<MapData> threadInfo = mapDataThreadInfoQueue.Dequeue();
- threadInfo.callback(threadInfo.parameter);
- }
- }
- if (meshDataThreadInfoQueue.Count > 0)
- {
- for(int i = 0; i < meshDataThreadInfoQueue.Count; i++)
- {
- MapThreadInfo<MeshData> threadInfo = meshDataThreadInfoQueue.Dequeue();
- threadInfo.callback(threadInfo.parameter);
- }
- }
- }
- MapData GenerateMapData(Vector2 centre)
- {
- float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, centre + offset, normalizeMode);
- Color[] colourMap = new Color[mapChunkSize * mapChunkSize];
- for(int y=0; y<mapChunkSize; y++)
- {
- for (int x=0; x<mapChunkSize; x++)
- {
- if (useFallOff)
- {
- noiseMap[x, y] = Mathf.Clamp(noiseMap[x, y] - fallOffMap[x, y], 0, 1);
- }
- float currentHeight = noiseMap[x, y];
- for (int i = 0; i < regions.Length; i++)
- {
- if(currentHeight >= regions[i].height)
- {
- colourMap[y*mapChunkSize + x] = regions[i].colour;
- }
- else
- {
- break;
- }
- }
- }
- }
- return new MapData(noiseMap, colourMap);
- }
- private void OnValidate()
- {
- if (lacunarity < 1)
- {
- lacunarity = 1;
- }
- if(octaves < 0)
- {
- octaves = 0;
- }
- fallOffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize);
- }
- struct MapThreadInfo<T>
- {
- public readonly Action<T> callback;
- public readonly T parameter;
- public MapThreadInfo(Action<T> callback, T parameter)
- {
- this.callback = callback;
- this.parameter = parameter;
- }
- }
- }
- [System.Serializable]
- public struct TerrainType
- {
- public string name;
- public float height;
- public Color colour;
- }
- public struct MapData
- {
- public readonly float[,] heightMap;
- public readonly Color[] colourMap;
- public MapData(float[,] heightMap, Color[] colourMap)
- {
- this.heightMap = heightMap;
- this.colourMap = colourMap;
- }
- }
|