WorldTree
The WorldTree is a tree data structure that is used to store the scene graph.
Constructors
constructor
Returns: WorldTree
Accessors
nodeCount
Gets the total node count for the tree.
Returns: number
root
Gets the root TreeNode.
Returns: TreeNode
Methods
addNode
Adds a TreeNode as a child of the provided parent node.
Parameters
Returns: void
addSubtree
Adds a TreeNode as the root of a subtree. The world tree can be split into subtrees, each of which will have it’s dedicated NodeMap for optimal searching speed. A subtree does not differ structurally from a regular node, and it does not alter the overall hierarchy of the world tree in any way.
Parameters
- node: The TreeNode to add as a subtree
Returns: void
findAll
Goes throught the tree starting at node if provided, otherwise at the tree root and runs the provided predicate for each node. All nodes which satisfy the predicate are returned.
Be mindful about the predicate’s contents. If the tree is very large this operation can lock the main thread for too long. If you need to execute complex predicates on large trees, walkAsync is a better candidate.
Parameters
- predicate: The SearchPredicate to run for each node
- (optional) node: The TreeNode to start at. If not provided, the tree root will be used
Returns: TreeNode[]
findId
Find a node by id. The optional subtreeId argument can narrow down the search to a specific subtree, otherwise it will search the entire tree. It returns an array of nodes because multiple nodes can have the same id, like in the case of instances.
Using this method for tree searches is encouraged because it’s accelerated by a backing NodeMap which brings down searches to just one or more lookups
Parameters
- id: The id of the node to search for
- (optional) subtreeId: The id of the subtree to search in. If undefined the search will include the entire tree
Returns: TreeNode[]
getAncestors
Gets the full list of node ancestors in hierarchical order.
Parameters
- node: The node to search ancestors for
Returns: TreeNode[]
getInstances
Gets all the instances in the provided subtree id.
Parameters
- subtree: The root subtree id
Returns: A dictionary where each instance id holds a record of TreeNode grouped by their instance unique id.
getRenderTree
Gets the RenderTree instance of the provided subtree id.
If the subtree id is not found, null
is returned. The overloaded version with
no argument gets the RenderTree instance for the entire tree, which can never be null.
Parameters
- subtreeId: The root subtree id
Returns: RenderTree
isRoot
Checks is a TreeNode is root.
Parameters
- node: TreeNode
Returns: boolean
parse
Default way of creating TreeNodes. The input model needs to follow the form.
The input model can contain virtually anything, but it should have at least the properties defined above.
Parameters
- node:
{ id: string, raw?: object, atomic?: boolean, children: []}
Returns: TreeNode
purge
Destroys part of the tree, or in the absence of a subtreeId argument, the entire tree.
Purged trees are no longer usable!
Parameters
- optional subtreeId: The subtree root id. If undefined the whole tree will get purged
Returns: void
removeNode
Removed the provided TreeNode from the tree.
Parameters
- node: TreeNode
Returns: void
walk
Walks the tree starting at node and executes the SearchPredicate for each node. If node argument is undefined, walking starts at root. Walking is stopped when the predicate returns false.
This function is synchronous and depending on the complexity of your SearchPredicate and the total number of nodes, it might block the main thread. For a heavy SearchPredicate use walkAsync.
Parameters
- predicate: SearchPredicate
- optional node: TreeNode
Returns: void
walkAsync
The asynchronous version of walk. The function will yield for 16ms (one frame) after a cummulated 100ms spent executing. The return promise will resolve to a boolean which determines if the tree was completely walked (true) or not (false).
Parameters
- predicate: SearchPredicate
- optional node: TreeNode
Returns: Promise< boolean >
Typedefs
NodeData
This is the data payload for each TreeNode.
- raw: Raw from node creation with parse
- children: Children TreeNodes
- atomic: Whether this node is a complete object (true) or just part of another object (false)
- optional subtreeId: Assigned at runtime used for search acceleration
- optional renderView: Data required for everything rendering related
- optional instanced: Whether this node is an instance
SearchPredicate
Delegate type used in tree’s findAll, walk and walkAsync methods.
TreeNode
Abstraction of a tree node. The tree is implemented on top of an existing tree library which defines the tree nodes it’s own way. At runtime the nodes will consist of: