Class Node
- java.lang.Object
-
- com.google.ar.sceneform.NodeParent
-
- com.google.ar.sceneform.Node
-
- All Implemented Interfaces:
TransformProvider
- Direct Known Subclasses:
AnchorNode
,Camera
public class Node extends NodeParent implements TransformProvider
A Node represents a transformation within the scene graph's hierarchy. It can contain a renderable for the rendering engine to render.Each node can have an arbitrary number of child nodes and one parent. The parent may be another node, or the scene.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Node.LifecycleListener
Interface definition for callbacks to be invoked when node lifecycle events occur.static interface
Node.OnTapListener
Interface definition for a callback to be invoked when a node is tapped.static interface
Node.OnTouchListener
Interface definition for a callback to be invoked when a touch event is dispatched to this node.static interface
Node.TransformChangedListener
Interface definition for callbacks to be invoked when the transformation of the node changes.
-
Constructor Summary
Constructors Constructor Description Node()
Creates a node with no parent.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addLifecycleListener(Node.LifecycleListener lifecycleListener)
Adds a listener that will be called when node lifecycle events occur.void
addTransformChangedListener(Node.TransformChangedListener transformChangedListener)
Adds a listener that will be called when the node's transformation changes.void
callOnHierarchy(java.util.function.Consumer<Node> consumer)
Traverses the hierarchy and call a method on each node (including this node).Node
findInHierarchy(java.util.function.Predicate<Node> condition)
Traverses the hierarchy to find the first node (including this node) that meets a condition.Vector3
getBack()
Gets the world-space back vector (+z) of this node.CollisionShape
getCollisionShape()
Gets the shape to use for collisions with this node.Vector3
getDown()
Gets the world-space down vector (-y) of this node.Vector3
getForward()
Gets the world-space forward vector (-z) of this node.Vector3
getLeft()
Gets the world-space left vector (-x) of this node.Light
getLight()
Gets the current light, which is mutable.Vector3
getLocalPosition()
Gets a copy of the nodes position relative to its parent (local-space).Quaternion
getLocalRotation()
Gets a copy of the nodes rotation relative to its parent (local-space).Vector3
getLocalScale()
Gets a copy of the nodes scale relative to its parent (local-space).java.lang.String
getName()
Returns the name of the node.Node
getParent()
Returns the parent of this node.Renderable
getRenderable()
Gets the renderable to display for this node.RenderableInstance
getRenderableInstance()
Vector3
getRight()
Gets the world-space right vector (+x) of this node.Scene
getScene()
Returns the scene that this node is part of, null if it isn't part of any scene.Vector3
getUp()
Gets the world-space up vector (+y) of this node.Matrix
getWorldModelMatrix()
Vector3
getWorldPosition()
Get a copy of the nodes world-space position.Quaternion
getWorldRotation()
Gets a copy of the nodes world-space rotation.Vector3
getWorldScale()
Gets a copy of the nodes world-space scale.boolean
isActive()
Returns true if the node is active.boolean
isDescendantOf(NodeParent ancestor)
Checks whether the given node parent is an ancestor of this node recursively.boolean
isEnabled()
Gets the enabled state of this node.boolean
isSelectable()
Retrieve if the node can be selected within the when a touch event happened.boolean
isTopLevel()
Returns true if this node is top level.Vector3
localToWorldDirection(Vector3 direction)
Converts a direction from the local-space of this node to world-space.Vector3
localToWorldPoint(Vector3 point)
Converts a point in the local-space of this node to world-space.void
onActivate()
Handles when this node becomes active.void
onDeactivate()
Handles when this node becomes inactivate.boolean
onTouchEvent(HitTestResult hitTestResult, android.view.MotionEvent motionEvent)
Handles when this node is touched.void
onTransformChange(Node originatingNode)
Handles when this node's transformation is changed.void
onUpdate(FrameTime frameTime)
Handles when this node is updated.void
removeLifecycleListener(Node.LifecycleListener lifecycleListener)
Removes a listener that will be called when node lifecycle events occur.void
removeTransformChangedListener(Node.TransformChangedListener transformChangedListener)
Removes a listener that will be called when the node's transformation changes.void
setCollisionShape(CollisionShape collisionShape)
Sets the shape to used to detect collisions for thisNode
.void
setEnabled(boolean enabled)
Sets the enabled state of this node.void
setLight(Light light)
Sets theLight
to display.void
setLocalPosition(Vector3 position)
Sets the position of this node relative to its parent (local-space).void
setLocalRotation(Quaternion rotation)
Sets the rotation of this node relative to its parent (local-space).void
setLocalScale(Vector3 scale)
Sets the scale of this node relative to its parent (local-space).void
setLookDirection(Vector3 lookDirection)
Sets the direction that the node is looking at in world-space.void
setLookDirection(Vector3 lookDirection, Vector3 upDirection)
Sets the direction that the node is looking at in world-space.void
setName(java.lang.String name)
Sets the name of this node.void
setOnTapListener(Node.OnTapListener onTapListener)
Registers a callback to be invoked when this node is tapped.void
setOnTouchListener(Node.OnTouchListener onTouchListener)
Registers a callback to be invoked when a touch event is dispatched to this node.void
setParent(NodeParent parent)
Changes the parent node of this node.RenderableInstance
setRenderable(Renderable renderable)
Sets theRenderable
to display for this node.void
setSelectable(boolean selectable)
Defines if the node can be selected within the when a touch event happened.void
setWorldPosition(Vector3 position)
Sets the world-space position of this node.void
setWorldRotation(Quaternion rotation)
Sets the world-space rotation of this node.void
setWorldScale(Vector3 scale)
Sets the world-space scale of this node.java.lang.String
toString()
Vector3
worldToLocalDirection(Vector3 direction)
Converts a direction from world-space to the local-space of this node.Vector3
worldToLocalPoint(Vector3 point)
Converts a point in world-space to the local-space of this node.-
Methods inherited from class com.google.ar.sceneform.NodeParent
addChild, findByName, getChildren, removeChild
-
-
-
-
Method Detail
-
setName
public final void setName(java.lang.String name)
Sets the name of this node. Nodes can be found using their names. Multiple nodes may have the same name, in which case callingNodeParent.findByName(String)
will return the first node with the given name.- Parameters:
name
- The name of the node.
-
getName
public final java.lang.String getName()
Returns the name of the node. The default value is "Node".
-
setParent
public void setParent(@Nullable NodeParent parent)
Changes the parent node of this node. If set to null, this node will be detached from its parent. The local position, rotation, and scale of this node will remain the same. Therefore, the world position, rotation, and scale of this node may be different after the parent changes.The parent may be another
Node
or aScene
. If it is a scene, then thisNode
is considered top level.getParent()
will return null, andgetScene()
will return the scene.- Parameters:
parent
- The new parent that this node will be a child of. If null, this node will be detached from its parent.- See Also:
getParent()
,getScene()
-
getScene
@Nullable public final Scene getScene()
Returns the scene that this node is part of, null if it isn't part of any scene. A node is part of a scene if its highest level ancestor is aScene
-
getParent
@Nullable public final Node getParent()
-
isTopLevel
public boolean isTopLevel()
Returns true if this node is top level. A node is considered top level if it has no parent or if the parent is the scene.- Returns:
- true if the node is top level
-
isDescendantOf
public final boolean isDescendantOf(NodeParent ancestor)
Checks whether the given node parent is an ancestor of this node recursively.- Parameters:
ancestor
- the node parent to check- Returns:
- true if the node is an ancestor of this node
-
setEnabled
public final void setEnabled(boolean enabled)
Sets the enabled state of this node. Note that a Node may be enabled but still inactive if it isn't part of the scene or if its parent is inactive.- Parameters:
enabled
- the new enabled status of the node- See Also:
isActive()
-
isEnabled
public final boolean isEnabled()
Gets the enabled state of this node. Note that a Node may be enabled but still inactive if it isn't part of the scene or if its parent is inactive.- Returns:
- the node's enabled status.
- See Also:
isActive()
-
isActive
public final boolean isActive()
Returns true if the node is active. A node is considered active if it meets ALL of the following conditions:- The node is part of a scene.
- the node's parent is active.
- The node is enabled.
An active Node has the following behavior:
- The node's
onUpdate(FrameTime)
function will be called every frame. - The node's
getRenderable()
will be rendered. - The node's
getCollisionShape()
will be checked in calls to Scene.hitTest. - The node's
onTouchEvent(HitTestResult, MotionEvent)
function will be called when the node is touched.
- Returns:
- the node's active status
- See Also:
onActivate()
,onDeactivate()
-
isSelectable
public boolean isSelectable()
Retrieve if the node can be selected within the when a touch event happened.- Returns:
- true if the node can be selected
-
setSelectable
public void setSelectable(boolean selectable)
Defines if the node can be selected within the when a touch event happened.- Parameters:
selectable
- true if the node can be selected
-
setOnTouchListener
public void setOnTouchListener(@Nullable Node.OnTouchListener onTouchListener)
Registers a callback to be invoked when a touch event is dispatched to this node. The way that touch events are propagated mirrors the way touches are propagated to Android Views. This is only called when the node is active.When an ACTION_DOWN event occurs, that represents the start of a gesture. ACTION_UP or ACTION_CANCEL represents when a gesture ends. When a gesture starts, the following is done:
- Dispatch touch events to the node that was touched as detected by
Scene.hitTest(Ray, boolean)
. - If the node doesn't consume the event, recurse upwards through the node's parents and dispatch the touch event until one of the node's consumes the event.
- If no nodes consume the event, the gesture is ignored and subsequent events that are part of the gesture will not be passed to any nodes.
- If one of the node's consumes the event, then that node will consume all future touch events for the gesture.
When a touch event is dispatched to a node, the event is first passed to the node's
Node.OnTouchListener
. If theNode.OnTouchListener
doesn't handle the event, it is passed toonTouchEvent(HitTestResult, MotionEvent)
.- See Also:
Node.OnTouchListener
- Dispatch touch events to the node that was touched as detected by
-
setOnTapListener
public void setOnTapListener(@Nullable Node.OnTapListener onTapListener)
Registers a callback to be invoked when this node is tapped. If there is a callback registered, then touch events will not bubble to this node's parent. If the Node.onTouchEvent is overridden and super.onTouchEvent is not called, then the tap will not occur.- See Also:
Node.OnTapListener
-
addLifecycleListener
public void addLifecycleListener(Node.LifecycleListener lifecycleListener)
Adds a listener that will be called when node lifecycle events occur. The listeners will be called in the order in which they were added.
-
removeLifecycleListener
public void removeLifecycleListener(Node.LifecycleListener lifecycleListener)
Removes a listener that will be called when node lifecycle events occur.
-
addTransformChangedListener
public void addTransformChangedListener(Node.TransformChangedListener transformChangedListener)
Adds a listener that will be called when the node's transformation changes.
-
removeTransformChangedListener
public void removeTransformChangedListener(Node.TransformChangedListener transformChangedListener)
Removes a listener that will be called when the node's transformation changes.
-
getLocalPosition
public final Vector3 getLocalPosition()
Gets a copy of the nodes position relative to its parent (local-space). IfisTopLevel()
is true, then this is the same asgetWorldPosition()
.- Returns:
- a new vector that represents the node's local-space position
- See Also:
setLocalPosition(Vector3)
-
getLocalRotation
public final Quaternion getLocalRotation()
Gets a copy of the nodes rotation relative to its parent (local-space). IfisTopLevel()
is true, then this is the same asgetWorldRotation()
.- Returns:
- a new quaternion that represents the node's local-space rotation
- See Also:
setLocalRotation(Quaternion)
-
getLocalScale
public final Vector3 getLocalScale()
Gets a copy of the nodes scale relative to its parent (local-space). IfisTopLevel()
is true, then this is the same asgetWorldScale()
.- Returns:
- a new vector that represents the node's local-space scale
- See Also:
setLocalScale(Vector3)
-
getWorldPosition
public final Vector3 getWorldPosition()
Get a copy of the nodes world-space position.- Returns:
- a new vector that represents the node's world-space position
- See Also:
setWorldPosition(Vector3)
-
getWorldRotation
public final Quaternion getWorldRotation()
Gets a copy of the nodes world-space rotation.- Returns:
- a new quaternion that represents the node's world-space rotation
- See Also:
setWorldRotation(Quaternion)
-
getWorldScale
public final Vector3 getWorldScale()
Gets a copy of the nodes world-space scale. Some precision will be lost if the node is skewed.- Returns:
- a new vector that represents the node's world-space scale
- See Also:
setWorldScale(Vector3)
-
setLocalPosition
public void setLocalPosition(Vector3 position)
Sets the position of this node relative to its parent (local-space). IfisTopLevel()
is true, then this is the same assetWorldPosition(Vector3)
.- Parameters:
position
- The position to apply.- See Also:
getLocalPosition()
-
setLocalRotation
public void setLocalRotation(Quaternion rotation)
Sets the rotation of this node relative to its parent (local-space). IfisTopLevel()
is true, then this is the same assetWorldRotation(Quaternion)
.- Parameters:
rotation
- The rotation to apply.- See Also:
getLocalRotation()
-
setLocalScale
public void setLocalScale(Vector3 scale)
Sets the scale of this node relative to its parent (local-space). IfisTopLevel()
is true, then this is the same assetWorldScale(Vector3)
.- Parameters:
scale
- The scale to apply.- See Also:
getLocalScale()
-
setWorldPosition
public void setWorldPosition(Vector3 position)
Sets the world-space position of this node.- Parameters:
position
- The position to apply.- See Also:
getWorldPosition()
-
setWorldRotation
public void setWorldRotation(Quaternion rotation)
Sets the world-space rotation of this node.- Parameters:
rotation
- The rotation to apply.- See Also:
getWorldRotation()
-
setWorldScale
public void setWorldScale(Vector3 scale)
Sets the world-space scale of this node.- Parameters:
scale
- The scale to apply.- See Also:
getWorldScale()
-
localToWorldPoint
public final Vector3 localToWorldPoint(Vector3 point)
Converts a point in the local-space of this node to world-space.- Parameters:
point
- the point in local-space to convert- Returns:
- a new vector that represents the point in world-space
-
worldToLocalPoint
public final Vector3 worldToLocalPoint(Vector3 point)
Converts a point in world-space to the local-space of this node.- Parameters:
point
- the point in world-space to convert- Returns:
- a new vector that represents the point in local-space
-
localToWorldDirection
public final Vector3 localToWorldDirection(Vector3 direction)
Converts a direction from the local-space of this node to world-space. Not impacted by the position or scale of the node.- Parameters:
direction
- the direction in local-space to convert- Returns:
- a new vector that represents the direction in world-space
-
worldToLocalDirection
public final Vector3 worldToLocalDirection(Vector3 direction)
Converts a direction from world-space to the local-space of this node. Not impacted by the position or scale of the node.- Parameters:
direction
- the direction in world-space to convert- Returns:
- a new vector that represents the direction in local-space
-
getForward
public final Vector3 getForward()
Gets the world-space forward vector (-z) of this node.- Returns:
- a new vector that represents the node's forward direction in world-space
-
getBack
public final Vector3 getBack()
Gets the world-space back vector (+z) of this node.- Returns:
- a new vector that represents the node's back direction in world-space
-
getRight
public final Vector3 getRight()
Gets the world-space right vector (+x) of this node.- Returns:
- a new vector that represents the node's right direction in world-space
-
getLeft
public final Vector3 getLeft()
Gets the world-space left vector (-x) of this node.- Returns:
- a new vector that represents the node's left direction in world-space
-
getUp
public final Vector3 getUp()
Gets the world-space up vector (+y) of this node.- Returns:
- a new vector that represents the node's up direction in world-space
-
getDown
public final Vector3 getDown()
Gets the world-space down vector (-y) of this node.- Returns:
- a new vector that represents the node's down direction in world-space
-
setRenderable
public RenderableInstance setRenderable(@Nullable Renderable renderable)
Sets theRenderable
to display for this node. IfsetCollisionShape(CollisionShape)
is not set, thenRenderable.getCollisionShape()
is used to detect collisions for thisNode
.- Parameters:
renderable
- Usually a 3D model. If null, this node's current renderable will be removed.- Returns:
- the created renderable instance
- See Also:
ModelRenderable
,ViewRenderable
-
getRenderable
@Nullable public Renderable getRenderable()
Gets the renderable to display for this node.- Returns:
- renderable to display for this node
-
setCollisionShape
public void setCollisionShape(@Nullable CollisionShape collisionShape)
Sets the shape to used to detect collisions for thisNode
. If the shape is not set andsetRenderable(Renderable)
is set, thenRenderable.getCollisionShape()
is used to detect collisions for thisNode
.- Parameters:
collisionShape
- represents a geometric shape, i.e. sphere, box, convex hull. If null, this node's current collision shape will be removed.- See Also:
Scene.hitTest(Ray, boolean)
,Scene.hitTestAll(Ray)
,Scene.overlapTest(Node)
,Scene.overlapTestAll(Node)
-
getCollisionShape
@Nullable public CollisionShape getCollisionShape()
Gets the shape to use for collisions with this node. If the shape is null andsetRenderable(Renderable)
is set, thenRenderable.getCollisionShape()
is used to detect collisions for thisNode
.- Returns:
- represents a geometric shape, i.e. sphere, box, convex hull.
- See Also:
Scene.hitTest(Ray, boolean)
,Scene.hitTestAll(Ray)
,Scene.overlapTest(Node)
,Scene.overlapTestAll(Node)
-
setLight
public void setLight(@Nullable Light light)
Sets theLight
to display. To use, first create aLight
usingLight.Builder
. Set the parameters you care about and then attach it to the node using this function. A node may have a renderable and a light or just act as aLight
.- Parameters:
light
- Properties of theLight
to render, pass null to remove the light.
-
getLight
@Nullable public Light getLight()
Gets the current light, which is mutable.
-
setLookDirection
public final void setLookDirection(Vector3 lookDirection, Vector3 upDirection)
Sets the direction that the node is looking at in world-space. After calling this,getForward()
will match the look direction passed in. The up direction will determine the orientation of the node around the direction. The look direction and up direction cannot be coincident (parallel) or the orientation will be invalid.- Parameters:
lookDirection
- a vector representing the desired look direction in world-spaceupDirection
- a vector representing a valid up vector to use, such as Vector3.up()
-
setLookDirection
public final void setLookDirection(Vector3 lookDirection)
Sets the direction that the node is looking at in world-space. After calling this,getForward()
will match the look direction passed in. World-space up (0, 1, 0) will be used to determine the orientation of the node around the direction.- Parameters:
lookDirection
- a vector representing the desired look direction in world-space
-
getWorldModelMatrix
public final Matrix getWorldModelMatrix()
- Specified by:
getWorldModelMatrix
in interfaceTransformProvider
-
onActivate
public void onActivate()
Handles when this node becomes active. A Node is active if it's enabled, part of a scene, and its parent is active.Override to perform any setup that needs to occur when the node is activated.
- See Also:
isActive()
,isEnabled()
-
onDeactivate
public void onDeactivate()
Handles when this node becomes inactivate. A Node is inactive if it's disabled, not part of a scene, or its parent is inactive.Override to perform any setup that needs to occur when the node is deactivated.
- See Also:
isActive()
,isEnabled()
-
onUpdate
public void onUpdate(FrameTime frameTime)
Handles when this node is updated. A node is updated before rendering each frame. This is only called when the node is active.Override to perform any updates that need to occur each frame.
- Parameters:
frameTime
- provides time information for the current frame
-
onTouchEvent
public boolean onTouchEvent(HitTestResult hitTestResult, android.view.MotionEvent motionEvent)
Handles when this node is touched.Override to perform any logic that should occur when this node is touched. The way that touch events are propagated mirrors the way touches are propagated to Android Views. This is only called when the node is active.
When an ACTION_DOWN event occurs, that represents the start of a gesture. ACTION_UP or ACTION_CANCEL represents when a gesture ends. When a gesture starts, the following is done:
- Dispatch touch events to the node that was touched as detected by
Scene.hitTest(MotionEvent, boolean)
. - If the node doesn't consume the event, recurse upwards through the node's parents and dispatch the touch event until one of the node's consumes the event.
- If no nodes consume the event, the gesture is ignored and subsequent events that are part of the gesture will not be passed to any nodes.
- If one of the node's consumes the event, then that node will consume all future touch events for the gesture.
When a touch event is dispatched to a node, the event is first passed to the node's
Node.OnTouchListener
. If theNode.OnTouchListener
doesn't handle the event, it is passed toonTouchEvent(HitTestResult, MotionEvent)
.- Parameters:
hitTestResult
- Represents the node that was touched, and information about where it was touched. On ACTION_DOWN events,HitTestResult.getNode()
will always be this node or one of its children. On other events, the touch may have moved causing theHitTestResult.getNode()
to change (or possibly be null).motionEvent
- The motion event.- Returns:
- True if the event was handled, false otherwise.
- Dispatch touch events to the node that was touched as detected by
-
onTransformChange
public void onTransformChange(Node originatingNode)
Handles when this node's transformation is changed.The originating node is the most top-level node in the hierarchy that triggered this node to change. It will always be either the same node or one of its' parents. i.e. if node A's position is changed, then that will trigger
onTransformChange(Node)
to be called for all of it's children with the originatingNode being node A.- Parameters:
originatingNode
- the node that triggered this node's transformation to change
-
callOnHierarchy
public void callOnHierarchy(java.util.function.Consumer<Node> consumer)
Traverses the hierarchy and call a method on each node (including this node). Traversal is depth first.- Overrides:
callOnHierarchy
in classNodeParent
- Parameters:
consumer
- the method to call on each node
-
findInHierarchy
@Nullable public Node findInHierarchy(java.util.function.Predicate<Node> condition)
Traverses the hierarchy to find the first node (including this node) that meets a condition. Once the predicate is met, the traversal stops. Traversal is depth first.- Overrides:
findInHierarchy
in classNodeParent
- Parameters:
condition
- predicate the defines the conditions of the node to search for.- Returns:
- the first node that matches the conditions of the predicate, otherwise null is returned
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getRenderableInstance
@Nullable public RenderableInstance getRenderableInstance()
-
-