From 60850b0cbfdd5c309e2211bfd26800f6c98e383e Mon Sep 17 00:00:00 2001 From: ikpil Date: Thu, 27 Jul 2023 15:39:21 +0900 Subject: [PATCH] added tile tool --- src/DotRecast.Recast.Demo/Tools/TileTool.cs | 12 ++- .../Builder/TileNavMeshBuilder.cs | 4 +- .../Tools/TileToolImpl.cs | 83 +++++++++++++++---- src/DotRecast.Recast/RecastBuilder.cs | 2 +- 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/src/DotRecast.Recast.Demo/Tools/TileTool.cs b/src/DotRecast.Recast.Demo/Tools/TileTool.cs index 7d13215..05d93bd 100644 --- a/src/DotRecast.Recast.Demo/Tools/TileTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/TileTool.cs @@ -1,14 +1,17 @@ -using DotRecast.Core; +using System; +using DotRecast.Core; using DotRecast.Recast.Demo.Draw; using DotRecast.Recast.DemoTool; using DotRecast.Recast.DemoTool.Tools; using ImGuiNET; +using Serilog; using static DotRecast.Recast.Demo.Draw.DebugDraw; namespace DotRecast.Recast.Demo.Tools; public class TileTool : IRcTool { + private static readonly ILogger Logger = Log.ForContext(); private readonly TileToolImpl _impl; private bool _hitPosSet; @@ -58,7 +61,8 @@ public class TileTool : IRcTool } else { - _impl.BuildTile(_hitPos); + _impl.BuildTile(_hitPos, out var tileBuildTicks, out var tileTriCount, out var tileMemUsage); + Logger.Information($"{tileBuildTicks / (float)TimeSpan.TicksPerMillisecond}ms / {tileTriCount}Tris / {tileMemUsage}kB "); } } @@ -80,7 +84,7 @@ public class TileTool : IRcTool var settings = sample.GetSettings(); var s = settings.agentRadius; - + float ts = settings.tileSize * settings.cellSize; int tx = (int)((_hitPos.x - bmin[0]) / ts); int ty = (int)((_hitPos.z - bmin[2]) / ts); @@ -101,6 +105,8 @@ public class TileTool : IRcTool lastBuiltTileBmin.x, lastBuiltTileBmin.y, lastBuiltTileBmin.z, lastBuiltTileBmax.x, lastBuiltTileBmax.y, lastBuiltTileBmax.z, DuRGBA(255, 255, 255, 64), 1.0f); + + // 표기 } } diff --git a/src/DotRecast.Recast.DemoTool/Builder/TileNavMeshBuilder.cs b/src/DotRecast.Recast.DemoTool/Builder/TileNavMeshBuilder.cs index 246113a..f07d2b8 100644 --- a/src/DotRecast.Recast.DemoTool/Builder/TileNavMeshBuilder.cs +++ b/src/DotRecast.Recast.DemoTool/Builder/TileNavMeshBuilder.cs @@ -89,8 +89,8 @@ namespace DotRecast.Recast.DemoTool.Builder return navMesh; } - private List BuildMeshData(DemoInputGeomProvider geom, float cellSize, float cellHeight, float agentHeight, - float agentRadius, float agentMaxClimb, List results) + public List BuildMeshData(DemoInputGeomProvider geom, float cellSize, float cellHeight, float agentHeight, + float agentRadius, float agentMaxClimb, IList results) { // Add tiles to nav mesh List meshData = new List(); diff --git a/src/DotRecast.Recast.DemoTool/Tools/TileToolImpl.cs b/src/DotRecast.Recast.DemoTool/Tools/TileToolImpl.cs index 0fcc072..0f11f3a 100644 --- a/src/DotRecast.Recast.DemoTool/Tools/TileToolImpl.cs +++ b/src/DotRecast.Recast.DemoTool/Tools/TileToolImpl.cs @@ -1,4 +1,7 @@ -using DotRecast.Core; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using DotRecast.Core; using DotRecast.Detour.TileCache; using DotRecast.Recast.DemoTool.Builder; @@ -7,12 +10,12 @@ namespace DotRecast.Recast.DemoTool.Tools public class TileToolImpl : ISampleTool { private Sample _sample; - + public string GetName() { return "Create Tiles"; } - + public void SetSample(Sample sample) { _sample = sample; @@ -23,40 +26,84 @@ namespace DotRecast.Recast.DemoTool.Tools return _sample; } - public void BuildTile(RcVec3f pos) + public bool BuildTile(RcVec3f pos, out long tileBuildTicks, out int tileTriCount, out int tileMemUsage) { var settings = _sample.GetSettings(); var geom = _sample.GetInputGeom(); var navMesh = _sample.GetNavMesh(); + tileBuildTicks = 0; + tileTriCount = 0; + tileMemUsage = 0; + if (null == settings || null == geom || navMesh == null) - return; - + return false; + float ts = settings.tileSize * settings.cellSize; - - var bmin = geom.GetMeshBoundsMin(); + + RcVec3f bmin = geom.GetMeshBoundsMin(); + RcVec3f bmax = geom.GetMeshBoundsMax(); int tx = (int)((pos.x - bmin[0]) / ts); int ty = (int)((pos.z - bmin[2]) / ts); - // var tnmb = new TileNavMeshBuilder(); - // tnmb.BuildRecastResult(geom, settings.partitioning, ) + RcConfig cfg = new RcConfig( + true, + settings.tileSize, + settings.tileSize, + RcConfig.CalcBorder(settings.agentRadius, settings.cellSize), + settings.partitioning, + settings.cellSize, + settings.cellHeight, + settings.agentMaxSlope, + settings.filterLowHangingObstacles, + settings.filterLedgeSpans, + settings.filterWalkableLowHeightSpans, + settings.agentHeight, + settings.agentRadius, + settings.agentMaxClimb, + settings.minRegionSize * settings.minRegionSize * settings.cellSize * settings.cellSize, + settings.mergedRegionSize * settings.mergedRegionSize * settings.cellSize * settings.cellSize, + settings.edgeMaxLen, + settings.edgeMaxError, + settings.vertsPerPoly, + true, + settings.detailSampleDist, + settings.detailSampleMaxError, + SampleAreaModifications.SAMPLE_AREAMOD_WALKABLE + ); - //var tileRef = navMesh.GetTileRefAt(tx, ty, 0); - // navMesh.RemoveTile(tileRef); + var rb = new RecastBuilder(); + var result = rb.BuildTile(geom, cfg, bmin, bmax, tx, ty, new RcAtomicInteger(0), 1); + + var tb = new TileNavMeshBuilder(); + var meshData = tb.BuildMeshData(geom, + settings.cellSize, settings.cellHeight, settings.agentHeight, settings.agentRadius, settings.agentMaxClimb, + ImmutableArray.Create(result) + ).First(); + + navMesh.UpdateTile(meshData, 0); + + + var telemetry = result.GetTelemetry(); + tileBuildTicks = telemetry.ToList().Sum(x => x.Ticks); + tileTriCount = 0; // ... + tileMemUsage = 0; // ... + + return true; } - public void RemoveTile(RcVec3f pos) + public bool RemoveTile(RcVec3f pos) { var settings = _sample.GetSettings(); var geom = _sample.GetInputGeom(); var navMesh = _sample.GetNavMesh(); - + if (null == settings || null == geom || navMesh == null) - return; - + return false; + float ts = settings.tileSize * settings.cellSize; - + var bmin = geom.GetMeshBoundsMin(); int tx = (int)((pos.x - bmin[0]) / ts); @@ -64,6 +111,8 @@ namespace DotRecast.Recast.DemoTool.Tools var tileRef = navMesh.GetTileRefAt(tx, ty, 0); navMesh.RemoveTile(tileRef); + + return true; } } } \ No newline at end of file diff --git a/src/DotRecast.Recast/RecastBuilder.cs b/src/DotRecast.Recast/RecastBuilder.cs index 0cd1a1e..d4ca210 100644 --- a/src/DotRecast.Recast/RecastBuilder.cs +++ b/src/DotRecast.Recast/RecastBuilder.cs @@ -143,7 +143,7 @@ namespace DotRecast.Recast return Task.WhenAll(tasks.ToArray()); } - private RecastBuilderResult BuildTile(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tx, + public RecastBuilderResult BuildTile(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tx, int ty, RcAtomicInteger counter, int total) { RecastBuilderResult result = Build(geom, new RecastBuilderConfig(cfg, bmin, bmax, tx, ty));