RenderCellTree3D
Package: Libraries.Game.Graphics.Trees · Inherits from: Libraries.Language.Object, Libraries.Game.Graphics.Trees.RenderTreePart3D
RenderCellTree3D is a loose quadtree or loose octree: a part of a render tree that divides a region of the world into cells, each split into smaller cells as needed. QuadTree3D uses it without splitting the height, so each cell is a square column as tall as the region and splits into 4. Octree3D uses it splitting all three directions, so each cell is a cube and splits into 8. SetSplitsHeight chooses between the two. Each item is placed in one cell, chosen from its size and its center: the smallest cell at least as big as the item, among those containing its center. Because cells are tested with a box twice their size (they are "loose"), the item always fits in that box. Placing an item takes the same few steps however many items there are, and moving an item only changes the tree if its center moves to another cell or it changes size a lot. Cells are made only when an item needs them, and are kept after they empty. Each cell counts the leaves in it and below it, so searches skip empty cells. Items outside the region are kept in a separate list and tested one by one, so they are still found. The region is set with SetRegion, ideally before any items are added. The margin comes from RenderTreePart3D, and the box calculations are actions on RenderTreeNode3D and RenderTreeCell3D.
Example Code
use Libraries.Game.Graphics.Trees.RenderCellTree3D
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)
RenderCellTree3D cells
cells:SetRegion(-100, -100, -100, 100, 100, 100)
cells:Add(leaf, box)
Array<Item3D> found
cells:Collect(0, 0, 0, 5, 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, and places it in the cell its box belongs in.
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 cell whose loose box is entirely inside the volume adds all of its items without testing them, and a cell entirely outside is skipped with everything below it. 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. Cells whose loose box is out of reach are skipped with everything below them.
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. Cells whose loose box does not overlap it are skipped with everything below them.
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 and every cell. The leaves themselves are not changed, since they belong to the render tree.
GetFirstLeaf()
This action returns the first leaf in the tree's order, or undefined when the tree is empty. The leaves outside the region come first, then each cell's own leaves followed by its children's, starting from the top cell.
Returns
Libraries.Game.Graphics.Trees.RenderTreeNode3D: The first leaf, or undefined.
GetHeight()
This action returns the deepest level any cell has been made at, or -1 when the tree is empty.
Returns
integer: The deepest level made.
GetMaximumDepth()
This action returns the deepest a cell can be below the top cell.
Returns
integer: The deepest level.
GetNextLeaf(Libraries.Game.Graphics.Trees.RenderTreeNode3D leaf)
This action returns the leaf after the given one, or undefined after the last leaf. The next leaf is the next one in the same cell's list, if there is one. Otherwise it is the first leaf in the cell's children, and failing that, the first leaf in a later sibling of the cell or of a cell above it. Only parent and child links are followed, and cells with no leaves in them or below them are skipped by their counts.
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 leaves the tree holds, including those outside the region.
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. With a margin, nothing changes while the item stays inside the leaf's box. Otherwise the leaf's box is set to the new bounds, and it moves to another cell only if the new bounds belong in one.
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 moved to another cell.
Remove(Libraries.Game.Graphics.Trees.RenderTreeNode3D leaf)
This action removes a leaf from its cell, or from the list of leaves outside the region. The leaf itself is not reused here, since it belongs to the render tree.
Parameters
leafLibraries.Game.Graphics.Trees.RenderTreeNode3D: The leaf to remove.
SetMaximumDepth(integer depth)
This action sets the deepest a cell can be below the top cell. Each level halves the cell size. Deeper cells fit small items more tightly, but searches visit more cells. The default is 8. Leaves already in the tree are placed again.
Parameters
depthinteger: The deepest level, at least 0.
SetRegion(number minimumX, number minimumY, number minimumZ, number maximumX, number maximumY, number maximumZ)
This action sets the region of the world the cells cover. The top cell is as big as the region's largest side, centered on the region. A quadtree's cells are as tall as the region. Items outside the region still work, but are tested one by one. Leaves already in the tree are placed again.
Parameters
minimumXnumber: The smallest x of the region.minimumYnumber: The smallest y of the region.minimumZnumber: The smallest z of the region.maximumXnumber: The largest x of the region.maximumYnumber: The largest y of the region.maximumZnumber: The largest z of the region.
SetSplitsHeight(boolean value)
This action sets whether cells split the height too, as in an octree, or only across, as in a quadtree. Leaves already in the tree are placed again.
Parameters
valueboolean: True for an octree, false for a quadtree.
SplitsHeight()
This action returns true if cells split the height too, as in an octree, and false if they split only across, as in a quadtree.
Returns
boolean: True if cells split the height.