Create, Compete & Win at Redbrick Connect 2024! 🎉

Object3D

The base class for most objects in 3D space.

Besides the information below, you can also utilize various properties and methods of THREE.Object3D.

Properties

.castShadow

object.castShadow : Boolean

Specifies whether the object casts shadows.
Default is false.

.children

object.children : Array

Returns an array of child objects of the object.
Refer to Groups for detailed information on manually grouping objects.

.parent

object.parent : Object3D

Returns the parent object of the object.
An object can have at most one parent.

.position

object.position : Vector3

The position of the object.
Use this property to set or get the position of the object.
Default is (0, 0, 0).

.quaternion

object.quaternion : Quaternion

An alternative way to represent the rotation of the object.
Use this property to set or get the rotation state of the object.
Quaternions can represent rotations more accurately than Euler angles.

.receiveShadow

object.receiveShadow : Boolean

Specifies whether the object receives shadows.
This property is applicable to objects like THREE.Mesh and similar.
Default is false.

.rotation

object.rotation : Euler

The rotation of the object in radians.
Use this property to set or get the rotation angle of the object.

.scale

object.scale : Vector3

The scale of the object.
Use this property to set or get the scale of the object.
The object’s scale is relative to the scene’s coordinate system, so it can be affected by its parent’s scale if it is a child object.
Default is Vector3( 1, 1, 1 ).

.visible

object.visible : Boolean

Controls the visibility of the object.
Use this property to determine if the object should be rendered.
Default is true.

Methods

.add()

parentObject.add ( childObject : Object3D, ... ) : this

Used to add an object as a child of another object.
Multiple objects can be added.
Child objects maintain their position and rotation independently of their parent even if the parent object undergoes transformations like translation, rotation, or scaling.
An object can have at most one parent, so if it already has a parent, it will be removed before being added to the new parent.

.attach()

parentObject.attach ( childObject : Object3D ) : this

Attaches an object as a child of another object while maintaining the parent object’s world transform.
Therefore, if the parent object undergoes transformations like translation, rotation, or scaling, the child object will move or rotate accordingly.
add() is typically used to add objects to the scene, whereas attach() is used to establish hierarchical relationships between objects.

.clone()

originalObject.clone(recursive: Boolean): Object3D

recursive - If true, child objects of this object will also be cloned. Default is true.

Creates and returns a new object by cloning the original object.
Copies properties such as geometric shape, material, position, rotation, and scale from the original object to create the new object.
However, Physics and Behavior attributes are not copied.
Additionally, object cloned using .clone() cannot utilize methods belonging to the Object3D in API Extension.
If you wish to use these methods, use .cloneWithMethods() instead.

The cloned object is completely independent from the original object, so changes made to the cloned object after cloning do not affect the original object.

⚠️

The cloned object must be added as a child to the WORLD in order to be used.

EX
const obj = WORLD.getObject("obj");
const clone_obj = obj.clone();
WORLD.add(clone_obj);
clone_obj.position.set(10, 1, 10);

.copy()

originalObject.copy (targetObject : Object3D, recursive : Boolean ) : this

recursive - If true, child objects of this object will also be copied. Default is true.

Copies the properties of this object to another object, creating a new object.
The original object (targetObject) remains unchanged.

.getObjectById()

object.getObjectById ( id : Integer ) : Object3D

id - The unique identifier of the object instance.

Traverses from this object itself to its children to find the first object with the matching ID.
Returns null if no matching object is found.

IDs are assigned sequentially as 1, 2, 3, … and are unique without duplication, incrementing by 1 for each new object instance.

.getObjectByProperty()

.getObjectByProperty( propertyName : String, value : Any ) : Object3D

propertyName - The name of the property to search for.
value - The value of the property to search for.

The method searches the scene graph for an object that matches the specified property and value.
Searches the scene graph for an object based on a specific property and its value.
Returns the first object that matches. Returns null if no matching object is found.

.getWorldDirection()

object.getWorldDirection( target : Vector3 ) : Vector3

target - The Vector3 where the world direction will be set to match the object’s world direction.

Sets the target’s direction to match this object’s world direction and returns it.
This method calculates the world direction of the object, considering transformations of parent objects in the scene graph.
An exception may occur if you call this method on an object that is not part of the scene.

.getWorldPosition()

object.getWorldPosition( target : Vector3 ) : Vector3

target - The Vector3 where the world position will be set to match the object’s world position.

Sets the target’s world position to match this object’s world position and returns it.
This method calculates the world position of the object, considering transformations of parent objects in the scene graph.
An exception may occur if you call this method on an object that is not part of the scene.

.getWorldQuaternion()

object.getWorldQuaternion ( target : Quaternion ) : Quaternion

target - The Quaternion where the world quaternion will be set to match the object’s world quaternion.

Sets the target’s quaternion to match this object’s world quaternion and returns it.
This method calculates the world quaternion of the object, considering transformations of parent objects in the scene graph.
An exception may occur if you call this method on an object that is not part of the scene.

.getWorldScale()

object.getWorldScale( target : Vector3 ) : Vector3

target - The Vector3 where the world scale will be set to match the object’s world scale.

Sets the target’s scale to match this object’s world scale and returns it.
This method calculates the world scale of the object, considering transformations of parent objects in the scene graph.
An exception may occur if you call this method on an object that is not part of the scene.

.lookAt()

object.lookAt ( target : Vector3 ) : undefined
object.lookAt ( x : Float, y : Float, z : Float ) : undefined

target - A Vector3 representing the position that the object should look at.
x, y, z - Optionally specify the x, y, z components of the vector.

Rotates the object to face a specified point (vector) in its local coordinate system.
Therefore, it is affected by transformations of its parent object.

.raycast()

.raycast( raycaster : Raycaster, intersects : Array ) : undefined

raycaster - A THREE.Raycaster object defining the origin and direction of the ray.
intersects - An array to store intersection information. Information about the intersection point with the object will be added to this array if the ray intersects with the object.

Used to check if a ray intersects with a specific object and to identify the intersected object.
This method requires the object to have a mesh for accurate collision detection. Basic THREE.Mesh or objects inheriting from it are used for collision detection such as Mesh, Line, and Points.

.remove()

parentObject.remove ( childObject : Object3D, ... ) : this

Used to remove an object from its parent object.
Multiple objects can be removed.
Once removed, the object can be added to another parent or back to the scene for reuse.

.removeFromParent()

childObject.removeFromParent () : this

Removes this object from its parent object.

.rotateOnAxis()

object.rotateOnAxis ( axis : Vector3, angle : Float ) : this

axis - A Vector3 representing the axis around which to rotate.
angle - The rotation angle in radians.

Rotates the object around the specified axis in its local coordinate system.
Since it operates in the local coordinate system of the object, it is not affected by transformations of its parent object.

.rotateOnWorldAxis()

object.rotateOnWorldAxis( axis : Vector3, angle : Float) : this

axis - A Vector3 representing the axis around which to rotate in world coordinates.
angle - The rotation angle in radians.

Rotates the object around the specified axis in world coordinates.
This method rotates the object around the axis defined in the world coordinate system, not the object’s local axis.
Therefore, it does not affect transformations of its parent object.

.traverse()

object.traverse ( callback : Function ) : Not defined

callback - A callback function that will be called for each object. The function takes the object as a parameter.

Traverses the object hierarchy, calling the specified callback function for each object.