refactor: RcConvexVolumeTool

This commit is contained in:
ikpil 2023-09-10 11:30:28 +09:00
parent 4f3580d3f4
commit 122239030a
2 changed files with 92 additions and 59 deletions

View File

@ -19,13 +19,10 @@ freely, subject to the following restrictions:
*/ */
using System; using System;
using System.Collections.Generic;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Recast.Toolset.Builder; using DotRecast.Recast.Toolset.Builder;
using DotRecast.Recast.Demo.Draw; using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Geom;
using DotRecast.Recast.Toolset; using DotRecast.Recast.Toolset;
using DotRecast.Recast.Toolset.Geom;
using DotRecast.Recast.Toolset.Tools; using DotRecast.Recast.Toolset.Tools;
using ImGuiNET; using ImGuiNET;
using Serilog; using Serilog;
@ -41,13 +38,13 @@ public class ConvexVolumeSampleTool : ISampleTool
private DemoSample _sample; private DemoSample _sample;
private readonly RcConvexVolumeTool _tool; private readonly RcConvexVolumeTool _tool;
private int areaTypeValue = SampleAreaModifications.SAMPLE_AREAMOD_GRASS.Value; private float _boxHeight = 6f;
private RcAreaModification areaType = SampleAreaModifications.SAMPLE_AREAMOD_GRASS; private float _boxDescent = 1f;
private float boxHeight = 6f; private float _polyOffset = 0f;
private float boxDescent = 1f;
private float polyOffset = 0f; private int _areaTypeValue = SampleAreaModifications.SAMPLE_AREAMOD_GRASS.Value;
private readonly List<RcVec3f> pts = new(); private RcAreaModification _areaType = SampleAreaModifications.SAMPLE_AREAMOD_GRASS;
private readonly List<int> hull = new();
public ConvexVolumeSampleTool() public ConvexVolumeSampleTool()
{ {
@ -56,37 +53,36 @@ public class ConvexVolumeSampleTool : ISampleTool
public void Layout() public void Layout()
{ {
ImGui.SliderFloat("Shape Height", ref boxHeight, 0.1f, 20f, "%.1f"); ImGui.SliderFloat("Shape Height", ref _boxHeight, 0.1f, 20f, "%.1f");
ImGui.SliderFloat("Shape Descent", ref boxDescent, 0.1f, 20f, "%.1f"); ImGui.SliderFloat("Shape Descent", ref _boxDescent, 0.1f, 20f, "%.1f");
ImGui.SliderFloat("Poly Offset", ref polyOffset, 0.1f, 10f, "%.1f"); ImGui.SliderFloat("Poly Offset", ref _polyOffset, 0.1f, 10f, "%.1f");
ImGui.NewLine(); ImGui.NewLine();
int prevAreaTypeValue = _areaTypeValue;
ImGui.Text("Area Type"); ImGui.Text("Area Type");
ImGui.Separator(); ImGui.Separator();
int prevAreaTypeValue = areaTypeValue; ImGui.RadioButton("Ground", ref _areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_GROUND.Value);
ImGui.RadioButton("Ground", ref areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_GROUND.Value); ImGui.RadioButton("Water", ref _areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_WATER.Value);
ImGui.RadioButton("Water", ref areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_WATER.Value); ImGui.RadioButton("Road", ref _areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_ROAD.Value);
ImGui.RadioButton("Road", ref areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_ROAD.Value); ImGui.RadioButton("Door", ref _areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_DOOR.Value);
ImGui.RadioButton("Door", ref areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_DOOR.Value); ImGui.RadioButton("Grass", ref _areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_GRASS.Value);
ImGui.RadioButton("Grass", ref areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_GRASS.Value); ImGui.RadioButton("Jump", ref _areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_JUMP.Value);
ImGui.RadioButton("Jump", ref areaTypeValue, SampleAreaModifications.SAMPLE_AREAMOD_JUMP.Value);
ImGui.NewLine(); ImGui.NewLine();
if (prevAreaTypeValue != areaTypeValue) if (prevAreaTypeValue != _areaTypeValue)
{ {
areaType = SampleAreaModifications.OfValue(areaTypeValue); _areaType = SampleAreaModifications.OfValue(_areaTypeValue);
} }
if (ImGui.Button("Clear Shape")) if (ImGui.Button("Clear Shape"))
{ {
hull.Clear(); _tool.ClearShape();
pts.Clear();
} }
if (ImGui.Button("Remove All")) if (ImGui.Button("Remove All"))
{ {
hull.Clear(); _tool.ClearShape();
pts.Clear();
var geom = _sample.GetInputGeom(); var geom = _sample.GetInputGeom();
if (geom != null) if (geom != null)
@ -99,6 +95,10 @@ public class ConvexVolumeSampleTool : ISampleTool
public void HandleRender(NavMeshRenderer renderer) public void HandleRender(NavMeshRenderer renderer)
{ {
RecastDebugDraw dd = renderer.GetDebugDraw(); RecastDebugDraw dd = renderer.GetDebugDraw();
var pts = _tool.GetShapePoint();
var hull = _tool.GetShapeHull();
// Find height extent of the shape. // Find height extent of the shape.
float minh = float.MaxValue, maxh = 0; float minh = float.MaxValue, maxh = 0;
for (int i = 0; i < pts.Count; ++i) for (int i = 0; i < pts.Count; ++i)
@ -106,8 +106,8 @@ public class ConvexVolumeSampleTool : ISampleTool
minh = Math.Min(minh, pts[i].y); minh = Math.Min(minh, pts[i].y);
} }
minh -= boxDescent; minh -= _boxDescent;
maxh = minh + boxHeight; maxh = minh + _boxHeight;
dd.Begin(POINTS, 4.0f); dd.Begin(POINTS, 4.0f);
for (int i = 0; i < pts.Count; ++i) for (int i = 0; i < pts.Count; ++i)
@ -158,45 +158,17 @@ public class ConvexVolumeSampleTool : ISampleTool
public void HandleClick(RcVec3f s, RcVec3f p, bool shift) public void HandleClick(RcVec3f s, RcVec3f p, bool shift)
{ {
var geom = _sample.GetInputGeom(); var geom = _sample.GetInputGeom();
var settings = _sample.GetSettings();
var navMesh = _sample.GetNavMesh();
if (shift) if (shift)
{ {
_tool.RemoveByPos(geom, p); _tool.RemoveByPos(geom, p);
} }
else else
{ {
// Create if (_tool.PlottingShape(p, out var pts, out var hull))
// If clicked on that last pt, create the shape.
if (pts.Count > 0 && RcVec3f.DistSqr(p, pts[pts.Count - 1]) < 0.2f * 0.2f)
{
var vol = RcConvexVolumeTool.CreateConvexVolume(pts, hull, areaType, boxDescent, boxHeight, polyOffset);
if (null != vol)
{ {
var vol = RcConvexVolumeTool.CreateConvexVolume(pts, hull, _areaType, _boxDescent, _boxHeight, _polyOffset);
_tool.Add(geom, vol); _tool.Add(geom, vol);
} }
pts.Clear();
hull.Clear();
}
else
{
// Add new point
pts.Add(p);
// Update hull.
if (pts.Count > 3)
{
hull.Clear();
hull.AddRange(RcConvexUtils.Convexhull(pts));
}
else
{
hull.Clear();
}
}
} }
} }

View File

@ -7,11 +7,72 @@ namespace DotRecast.Recast.Toolset.Tools
{ {
public class RcConvexVolumeTool : IRcToolable public class RcConvexVolumeTool : IRcToolable
{ {
private readonly List<RcVec3f> _pts;
private readonly List<int> _hull;
public RcConvexVolumeTool()
{
_pts = new List<RcVec3f>();
_hull = new List<int>();
}
public string GetName() public string GetName()
{ {
return "Convex Volumes"; return "Convex Volumes";
} }
public List<RcVec3f> GetShapePoint()
{
return _pts;
}
public List<int> GetShapeHull()
{
return _hull;
}
public void ClearShape()
{
_pts.Clear();
_hull.Clear();
}
public bool PlottingShape(RcVec3f p, out List<RcVec3f> pts, out List<int> hull)
{
pts = null;
hull = null;
// Create
// If clicked on that last pt, create the shape.
if (_pts.Count > 0 && RcVec3f.DistSqr(p, _pts[_pts.Count - 1]) < 0.2f * 0.2f)
{
pts = new List<RcVec3f>(_pts);
hull = new List<int>(_hull);
_pts.Clear();
_hull.Clear();
return true;
}
// Add new point
_pts.Add(p);
// Update hull.
if (_pts.Count > 3)
{
_hull.Clear();
_hull.AddRange(RcConvexUtils.Convexhull(_pts));
}
else
{
_hull.Clear();
}
return false;
}
public RcConvexVolume RemoveByPos(IInputGeomProvider geom, RcVec3f pos) public RcConvexVolume RemoveByPos(IInputGeomProvider geom, RcVec3f pos)
{ {
// Delete // Delete