/// <summary> /// 构造函数 /// </summary> /// <param name="treeType">树类型</param> /// <param name="center">树中心</param> /// <param name="size">树区域大小</param> /// <param name="maxDepth">树最大深度</param> public SeparateTree(SeparateTreeType treeType, Vector3 center, Vector3 size, int maxDepth) { mTreeType = treeType; mMaxDepth = maxDepth; mRoot = new SeparateTreeNode(new Bounds(center, size), 0, mTreeType == SeparateTreeType.QuadTree ? 4 : 8); }
protected SeparateTreeNode CreateNode(ref SeparateTreeNode node, int depth, Vector3 center, Vector3 size, IEntity entity) { SeparateTreeNode result = null; if (node == null) { Bounds bounds = new Bounds(center, size); if (bounds.ContainsEx(entity.bounds)) { SeparateTreeNode newNode = new SeparateTreeNode(bounds, depth + 1, mChildCount); node = newNode; result = node; } } else if (node.bounds.ContainsEx(entity.bounds)) { result = node; } return(result); }
public SeparateTreeNode Insert(IEntity entity, int depth, int maxDepth) { if (mEntities.Contains(entity)) { return(this); } if (depth < maxDepth) { SeparateTreeNode node = GetContainerNode(entity, depth); if (node != null) { return(node.Insert(entity, depth + 1, maxDepth)); } } mEntities.AddFirst(entity); entity.node = this; return(this); }
protected SeparateTreeNode GetContainerNode(IEntity entity, int depth) { SeparateTreeNode result = null; int ix = -1; int iz = -1; int iy = mChildCount == 4 ? 0 : -1; if (mChildren == null) { mChildren = new SeparateTreeNode[mChildCount]; } int nodeIndex = 0; Vector3 halfSize = halfSize = new Vector3(mBounds.size.x / 2, mChildCount == 4? mBounds.size.y : mBounds.size.y / 2, mBounds.size.z / 2); for (int i = ix; i <= 1; i += 2) //i = -1, 1 { for (int j = iz; j <= 1; j += 2) //j = -1, 1 { for (int k = iy; k <= 1; k += 2) //k = 4 or -1,1 { result = CreateNode(ref mChildren[nodeIndex], depth, mBounds.center + new Vector3(i * halfSize.x / 2, k * halfSize.y / 2, j * halfSize.z / 2), halfSize, entity); if (result != null) { return(result); } nodeIndex += 1; } } } return(null); }