RenderBoundingVolumeTree3D
Package: Libraries.Game.Graphics.Trees · Inherits from: Libraries.Language.Object, Libraries.Game.Graphics.Trees.RenderTreePart3D
RenderBoundingVolumeTree3D is a dynamic bounding volume tree for drawing: a binary tree of boxes, where each leaf holds one drawable item and each branch holds the box around its two children. A search can skip a whole branch when its box is out of view. The algorithm is the one used by the physics tree, DynamicBoundingVolumeTree3D, which comes from Box2D and Bullet. Adding a leaf walks down from the root, choosing the child whose box grows least, measured by perimeter. Removing replaces the leaf's parent with its sibling. Both refit the boxes above and rebalance with rotations, so the tree stays shallow. This class is a separate copy rather than a shared one, so that drawing can change without any risk to physics. A fix to the shared algorithm in either class should be checked in the other. DynamicVolumeRenderTree3D uses two of these, as the parts of a StaticDynamicRenderTree3D: a static half with no margin, and a dynamic half whose leaves are made a little bigger than their items, so small moves need no change to the tree. The leaves belong to the render tree, which moves them between the two halves. This class creates and reuses only the branch nodes. The margin comes from RenderTreePart3D, and the box calculations are actions on RenderTreeNode3D.
Example Code
use Libraries.Game.Graphics.Trees.RenderBoundingVolumeTree3D
use Libraries.Game.Graphics.Trees.RenderTreeNode3D
use Libraries.Interface.Item3D
use Libraries.Game.BoundingBox
use Libraries.Containers.Array
Item3D item
RenderTreeNode3D leaf
leaf:SetItem(item)
BoundingBox box
box:Extend(1, 1, 1)
box:Extend(-1, -1, -1)
RenderBoundingVolumeTree3D tree
tree:Add(leaf, box)
Array<Item3D> found
tree:Collect(box, found)
output found:GetSize()
Actions
Add(Libraries.Game.Graphics.Trees.RenderTreeNode3D leaf, Libraries.Game.BoundingBox itemBounds)
This action adds a leaf, with a box around the given bounds of its item.
Parameters
leafLibraries.Game.Graphics.Trees.RenderTreeNode3D: The leaf to add. Its item must already be set.itemBoundsLibraries.Game.BoundingBox: The item's bounds in the world.
Collect(Libraries.Game.Graphics.Trees.CullingVolume3D volume, Libraries.Game.Graphics.Trees.RenderTree3DResult result)
This action adds to the result every item whose box is at least partly inside the volume. A branch entirely inside the volume adds all of its items without testing them, and a branch entirely outside is skipped. The result decides which of its lists each item goes in, and skips items it already has.
Parameters
volumeLibraries.Game.Graphics.Trees.CullingVolume3D: The volume, such as a camera's view or a light's shadow column.resultLibraries.Game.Graphics.Trees.RenderTree3DResult: The result to add the items to.
Collect(number x, number y, number z, number radius, Libraries.Containers.Array<Libraries.Interface.Item3D> results)
This action adds to the results every item whose box overlaps the sphere with the given center and radius, such as a point light's reach.
Parameters
xnumber: The x coordinate of the sphere's center.ynumber: The y coordinate of the sphere's center.znumber: The z coordinate of the sphere's center.radiusnumber: The sphere's radius.resultsLibraries.Containers.Array<Libraries.Interface.Item3D>: The array to add the items to.
Collect(Libraries.Game.BoundingBox box, Libraries.Containers.Array<Libraries.Interface.Item3D> results)
This action adds to the results every item whose box overlaps the given box.
Parameters
boxLibraries.Game.BoundingBox: The box, in world coordinates.resultsLibraries.Containers.Array<Libraries.Interface.Item3D>: The array to add the items to.
Collect(number x, number y, number z, number radius, Libraries.Game.Graphics.Trees.RenderTree3DResult result)
This action adds to the result every item whose box overlaps the sphere with the given center and radius, such as a point light's reach. The result decides which of its lists each item goes in, and skips items it already has.
Parameters
xnumber: The x coordinate of the sphere's center.ynumber: The y coordinate of the sphere's center.znumber: The z coordinate of the sphere's center.radiusnumber: The sphere's radius.resultLibraries.Game.Graphics.Trees.RenderTree3DResult: The result to add the items to.
Empty()
This action removes every leaf. The branch nodes are dropped rather than kept for reuse, since emptying is rare.
GetFirstLeaf()
This action returns the leftmost leaf, the first in the tree's order, or undefined when the tree is empty.
Returns
Libraries.Game.Graphics.Trees.RenderTreeNode3D: The first leaf, or undefined.
GetHeight()
This action returns how many levels the tree has below its root, or -1 when it is empty.
Returns
integer: The number of levels.
GetNextLeaf(Libraries.Game.Graphics.Trees.RenderTreeNode3D leaf)
This action returns the leaf after the given one, going from left to right, or undefined after the last leaf. It climbs from the leaf while it is a right child. The first time it is a left child, the next leaf is the leftmost leaf of its right sibling. If it reaches the root first, the given leaf was the last. Only parent and child links are followed, so each step takes at most the tree's height, and on average about two moves.
Parameters
leafLibraries.Game.Graphics.Trees.RenderTreeNode3D: A leaf in this tree.
Returns
Libraries.Game.Graphics.Trees.RenderTreeNode3D: The next leaf, or undefined.
GetSize()
This action returns how many items, or leaves, the tree holds.
Returns
integer: The number of leaves.
Move(Libraries.Game.Graphics.Trees.RenderTreeNode3D leaf, Libraries.Game.BoundingBox itemBounds)
This action updates a leaf whose item has moved. If the item's new bounds are still inside the leaf's box, nothing changes. Otherwise the leaf is removed, given a new box, and added again.
Parameters
leafLibraries.Game.Graphics.Trees.RenderTreeNode3D: The leaf whose item moved.itemBoundsLibraries.Game.BoundingBox: The item's new bounds in the world.
Returns
boolean: True if the leaf was moved in the tree.
Remove(Libraries.Game.Graphics.Trees.RenderTreeNode3D leaf)
This action removes a leaf. The leaf itself is not reused here, since it belongs to the render tree.
Parameters
leafLibraries.Game.Graphics.Trees.RenderTreeNode3D: The leaf to remove.