Scene

Provides global scene functionality like raycasting.

.constructor(engine: WonderlandEngine) ⇒ Scene 

ParamTypeDescription
engineWonderlandEngine

.onPostRender: Emitter<void[]> 

Called after the scene has been rendered

.onPreRender: Emitter<void[]> 

Called before rendering the scene

.activeViews: ViewComponent[] 

Currently active view components.

.children: Object3D[] 

0.8.5+

Top-level objects of this scene.

See children for more information.

.clearColor 

0.8.5+

Set the background clear color.

.colorClearEnabled 

0.8.5+

Set whether to clear the color framebuffer before drawing.

This function is useful if an external framework (e.g. an AR tracking framework) is responsible for drawing a camera frame before Wonderland Engine draws the scene on top of it.

.engine: WonderlandEngine 

Hosting engine instance.

.skyMaterial: null | Material 

Current sky material, or null if no sky is set.

.skyMaterial 

Set the current material to render the sky.

Note: The sky needs to be enabled in the editor when creating the scene. For more information, please refer to the background tutorial.

.addObject(parent: null | Object3D) ⇒ Object3D 

Add an object to the scene.

Returns: A newly created object.

ParamTypeDescription
parentnull | Object3DParent object or null.

.addObjects(count: number, parent: null | Object3D, componentCountHint: number) ⇒ Object3D[] 

Batch-add objects to the scene.

Will provide better performance for adding multiple objects (e.g. > 16) than calling addObject repeatedly in a loop.

By providing upfront information of how many objects will be required, the engine is able to batch-allocate the required memory rather than convervatively grow the memory in small steps.

Experimental: This API might change in upcoming versions.

Returns: Newly created objects

ParamTypeDescription
countnumberNumber of objects to add.
parentnull | Object3DParent object or null, default null.
componentCountHintnumberHint for how many components in total will be added to the created objects afterwards, default 0.

.append(file: string | ArrayBuffer, options: Partial<SceneAppendParameters>) ⇒ Promise<SceneAppendResult

Append a scene file.

Loads and parses the file and its images and appends the result to the currently active scene.

Supported formats are streamable Wonderland scene files (.bin) and glTF 3D scenes (.gltf, .glb).

1WL.scene.append(filename).then(root => {
2    // root contains the loaded scene
3});

In case the loadGltfExtensions option is set to true, the response will be an object containing both the root of the loaded scene and any glTF extensions found on nodes, meshes and the root of the file.

 1WL.scene.append(filename, { loadGltfExtensions: true }).then(({root, extensions}) => {
 2    // root contains the loaded scene
 3    // extensions.root contains any extensions at the root of glTF document
 4    const rootExtensions = extensions.root;
 5    // extensions.mesh and extensions.node contain extensions indexed by Object id
 6    const childObject = root.children[0];
 7    const meshExtensions = root.meshExtensions[childObject.objectId];
 8    const nodeExtensions = root.nodeExtensions[childObject.objectId];
 9    // extensions.idMapping contains a mapping from glTF node index to Object id
10});

If the file to be loaded is located in a subfolder, it might be useful to define the baseURL option. This will ensure any bin files referenced by the loaded bin file are loaded at the correct path.

1WL.scene.append(filename, { baseURL: 'scenes' }).then(({root, extensions}) => {
2    // do stuff
3});

Returns: Promise that resolves when the scene was appended.

ParamTypeDescription
filestring | ArrayBufferThe .bin, .gltf or .glb file to append. Should be a URL or an ArrayBuffer with the file content.
optionsPartial<SceneAppendParameters>Additional options for loading.

.findByName(name: string, recursive: boolean) ⇒ Object3D[] 

0.8.5+

Search for objects matching the name.

See findByName for more information.

Returns: An array of Object3D matching the name.

ParamTypeDescription
namestringThe name to search for.
recursivebooleanIf true, the method will look at all the objects of this scene. If false, this method will only perform the search in root objects.

.findByNameDirect(name: string) ⇒ Object3D[] 

0.8.5+

Search for all top-level objects matching the name.

See findByNameDirect for more information.

Returns: An array of Object3D matching the name.

ParamTypeDescription
namestringThe name to search for.

.findByNameRecursive(name: string) ⇒ Object3D[] 

0.8.5+

Search for all objects matching the name.

See findByNameRecursive for more information.

Returns: An array of Object3D matching the name.

ParamTypeDescription
namestringThe name to search for.

.load(options: string | SceneLoadOptions) ⇒ Promise<void

Load a scene file (.bin).

Will replace the currently active scene with the one loaded from given file. It is assumed that JavaScript components required by the new scene were registered in advance.

Once the scene is loaded successfully and initialized, onSceneLoaded is notified.

ArrayBuffer 

The load() method accepts an in-memory buffer:

1scene.load({
2    buffer: new ArrayBuffer(...),
3    baseURL: 'https://my-website/assets'
4})

Note: The baseURL is mandatory. It’s used to fetch images and languages.

Use setLoadingProgress to update the loading progress bar when using an ArrayBuffer.

Returns: Promise that resolves when the scene was loaded.

ParamTypeDescription
optionsstring | SceneLoadOptions

.rayCast(o: Readonly<NumberArray>, d: Readonly<NumberArray>, group: number, maxDistance: number) ⇒ RayHit 

Cast a ray through the scene and find intersecting objects.

The resulting ray hit will contain up to 4 closest ray hits, sorted by increasing distance.

Returns: The scene cached RayHit instance.

Note: The returned object is owned by the Scene instance will be reused with the next rayCast call.

ParamTypeDescription
oReadonly<NumberArray>Ray origin.
dReadonly<NumberArray>Ray direction.
groupnumberCollision group to filter by: only objects that are part of given group are considered for raycast.
maxDistancenumberMaximum inclusive hit distance. Defaults to 100.

.reserveObjects(objectCount: number, componentCountPerType: Record<string, number>) ⇒ void 

0.8.5+

Pre-allocate memory for a given amount of objects and components.

Will provide better performance for adding objects later with addObject and addObjects.

By providing upfront information of how many objects will be required, the engine is able to batch-allocate the required memory rather than conservatively grow the memory in small steps.

Experimental: This API might change in upcoming versions.

ParamTypeDescription
objectCountnumberNumber of objects to add.
componentCountPerTypeRecord<string, number>Amount of components to allocate for addComponent, e.g. {mesh: 100, collision: 200, "my-comp": 100}.

.reset() ⇒ void 

Reset the scene.

This method deletes all used and allocated objects, and components.

.setLoadingProgress(percentage: number) ⇒ void 

Update the loading screen progress bar.

ParamTypeDescription
percentagenumber