add recast demo message

This commit is contained in:
ikpil 2023-07-02 15:35:46 +09:00
parent a6b1cc1d5a
commit 5335c133e9
7 changed files with 81 additions and 46 deletions

View File

@ -20,7 +20,6 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using DotRecast.Core; using DotRecast.Core;
namespace DotRecast.Detour namespace DotRecast.Detour

View File

@ -0,0 +1,6 @@
namespace DotRecast.Recast.Demo.Messages;
public interface IRecastDemoChannel
{
void SendMessage(IRecastDemoMessage message);
}

View File

@ -0,0 +1,6 @@
namespace DotRecast.Recast.Demo.Messages;
public class IRecastDemoMessage
{
}

View File

@ -0,0 +1,6 @@
namespace DotRecast.Recast.Demo.Messages;
public class SourceGeomSelected : IRecastDemoMessage
{
public required string FilePath { get; init; }
}

View File

@ -37,6 +37,7 @@ using DotRecast.Detour.Extras.Unity.Astar;
using DotRecast.Detour.Io; using DotRecast.Detour.Io;
using DotRecast.Recast.DemoTool.Builder; using DotRecast.Recast.DemoTool.Builder;
using DotRecast.Recast.Demo.Draw; using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Demo.Messages;
using DotRecast.Recast.DemoTool.Geom; using DotRecast.Recast.DemoTool.Geom;
using DotRecast.Recast.Demo.Tools; using DotRecast.Recast.Demo.Tools;
using DotRecast.Recast.Demo.UI; using DotRecast.Recast.Demo.UI;
@ -48,7 +49,7 @@ using Window = Silk.NET.Windowing.Window;
namespace DotRecast.Recast.Demo; namespace DotRecast.Recast.Demo;
public class RecastDemo public class RecastDemo : IRecastDemoChannel
{ {
private static readonly ILogger Logger = Log.ForContext<RecastDemo>(); private static readonly ILogger Logger = Log.ForContext<RecastDemo>();
@ -117,9 +118,11 @@ public class RecastDemo
private long prevFrameTime; private long prevFrameTime;
private RecastDebugDraw dd; private RecastDebugDraw dd;
private Queue<IRecastDemoMessage> _messages;
public RecastDemo() public RecastDemo()
{ {
_messages = new();
} }
public void Run() public void Run()
@ -370,7 +373,7 @@ public class RecastDemo
DemoInputGeomProvider geom = LoadInputMesh(Loader.ToBytes("nav_test.obj")); DemoInputGeomProvider geom = LoadInputMesh(Loader.ToBytes("nav_test.obj"));
sample = new Sample(geom, ImmutableArray<RecastBuilderResult>.Empty, null); sample = new Sample(geom, ImmutableArray<RecastBuilderResult>.Empty, null);
settingsView = new RcSettingsView(); settingsView = new RcSettingsView(this);
settingsView.SetSample(sample); settingsView.SetSample(sample);
toolset = new RcToolsetView( toolset = new RcToolsetView(
@ -491,14 +494,7 @@ public class RecastDemo
simIter++; simIter++;
} }
if (settingsView.IsMeshInputTrigerred()) if (settingsView.IsNavMeshInputTrigerred())
{
var bytes = Loader.ToBytes(settingsView.GetMeshInputFilePath());
var geom = LoadInputMesh(bytes);
sample.Update(geom, ImmutableArray<RecastBuilderResult>.Empty, null);
}
else if (settingsView.IsNavMeshInputTrigerred())
{ {
// try (MemoryStack stack = StackPush()) { // try (MemoryStack stack = StackPush()) {
// PointerBuffer aFilterPatterns = stack.MallocPointer(4); // PointerBuffer aFilterPatterns = stack.MallocPointer(4);
@ -758,6 +754,11 @@ public class RecastDemo
toolset.SetSample(sample); toolset.SetSample(sample);
} }
if (_messages.TryDequeue(out var msg))
{
OnMessage(msg);
}
var io = ImGui.GetIO(); var io = ImGui.GetIO();
@ -794,4 +795,21 @@ public class RecastDemo
window.SwapBuffers(); window.SwapBuffers();
} }
public void SendMessage(IRecastDemoMessage message)
{
_messages.Enqueue(message);
}
public void OnMessage(IRecastDemoMessage message)
{
if (message is SourceGeomSelected args)
{
var bytes = Loader.ToBytes(args.FilePath);
var geom = LoadInputMesh(bytes);
sample.Update(geom, ImmutableArray<RecastBuilderResult>.Empty, null);
}
}
} }

View File

