RenderTree3D
Package: Libraries.Game.Graphics.Trees · Inherits from: Libraries.Language.Object
RenderTree3D keeps track of where every drawable item in a 3D scene is, so that drawing and shadows only handle the items that matter. It answers these questions: - Which items can the camera see? (CollectVisible) - Which items could cast a shadow into one cascade of a directional light's shadows? (CollectDirectionalShadowCasters) - Which items are within a point light's reach? (CollectPointShadowCasters) - Which items overlap a box or a sphere? (CollectInBox, CollectInSphere) The first three are asked while drawing, and add what they find to a RenderTree3DResult. The result puts each item in its visible list if the camera can see it, or its invisible list if not, and adds each item only once, however many searches find it. The last two answer general questions, and put what they find in an array. This class only describes what a render tree does. Its actions are blank. Use one of the kinds of render tree, which can be swapped for one another because they all have these actions: - DynamicVolumeRenderTree3D, a tree of boxes that fit around the items, like the one physics uses. A good choice for most scenes, and for scenes whose items are spread unevenly or move a lot. - QuadTree3D, which divides the ground into squares. A good choice for a scene that is mostly flat and spread out, such as terrain and the things on it. - Octree3D, which divides space into cubes. A good choice for a scene spread out in every direction, such as space or a tall city. Each answer is added to a RenderTree3DResult or an Array the caller passes in. The caller empties it when it starts a new frame or pass, with the result's Empty action or the array's Empty(false), so the storage is kept and nothing new is made. The answers include hidden items; the caller can leave them out with IsShowing or IsShowingWithParents. A render tree does not watch items for changes. Whoever owns it, such as Layer3D, calls Moved for each item whose position, rotation or scale has changed, once per frame before drawing, and EndFrame after drawing. An item's bounds come from its GetRenderBounds action, unless they are given to Add. An item with no bounds is not added, and Add returns false. The few actions here that are not blank are the same for every kind of tree: the Add and CollectVisible actions that fill in a missing value and pass the request on, and IsShowing and IsShowingWithParents.
Example Code
use Libraries.Game.Graphics.Trees.RenderTree3D
use Libraries.Game.Graphics.Trees.DynamicVolumeRenderTree3D
use Libraries.Interface.Item3D
use Libraries.Containers.Array
DynamicVolumeRenderTree3D volumeTree
RenderTree3D tree = volumeTree
Item3D box
box:SetDimensions(1, 1, 1)
tree:Add(box)
Array<Item3D> found
tree:CollectInSphere(0, 0, 0, 5, found)
output found:GetSize()
Actions
Add(Libraries.Interface.Item3D item, Libraries.Game.BoundingBox localBounds)
This action adds an item that is not expected to move, with the given bounds in its own coordinates.
Parameters
itemLibraries.Interface.Item3D: The item to add.localBoundsLibraries.Game.BoundingBox: The item's bounds in its own coordinates.
Returns
boolean: True if the item was added, false if it was already in
the tree.
Add(Libraries.Interface.Item3D item, boolean isStatic)
This action adds an item, using the bounds its GetRenderBounds action gives. Most items give a box of their width, height and depth, centered on them, and items shaped otherwise, such as terrain, give their own. To use other bounds, use the Add action that takes bounds instead.
Parameters
itemLibraries.Interface.Item3D: The item to add.isStaticboolean: True if the item is not expected to move.
Returns
boolean: True if the item was added. False if it was already in
the tree, or has no bounds the tree can find.
Add(Libraries.Interface.Item3D item)
This action adds an item that is not expected to move.
Parameters
itemLibraries.Interface.Item3D: The item to add.
Returns
boolean: True if the item was added. False if it was already in
the tree, or has no bounds the tree can find.
Add(Libraries.Interface.Item3D item, Libraries.Game.BoundingBox localBounds, boolean isStatic)
This action adds an item with the given bounds in its own coordinates. Use it for items whose bounds the tree cannot find, such as terrain chunks. A terrain chunk should give bounds that hold every level of detail, so changing the level does not change the tree.
Parameters
itemLibraries.Interface.Item3D: The item to add.localBoundsLibraries.Game.BoundingBox: The item's bounds in its own coordinates.isStaticboolean: True if the item is not expected to move.
Returns
boolean: True if the item was added, false if it was already in
the tree.
CollectDirectionalShadowCasters(Libraries.Game.Graphics.DirectionalLight light, Libraries.Game.Graphics.Camera camera, Libraries.Game.Graphics.Trees.RenderTree3DResult result)
This action adds the items that could cast a shadow from a directional light into view to the result, using the light's own limits. If the light has a culling range (see DirectionalLight:SetCullingRange), the items found are those in the column of space running from the camera toward the light, as wide as twice the culling range. Objects on screen are not limited by this. A layer finds those with CollectVisible. If the light has no culling range, the items found are those that could cast a shadow into any of the light's cascades, the same as calling the action that takes a cascade for each one. A light without shadow data, such as in OpenGL, then adds nothing.
Parameters
lightLibraries.Game.Graphics.DirectionalLight: The directional light.cameraLibraries.Game.Graphics.Camera: The camera the light's culling range is measured from.resultLibraries.Game.Graphics.Trees.RenderTree3DResult: The result to add the items to. It puts each item in its visible or invisible list by whether the camera can see it.
CollectDirectionalShadowCasters(Libraries.Game.Graphics.DirectionalLight light, integer cascade, Libraries.Game.Graphics.Trees.RenderTree3DResult result)
This action adds the items that could cast a shadow into one cascade of a directional light's shadows to the result. Each cascade covers part of the camera's view and is drawn in its own pass, so this is asked once per cascade. The cascade's view-projection matrix comes from the light's shadow data, which EnvironmentVulkan updates each frame, so ask after it has. A light without shadow data for the cascade, such as in OpenGL, adds nothing. A layer that wants every caster for a light, using the light's shadow distance, can use the action that takes a camera instead.
Parameters
lightLibraries.Game.Graphics.DirectionalLight: The directional light.cascadeinteger: Which cascade, counting from 0.resultLibraries.Game.Graphics.Trees.RenderTree3DResult: The result to add the items to. It puts each item in its visible or invisible list by whether the camera can see it.
CollectInBox(Libraries.Game.BoundingBox box, Libraries.Containers.Array<Libraries.Interface.Item3D> results)
This action adds the items overlapping a box to the results.
Parameters
boxLibraries.Game.BoundingBox: The box, in world coordinates.resultsLibraries.Containers.Array<Libraries.Interface.Item3D>: The array to add the items to.
CollectInSphere(number x, number y, number z, number radius, Libraries.Containers.Array<Libraries.Interface.Item3D> results)
This action adds the items overlapping a sphere to the results.
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.
CollectPointShadowCasters(Libraries.Game.Graphics.PointLight light, Libraries.Game.Graphics.Trees.RenderTree3DResult result)
This action adds the items within a point light's culling range, as given by its GetCullingRange action, to the result.
Parameters
lightLibraries.Game.Graphics.PointLight: The point light.resultLibraries.Game.Graphics.Trees.RenderTree3DResult: The result to add the items to. It puts each item in its visible or invisible list by whether the camera can see it.
CollectVisible(Libraries.Compute.Matrix4 viewProjection, Libraries.Game.Graphics.Trees.RenderTree3DResult result)
This action adds the items inside the view of the given view-projection matrix to the result.
Parameters
viewProjectionLibraries.Compute.Matrix4: The view-projection matrix.resultLibraries.Game.Graphics.Trees.RenderTree3DResult: The result to add the items to. It puts each item in its visible or invisible list by whether the camera can see it.
CollectVisible(Libraries.Game.Graphics.Camera camera, Libraries.Game.Graphics.Trees.RenderTree3DResult result)
This action adds the items the camera can see to the result.
Parameters
cameraLibraries.Game.Graphics.Camera: The camera.resultLibraries.Game.Graphics.Trees.RenderTree3DResult: The result to add the items to. It puts each item in its visible or invisible list by whether the camera can see it.
CollectsPointShadowCasters()
This action returns true if CollectPointShadowCasters finds the items near a point light, so a layer can draw only those into each point light's shadows. A tree that doesn't returns false, and every point light draws every shadow caster, as before.
Returns
boolean: True if the tree finds point lights' shadow casters.
Empty()
This action removes every item.
EndFrame()
This action ends a frame. Call it once per frame, after drawing.
GetDynamicHeight()
This action returns how many levels the dynamic part has below its top, or -1 when it is empty.
Returns
integer: The number of levels.
GetDynamicSize()
This action returns how many items are in the dynamic part of the tree, which holds items that are moving.
Returns
integer: The number of dynamic items.
GetFirstLeaf()
This action returns the first leaf in the tree's own order, or undefined when the tree is empty. With GetNextLeaf, it visits every item once, without making any objects. The order comes from how the tree is built, not from when items were added, and it changes as items move.
Returns
Libraries.Game.Graphics.Trees.RenderTreeNode3D: The first leaf, or undefined.
GetIterator()
This action returns an iterator over every item in the tree, in the tree's own order. It makes a new iterator each time. Code that runs every frame can use GetFirstLeaf and GetNextLeaf instead, which make nothing.
Returns
Libraries.Containers.Iterator<Libraries.Interface.Item3D>: An iterator over the items.
GetLeaf(Libraries.Interface.Item3D item)
This action returns the leaf that holds an item, or undefined if the item is not in the tree. Finding it means a lookup in a hash table, so an owner that tells the tree about moves every frame should get each item's leaf once, after adding the item, keep it, and pass it to Moved(leaf) instead of the item. The leaf must not be used after the item is removed, and its setters must not be called from outside the tree.
Parameters
itemLibraries.Interface.Item3D: The item to find.
Returns
Libraries.Game.Graphics.Trees.RenderTreeNode3D: The item's leaf, or undefined if it is not in the tree.
GetNextLeaf(Libraries.Game.Graphics.Trees.RenderTreeNode3D leaf)
This action returns the leaf after the given one in the tree's own order, or undefined after the last. It follows the links between the tree's nodes, so it takes a few steps however many items there are. To visit every item while some may be removed, get the next leaf before handling the current one. Removing the current item is then safe. Removing other items, or adding items, can change the tree's shape, so some items may be missed or visited twice in that walk.
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 are in the tree.
Returns
integer: The number of items.
GetStaticHeight()
This action returns how many levels the static part has below its top, or -1 when it is empty. What a level is depends on the kind of tree.
Returns
integer: The number of levels.
GetStaticSize()
This action returns how many items are in the static part of the tree, which holds items that are not moving.
Returns
integer: The number of static items.
GetStaticVersion()
This action returns a number that goes up by one every time the static part changes. Items in the static part never move, so anything worked out from them stays correct until this number changes. Use it to keep work that depends only on static items, such as a list of the static items within a light's reach, or data prepared for drawing static items. Save the number when doing the work, and redo the work only when GetStaticVersion returns a different number.
Returns
integer: The static version.
Has(Libraries.Interface.Item3D item)
This action returns true if the item is in the tree.
Parameters
itemLibraries.Interface.Item3D: The item to look for.
Returns
boolean: True if the item is in the tree.
IsShowing(Libraries.Interface.Item3D item)
This action returns true if the item is showing. It does not look at the item's parents, so an item whose parent is hidden still counts as showing.
Parameters
itemLibraries.Interface.Item3D: The item.
Returns
boolean: True if the item is showing.
IsShowingWithParents(Libraries.Interface.Item3D item)
This action returns true if the item and all of its parents are showing. Layer3D draws by walking down from each item, so a hidden parent hides its children. The tree holds items on their own, so this checks upward instead.
Parameters
itemLibraries.Interface.Item3D: The item.
Returns
boolean: True if the item and all of its parents are showing.
IsStatic(Libraries.Interface.Item3D item)
This action returns true if the item is in the static part of the tree, and false if it is in the dynamic part or not in the tree.
Parameters
itemLibraries.Interface.Item3D: The item.
Returns
boolean: True if the item is static.
Moved(Libraries.Interface.Item3D item)
This action tells the tree an item's position, rotation or scale has changed. It looks up the item's leaf each time. Moved(leaf) does not.
Parameters
itemLibraries.Interface.Item3D: The item that moved.
Moved(Libraries.Game.Graphics.Trees.RenderTreeNode3D leaf)
This action does the same as Moved(item), for the leaf GetLeaf returned for that item, without looking it up. A leaf whose item has been removed is ignored.
Parameters
leafLibraries.Game.Graphics.Trees.RenderTreeNode3D: The leaf GetLeaf returned for the item.
Remove(Libraries.Interface.Item3D item)
This action removes an item. If the item is a point light, anything the tree remembered for it is also forgotten.
Parameters
itemLibraries.Interface.Item3D: The item to remove.
Returns
boolean: True if the item was in the tree.
SetLocalBounds(Libraries.Interface.Item3D item, Libraries.Game.BoundingBox localBounds)
This action changes an item's bounds in its own coordinates, for example when its mesh or its level of detail changes.
Parameters
itemLibraries.Interface.Item3D: The item whose bounds changed.localBoundsLibraries.Game.BoundingBox: The item's new bounds in its own coordinates.