From 9264cf64468e2a21f58936dd2dfc62886249efb0 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 2 Jul 2023 15:44:34 +0900 Subject: [PATCH] add build event --- .../Messages/NavMeshBuildEvent.cs | 6 + ...cted.cs => SourceGeomFileSelectedEvent.cs} | 2 +- src/DotRecast.Recast.Demo/RecastDemo.cs | 226 +++++++++--------- .../UI/RcSettingsView.cs | 16 +- 4 files changed, 129 insertions(+), 121 deletions(-) create mode 100644 src/DotRecast.Recast.Demo/Messages/NavMeshBuildEvent.cs rename src/DotRecast.Recast.Demo/Messages/{SourceGeomSelected.cs => SourceGeomFileSelectedEvent.cs} (61%) diff --git a/src/DotRecast.Recast.Demo/Messages/NavMeshBuildEvent.cs b/src/DotRecast.Recast.Demo/Messages/NavMeshBuildEvent.cs new file mode 100644 index 0000000..4509271 --- /dev/null +++ b/src/DotRecast.Recast.Demo/Messages/NavMeshBuildEvent.cs @@ -0,0 +1,6 @@ +namespace DotRecast.Recast.Demo.Messages; + +public class NavMeshBuildEvent : IRecastDemoMessage +{ + +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Messages/SourceGeomSelected.cs b/src/DotRecast.Recast.Demo/Messages/SourceGeomFileSelectedEvent.cs similarity index 61% rename from src/DotRecast.Recast.Demo/Messages/SourceGeomSelected.cs rename to src/DotRecast.Recast.Demo/Messages/SourceGeomFileSelectedEvent.cs index 37fd46d..7c6147d 100644 --- a/src/DotRecast.Recast.Demo/Messages/SourceGeomSelected.cs +++ b/src/DotRecast.Recast.Demo/Messages/SourceGeomFileSelectedEvent.cs @@ -1,6 +1,6 @@ namespace DotRecast.Recast.Demo.Messages; -public class SourceGeomSelected : IRecastDemoMessage +public class SourceGeomFileSelectedEvent : IRecastDemoMessage { public required string FilePath { get; init; } } \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/RecastDemo.cs b/src/DotRecast.Recast.Demo/RecastDemo.cs index 1b8af5d..b839059 100644 --- a/src/DotRecast.Recast.Demo/RecastDemo.cs +++ b/src/DotRecast.Recast.Demo/RecastDemo.cs @@ -66,7 +66,6 @@ public class RecastDemo : IRecastDemoChannel //private readonly RecastDebugDraw dd; private NavMeshRenderer renderer; - private bool building = false; private float timeAcc = 0; private float camr = 1000; @@ -519,108 +518,6 @@ public class RecastDemo : IRecastDemoChannel // } } - if (settingsView.IsBuildTriggered() && sample.GetInputGeom() != null) - { - if (!building) - { - var settings = sample.GetSettings(); - var partitioning = settings.partitioning; - var cellSize = settings.cellSize; - var cellHeight = settings.cellHeight; - var agentHeight = settings.agentHeight; - var agentRadius = settings.agentRadius; - var agentMaxClimb = settings.agentMaxClimb; - var agentMaxSlope = settings.agentMaxSlope; - var regionMinSize = settings.minRegionSize; - var regionMergeSize = settings.mergedRegionSize; - var edgeMaxLen = settings.edgeMaxLen; - var edgeMaxError = settings.edgeMaxError; - var vertsPerPoly = settings.vertsPerPoly; - var detailSampleDist = settings.detailSampleDist; - var detailSampleMaxError = settings.detailSampleMaxError; - var filterLowHangingObstacles = settings.filterLowHangingObstacles; - var filterLedgeSpans = settings.filterLedgeSpans; - var filterWalkableLowHeightSpans = settings.filterWalkableLowHeightSpans; - var tileSize = settings.tileSize; - - long t = RcFrequency.Ticks; - - Logger.Information($"build"); - - NavMeshBuildResult buildResult; - if (settings.tiled) - { - buildResult = tileNavMeshBuilder.Build( - sample.GetInputGeom(), - partitioning, - cellSize, - cellHeight, - agentHeight, - agentRadius, - agentMaxClimb, - agentMaxSlope, - regionMinSize, - regionMergeSize, - edgeMaxLen, - edgeMaxError, - vertsPerPoly, - detailSampleDist, - detailSampleMaxError, - filterLowHangingObstacles, - filterLedgeSpans, - filterWalkableLowHeightSpans, - tileSize - ); - } - else - { - buildResult = soloNavMeshBuilder.Build( - sample.GetInputGeom(), - partitioning, - cellSize, - cellHeight, - agentHeight, - agentRadius, - agentMaxClimb, - agentMaxSlope, - regionMinSize, - regionMergeSize, - edgeMaxLen, - edgeMaxError, - vertsPerPoly, - detailSampleDist, - detailSampleMaxError, - filterLowHangingObstacles, - filterLedgeSpans, - filterWalkableLowHeightSpans - ); - } - - sample.Update(sample.GetInputGeom(), buildResult.RecastBuilderResults, buildResult.NavMesh); - sample.SetChanged(false); - settingsView.SetBuildTime((RcFrequency.Ticks - t) / TimeSpan.TicksPerMillisecond); - //settingsUI.SetBuildTelemetry(buildResult.Item1.Select(x => x.GetTelemetry()).ToList()); - toolset.SetSample(sample); - - Logger.Information($"build times"); - Logger.Information($"-----------------------------------------"); - var telemetries = buildResult.RecastBuilderResults - .Select(x => x.GetTelemetry()) - .SelectMany(x => x.ToList()) - .GroupBy(x => x.Key) - .ToImmutableSortedDictionary(x => x.Key, x => x.Sum(y => y.Millis)); - - foreach (var (key, millis) in telemetries) - { - Logger.Information($"{key}: {millis} ms"); - } - } - } - else - { - building = false; - } - if (!_mouseOverMenu) { GLU.GlhUnProjectf(mousePos[0], viewport[3] - 1 - mousePos[1], 0.0f, modelviewMatrix, projectionMatrix, viewport, ref rayStart); @@ -801,15 +698,124 @@ public class RecastDemo : IRecastDemoChannel _messages.Enqueue(message); } - public void OnMessage(IRecastDemoMessage message) + private void OnMessage(IRecastDemoMessage message) { - if (message is SourceGeomSelected args) + if (message is SourceGeomFileSelectedEvent args) { - var bytes = Loader.ToBytes(args.FilePath); - var geom = LoadInputMesh(bytes); - - sample.Update(geom, ImmutableArray.Empty, null); + OnSourceGeomFileSelected(args); + } + else if (message is NavMeshBuildEvent args2) + { + OnNavMeshBuild(args2); + } + } + + private void OnSourceGeomFileSelected(SourceGeomFileSelectedEvent args) + { + var bytes = Loader.ToBytes(args.FilePath); + var geom = LoadInputMesh(bytes); + + sample.Update(geom, ImmutableArray.Empty, null); + } + + private void OnNavMeshBuild(NavMeshBuildEvent args) + { + if (null == sample.GetInputGeom()) + { + Logger.Information($"not found source geom"); + return; + } + + var settings = sample.GetSettings(); + var partitioning = settings.partitioning; + var cellSize = settings.cellSize; + var cellHeight = settings.cellHeight; + var agentHeight = settings.agentHeight; + var agentRadius = settings.agentRadius; + var agentMaxClimb = settings.agentMaxClimb; + var agentMaxSlope = settings.agentMaxSlope; + var regionMinSize = settings.minRegionSize; + var regionMergeSize = settings.mergedRegionSize; + var edgeMaxLen = settings.edgeMaxLen; + var edgeMaxError = settings.edgeMaxError; + var vertsPerPoly = settings.vertsPerPoly; + var detailSampleDist = settings.detailSampleDist; + var detailSampleMaxError = settings.detailSampleMaxError; + var filterLowHangingObstacles = settings.filterLowHangingObstacles; + var filterLedgeSpans = settings.filterLedgeSpans; + var filterWalkableLowHeightSpans = settings.filterWalkableLowHeightSpans; + var tileSize = settings.tileSize; + + long t = RcFrequency.Ticks; + + Logger.Information($"build"); + + NavMeshBuildResult buildResult; + if (settings.tiled) + { + buildResult = tileNavMeshBuilder.Build( + sample.GetInputGeom(), + partitioning, + cellSize, + cellHeight, + agentHeight, + agentRadius, + agentMaxClimb, + agentMaxSlope, + regionMinSize, + regionMergeSize, + edgeMaxLen, + edgeMaxError, + vertsPerPoly, + detailSampleDist, + detailSampleMaxError, + filterLowHangingObstacles, + filterLedgeSpans, + filterWalkableLowHeightSpans, + tileSize + ); + } + else + { + buildResult = soloNavMeshBuilder.Build( + sample.GetInputGeom(), + partitioning, + cellSize, + cellHeight, + agentHeight, + agentRadius, + agentMaxClimb, + agentMaxSlope, + regionMinSize, + regionMergeSize, + edgeMaxLen, + edgeMaxError, + vertsPerPoly, + detailSampleDist, + detailSampleMaxError, + filterLowHangingObstacles, + filterLedgeSpans, + filterWalkableLowHeightSpans + ); + } + + sample.Update(sample.GetInputGeom(), buildResult.RecastBuilderResults, buildResult.NavMesh); + sample.SetChanged(false); + settingsView.SetBuildTime((RcFrequency.Ticks - t) / TimeSpan.TicksPerMillisecond); + //settingsUI.SetBuildTelemetry(buildResult.Item1.Select(x => x.GetTelemetry()).ToList()); + toolset.SetSample(sample); + + Logger.Information($"build times"); + Logger.Information($"-----------------------------------------"); + var telemetries = buildResult.RecastBuilderResults + .Select(x => x.GetTelemetry()) + .SelectMany(x => x.ToList()) + .GroupBy(x => x.Key) + .ToImmutableSortedDictionary(x => x.Key, x => x.Sum(y => y.Millis)); + + foreach (var (key, millis) in telemetries) + { + Logger.Information($"{key}: {millis} ms"); } } - } \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/UI/RcSettingsView.cs b/src/DotRecast.Recast.Demo/UI/RcSettingsView.cs index d184f4f..de28d83 100644 --- a/src/DotRecast.Recast.Demo/UI/RcSettingsView.cs +++ b/src/DotRecast.Recast.Demo/UI/RcSettingsView.cs @@ -35,7 +35,6 @@ public class RcSettingsView : IRcView private static readonly ILogger Logger = Log.ForContext(); private readonly IRecastDemoChannel _channel; - private bool buildTriggered; private long buildTime; private readonly int[] voxels = new int[2]; @@ -105,7 +104,7 @@ public class RcSettingsView : IRcView var picker = ImFilePicker.GetFilePicker(strLoadSourceGeom, Path.Combine(Environment.CurrentDirectory), ".obj"); if (picker.Draw()) { - _channel.SendMessage(new SourceGeomSelected() + _channel.SendMessage(new SourceGeomFileSelectedEvent() { FilePath = picker.SelectedFile, }); @@ -191,7 +190,11 @@ public class RcSettingsView : IRcView ImGui.Text($"Build Time: {buildTime} ms"); ImGui.Separator(); - buildTriggered = ImGui.Button("Build NavMesh"); + if (ImGui.Button("Build NavMesh")) + { + _channel.SendMessage(new NavMeshBuildEvent()); + } + { const string strLoadNavMesh = "Load NavMesh"; if (ImGui.Button(strLoadNavMesh)) @@ -233,13 +236,6 @@ public class RcSettingsView : IRcView ImGui.End(); } - - public bool IsBuildTriggered() - { - return buildTriggered; - } - - public void SetBuildTime(long buildTime) { this.buildTime = buildTime;