Browse Source

Added support for building Navigation Meshes

James Peret 2 years ago
parent
commit
c3e569af3b
3 changed files with 12 additions and 1 deletions
  1. 1 0
      ChangeLog.md
  2. 9 0
      Runtime/ChunkTerrainGenerator.cs
  3. 2 1
      Runtime/TerrainEngine.asmdef

+ 1 - 0
ChangeLog.md

@@ -2,6 +2,7 @@
 
 ### v0.0.2
 
+- Added support for building Navigation Meshes after the terrain is generated
 - Added terrain generation on start or on Generic Event
 - Added ability to change the generated terrain mesh GameObject tag
 - Added ability to change the generated terrain mesh GameObject layer

+ 9 - 0
Runtime/ChunkTerrainGenerator.cs

@@ -1,6 +1,7 @@
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
+using NavMeshSurface = UnityEngine.AI.NavMeshSurface;
 using KairoEngine.Core;
 using KairoEngine.Chunks;
 using KairoEngine.NoiseUtilities;
@@ -8,6 +9,7 @@ using Utilities = KairoEngine.Utility.Utilities;
 using Sirenix.OdinInspector;
 using UniRx;
 
+
 namespace KairoEngine.TerrainEngine
 {
     [HideMonoScript]
@@ -20,6 +22,8 @@ namespace KairoEngine.TerrainEngine
         [BoxGroup("Configurations"), ShowIf("@startConfiguration == StartConfig.WaitForEvent")] public string generateWorldEvent = "GenerateWorld";
         [BoxGroup("Configurations")] public int dataProcessLimit = 4;
         [BoxGroup("Configurations")] public int parallelMeshJobs = 4;
+        [HorizontalGroup("Configurations/NavMesh")] public bool processNavMesh = false;
+        [HorizontalGroup("Configurations/NavMesh"), ShowIf("@processNavMesh"), HideLabel] public NavMeshSurface navMeshSurface;
         [BoxGroup("Configurations")] public bool showChunkData = false;
         
         [InlineProperty, HideLabel, BoxGroup("Marching Cubes")] public MarchingCubes4 marchingCubes = new();
@@ -248,6 +252,7 @@ namespace KairoEngine.TerrainEngine
 
         private void ProcessChunkMeshBuffer()
         {
+            bool terrainChanged = false;
             marchingCubes.UpdateFinishedJobs();
             int limit = marchingCubes.GetJobCount() < parallelMeshJobs ? parallelMeshJobs - marchingCubes.GetJobCount() : 0;
             limit = limit < chunkMeshBuffer.Count ? limit : chunkMeshBuffer.Count;
@@ -255,7 +260,11 @@ namespace KairoEngine.TerrainEngine
             {
                 GenerateTerrainMesh(chunkMeshBuffer[i]);
             }
+            if(chunkMeshBuffer.Count > 0) terrainChanged = true;
             chunkMeshBuffer.RemoveRange(0, limit);
+            if(!processNavMesh) return;
+            if(terrainChanged && chunkMeshBuffer.Count == 0 && chunkDataBuffer.Count == 0 && navMeshSurface != null)
+                Timer.ExecuteRealTime(50, () => navMeshSurface.BuildNavMesh());
         }
 
         public void DestroyChunk(Vector3 position)

+ 2 - 1
Runtime/TerrainEngine.asmdef

@@ -10,7 +10,8 @@
         "GUID:560b04d1a97f54a4e82edc0cbbb69285",
         "GUID:f452697229e6bcc469c0eff1574ac090",
         "GUID:142285d3db5e7e849b02ea3a75bc2de7",
-        "GUID:165d83fc3bb2a4144925c85421871d8e"
+        "GUID:165d83fc3bb2a4144925c85421871d8e",
+        "GUID:8c4dd21966739024fbd72155091d199e"
     ],
     "includePlatforms": [],
     "excludePlatforms": [],