Sfoglia il codice sorgente

Refactored to use a map data generator interface

James Peret 2 anni fa
parent
commit
1a3f6f0a41
3 ha cambiato i file con 48 aggiunte e 50 eliminazioni
  1. 32 22
      Runtime/ChunkTerrainGenerator.cs
  2. 16 27
      Runtime/MarchingCubes4.cs
  3. 0 1
      Runtime/TerrainEngine.asmdef

+ 32 - 22
Runtime/ChunkTerrainGenerator.cs

@@ -4,7 +4,6 @@ using UnityEngine;
 using KairoEngine.Core;
 using KairoEngine.Chunks;
 using KairoEngine.NoiseUtilities;
-using KairoEngine.UniverseGenerator;
 using Utilities = KairoEngine.Utility.Utilities;
 using Sirenix.OdinInspector;
 using UniRx;
@@ -14,20 +13,21 @@ namespace KairoEngine.TerrainEngine
     [HideMonoScript]
     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 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> 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 void Update()
+        {
+            marchingCubes.UpdateFinishedJobs();
+        }
+
+        private void OnDestroy()
+        {
+            marchingCubes.OnDestroy();
+        }
+
         public void Initialize ()
         {
             chunkSystem = new ChunkSystem<BlockBase>(chunkSize,voxelSize, true);
@@ -57,7 +67,7 @@ namespace KairoEngine.TerrainEngine
         [Button("Generate World"), ButtonGroup("Buttons")]
         void GenerateWorld()
         {
-            if(planetTemplate == null)
+            if(mapDataGenerator == null)
             {
                 Debug.LogError("Missing planet template", this.gameObject);
                 return;
@@ -121,9 +131,9 @@ namespace KairoEngine.TerrainEngine
                         Vector3 pos = initialPos + new Vector3(x * voxelSize.x, y * voxelSize.y, z * voxelSize.z);
                         // Old example noise:
                         //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;
-                        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));
                         chunkSystem.SetBlock(pos, block);
                     }
@@ -154,22 +164,22 @@ namespace KairoEngine.TerrainEngine
             bool meshExists = terrainMeshes.TryGetValue(position, out GameObject obj);
             if(meshExists && !overwrite) return;
             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()
         {
-            if(marchingCubes3.GetJobCount() == 0) ProcessChunkDataBuffer();
+            if(marchingCubes.GetJobCount() == 0) ProcessChunkDataBuffer();
             ProcessChunkMeshBuffer();
         }
 
         public void UpdateEditor ()
         {
             Timer.ExecuteRealTime(50, () => {
-                if(marchingCubes3 != null)
+                if(marchingCubes != null)
                 {
                     UpdateGenerator();
-                    if(!marchingCubes3.IsGeneratorDone()) UpdateEditor();
+                    if(!marchingCubes.IsGeneratorDone()) UpdateEditor();
                 }
             });
         }
@@ -226,8 +236,8 @@ namespace KairoEngine.TerrainEngine
 
         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;
             for (int i = 0; i < limit; i++)
             {

+ 16 - 27
Runtime/MarchingCubes4.cs

@@ -11,42 +11,32 @@ using KairoEngine.Chunks;
 
 namespace KairoEngine.TerrainEngine
 {
-    public class MarchingCubes4 : MonoBehaviour
+    [System.Serializable]
+    public class MarchingCubes4
     {
         public float terrainSurface = 0.5f;
         public bool smoothTerrain;
         public bool flatShaded;
         public Material material;
+        public GameObject parent;
         
 
-        MeshFilter meshFilter;
-        MeshCollider meshCollider;
-
-        GameObject prefab;
-
-        
-        int width = 32;
-        int length = 32;
-        int height = 8;
-
-        Vector3 voxelSize = new Vector3(1f, 1f, 1f);
-
-        float[,,] terrainMap;
-        uint[,,] terrainCodes;
-
-        List<Vector3> vertices = new List<Vector3>();
-        List<int> triangles = new List<int>();
-
+        private MeshFilter meshFilter;
+        private MeshCollider meshCollider;
+        private GameObject prefab;
+        private int width = 32;
+        private int length = 32;
+        private int height = 8;
+        private Vector3 voxelSize = new Vector3(1f, 1f, 1f);
+        private float[,,] terrainMap;
+        private uint[,,] terrainCodes;
+        private List<Vector3> vertices = new List<Vector3>();
+        private List<int> triangles = new List<int>();
         private ChunkSystem<BlockBase> chunkSystem;
         private List<JobHandle> jobHandles = new List<JobHandle>();
         private List<MarchingCubesJob4> jobs = new List<MarchingCubesJob4>();
         private ChunkTerrainGenerator chunkTerrainGenerator;
 
-        private void Update()
-        {
-            UpdateFinishedJobs();
-        }
-
         public void OnDestroy()
         {
             // Make sure we run our jobs to completion before exiting.
@@ -63,11 +53,11 @@ namespace KairoEngine.TerrainEngine
             for (int i = 0; i < jobHandles.Count; i++)
             {
                 
-                if(!jobHandles[i].IsCompleted) return;
+                if(!jobHandles[i].IsCompleted) continue;
                 jobHandles[i].Complete();
                 //Debug.Log($"Job {i} is completed");    
                 if(prefab == null) prefab = new GameObject();
-                GameObject target = Instantiate(prefab, jobs[i].initialPosition, transform.rotation, this.transform);
+                GameObject target = GameObject.Instantiate(prefab, jobs[i].initialPosition, parent.transform.rotation, parent.transform);
                 // Create vertices and triangle arrays
                 int verticeCount = jobs[i].arrayIndexes[0];
                 int trianglesCount = jobs[i].arrayIndexes[1];
@@ -89,7 +79,6 @@ namespace KairoEngine.TerrainEngine
                 jobs.RemoveAt(i);
                 jobHandles.RemoveAt(i);
                 i -= 1;
-                
             }
         }
 

+ 0 - 1
Runtime/TerrainEngine.asmdef

@@ -6,7 +6,6 @@
         "GUID:7e5ae6a38d1532248b4c890eca668b06",
         "GUID:22b3fea425824d6448cb89ed3c76c9a8",
         "GUID:d09ad6089c871be4581e7b220db144be",
-        "GUID:3ffdbd0bbde813141a8a14a897160240",
         "GUID:e0cd26848372d4e5c891c569017e11f1",
         "GUID:560b04d1a97f54a4e82edc0cbbb69285",
         "GUID:f452697229e6bcc469c0eff1574ac090",