private static void FindTriangle(Transform transform, List <NavMeshTriangle> triangles, float angle, ref Vector3 max, ref Vector3 min, ref float maxArea) { MeshFilter mf = transform.GetComponent <MeshFilter>(); if (!mf || !mf.sharedMesh) { return; } Vector3[] vlist = mf.sharedMesh.vertices; Vector3[] nlist = mf.sharedMesh.normals; Vector2[] ulist = mf.sharedMesh.uv; int[] ilist = mf.sharedMesh.triangles; for (int i = 0; i < ilist.Length; i += 3) { Vector3 n0 = transform.localToWorldMatrix.MultiplyVector(nlist[ilist[i]]); Vector3 n1 = transform.localToWorldMatrix.MultiplyVector(nlist[ilist[i + 1]]); Vector3 n2 = transform.localToWorldMatrix.MultiplyVector(nlist[ilist[i + 2]]); float ag0 = Vector3.Angle(Vector3.up, n0); float ag1 = Vector3.Angle(Vector3.up, n1); float ag2 = Vector3.Angle(Vector3.up, n2); if (ag0 > angle || ag1 > angle || ag2 > angle) { continue; } Vector3 v0 = transform.localToWorldMatrix.MultiplyPoint(vlist[ilist[i]]); Vector3 v1 = transform.localToWorldMatrix.MultiplyPoint(vlist[ilist[i + 1]]); Vector3 v2 = transform.localToWorldMatrix.MultiplyPoint(vlist[ilist[i + 2]]); Vector2 u0 = ilist[i] < ulist.Length ? ulist[ilist[i]] : Vector2.zero; Vector2 u1 = ilist[i + 1] < ulist.Length ? ulist[ilist[i + 1]] : Vector2.zero; Vector2 u2 = ilist[i + 2] < ulist.Length ? ulist[ilist[i + 2]] : Vector2.zero; max = Vector3.Max(max, v0); max = Vector3.Max(max, v1); max = Vector3.Max(max, v2); min = Vector3.Min(min, v0); min = Vector3.Min(min, v1); min = Vector3.Min(min, v2); NavMeshTriangle triangle = new NavMeshTriangle(v0, v1, v2, u0, u1, u2); float area = triangle.GetArea(); if (area > maxArea) { maxArea = area; } triangles.Add(triangle); } }
/// <summary> /// 插入数据 /// </summary> /// <param name="item"></param> public void Add(NavMeshTriangle item) { if (m_Root != null) { NavMeshOcTreeNode node = m_Root.Insert(item, 0, m_MaxDepth); if (node != null) { { m_Count++; } } } }
public NavMeshOcTreeNode Insert(NavMeshTriangle item, int depth, int maxDepth) { if (depth < maxDepth) { NavMeshOcTreeNode node = GetContainerNode(item); if (node != null) { return(node.Insert(item, depth + 1, maxDepth)); } } m_ItemList.Add(item); return(this); }
private NavMeshOcTreeNode GetContainerNode(ref NavMeshOcTreeNode node, Vector3 centerPos, Vector3 size, NavMeshTriangle item) { NavMeshOcTreeNode result = null; Bounds bd = item.bounds; if (node == null) { Bounds bounds = new Bounds(centerPos, size); if (bounds.IsBoundsContainsAnotherBounds(bd)) { node = new NavMeshOcTreeNode(bounds); result = node; } } else if (node.bounds.IsBoundsContainsAnotherBounds(bd)) { result = node; } return(result); }
private NavMeshOcTreeNode GetContainerNode(NavMeshTriangle item) { Vector3 halfSize = bounds.size / 2; NavMeshOcTreeNode result = null; result = GetContainerNode(ref m_ChildNodes[0], bounds.center + new Vector3(-halfSize.x / 2, halfSize.y / 2, halfSize.z / 2), halfSize, item); if (result != null) { return(result); } result = GetContainerNode(ref m_ChildNodes[1], bounds.center + new Vector3(-halfSize.x / 2, halfSize.y / 2, -halfSize.z / 2), halfSize, item); if (result != null) { return(result); } result = GetContainerNode(ref m_ChildNodes[2], bounds.center + new Vector3(halfSize.x / 2, halfSize.y / 2, halfSize.z / 2), halfSize, item); if (result != null) { return(result); } result = GetContainerNode(ref m_ChildNodes[3], bounds.center + new Vector3(halfSize.x / 2, halfSize.y / 2, -halfSize.z / 2), halfSize, item); if (result != null) { return(result); } result = GetContainerNode(ref m_ChildNodes[4], bounds.center + new Vector3(-halfSize.x / 2, -halfSize.y / 2, halfSize.z / 2), halfSize, item); if (result != null) { return(result); } result = GetContainerNode(ref m_ChildNodes[5], bounds.center + new Vector3(-halfSize.x / 2, -halfSize.y / 2, -halfSize.z / 2), halfSize, item); if (result != null) { return(result); } result = GetContainerNode(ref m_ChildNodes[6], bounds.center + new Vector3(halfSize.x / 2, -halfSize.y / 2, halfSize.z / 2), halfSize, item); if (result != null) { return(result); } result = GetContainerNode(ref m_ChildNodes[7], bounds.center + new Vector3(halfSize.x / 2, -halfSize.y / 2, -halfSize.z / 2), halfSize, item); if (result != null) { return(result); } return(null); }