//recursive, MUST be called BEFORE objects inserted public void Split(int level) { // Bounds.Extents.x is the same as y and z, all nodes are square // We want each child node to be half as big as this node float splitSize = Bounds.Extents.X / 2.0f; // Also, the constructor of the Octree node takes in the CENTER // position of the new node, therefore we want to create new // nodes from the center of this node + or - 1/4 the size of // this node. This is similar to BVH, but different in size. Vector3[] childPattern = new Vector3[] { new Vector3(+1f, -1f, -1f), //Right,top, Front new Vector3(+1f, -1f, +1f), //Right,top, Back new Vector3(+1f, +1f, -1f), //Right,Bottom, Front new Vector3(+1f, +1f, +1f), //Right,Bottom, Back new Vector3(-1f, -1f, -1f), //Left, Top, Front new Vector3(-1f, -1f, +1f), //Left, Top, Back new Vector3(-1f, +1f, -1f), //Left, Bottom, Front new Vector3(-1f, +1f, +1f), //Left, Bottom, Back }; //create child nodes Children = new List <OctreeNode>(); Contents = null;//no longer a leaf node foreach (Vector3 offset in childPattern) { //account for the center of current node Point position = new Point(Bounds.Center.ToVector() + offset * splitSize); OctreeNode child = new OctreeNode(position, splitSize, this); Children.Add(child); } //if no max depth, go recursive for all children if (level > 1) { foreach (OctreeNode child in Children) { child.Split(level - 1); } } }
public void Initialize(float octreeSize) { Octree = new OctreeNode(new Point(0, 0, 0), octreeSize, null); Octree.Split(3); }