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;
|
2023-03-28 18:03:33 +03:00
|
|
|
using DotRecast.Core;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:09:10 +03:00
|
|
|
namespace DotRecast.Recast
|
|
|
|
{
|
2023-06-08 14:53:03 +03:00
|
|
|
using static RcConstants;
|
2023-03-16 19:09:10 +03:00
|
|
|
|
2023-06-01 16:21:31 +03:00
|
|
|
public static class Recast
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-06-01 16:21:31 +03:00
|
|
|
public static void CalcBounds(float[] verts, int nv, float[] bmin, float[] bmax)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
bmin[i] = verts[i];
|
|
|
|
bmax[i] = verts[i];
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 1; i < nv; ++i)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
bmin[j] = Math.Min(bmin[j], verts[i * 3 + j]);
|
|
|
|
bmax[j] = Math.Max(bmax[j], verts[i * 3 + j]);
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
// Calculate bounding box.
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-06-03 15:47:26 +03:00
|
|
|
public static void CalcGridSize(RcVec3f bmin, RcVec3f bmax, float cs, out int sizeX, out int sizeZ)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-06-01 16:21:31 +03:00
|
|
|
sizeX = (int)((bmax.x - bmin.x) / cs + 0.5f);
|
|
|
|
sizeZ = (int)((bmax.z - bmin.z) / cs + 0.5f);
|
2023-03-28 18:03:33 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-06-03 15:47:26 +03:00
|
|
|
public static void CalcTileCount(RcVec3f bmin, RcVec3f bmax, float cs, int tileSizeX, int tileSizeZ, out int tw, out int td)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-06-01 16:21:31 +03:00
|
|
|
CalcGridSize(bmin, bmax, cs, out var gw, out var gd);
|
2023-06-01 16:25:15 +03:00
|
|
|
tw = (gw + tileSizeX - 1) / tileSizeX;
|
|
|
|
td = (gd + tileSizeZ - 1) / tileSizeZ;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// @par
|
|
|
|
///
|
|
|
|
/// Modifies the area id of all triangles with a slope below the specified value.
|
|
|
|
///
|
|
|
|
/// See the #rcConfig documentation for more information on the configuration parameters.
|
|
|
|
///
|
|
|
|
/// @see rcHeightfield, rcClearUnwalkableTriangles, rcRasterizeTriangles
|
2023-07-10 13:03:47 +03:00
|
|
|
public static int[] MarkWalkableTriangles(RcTelemetry ctx, float walkableSlopeAngle, float[] verts, int[] tris, int nt, RcAreaModification areaMod)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
int[] areas = new int[nt];
|
|
|
|
float walkableThr = (float)Math.Cos(walkableSlopeAngle / 180.0f * Math.PI);
|
2023-06-03 15:47:26 +03:00
|
|
|
RcVec3f norm = new RcVec3f();
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < nt; ++i)
|
|
|
|
{
|
|
|
|
int tri = i * 3;
|
2023-05-05 02:44:48 +03:00
|
|
|
CalcTriNormal(verts, tris[tri], tris[tri + 1], tris[tri + 2], ref norm);
|
2023-03-16 19:48:49 +03:00
|
|
|
// Check if the face is walkable.
|
2023-04-29 07:00:19 +03:00
|
|
|
if (norm.y > walkableThr)
|
2023-05-05 02:44:48 +03:00
|
|
|
areas[i] = areaMod.Apply(areas[i]);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
return areas;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-06-03 15:47:26 +03:00
|
|
|
public static void CalcTriNormal(float[] verts, int v0, int v1, int v2, ref RcVec3f norm)
|
2023-03-28 18:03:33 +03:00
|
|
|
{
|
2023-06-03 15:47:26 +03:00
|
|
|
RcVec3f e0 = new RcVec3f();
|
|
|
|
RcVec3f e1 = new RcVec3f();
|
|
|
|
RcVec3f.Sub(ref e0, verts, v1 * 3, v0 * 3);
|
|
|
|
RcVec3f.Sub(ref e1, verts, v2 * 3, v0 * 3);
|
|
|
|
RcVec3f.Cross(ref norm, e0, e1);
|
|
|
|
RcVec3f.Normalize(ref norm);
|
2023-03-28 18:03:33 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// @par
|
|
|
|
///
|
|
|
|
/// Only sets the area id's for the unwalkable triangles. Does not alter the
|
|
|
|
/// area id's for walkable triangles.
|
|
|
|
///
|
|
|
|
/// See the #rcConfig documentation for more information on the configuration parameters.
|
|
|
|
///
|
|
|
|
/// @see rcHeightfield, rcClearUnwalkableTriangles, rcRasterizeTriangles
|
2023-07-01 06:50:25 +03:00
|
|
|
public static void ClearUnwalkableTriangles(RcTelemetry ctx, float walkableSlopeAngle, float[] verts, int nv, int[] tris, int nt, int[] areas)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
float walkableThr = (float)Math.Cos(walkableSlopeAngle / 180.0f * Math.PI);
|
|
|
|
|
2023-06-03 15:47:26 +03:00
|
|
|
RcVec3f norm = new RcVec3f();
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < nt; ++i)
|
|
|
|
{
|
|
|
|
int tri = i * 3;
|
2023-05-05 02:44:48 +03:00
|
|
|
CalcTriNormal(verts, tris[tri], tris[tri + 1], tris[tri + 2], ref norm);
|
2023-03-16 19:48:49 +03:00
|
|
|
// Check if the face is walkable.
|
2023-04-29 07:00:19 +03:00
|
|
|
if (norm.y <= walkableThr)
|
2023-03-16 19:48:49 +03:00
|
|
|
areas[i] = RC_NULL_AREA;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 16:21:31 +03:00
|
|
|
}
|