|
|
@@ -7,54 +7,43 @@ using System.Collections.Generic;
|
|
|
public class MapGenerator : MonoBehaviour
|
|
|
{
|
|
|
|
|
|
- public enum DrawMode { NoiseMap, ColourMap, Mesh, FalloffMap };
|
|
|
+ public enum DrawMode { NoiseMap, Mesh, FalloffMap };
|
|
|
public DrawMode drawMode;
|
|
|
|
|
|
- public Noise.NormalizeMode normalizeMode;
|
|
|
+ public TerrainData terrainData;
|
|
|
+ public NoiseData noiseData;
|
|
|
+ public TextureData textureData;
|
|
|
|
|
|
- public bool useFlatShading;
|
|
|
+ public Material terrainMaterial;
|
|
|
|
|
|
[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;
|
|
|
- static MapGenerator instance;
|
|
|
-
|
|
|
float[,] falloffMap;
|
|
|
|
|
|
Queue<MapThreadInfo<MapData>> mapDataThreadInfoQueue = new Queue<MapThreadInfo<MapData>>();
|
|
|
Queue<MapThreadInfo<MeshData>> meshDataThreadInfoQueue = new Queue<MapThreadInfo<MeshData>>();
|
|
|
|
|
|
- void Awake()
|
|
|
+ void OnValuesUpdated()
|
|
|
+ {
|
|
|
+ if (!Application.isPlaying)
|
|
|
+ {
|
|
|
+ DrawMapInEditor();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnTextureValuesUpdated()
|
|
|
{
|
|
|
- falloffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize);
|
|
|
+ textureData.ApplyToMaterial(terrainMaterial);
|
|
|
}
|
|
|
|
|
|
- public static int mapChunkSize
|
|
|
+ public int mapChunkSize
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
- if(instance == null)
|
|
|
- {
|
|
|
- instance = FindFirstObjectByType<MapGenerator>();
|
|
|
- }
|
|
|
- if (instance.useFlatShading)
|
|
|
+ if (terrainData.useFlatShading)
|
|
|
{
|
|
|
return 95;
|
|
|
}
|
|
|
@@ -74,13 +63,9 @@ public class MapGenerator : MonoBehaviour
|
|
|
{
|
|
|
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, useFlatShading), TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
|
|
|
+ display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, terrainData.meshHeightMultiplier, terrainData.meshHeightCurve, editorPreviewLOD, terrainData.useFlatShading));
|
|
|
}
|
|
|
else if (drawMode == DrawMode.FalloffMap)
|
|
|
{
|
|
|
@@ -117,7 +102,7 @@ public class MapGenerator : MonoBehaviour
|
|
|
|
|
|
void MeshDataThread(MapData mapData, int lod, Action<MeshData> callback)
|
|
|
{
|
|
|
- MeshData meshData = MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, lod, useFlatShading);
|
|
|
+ MeshData meshData = MeshGenerator.GenerateTerrainMesh(mapData.heightMap, terrainData.meshHeightMultiplier, terrainData.meshHeightCurve, lod, terrainData.useFlatShading);
|
|
|
lock (meshDataThreadInfoQueue)
|
|
|
{
|
|
|
meshDataThreadInfoQueue.Enqueue(new MapThreadInfo<MeshData>(callback, meshData));
|
|
|
@@ -147,48 +132,48 @@ public class MapGenerator : MonoBehaviour
|
|
|
|
|
|
MapData GenerateMapData(Vector2 centre)
|
|
|
{
|
|
|
- float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, seed, noiseScale, octaves, persistance, lacunarity, centre + offset, normalizeMode);
|
|
|
+ float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, noiseData.seed, noiseData.noiseScale, noiseData.octaves, noiseData.persistance, noiseData.lacunarity, centre + noiseData.offset, noiseData.normalizeMode);
|
|
|
|
|
|
- Color[] colourMap = new Color[mapChunkSize * mapChunkSize];
|
|
|
- for (int y = 0; y < mapChunkSize; y++)
|
|
|
+ if (terrainData.useFalloff)
|
|
|
{
|
|
|
- for (int x = 0; x < mapChunkSize; x++)
|
|
|
+ if(falloffMap == null)
|
|
|
{
|
|
|
- if (useFalloff)
|
|
|
- {
|
|
|
- noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x, y]);
|
|
|
- }
|
|
|
- float currentHeight = noiseMap[x, y];
|
|
|
- for (int i = 0; i < regions.Length; i++)
|
|
|
+ falloffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize + 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int y = 0; y < mapChunkSize + 2; y++)
|
|
|
+ {
|
|
|
+ for (int x = 0; x < mapChunkSize + 2; x++)
|
|
|
{
|
|
|
- if (currentHeight >= regions[i].height)
|
|
|
- {
|
|
|
- colourMap[y * mapChunkSize + x] = regions[i].colour;
|
|
|
- }
|
|
|
- else
|
|
|
+ if (terrainData.useFalloff)
|
|
|
{
|
|
|
- break;
|
|
|
+ noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x, y]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- return new MapData(noiseMap, colourMap);
|
|
|
+ return new MapData(noiseMap);
|
|
|
}
|
|
|
|
|
|
void OnValidate()
|
|
|
{
|
|
|
- if (lacunarity < 1)
|
|
|
+ if(terrainData != null)
|
|
|
{
|
|
|
- lacunarity = 1;
|
|
|
+ terrainData.OnValuesUpdated -= OnValuesUpdated;
|
|
|
+ terrainData.OnValuesUpdated += OnValuesUpdated;
|
|
|
}
|
|
|
- if (octaves < 0)
|
|
|
+ if (noiseData != null)
|
|
|
{
|
|
|
- octaves = 0;
|
|
|
+ noiseData.OnValuesUpdated -= OnValuesUpdated;
|
|
|
+ noiseData.OnValuesUpdated += OnValuesUpdated;
|
|
|
+ }
|
|
|
+ if(textureData != null)
|
|
|
+ {
|
|
|
+ textureData.OnValuesUpdated -= OnValuesUpdated;
|
|
|
+ textureData.OnValuesUpdated += OnValuesUpdated;
|
|
|
}
|
|
|
-
|
|
|
- falloffMap = FallOffGenerator.GenerateFallOffMap(mapChunkSize);
|
|
|
}
|
|
|
|
|
|
struct MapThreadInfo<T>
|
|
|
@@ -206,22 +191,13 @@ public class MapGenerator : MonoBehaviour
|
|
|
|
|
|
}
|
|
|
|
|
|
-[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)
|
|
|
+
|
|
|
+ public MapData(float[,] heightMap)
|
|
|
{
|
|
|
this.heightMap = heightMap;
|
|
|
- this.colourMap = colourMap;
|
|
|
}
|
|
|
}
|