|
@@ -4,7 +4,6 @@ using UnityEngine;
|
|
using KairoEngine.Core;
|
|
using KairoEngine.Core;
|
|
using KairoEngine.Chunks;
|
|
using KairoEngine.Chunks;
|
|
using KairoEngine.NoiseUtilities;
|
|
using KairoEngine.NoiseUtilities;
|
|
-using KairoEngine.UniverseGenerator;
|
|
|
|
using Utilities = KairoEngine.Utility.Utilities;
|
|
using Utilities = KairoEngine.Utility.Utilities;
|
|
using Sirenix.OdinInspector;
|
|
using Sirenix.OdinInspector;
|
|
using UniRx;
|
|
using UniRx;
|
|
@@ -14,20 +13,21 @@ namespace KairoEngine.TerrainEngine
|
|
[HideMonoScript]
|
|
[HideMonoScript]
|
|
public class ChunkTerrainGenerator : MonoBehaviour
|
|
public class ChunkTerrainGenerator : MonoBehaviour
|
|
{
|
|
{
|
|
- public MarchingCubes4 marchingCubes3;
|
|
|
|
- public MarchingCubes2 marchingCubes2;
|
|
|
|
- public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
|
|
|
|
- public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
|
|
|
|
- public Vector3Int chunkLimit = new Vector3Int(2, 1, 2);
|
|
|
|
- public int dataProcessLimit = 4;
|
|
|
|
- public int parallelMeshJobs = 4;
|
|
|
|
|
|
+
|
|
|
|
+ [BoxGroup("Configurations")] public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
|
|
|
|
+ [BoxGroup("Configurations")] public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
|
|
|
|
+ [BoxGroup("Configurations")] public Vector3Int chunkLimit = new Vector3Int(2, 1, 2);
|
|
|
|
+ [BoxGroup("Configurations")] public int dataProcessLimit = 4;
|
|
|
|
+ [BoxGroup("Configurations")] public int parallelMeshJobs = 4;
|
|
|
|
+ [BoxGroup("Configurations")] public bool showChunkData = false;
|
|
|
|
+
|
|
|
|
+ [InlineProperty, HideLabel, BoxGroup("Marching Cubes")] public MarchingCubes4 marchingCubes = new();
|
|
|
|
+ [BoxGroup("Map Data Generator"), InlineProperty, HideLabel, SerializeReference] public IMapDataGenerator mapDataGenerator;
|
|
|
|
+
|
|
|
|
+ [ShowIf("@showChunkData"), BoxGroup("Chunk System"), InlineProperty, HideLabel] public ChunkSystem<BlockBase> chunkSystem;
|
|
|
|
+
|
|
public Dictionary<Vector3, GameObject> terrainMeshes = new Dictionary<Vector3, GameObject>();
|
|
public Dictionary<Vector3, GameObject> terrainMeshes = new Dictionary<Vector3, GameObject>();
|
|
|
|
|
|
- //public float noiseMultiplier = 1.5f;
|
|
|
|
- [InlineEditor(InlineEditorObjectFieldModes.Foldout)] public PlanetTemplate planetTemplate;
|
|
|
|
-
|
|
|
|
- public ChunkSystem<BlockBase> chunkSystem;
|
|
|
|
-
|
|
|
|
private List<Vector3> chunkDataBuffer = new List<Vector3>(); // Store list of chunks for populating with data
|
|
private List<Vector3> chunkDataBuffer = new List<Vector3>(); // Store list of chunks for populating with data
|
|
private List<Vector3> chunkMeshBuffer = new List<Vector3>(); // Store list of chunks for generating the mesh
|
|
private List<Vector3> chunkMeshBuffer = new List<Vector3>(); // Store list of chunks for generating the mesh
|
|
|
|
|
|
@@ -46,6 +46,16 @@ namespace KairoEngine.TerrainEngine
|
|
}
|
|
}
|
|
private Dictionary<Vector3,List<VoxelUpdateData>> chunkUpdates = new Dictionary<Vector3, List<VoxelUpdateData>>();
|
|
private Dictionary<Vector3,List<VoxelUpdateData>> chunkUpdates = new Dictionary<Vector3, List<VoxelUpdateData>>();
|
|
|
|
|
|
|
|
+ private void Update()
|
|
|
|
+ {
|
|
|
|
+ marchingCubes.UpdateFinishedJobs();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void OnDestroy()
|
|
|
|
+ {
|
|
|
|
+ marchingCubes.OnDestroy();
|
|
|
|
+ }
|
|
|
|
+
|
|
public void Initialize ()
|
|
public void Initialize ()
|
|
{
|
|
{
|
|
chunkSystem = new ChunkSystem<BlockBase>(chunkSize,voxelSize, true);
|
|
chunkSystem = new ChunkSystem<BlockBase>(chunkSize,voxelSize, true);
|
|
@@ -57,7 +67,7 @@ namespace KairoEngine.TerrainEngine
|
|
[Button("Generate World"), ButtonGroup("Buttons")]
|
|
[Button("Generate World"), ButtonGroup("Buttons")]
|
|
void GenerateWorld()
|
|
void GenerateWorld()
|
|
{
|
|
{
|
|
- if(planetTemplate == null)
|
|
|
|
|
|
+ if(mapDataGenerator == null)
|
|
{
|
|
{
|
|
Debug.LogError("Missing planet template", this.gameObject);
|
|
Debug.LogError("Missing planet template", this.gameObject);
|
|
return;
|
|
return;
|
|
@@ -121,9 +131,9 @@ namespace KairoEngine.TerrainEngine
|
|
Vector3 pos = initialPos + new Vector3(x * voxelSize.x, y * voxelSize.y, z * voxelSize.z);
|
|
Vector3 pos = initialPos + new Vector3(x * voxelSize.x, y * voxelSize.y, z * voxelSize.z);
|
|
// Old example noise:
|
|
// Old example noise:
|
|
//float thisHeight = height * Mathf.PerlinNoise((float)pos.x / width * noiseMultiplier + 0.001f, (float)pos.z / length * noiseMultiplier + 0.001f);
|
|
//float thisHeight = height * Mathf.PerlinNoise((float)pos.x / width * noiseMultiplier + 0.001f, (float)pos.z / length * noiseMultiplier + 0.001f);
|
|
- float elevation = planetTemplate.SamplePoint((float)pos.x / width, (float)pos.z / length);
|
|
|
|
|
|
+ float elevation = mapDataGenerator.SamplePoint((float)pos.x / width, (float)pos.z / length);
|
|
float thisHeight = height * elevation;
|
|
float thisHeight = height * elevation;
|
|
- uint code = planetTemplate.SamplePointCode((float)pos.x / width, (float)pos.z / length, elevation);
|
|
|
|
|
|
+ uint code = mapDataGenerator.SamplePointColor((float)pos.x / width, (float)pos.z / length, elevation);
|
|
BlockBase block = new BlockBase(code, (uint)Mathf.FloorToInt(thisHeight * 1000));
|
|
BlockBase block = new BlockBase(code, (uint)Mathf.FloorToInt(thisHeight * 1000));
|
|
chunkSystem.SetBlock(pos, block);
|
|
chunkSystem.SetBlock(pos, block);
|
|
}
|
|
}
|
|
@@ -154,22 +164,22 @@ namespace KairoEngine.TerrainEngine
|
|
bool meshExists = terrainMeshes.TryGetValue(position, out GameObject obj);
|
|
bool meshExists = terrainMeshes.TryGetValue(position, out GameObject obj);
|
|
if(meshExists && !overwrite) return;
|
|
if(meshExists && !overwrite) return;
|
|
if(!overwrite) terrainMeshes.Add(position, null);
|
|
if(!overwrite) terrainMeshes.Add(position, null);
|
|
- if(marchingCubes3 != null) marchingCubes3.Generate(chunkSystem, this, position);
|
|
|
|
|
|
+ if(marchingCubes != null) marchingCubes.Generate(chunkSystem, this, position);
|
|
}
|
|
}
|
|
|
|
|
|
public void UpdateGenerator()
|
|
public void UpdateGenerator()
|
|
{
|
|
{
|
|
- if(marchingCubes3.GetJobCount() == 0) ProcessChunkDataBuffer();
|
|
|
|
|
|
+ if(marchingCubes.GetJobCount() == 0) ProcessChunkDataBuffer();
|
|
ProcessChunkMeshBuffer();
|
|
ProcessChunkMeshBuffer();
|
|
}
|
|
}
|
|
|
|
|
|
public void UpdateEditor ()
|
|
public void UpdateEditor ()
|
|
{
|
|
{
|
|
Timer.ExecuteRealTime(50, () => {
|
|
Timer.ExecuteRealTime(50, () => {
|
|
- if(marchingCubes3 != null)
|
|
|
|
|
|
+ if(marchingCubes != null)
|
|
{
|
|
{
|
|
UpdateGenerator();
|
|
UpdateGenerator();
|
|
- if(!marchingCubes3.IsGeneratorDone()) UpdateEditor();
|
|
|
|
|
|
+ if(!marchingCubes.IsGeneratorDone()) UpdateEditor();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -226,8 +236,8 @@ namespace KairoEngine.TerrainEngine
|
|
|
|
|
|
private void ProcessChunkMeshBuffer()
|
|
private void ProcessChunkMeshBuffer()
|
|
{
|
|
{
|
|
- marchingCubes3.UpdateFinishedJobs();
|
|
|
|
- int limit = marchingCubes3.GetJobCount() < parallelMeshJobs ? parallelMeshJobs - marchingCubes3.GetJobCount() : 0;
|
|
|
|
|
|
+ marchingCubes.UpdateFinishedJobs();
|
|
|
|
+ int limit = marchingCubes.GetJobCount() < parallelMeshJobs ? parallelMeshJobs - marchingCubes.GetJobCount() : 0;
|
|
limit = limit < chunkMeshBuffer.Count ? limit : chunkMeshBuffer.Count;
|
|
limit = limit < chunkMeshBuffer.Count ? limit : chunkMeshBuffer.Count;
|
|
for (int i = 0; i < limit; i++)
|
|
for (int i = 0; i < limit; i++)
|
|
{
|
|
{
|