/// <summary> /// 划分场景 /// </summary> void SplitScene(List <SceneItem> items, Bounds rootBounds) { int itemCount = items.Count; // 划分 _root = new SceneTreeNode(this, 0); _root.Split(items, rootBounds, _settings.maxDepth); }
/// <summary> /// 生成节点信息 /// </summary> void GenNodeInfo(StringBuilder sb, SceneTreeNode node, int childIndex) { sb.AppendFormat("({0}) ", node.items.Length); }
/// <summary> /// 划分 /// </summary> public void Split(List <SceneTree.SceneItem> items, Bounds bounds, int maxDepth) { do { // 包围盒 var itemCount = items.Count; if (0 == _depth) { _bounds = bounds; } else { _bounds = new Bounds(); for (int i = 0; i < itemCount; ++i) { var item = items[i]; if (0 == i) { _bounds = item.bounds; continue; } _bounds.Encapsulate(item.bounds); } } // 没有对象了 if (itemCount < 1) { Debug.LogErrorFormat("Empty items! Tree depth is {0}", _depth); break; } // 最大深度 if (_depth >= maxDepth) { break; } // 子节点包围盒 var childrenBounds = GenChildrenBounds(_tree.settings.splitType); if (null == childrenBounds) { break; } // 尝试放入子节点 var its = new List <SceneTree.SceneItem>(); for (int i = 0; i < childrenBounds.Length; ++i) { its.Clear(); var childBounds = childrenBounds[i]; for (int j = 0, cj = items.Count; j < cj; ++j) { var item = items[j]; if (MathUtil.BoundsContains(childBounds, item.bounds)) { its.Add(item); items.RemoveAt(j--); --cj; } } if (its.Count > 0) { if (null == _children) { _children = new SceneTreeNode[childrenBounds.Length]; } _children[i] = new SceneTreeNode(_tree, depth + 1); _children[i].Split(its, childBounds, maxDepth); } } }while(false); // 剩下的,放到自己里面 _items = items.ToArray(); }