/// <summary> /// Grow the octree to fit in all objects. /// </summary> /// <param name="direction">Direction to grow.</param> void Grow(Vector3 direction) { int xDirection = direction.x >= 0 ? 1 : -1; int yDirection = direction.y >= 0 ? 1 : -1; int zDirection = direction.z >= 0 ? 1 : -1; BoundsOctreeNode <T> oldRoot = rootNode; float half = rootNode.BaseLength / 2; float newLength = rootNode.BaseLength * 2; Vector3 newCenter = rootNode.Center + new Vector3(xDirection * half, yDirection * half, zDirection * half); // Create a new, bigger octree root node rootNode = new BoundsOctreeNode <T>(newLength, minSize, looseness, newCenter); // Create 7 new octree children to go with the old root as children of the new root int rootPos = GetRootPosIndex(xDirection, yDirection, zDirection); BoundsOctreeNode <T>[] children = new BoundsOctreeNode <T> [8]; for (int i = 0; i < 8; i++) { if (i == rootPos) { children[i] = oldRoot; } else { xDirection = i % 2 == 0 ? -1 : 1; yDirection = i > 3 ? -1 : 1; zDirection = (i < 2 || (i > 3 && i < 6)) ? -1 : 1; children[i] = new BoundsOctreeNode <T>(rootNode.BaseLength, minSize, looseness, newCenter + new Vector3(xDirection * half, yDirection * half, zDirection * half)); } } // Attach the new children to the new root node rootNode.SetChildren(children); }
/// <summary> /// Grow the octree to fit in all objects. /// </summary> /// <param name="direction">Direction to grow.</param> void Grow(Vector3 direction) { int xDirection = direction.x >= 0 ? 1 : -1; int yDirection = direction.y >= 0 ? 1 : -1; int zDirection = direction.z >= 0 ? 1 : -1; BoundsOctreeNode <T> oldRoot = rootNode; float half = rootNode.BaseLength / 2; float newLength = rootNode.BaseLength * 2; Vector3 newCenter = rootNode.Center + new Vector3(xDirection * half, yDirection * half, zDirection * half); GameObject go = GameObject.Find("Node " + rootNode.GetHashCode().ToString()); // Create a new, bigger octree root node rootNode = new BoundsOctreeNode <T>(newLength, minSize, looseness, newCenter); if (go != null) { go.transform.position = newCenter; go.transform.localScale = Vector3.one * newLength; go.name = "Node " + rootNode.GetHashCode(); Debug.Log("Node: Change scale #" + rootNode.GetHashCode().ToString()); } else { GameObject newGameObject = GameObject.Instantiate(GameObject.Find("TempNode"), newCenter, Quaternion.identity); newGameObject.transform.localScale = Vector3.one * newLength; newGameObject.name = "Node " + rootNode.GetHashCode(); Debug.Log("Node: New game object #" + rootNode.GetHashCode()); } if (oldRoot.HasAnyObjects()) { // Create 7 new octree children to go with the old root as children of the new root int rootPos = GetRootPosIndex(xDirection, yDirection, zDirection); BoundsOctreeNode <T>[] children = new BoundsOctreeNode <T> [8]; for (int i = 0; i < 8; i++) { if (i == rootPos) { children[i] = oldRoot; } else { xDirection = i % 2 == 0 ? -1 : 1; yDirection = i > 3 ? -1 : 1; zDirection = (i < 2 || (i > 3 && i < 6)) ? -1 : 1; children[i] = new BoundsOctreeNode <T>(rootNode.BaseLength, minSize, looseness, newCenter + new Vector3(xDirection * half, yDirection * half, zDirection * half)); } } // Attach the new children to the new root node rootNode.SetChildren(children); } }
/// <summary> /// Grow the octree to fit in all objects. /// </summary> /// <param name="direction">Direction to grow.</param> // 扩展八叉树的范围,原来的范围*2 // 会创建7个新的节点,同旧的根节点,一个8个节点 // 作为新的根节点的孩子 void Grow(Vector3 direction) { int xDirection = direction.x >= 0 ? 1 : -1; int yDirection = direction.y >= 0 ? 1 : -1; int zDirection = direction.z >= 0 ? 1 : -1; BoundsOctreeNode <T> oldRoot = rootNode; float half = rootNode.BaseLength / 2; // 新的根节点大小,是原来的2倍 float newLength = rootNode.BaseLength * 2; // 新的根节点的中心坐标 Vector3 newCenter = rootNode.Center + new Vector3(xDirection * half, yDirection * half, zDirection * half); // Create a new, bigger octree root node rootNode = new BoundsOctreeNode <T>(newLength, minSize, looseness, newCenter); // 创建其他的7个子节点,连同旧的root,是8个子节点 // 作为新的根节点的孩子 if (oldRoot.HasAnyObjects()) { // Create 7 new octree children to go with the old root as children of the new root int rootPos = rootNode.BestFitChild(oldRoot.Center); BoundsOctreeNode <T>[] children = new BoundsOctreeNode <T> [8]; for (int i = 0; i < 8; i++) { if (i == rootPos) { children[i] = oldRoot; } else { // i = 0, i%2 = 0 // i = 1, i%2 = 1 xDirection = i % 2 == 0 ? -1 : 1; // 上下2层 yDirection = i > 3 ? -1 : 1; zDirection = (i < 2 || (i > 3 && i < 6)) ? -1 : 1; children[i] = new BoundsOctreeNode <T>(oldRoot.BaseLength, minSize, looseness, newCenter + new Vector3(xDirection * half, yDirection * half, zDirection * half)); } } // Attach the new children to the new root node rootNode.SetChildren(children); } }
/// <summary> /// Grow the octree to fit in all objects. /// </summary> /// <param name="direction">Direction to grow.</param> void Grow(Vector3 direction) { int xDirection = direction.x >= 0 ? 1 : -1; int yDirection = direction.y >= 0 ? 1 : -1; int zDirection = direction.z >= 0 ? 1 : -1; BoundsOctreeNode <T> oldRoot = rootNode; Vector3 half = rootNode.BaseSize / 2; Vector3 newLength = rootNode.BaseSize * 2; Vector3 newCenter = rootNode.Center + new Vector3(xDirection * half.x, yDirection * half.y, zDirection * half.z); // Create a new, bigger octree root node rootNode = new BoundsOctreeNode <T>(newLength, minSize, looseness, newCenter); if (oldRoot.HasAnyObjects()) { // Create 7 new octree children to go with the old root as children of the new root int rootPos = rootNode.BestFitChild(oldRoot.Center); BoundsOctreeNode <T>[] children = new BoundsOctreeNode <T> [8]; for (int i = 0; i < 8; i++) { if (i == rootPos) { children[i] = oldRoot; } else { xDirection = i % 2 == 0 ? -1 : 1; yDirection = i > 3 ? -1 : 1; zDirection = (i < 2 || (i > 3 && i < 6)) ? -1 : 1; children[i] = new BoundsOctreeNode <T>(oldRoot.BaseSize, minSize, looseness, newCenter + new Vector3(xDirection * half.x, yDirection * half.y, zDirection * half.z)); } } // Attach the new children to the new root node rootNode.SetChildren(children); } }