@ -23,6 +23,7 @@ using System.Linq;
using System.Numerics; using System.Numerics;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Recast.Demo.Draw; using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Demo.Messages;
using DotRecast.Recast.DemoTool; using DotRecast.Recast.DemoTool;
using ImGuiNET; using ImGuiNET;
using Serilog; using Serilog;
@ -31,8 +32,9 @@ namespace DotRecast.Recast.Demo.UI;
public class RcSettingsView : IRcView public class RcSettingsView : IRcView
{ {
private static readonly ILogger Logger = Log.ForContext<RecastDemo>(); private static readonly ILogger Logger = Log.ForContext<RcSettingsView>();
private readonly IRecastDemoChannel _channel;
private bool buildTriggered; private bool buildTriggered;
private long buildTime; private long buildTime;
@ -43,8 +45,6 @@ public class RcSettingsView : IRcView
private int drawMode = DrawMode.DRAWMODE_NAVMESH.Idx; private int drawMode = DrawMode.DRAWMODE_NAVMESH.Idx;
private string meshInputFilePath;
private bool meshInputTrigerred;
private bool navMeshInputTrigerred; private bool navMeshInputTrigerred;
private bool _isHovered; private bool _isHovered;
@ -53,8 +53,9 @@ public class RcSettingsView : IRcView
private Sample _sample; private Sample _sample;
private RcCanvas _canvas; private RcCanvas _canvas;
public RcSettingsView() public RcSettingsView(IRecastDemoChannel channel)
{ {
_channel = channel;
} }
public void SetSample(Sample sample) public void SetSample(Sample sample)
@ -104,17 +105,15 @@ public class RcSettingsView : IRcView
var picker = ImFilePicker.GetFilePicker(strLoadSourceGeom, Path.Combine(Environment.CurrentDirectory), ".obj"); var picker = ImFilePicker.GetFilePicker(strLoadSourceGeom, Path.Combine(Environment.CurrentDirectory), ".obj");
if (picker.Draw()) if (picker.Draw())
{ {
meshInputTrigerred = true; _channel.SendMessage(new SourceGeomSelected()
meshInputFilePath = picker.SelectedFile; {
FilePath = picker.SelectedFile,
});
ImFilePicker.RemoveFilePicker(strLoadSourceGeom); ImFilePicker.RemoveFilePicker(strLoadSourceGeom);
} }
ImGui.EndPopup(); ImGui.EndPopup();
} }
else
{
meshInputTrigerred = false;
}
ImGui.Text($"Verts: {voxels[0]} Tris: {voxels[1]}"); ImGui.Text($"Verts: {voxels[0]} Tris: {voxels[1]}");
ImGui.NewLine(); ImGui.NewLine();
@ -192,8 +191,9 @@ public class RcSettingsView : IRcView
ImGui.Text($"Build Time: {buildTime} ms"); ImGui.Text($"Build Time: {buildTime} ms");
ImGui.Separator(); ImGui.Separator();
buildTriggered = ImGui.Button("Build Nav Mesh"); buildTriggered = ImGui.Button("Build NavMesh");
const string strLoadNavMesh = "Load Nav Mesh..."; {
const string strLoadNavMesh = "Load NavMesh";
if (ImGui.Button(strLoadNavMesh)) if (ImGui.Button(strLoadNavMesh))
{ {
ImGui.OpenPopup(strLoadNavMesh); ImGui.OpenPopup(strLoadNavMesh);
@ -205,12 +205,22 @@ public class RcSettingsView : IRcView
var picker = ImFilePicker.GetFilePicker(strLoadNavMesh, Path.Combine(Environment.CurrentDirectory)); var picker = ImFilePicker.GetFilePicker(strLoadNavMesh, Path.Combine(Environment.CurrentDirectory));
if (picker.Draw()) if (picker.Draw())
{ {
Console.WriteLine(picker.SelectedFile); Logger.Information(picker.SelectedFile);
ImFilePicker.RemoveFilePicker(strLoadNavMesh); ImFilePicker.RemoveFilePicker(strLoadNavMesh);
} }
ImGui.EndPopup(); ImGui.EndPopup();
} }
}
{
const string strSaveNavMesh = "Save NavMesh";
if (ImGui.Button(strSaveNavMesh))
{
}
}
ImGui.NewLine(); ImGui.NewLine();
@ -262,16 +272,6 @@ public class RcSettingsView : IRcView
this.maxPolys = maxPolys; this.maxPolys = maxPolys;
} }
public bool IsMeshInputTrigerred()
{
return meshInputTrigerred;
}
public string GetMeshInputFilePath()
{
return meshInputFilePath;
}
public bool IsNavMeshInputTrigerred() public bool IsNavMeshInputTrigerred()
{ {
return navMeshInputTrigerred; return navMeshInputTrigerred;

View File

@ -2,13 +2,13 @@
{ {
public class ConvexVolumeToolImpl : ISampleTool public class ConvexVolumeToolImpl : ISampleTool
{ {
private Sample _sample;
public string GetName() public string GetName()
{ {
return "Create Convex Volumes"; return "Create Convex Volumes";
} }
private Sample _sample;
public void SetSample(Sample sample) public void SetSample(Sample sample)
{ {
_sample = sample; _sample = sample;