protected SceneSeparateTreeNode <T> GetContainerNode(T obj, int depth) { SceneSeparateTreeNode <T> result = null; int ix = -1; int iz = -1; int iy = m_ChildNodes.Length == 4 ? 0 : -1; int nodeIndex = 0; for (int i = ix; i <= 1; i += 2) { for (int j = iz; j <= 1; j += 2) { for (int k = iy; k <= 1; k += 2) { result = CreateNode(ref m_ChildNodes[nodeIndex], depth, m_Bounds.center + new Vector3(i * m_HalfSize.x / 2, k * m_HalfSize.y / 2, j * m_HalfSize.z / 2), m_HalfSize, obj); if (result != null) { return(result); } nodeIndex += 1; } } } return(null); }
/// <summary> /// 构造函数 /// </summary> /// <param name="treeType">树类型</param> /// <param name="center">树中心</param> /// <param name="size">树区域大小</param> /// <param name="maxDepth">树最大深度</param> public SceneSeparateTree(SceneSeparateTreeType treeType, Vector3 center, Vector3 size, int maxDepth) { this.m_TreeType = treeType; this.m_MaxDepth = maxDepth; if (treeType == SceneSeparateTreeType.QuadTree) { this.m_Root = new SceneSeparateTreeNode <T>(new Bounds(center, size), 0, 4); } else { this.m_Root = new SceneSeparateTreeNode <T>(new Bounds(center, size), 0, 8); } }
public SceneSeparateTreeNode <T> Insert(T obj, int depth, int maxDepth) { if (m_ObjectList.Contains(obj)) { return(this); } if (depth < maxDepth) { SceneSeparateTreeNode <T> node = GetContainerNode(obj, depth); if (node != null) { return(node.Insert(obj, depth + 1, maxDepth)); } } var n = m_ObjectList.AddFirst(obj); obj.SetLinkedListNode(n); return(this); }
protected SceneSeparateTreeNode <T> CreateNode(ref SceneSeparateTreeNode <T> node, int depth, Vector3 centerPos, Vector3 size, T obj) { SceneSeparateTreeNode <T> result = null; if (node == null) { Bounds bounds = new Bounds(centerPos, size); if (bounds.IsBoundsContainsAnotherBounds(obj.Bounds)) { SceneSeparateTreeNode <T> newNode = new SceneSeparateTreeNode <T>(bounds, depth + 1, m_ChildNodes.Length); node = newNode; result = node; } } else if (node.Bounds.IsBoundsContainsAnotherBounds(obj.Bounds)) { result = node; } return(result); }