From 80e07ebb3c18a4963aeba101b575a4cb5522faf3 Mon Sep 17 00:00:00 2001 From: ikpil Date: Wed, 17 Apr 2024 23:50:48 +0900 Subject: [PATCH] Tick 'Keep Intermediate Results' option. --- src/DotRecast.Recast.Demo/RecastDemo.cs | 15 ++++--- .../Builder/SoloNavMeshBuilder.cs | 27 +++++++++--- .../Builder/TileNavMeshBuilder.cs | 15 ++++--- .../Tools/RcTileTool.cs | 2 +- src/DotRecast.Recast/RcBuilder.cs | 44 +++++++++++++------ 5 files changed, 71 insertions(+), 32 deletions(-) diff --git a/src/DotRecast.Recast.Demo/RecastDemo.cs b/src/DotRecast.Recast.Demo/RecastDemo.cs index b6926e9..fbddfe9 100644 --- a/src/DotRecast.Recast.Demo/RecastDemo.cs +++ b/src/DotRecast.Recast.Demo/RecastDemo.cs @@ -368,7 +368,7 @@ public class RecastDemo : IRecastDemoChannel var scale = (float)_resolution.X / 1920; int fontSize = Math.Max(10, (int)(16 * scale)); - + // for windows : Microsoft Visual C++ Redistributable Package // link - https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist var imGuiFontConfig = new ImGuiFontConfig(Path.Combine("resources\\fonts", "DroidSans.ttf"), fontSize, null); @@ -379,7 +379,7 @@ public class RecastDemo : IRecastDemoChannel DemoInputGeomProvider geom = LoadInputMesh("nav_test.obj"); _sample = new DemoSample(geom, ImmutableArray.Empty, null); - + _menuView = new RcMenuView(); settingsView = new RcSettingsView(this); settingsView.SetSample(_sample); @@ -537,7 +537,7 @@ public class RecastDemo : IRecastDemoChannel bool hasBound = false; RcVec3f bminN = RcVec3f.Zero; RcVec3f bmaxN = RcVec3f.Zero; - + if (_sample.GetInputGeom() != null) { bminN = _sample.GetInputGeom().GetMeshBoundsMin(); @@ -584,8 +584,8 @@ public class RecastDemo : IRecastDemoChannel RcVec3f bmin = bminN; RcVec3f bmax = bmaxN; - camr = (float)(Math.Sqrt(RcMath.Sqr(bmax.X - bmin.X) + - RcMath.Sqr(bmax.Y - bmin.Y) + + camr = (float)(Math.Sqrt(RcMath.Sqr(bmax.X - bmin.X) + + RcMath.Sqr(bmax.Y - bmin.Y) + RcMath.Sqr(bmax.Z - bmin.Z)) / 2); cameraPos.X = (bmax.X + bmin.X) / 2 + camr; cameraPos.Y = (bmax.Y + bmin.Y) / 2 + camr; @@ -692,14 +692,15 @@ public class RecastDemo : IRecastDemoChannel NavMeshBuildResult buildResult; + var geom = _sample.GetInputGeom(); var settings = _sample.GetSettings(); if (settings.tiled) { - buildResult = tileNavMeshBuilder.Build(_sample.GetInputGeom(), settings); + buildResult = tileNavMeshBuilder.Build(geom, settings); } else { - buildResult = soloNavMeshBuilder.Build(_sample.GetInputGeom(), settings); + buildResult = soloNavMeshBuilder.Build(geom, settings); } if (!buildResult.Success) diff --git a/src/DotRecast.Recast.Toolset/Builder/SoloNavMeshBuilder.cs b/src/DotRecast.Recast.Toolset/Builder/SoloNavMeshBuilder.cs index ab145f6..ced313d 100644 --- a/src/DotRecast.Recast.Toolset/Builder/SoloNavMeshBuilder.cs +++ b/src/DotRecast.Recast.Toolset/Builder/SoloNavMeshBuilder.cs @@ -34,7 +34,8 @@ namespace DotRecast.Recast.Toolset.Builder settings.edgeMaxLen, settings.edgeMaxError, settings.vertsPerPoly, settings.detailSampleDist, settings.detailSampleMaxError, - settings.filterLowHangingObstacles, settings.filterLedgeSpans, settings.filterWalkableLowHeightSpans); + settings.filterLowHangingObstacles, settings.filterLedgeSpans, settings.filterWalkableLowHeightSpans, + settings.keepInterResults); } public NavMeshBuildResult Build(DemoInputGeomProvider geom, @@ -45,7 +46,8 @@ namespace DotRecast.Recast.Toolset.Builder float edgeMaxLen, float edgeMaxError, int vertsPerPoly, float detailSampleDist, float detailSampleMaxError, - bool filterLowHangingObstacles, bool filterLedgeSpans, bool filterWalkableLowHeightSpans) + bool filterLowHangingObstacles, bool filterLedgeSpans, bool filterWalkableLowHeightSpans, + bool keepInterResults) { RcConfig cfg = new RcConfig( partitionType, @@ -58,7 +60,7 @@ namespace DotRecast.Recast.Toolset.Builder filterLowHangingObstacles, filterLedgeSpans, filterWalkableLowHeightSpans, SampleAreaModifications.SAMPLE_AREAMOD_WALKABLE, true); - RcBuilderResult rcResult = BuildRecastResult(geom, cfg); + RcBuilderResult rcResult = BuildRecastResult(geom, cfg, keepInterResults); var meshData = BuildMeshData(geom, cellSize, cellHeight, agentHeight, agentRadius, agentMaxClimb, rcResult); if (null == meshData) { @@ -74,11 +76,26 @@ namespace DotRecast.Recast.Toolset.Builder return new DtNavMesh(meshData, vertsPerPoly, 0); } - private RcBuilderResult BuildRecastResult(DemoInputGeomProvider geom, RcConfig cfg) + private RcBuilderResult BuildRecastResult(DemoInputGeomProvider geom, RcConfig cfg, bool keepInterResults) { RcBuilderConfig bcfg = new RcBuilderConfig(cfg, geom.GetMeshBoundsMin(), geom.GetMeshBoundsMax()); RcBuilder rcBuilder = new RcBuilder(); - return rcBuilder.Build(geom, bcfg); + var result = rcBuilder.Build(geom, bcfg); + if (!keepInterResults) + { + return new RcBuilderResult( + result.TileX, + result.TileZ, + null, + result.CompactHeightfield, + result.ContourSet, + result.Mesh, + result.MeshDetail, + result.Context + ); + } + + return result; } public DtMeshData BuildMeshData(DemoInputGeomProvider geom, diff --git a/src/DotRecast.Recast.Toolset/Builder/TileNavMeshBuilder.cs b/src/DotRecast.Recast.Toolset/Builder/TileNavMeshBuilder.cs index 44a4945..15a431e 100644 --- a/src/DotRecast.Recast.Toolset/Builder/TileNavMeshBuilder.cs +++ b/src/DotRecast.Recast.Toolset/Builder/TileNavMeshBuilder.cs @@ -41,7 +41,8 @@ namespace DotRecast.Recast.Toolset.Builder settings.minRegionSize, settings.mergedRegionSize, settings.edgeMaxLen, settings.edgeMaxError, settings.vertsPerPoly, settings.detailSampleDist, settings.detailSampleMaxError, - settings.filterLowHangingObstacles, settings.filterLedgeSpans, settings.filterWalkableLowHeightSpans); + settings.filterLowHangingObstacles, settings.filterLedgeSpans, settings.filterWalkableLowHeightSpans, + settings.keepInterResults, settings.buildAll); } public NavMeshBuildResult Build(IInputGeomProvider geom, @@ -53,7 +54,8 @@ namespace DotRecast.Recast.Toolset.Builder float edgeMaxLen, float edgeMaxError, int vertsPerPoly, float detailSampleDist, float detailSampleMaxError, - bool filterLowHangingObstacles, bool filterLedgeSpans, bool filterWalkableLowHeightSpans) + bool filterLowHangingObstacles, bool filterLedgeSpans, bool filterWalkableLowHeightSpans, + bool keepInterResults, bool buildAll) { List results = BuildRecastResult( geom, @@ -65,7 +67,8 @@ namespace DotRecast.Recast.Toolset.Builder edgeMaxLen, edgeMaxError, vertsPerPoly, detailSampleDist, detailSampleMaxError, - filterLowHangingObstacles, filterLedgeSpans, filterWalkableLowHeightSpans + filterLowHangingObstacles, filterLedgeSpans, filterWalkableLowHeightSpans, + keepInterResults, buildAll ); var tileMeshData = BuildMeshData(geom, cellSize, cellHeight, agentHeight, agentRadius, agentMaxClimb, results); @@ -82,7 +85,8 @@ namespace DotRecast.Recast.Toolset.Builder float edgeMaxLen, float edgeMaxError, int vertsPerPoly, float detailSampleDist, float detailSampleMaxError, - bool filterLowHangingObstacles, bool filterLedgeSpans, bool filterWalkableLowHeightSpans) + bool filterLowHangingObstacles, bool filterLedgeSpans, bool filterWalkableLowHeightSpans, + bool keepInterResults, bool buildAll) { RcConfig cfg = new RcConfig(true, tileSize, tileSize, @@ -125,8 +129,7 @@ namespace DotRecast.Recast.Toolset.Builder { int x = result.TileX; int z = result.TileZ; - DtNavMeshCreateParams option = DemoNavMeshBuilder - .GetNavMeshCreateParams(geom, cellSize, cellHeight, agentHeight, agentRadius, agentMaxClimb, result); + DtNavMeshCreateParams option = DemoNavMeshBuilder.GetNavMeshCreateParams(geom, cellSize, cellHeight, agentHeight, agentRadius, agentMaxClimb, result); option.tileX = x; option.tileZ = z; diff --git a/src/DotRecast.Recast.Toolset/Tools/RcTileTool.cs b/src/DotRecast.Recast.Toolset/Tools/RcTileTool.cs index 020b6ce..f20f763 100644 --- a/src/DotRecast.Recast.Toolset/Tools/RcTileTool.cs +++ b/src/DotRecast.Recast.Toolset/Tools/RcTileTool.cs @@ -94,7 +94,7 @@ namespace DotRecast.Recast.Toolset.Tools var beginTick = RcFrequency.Ticks; var rb = new RcBuilder(); - var result = rb.BuildTile(geom, cfg, bmin, bmax, tx, ty, new RcAtomicInteger(0), 1); + var result = rb.BuildTile(geom, cfg, bmin, bmax, tx, ty, new RcAtomicInteger(0), 1, settings.keepInterResults); var tb = new TileNavMeshBuilder(); var meshData = tb.BuildMeshData(geom, settings.cellSize, settings.cellHeight, settings.agentHeight, settings.agentRadius, settings.agentMaxClimb, RcImmutableArray.Create(result) diff --git a/src/DotRecast.Recast/RcBuilder.cs b/src/DotRecast.Recast/RcBuilder.cs index 9123b92..f0c6ed2 100644 --- a/src/DotRecast.Recast/RcBuilder.cs +++ b/src/DotRecast.Recast/RcBuilder.cs @@ -48,7 +48,8 @@ namespace DotRecast.Recast } public List BuildTiles(IInputGeomProvider geom, RcConfig cfg, - int threads = 0, TaskFactory taskFactory = null, CancellationToken cancellation = default) + int threads = 0, TaskFactory taskFactory = null, CancellationToken cancellation = default, + bool keepInterResults = false, bool buildAll = true) { RcVec3f bmin = geom.GetMeshBoundsMin(); RcVec3f bmax = geom.GetMeshBoundsMax(); @@ -56,13 +57,14 @@ namespace DotRecast.Recast if (1 < threads) { - return BuildMultiThread(geom, cfg, bmin, bmax, tw, th, threads, taskFactory ?? Task.Factory, cancellation); + return BuildMultiThread(geom, cfg, bmin, bmax, tw, th, threads, taskFactory ?? Task.Factory, cancellation, keepInterResults, buildAll); } - return BuildSingleThread(geom, cfg, bmin, bmax, tw, th); + return BuildSingleThread(geom, cfg, bmin, bmax, tw, th, keepInterResults, buildAll); } - private List BuildSingleThread(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th) + private List BuildSingleThread(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th, + bool keepInterResults, bool buildAll) { var results = new List(th * tw); RcAtomicInteger counter = new RcAtomicInteger(0); @@ -71,7 +73,7 @@ namespace DotRecast.Recast { for (int x = 0; x < tw; ++x) { - var result = BuildTile(geom, cfg, bmin, bmax, x, y, counter, tw * th); + var result = BuildTile(geom, cfg, bmin, bmax, x, y, counter, tw * th, keepInterResults); results.Add(result); } } @@ -80,7 +82,8 @@ namespace DotRecast.Recast } private List BuildMultiThread(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th, - int threads, TaskFactory taskFactory, CancellationToken cancellation) + int threads, TaskFactory taskFactory, CancellationToken cancellation, + bool keepInterResults, bool buildAll) { var results = new ConcurrentQueue(); RcAtomicInteger progress = new RcAtomicInteger(0); @@ -99,7 +102,7 @@ namespace DotRecast.Recast try { - RcBuilderResult result = BuildTile(geom, cfg, bmin, bmax, tx, ty, progress, tw * th); + RcBuilderResult result = BuildTile(geom, cfg, bmin, bmax, tx, ty, progress, tw * th, keepInterResults); results.Enqueue(result); } catch (Exception e) @@ -127,26 +130,41 @@ namespace DotRecast.Recast return list; } - public RcBuilderResult BuildTile(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tx, int ty, RcAtomicInteger progress, int total) + public RcBuilderResult BuildTile(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tx, int ty, RcAtomicInteger progress, int total, bool keepInterResults) { - RcBuilderResult result = Build(geom, new RcBuilderConfig(cfg, bmin, bmax, tx, ty)); + var bcfg = new RcBuilderConfig(cfg, bmin, bmax, tx, ty); + RcBuilderResult result = Build(geom, bcfg); if (_progressListener != null) { _progressListener.OnProgress(progress.IncrementAndGet(), total); } + if (!keepInterResults) + { + return new RcBuilderResult( + result.TileX, + result.TileZ, + null, + result.CompactHeightfield, + result.ContourSet, + result.Mesh, + result.MeshDetail, + result.Context + ); + } + return result; } - public RcBuilderResult Build(IInputGeomProvider geom, RcBuilderConfig builderCfg) + public RcBuilderResult Build(IInputGeomProvider geom, RcBuilderConfig bcfg) { - RcConfig cfg = builderCfg.cfg; + RcConfig cfg = bcfg.cfg; RcContext ctx = new RcContext(); // // Step 1. Rasterize input polygon soup. // - RcHeightfield solid = RcVoxelizations.BuildSolidHeightfield(ctx, geom, builderCfg); - return Build(ctx, builderCfg.tileX, builderCfg.tileZ, geom, cfg, solid); + RcHeightfield solid = RcVoxelizations.BuildSolidHeightfield(ctx, geom, bcfg); + return Build(ctx, bcfg.tileX, bcfg.tileZ, geom, cfg, solid); } public RcBuilderResult Build(RcContext ctx, int tileX, int tileZ, IInputGeomProvider geom, RcConfig cfg, RcHeightfield solid)