PC-20230316NUNE\Administrator 894100ae37 提交Unity 联机Pro
2024-08-17 14:27:18 +08:00

63 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using JNGame.Math;
namespace JNGame.PathFinding
{
/// <summary>
/// 二叉空间分割树
/// </summary>
public partial class BspTree
{
private readonly List<TriangleRef> m_AllTriangle = new List<TriangleRef>();
/// <summary>
/// 根节点
/// </summary>
private BspTreeNode m_RootNode;
/// <summary>
/// 查找点邻接三角形的缓存,每次查找都会清理,外部不可缓存此列表,只可一次性使用
/// </summary>
private readonly List<int> m_TempNeareastTrianges = new List<int>(16);
/// <summary>
/// 构造二叉空间分割树
/// </summary>
/// <param name="rawTriangles">原始的三角形数据</param>
public void Init(List<NavTriangle> rawTriangles)
{
foreach (var tri in rawTriangles)
{
m_AllTriangle.Add(new TriangleRef(tri));
}
m_RootNode = new BspTreeNode();
m_RootNode.Init(m_AllTriangle);
}
/// <summary>
/// 根据位置检索BSP树快速定位点所在三角形区域
/// </summary>
/// <param name="pos"></param>
/// <param name="nearTriangleIndices"></param>
/// <returns></returns>
public int GetTriangle(in LVector3 pos, out List<int> nearTriangleIndices)
{
m_TempNeareastTrianges.Clear();
var pos2d = pos.ToLVector2XZ();
int triangleIndex = m_RootNode.GetTriangle(pos2d, out _, out List<TriangleRef> nearTriangles);
if (triangleIndex == -1)
{ // 未找到落位的三角形,说明在阻挡内,获取阻挡周边的一些三角形
for (int i = 0; i < nearTriangles.Count; ++i)
{
if (!m_TempNeareastTrianges.Contains(nearTriangles[i].index))
{
m_TempNeareastTrianges.Add(nearTriangles[i].index);
}
}
}
nearTriangleIndices = m_TempNeareastTrianges;
return triangleIndex;
}
}
}