Class: World
WebSG.World
Class representing a 3D world composed of scenes, nodes, meshes, materials, and other properties defined by the glTF 2.0 specification.
Currently a World contains resources loaded for the environment's glTF document. This means you do not have direct access to user's avatars in the world's scene graph. On script initialization, the world will be empty. It is not until world.onload is called that world.environment will be set to the default scene in the world's initial glTF document. All other resources such as textures, materials, and meshes referenced by the document will be loaded at this time and can be accessed via methods such as world.findNodeByName .
Example
In the following example world.findNodeByName is used to find a node by its name and log the reference to the console.
// World not yet loaded
world.onload = () => {
// World loaded
const lightNode = world.findNodeByName("Light");
console.log(lightNode);
};
Once a world is loaded you can modify the scene graph by adding, removing, or modifying nodes.
Example
world.onload = () => {
const newNode = world.createNode();
world.environment.addNode(newNode); // Nodes must be added to a scene to be rendered
newNode.mesh = world.findMeshByName("Teapot");
world.environment.removeNode(newNode);
};
If you want to modify the scene graph each frame you can use the world.onupdate callback.
Example
world.onload = () => {
const newNode = world.createNode();
world.environment.addNode(newNode);
newNode.mesh = world.findMeshByName("Teapot");
world.onupdate = (dt, time) => {
newNode.translation.y = Math.sin(time) * 5;
};
};
Once the local user has entered the world, the networking interface will be fully initialized. You access the local user's peer via the global network.local variable. This can be used to get the local user's transform.
Example
world.onenter = () => {
const localUser = network.local;
console.log(localUser.transform);
console.log(localUser.rotation);
};
Overall, world is the main interface for creating new resources. See the individual factory functions for more details.
Table of contents
Constructors
Properties
Accessors
Methods
- createAccessorFrom
- createBoxMesh
- createCollider
- createCollisionListener
- createLight
- createMaterial
- createMesh
- createNode
- createScene
- createUIButton
- createUICanvas
- createUIElement
- createUIText
- createUnlitMaterial
- findAccessorByName
- findColliderByName
- findComponentStoreByName
- findImageByName
- findLightByName
- findMaterialByName
- findMeshByName
- findNodeByName
- findSceneByName
- findTextureByName
- findUICanvasByName
- findUIElementByName
- stopOrbit
Constructors
constructor
• new World()
Properties
onenter
• onenter: null
| () => any
Called when the user enters the world. The network.local peer has been set and the user has been spawned into the world.
Defined in
onload
• onload: null
| () => any
Called when the world is loaded. The glTF document has been loaded and all resources are available.
Defined in
onupdate
• onupdate: null
| (dt
: number
, time
: number
) => any
Called once per frame when the world is updated.
Param
The time since the last update in seconds.
Param
The total time since the start of the world in seconds.
Defined in
Accessors
componentStoreSize
• get
componentStoreSize(): number
Returns the maximum number of components per type that can be stored in the world. Defaults to 10000.
Returns
number
Defined in
• set
componentStoreSize(value
): void
Sets the maximum number of components per type that can be stored in the world. Defaults to 10000.
Parameters
Name | Type |
---|---|
value | number |
Returns
void
Defined in
environment
• get
environment(): Scene
Gets the environment of the world. Note this is not set until world.onload
is called.
Returns
Defined in
• set
environment(scene
): void
Sets the environment of the world.
Parameters
Name | Type | Description |
---|---|---|
scene | Scene | The new environment scene for the world. |
Returns
void
Defined in
primaryInputSourceDirection
• get
primaryInputSourceDirection(): Vector3
Get the primary input source's direction in world space. The primary input source in XR is the user's primary controller otherwise it's the camera. This API is experimental and may change or be removed in a future release.
Returns
Defined in
primaryInputSourceOrigin
• get
primaryInputSourceOrigin(): Vector3
Get the primary input source's origin in world space. The primary input source in XR is the user's primary controller otherwise it's the camera. This API is experimental and may change or be removed in a future release.
Returns
Defined in
Methods
createAccessorFrom
▸ createAccessorFrom(buffer
, props
): Accessor
Creates an Accessor from the given ArrayBuffer and properties.
Parameters
Name | Type | Description |
---|---|---|
buffer | ArrayBuffer | The ArrayBuffer to create the Accessor from. |
props | AccessorFromProps | The properties for the new Accessor. |
Returns
Defined in
createBoxMesh
▸ createBoxMesh(props
): Mesh
Creates a Box Mesh with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props | BoxMeshProps | The properties for the new Box Mesh. |
Returns
Defined in
createCollider
▸ createCollider(props
): Collider
Creates a Collider with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props | ColliderProps | The properties for the new Collider. |
Returns
Defined in
createCollisionListener
▸ createCollisionListener(): CollisionListener
Creates a new CollisionListener for listening to collisions between nodes with colliders set on them.
Returns
Defined in
createLight
▸ createLight(props
): Light
Creates a Light with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props | LightProps | The properties for the new Light. |
Returns
Defined in
createMaterial
▸ createMaterial(props
): Material
Creates a Material with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props | MaterialProps | The properties for the new Material. |
Returns
Defined in
createMesh
▸ createMesh(props
): Mesh
Creates a Mesh with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props | MeshProps | The properties for the new Mesh. |
Returns
Defined in
createNode
▸ createNode(props?
): Node
Creates a new Node with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props? | NodeProps | Optional properties to set on the new node. |
Returns
Defined in
createScene
▸ createScene(props?
): Scene
Creates a new Scene with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props? | SceneProps | Optional properties to set on the new scene. |
Returns
Defined in
createUIButton
▸ createUIButton(props?
): UIButton
Creates a new UIButton with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props? | UIButtonProps | Optional properties to set on the new UIButton. |
Returns
Defined in
createUICanvas
▸ createUICanvas(props?
): UICanvas
Creates a new UICanvas with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props? | UICanvasProps | Optional properties to set on the new UICanvas. |
Returns
Defined in
createUIElement
▸ createUIElement(props?
): UIElement
Creates a new UIElement with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props? | UIElementProps | Optional properties to set on the new UIElement. |
Returns
Defined in
createUIText
▸ createUIText(props?
): UIText
Creates a new UIText with the given properties.
Method
createUIText
Parameters
Name | Type | Description |
---|---|---|
props? | UITextProps | Optional properties to set on the new UIText. |
Returns
Defined in
createUnlitMaterial
▸ createUnlitMaterial(props
): Material
Creates an unlit Material with the given properties.
Parameters
Name | Type | Description |
---|---|---|
props | UnlitMaterialProps | The properties for the new unlit Material. |
Returns
Defined in
findAccessorByName
▸ findAccessorByName(name
): undefined
| Accessor
Finds an Accessor by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the Accessor to find. |
Returns
undefined
| Accessor
Defined in
findColliderByName
▸ findColliderByName(name
): undefined
| Collider
Finds a Collider by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the Collider to find. |
Returns
undefined
| Collider
Defined in
findComponentStoreByName
▸ findComponentStoreByName(name
): undefined
| ComponentStore
Find the ComponentStore for the given component type. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the component store to find. |
Returns
undefined
| ComponentStore
Defined in
findImageByName
▸ findImageByName(name
): undefined
| Image
Finds an image by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the image to find. |
Returns
undefined
| Image
Defined in
findLightByName
▸ findLightByName(name
): undefined
| Light
Finds a Light by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the Light to find. |
Returns
undefined
| Light
Defined in
findMaterialByName
▸ findMaterialByName(name
): undefined
| Material
Finds a Material by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the Material to find. |
Returns
undefined
| Material
Defined in
findMeshByName
▸ findMeshByName(name
): undefined
| Mesh
Finds a Mesh by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the mesh to find. |
Returns
undefined
| Mesh
Defined in
findNodeByName
▸ findNodeByName(name
): undefined
| Node
Finds a node by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the node to find. |
Returns
undefined
| Node
Defined in
findSceneByName
▸ findSceneByName(name
): undefined
| Scene
Finds a scene by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the scene to find. |
Returns
undefined
| Scene
Defined in
findTextureByName
▸ findTextureByName(name
): undefined
| Texture
Finds a texture by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the texture to find. |
Returns
undefined
| Texture
Defined in
findUICanvasByName
▸ findUICanvasByName(name
): undefined
| UICanvas
Finds a UICanvas by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the UICanvas to find. |
Returns
undefined
| UICanvas
Defined in
findUIElementByName
▸ findUIElementByName(name
): undefined
| UIElement
Finds a UIElement by its name. Returns undefined if not found.
Parameters
Name | Type | Description |
---|---|---|
name | string | The name of the UIElement to find. |
Returns
undefined
| UIElement
Defined in
stopOrbit
▸ stopOrbit(): undefined
Stops any ongoing orbiting operation.
Returns
undefined