kindred-node
Builds on top of scene-tree to include a component system (see kindred-component) and game loop.
var Turntable =var Sphere =var Render =var Node =var camera = Nodevar sphere =var scene = Nodescenescene
Note: while this currently builds on top of scene-tree, it may fork into its own implementation for simplicity.
Usage
node = KindredNode(props)
Creates a new node. props
is an optional object that may be supplied for assigning additional data to a node. This accepts any data you supply, but the following properties take on special behaviour:
position
: an[x, y, z]
array specifying the node's position relative to its parent.scale
: an[x, y, z]
array specifying the node's scale. You can also pass in a single number.rotation
: an[x, y, z, w]
array specifying the node's rotation as a quaternion.
var Node =var node = Nodeposition: 0 1 0scale: 5rotation: 0 0 MathPI / 2
node.data
The data you supplied when creating the node is made accessible here.
var node = Nodeposition: 0 1 0color: 1 0 1 1console // [0, 1, 0]console // [1, 0, 1, 1]
Wrangling Components
node.use(Component, props)
node.unuse(Component)
node.component(Component)
node.components()
Game Loop
scene.loop(options, eachFrame)
scene.perspective(fov, width, height, near, far)
scene.step(props)
scene.draw(gl, camera)
3D Transforms
node.setPosition(x, y, z)
Updates the node's position. Note that this method should be used instead of modifying node.data.position
directly, as it also triggers an update of the node's matrices.
var node = Nodenodenode
node.setRotation(x, y, z)
Updates the node's rotation quaternion. Again, this should be called instead of modifying node.data.rotation
directly.
var node = Nodenodenode
node.setEuler(x, y, z, order)
Update the node's rotation quaternion using euler (XYZ) angles. Optionally you can pass in an order
string to specify the order in which to apply the rotations. This method is included for convenience, but is generally slower than using node.setRotation
directly.
var node = Nodenodenodenodenode
node.setScale(x, y, z)
Updates the node's scale. Again, this should be called instead of modifying node.data.rotation
directly.
var node = Nodenodenodenode
node.modelMatrix
4x4 Float32Array
matrix that contains the transformation required to place the node at its correct position in the scene.
node.normalMatrix
3x3 Float32Array
matrix that contains the transformation required for the node's normals to correctly light the object given its new position in the scene.
Scene Hierarchy
node.add(children...)
Adds one or more children
to a given node
. Accepts a single node, an array of nodes, or a mixture of both across multiple arguments.
var root = Noderootrootroot
node.addOne(child)
node.add()
without any of the sugar: adds a single child
node.
var root = Noderoot
node.remove(child)
Removes child
from node
, if applicable.
var root = Nodevar child = Noderootroot
node.clear()
Removes any children attached to the given node
.
var root = Noderootrootconsole // 2rootclearconsole // 0
node.each(visitor)
Calls the visitor
function on each descendent node in the tree (depth first,
pre-order). Note that visitor
is not called on node
itself.
var root = Node id: 0root// 1// 2// 3// 4// 5
node.findup(visitor)
Walks up the tree from node
until hitting the root element, calling visitor
on each node along the way.
var child = Node id: 0var root = Node id: 1rootchild// 4// 1
node.flat(output)
Returns a flat array of all the child nodes in a tree.
var root = Node id: 0var ids = rootconsole // [0, 1, 2, 3, 4, 5]
node.size()
Gets the total number of descendent nodes of node
, not including node
itself:
var root = Node id: 0console // 5
getNodeList = node.list([sortFunction])
Returns a function that sorts descendent nodes only as required using sortFunction
, returning a flat array of the results. This is the preferred way to iterate over the elements in the tree when you're ready to render them.
var root = Nodevar getNodeList = rootroot{var nodes =for var i = 0; i < nodeslength; i++}
node.resetLists()
Call this on a node whenever something has occurred that would change its sort order, e.g. its position has been changed. This will reset any existing lists to sort their contents again, provided the root node is an ancestor of node
.
License
MIT. See LICENSE.md for details.