2023-04-14 16:39:34 +03:00
|
|
|
/*
|
|
|
|
recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
|
|
|
|
|
|
|
|
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.Collections.Generic;
|
|
|
|
using DotRecast.Core;
|
|
|
|
|
|
|
|
namespace DotRecast.Recast
|
|
|
|
{
|
|
|
|
public static class PolyMeshRaycast
|
|
|
|
{
|
2023-06-03 15:47:26 +03:00
|
|
|
public static float? Raycast(IList<RecastBuilderResult> results, RcVec3f src, RcVec3f dst)
|
2023-04-14 16:39:34 +03:00
|
|
|
{
|
|
|
|
foreach (RecastBuilderResult result in results)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
if (result.GetMeshDetail() != null)
|
2023-04-14 16:39:34 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
float? intersection = Raycast(result.GetMesh(), result.GetMeshDetail(), src, dst);
|
2023-04-14 16:39:34 +03:00
|
|
|
if (null != intersection)
|
|
|
|
{
|
|
|
|
return intersection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-06-08 14:53:03 +03:00
|
|
|
private static float? Raycast(RcPolyMesh poly, RcPolyMeshDetail meshDetail, RcVec3f sp, RcVec3f sq)
|
2023-04-14 16:39:34 +03:00
|
|
|
{
|
|
|
|
if (meshDetail != null)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < meshDetail.nmeshes; ++i)
|
|
|
|
{
|
|
|
|
int m = i * 4;
|
|
|
|
int bverts = meshDetail.meshes[m];
|
|
|
|
int btris = meshDetail.meshes[m + 2];
|
|
|
|
int ntris = meshDetail.meshes[m + 3];
|
|
|
|
int verts = bverts * 3;
|
|
|
|
int tris = btris * 4;
|
|
|
|
for (int j = 0; j < ntris; ++j)
|
|
|
|
{
|
2023-06-03 15:47:26 +03:00
|
|
|
RcVec3f[] vs = new RcVec3f[3];
|
2023-04-14 16:39:34 +03:00
|
|
|
for (int k = 0; k < 3; ++k)
|
|
|
|
{
|
2023-04-29 05:21:25 +03:00
|
|
|
vs[k].x = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3];
|
|
|
|
vs[k].y = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3 + 1];
|
|
|
|
vs[k].z = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3 + 2];
|
2023-04-14 16:39:34 +03:00
|
|
|
}
|
|
|
|
|
2023-07-30 05:27:10 +03:00
|
|
|
if (Intersections.IntersectSegmentTriangle(sp, sq, vs[0], vs[1], vs[2], out var intersection))
|
2023-04-14 16:39:34 +03:00
|
|
|
{
|
|
|
|
return intersection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: check PolyMesh instead
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 05:27:10 +03:00
|
|
|
}
|