2023-03-14 08:02:43 +03:00
|
|
|
/*
|
|
|
|
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
|
|
|
|
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
|
2023-03-15 17:00:29 +03:00
|
|
|
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Silk.NET.Windowing;
|
|
|
|
using DotRecast.Core;
|
|
|
|
using DotRecast.Recast.Demo.Builder;
|
|
|
|
using DotRecast.Recast.Demo.Draw;
|
|
|
|
using DotRecast.Recast.Demo.Geom;
|
|
|
|
using static DotRecast.Recast.Demo.Draw.DebugDraw;
|
|
|
|
using static DotRecast.Recast.Demo.Draw.DebugDrawPrimitives;
|
|
|
|
|
|
|
|
namespace DotRecast.Recast.Demo.Tools;
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public class ConvexVolumeTool : Tool
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
private Sample sample;
|
|
|
|
private AreaModification areaType = SampleAreaModifications.SAMPLE_AREAMOD_GRASS;
|
|
|
|
private readonly float[] boxHeight = new[] { 6f };
|
|
|
|
private readonly float[] boxDescent = new[] { 1f };
|
|
|
|
private readonly float[] polyOffset = new[] { 0f };
|
|
|
|
private readonly List<float> pts = new();
|
|
|
|
private readonly List<int> hull = new();
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public override void setSample(Sample m_sample)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
sample = m_sample;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public override void handleClick(float[] s, float[] p, bool shift)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
DemoInputGeomProvider geom = sample.getInputGeom();
|
2023-03-16 19:48:49 +03:00
|
|
|
if (geom == null)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
if (shift)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
// Delete
|
|
|
|
int nearestIndex = -1;
|
|
|
|
IList<ConvexVolume> vols = geom.convexVolumes();
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < vols.Count; ++i)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
if (PolyUtils.pointInPoly(vols[i].verts, p) && p[1] >= vols[i].hmin
|
2023-03-16 19:48:49 +03:00
|
|
|
&& p[1] <= vols[i].hmax)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
nearestIndex = i;
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
// If end point close enough, delete it.
|
2023-03-16 19:48:49 +03:00
|
|
|
if (nearestIndex != -1)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
geom.convexVolumes().RemoveAt(nearestIndex);
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
// Create
|
|
|
|
|
|
|
|
// If clicked on that last pt, create the shape.
|
|
|
|
if (pts.Count > 0 && DemoMath.vDistSqr(p,
|
|
|
|
new float[] { pts[pts.Count - 3], pts[pts.Count - 2], pts[pts.Count - 1] },
|
2023-03-16 19:48:49 +03:00
|
|
|
0) < 0.2f * 0.2f)
|
|
|
|
{
|
|
|
|
if (hull.Count > 2)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
// Create shape.
|
|
|
|
float[] verts = new float[hull.Count * 3];
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < hull.Count; ++i)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
verts[i * 3] = pts[hull[i] * 3];
|
|
|
|
verts[i * 3 + 1] = pts[hull[i] * 3 + 1];
|
|
|
|
verts[i * 3 + 2] = pts[hull[i] * 3 + 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
float minh = float.MaxValue, maxh = 0;
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < hull.Count; ++i)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
minh = Math.Min(minh, verts[i * 3 + 1]);
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
minh -= boxDescent[0];
|
|
|
|
maxh = minh + boxHeight[0];
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
if (polyOffset[0] > 0.01f)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
float[] offset = new float[verts.Length * 2];
|
|
|
|
int noffset = PolyUtils.offsetPoly(verts, hull.Count, polyOffset[0], offset,
|
2023-03-16 19:48:49 +03:00
|
|
|
offset.Length);
|
|
|
|
if (noffset > 0)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
geom.addConvexVolume(ArrayUtils.CopyOf(offset, 0, noffset * 3), minh, maxh, areaType);
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
geom.addConvexVolume(verts, minh, maxh, areaType);
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
pts.Clear();
|
|
|
|
hull.Clear();
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
// Add new point
|
|
|
|
pts.Add(p[0]);
|
|
|
|
pts.Add(p[1]);
|
|
|
|
pts.Add(p[2]);
|
|
|
|
// Update hull.
|
2023-03-16 19:48:49 +03:00
|
|
|
if (pts.Count > 3)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
hull.Clear();
|
|
|
|
hull.AddRange(ConvexUtils.convexhull(pts));
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
hull.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public override void handleRender(NavMeshRenderer renderer)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
RecastDebugDraw dd = renderer.getDebugDraw();
|
|
|
|
// Find height extent of the shape.
|
|
|
|
float minh = float.MaxValue, maxh = 0;
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < pts.Count; i += 3)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
minh = Math.Min(minh, pts[i + 1]);
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
minh -= boxDescent[0];
|
|
|
|
maxh = minh + boxHeight[0];
|
|
|
|
|
|
|
|
dd.begin(POINTS, 4.0f);
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < pts.Count; i += 3)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
int col = duRGBA(255, 255, 255, 255);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (i == pts.Count - 3)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
col = duRGBA(240, 32, 16, 255);
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
dd.vertex(pts[i + 0], pts[i + 1] + 0.1f, pts[i + 2], col);
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
dd.end();
|
|
|
|
|
|
|
|
dd.begin(LINES, 2.0f);
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0, j = hull.Count - 1; i < hull.Count; j = i++)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
int vi = hull[j] * 3;
|
|
|
|
int vj = hull[i] * 3;
|
|
|
|
dd.vertex(pts[vj + 0], minh, pts[vj + 2], duRGBA(255, 255, 255, 64));
|
|
|
|
dd.vertex(pts[vi + 0], minh, pts[vi + 2], duRGBA(255, 255, 255, 64));
|
|
|
|
dd.vertex(pts[vj + 0], maxh, pts[vj + 2], duRGBA(255, 255, 255, 64));
|
|
|
|
dd.vertex(pts[vi + 0], maxh, pts[vi + 2], duRGBA(255, 255, 255, 64));
|
|
|
|
dd.vertex(pts[vj + 0], minh, pts[vj + 2], duRGBA(255, 255, 255, 64));
|
|
|
|
dd.vertex(pts[vj + 0], maxh, pts[vj + 2], duRGBA(255, 255, 255, 64));
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
dd.end();
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public override void layout(IWindow ctx)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
// nk_layout_row_dynamic(ctx, 20, 1);
|
|
|
|
// nk_property_float(ctx, "Shape Height", 0.1f, boxHeight, 20f, 0.1f, 0.1f);
|
|
|
|
// nk_layout_row_dynamic(ctx, 20, 1);
|
|
|
|
// nk_property_float(ctx, "Shape Descent", 0.1f, boxDescent, 20f, 0.1f, 0.1f);
|
|
|
|
// nk_layout_row_dynamic(ctx, 20, 1);
|
|
|
|
// nk_property_float(ctx, "Poly Offset", 0.1f, polyOffset, 10f, 0.1f, 0.1f);
|
|
|
|
// nk_label(ctx, "Area Type", NK_TEXT_ALIGN_LEFT);
|
|
|
|
// nk_layout_row_dynamic(ctx, 20, 1);
|
|
|
|
// if (nk_option_label(ctx, "Ground", areaType == SampleAreaModifications.SAMPLE_AREAMOD_GROUND)) {
|
|
|
|
// areaType = SampleAreaModifications.SAMPLE_AREAMOD_GROUND;
|
|
|
|
// }
|
|
|
|
// if (nk_option_label(ctx, "Water", areaType == SampleAreaModifications.SAMPLE_AREAMOD_WATER)) {
|
|
|
|
// areaType = SampleAreaModifications.SAMPLE_AREAMOD_WATER;
|
|
|
|
// }
|
|
|
|
// if (nk_option_label(ctx, "Road", areaType == SampleAreaModifications.SAMPLE_AREAMOD_ROAD)) {
|
|
|
|
// areaType = SampleAreaModifications.SAMPLE_AREAMOD_ROAD;
|
|
|
|
// }
|
|
|
|
// if (nk_option_label(ctx, "Door", areaType == SampleAreaModifications.SAMPLE_AREAMOD_DOOR)) {
|
|
|
|
// areaType = SampleAreaModifications.SAMPLE_AREAMOD_DOOR;
|
|
|
|
// }
|
|
|
|
// if (nk_option_label(ctx, "Grass", areaType == SampleAreaModifications.SAMPLE_AREAMOD_GRASS)) {
|
|
|
|
// areaType = SampleAreaModifications.SAMPLE_AREAMOD_GRASS;
|
|
|
|
// }
|
|
|
|
// if (nk_option_label(ctx, "Jump", areaType == SampleAreaModifications.SAMPLE_AREAMOD_JUMP)) {
|
|
|
|
// areaType = SampleAreaModifications.SAMPLE_AREAMOD_JUMP;
|
|
|
|
// }
|
|
|
|
// if (nk_button_text(ctx, "Clear Shape")) {
|
|
|
|
// hull.clear();
|
|
|
|
// pts.clear();
|
|
|
|
// }
|
|
|
|
// if (nk_button_text(ctx, "Remove All")) {
|
|
|
|
// hull.clear();
|
|
|
|
// pts.clear();
|
|
|
|
// DemoInputGeomProvider geom = sample.getInputGeom();
|
|
|
|
// if (geom != null) {
|
|
|
|
// geom.clearConvexVolumes();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public override string getName()
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return "Create Convex Volumes";
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public override void handleUpdate(float dt)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|