Constructor
new SceneManager(container, optsopt)
Creates and initializes a new SceneManager instance for rendering and interacting with a 3D avatar scene.
The SceneManager sets up the Three.js renderer, camera, scene, lighting, controls, and optional environment/background. It supports flexible lighting configuration, environment maps, and a 2D background image overlay.
| Name | Type | Attributes | Description |
|---|---|---|---|
container | HTMLElement | DOM element to render into. | |
opts | SceneManagerOptions | <optional> | Optional configuration object to override default scene settings SceneManager.SceneManagerOptions. |
If the container is not provided.
- Type
- Error
const sceneManager = new AvaCapoSDK.SceneManager(container, {
camera: { distance: 8, offsetY: 1.2, rotX: 0.1, rotY: 0.2 },
lighting: { ambient: { intensity: 1.5 } },
environment: 'room',
devtools: {
stats: {
enabled: true,
// container: container, // optional, default is SceneManager.container
// style: 'position:absolute;top:0;right:0;pointer-events:none;' // optional
}
}
});Members
(static, constant) SCENE_DEFAULTS :SceneManagerOptions
Default options of SceneManager.
{
fps: 30,
modelPixelRatio: 1,
environment: 'room', // 'room' | 'none' | Texture | CubeTexture | Scene
useEnvAsBackground: false,
transparentBackground: false,
camera: {
distance: 0,
offsetX: 0,
offsetY: 0,
rotX: 0,
rotY: 0,
rotZ: 0,
cameraRotateEnable: false,
cameraPanEnable: false,
cameraZoomEnable: false,
},
lighting: {
ambient: {
color: 0xffffff,
intensity: 2,
},
directional: {
color: 0x8888aa,
intensity: 30,
phi: 1,
theta: 2,
},
spot: {
intensity: 0,
color: 0x3388ff,
phi: 0.1,
theta: 4,
angle: 1,
}
},
devtools: {
stats: {
enabled: false,
container: null, // HTMLElement or null (defaults to SceneManager.container)
style: null, // inline CSS string
}
}
- SceneManagerOptions
Methods
addLoopObserver(observer) → {void}
Registers a loop observer (plugin) to receive cycle hooks. Returns a disposer function that removes the observer.
| Name | Type | Description |
|---|---|---|
observer | SceneManager. |
- Type:
- void
addTicker(fn) → {function}
Adds a custom function to be called on each internal logic update (update()). Useful for updating animations, physics, or any time-based logic at a fixed FPS rate.
| Name | Type | Description |
|---|---|---|
fn | function | Callback called every logic frame with delta time (ms). |
Disposer function to remove this ticker.
- Type:
- function
const stop = sceneManager.addTicker((dtMs) => {
myObject.rotation.y += dtMs * 0.001;
});
// Later, to remove:
stop(); // removes the tickerclearBackgroundImage()
Clears the current 2D background image, if any.
Disposes the texture and removes it from the background material. After calling this, only the 3D scene or environment map will be rendered.
sceneManager.clearBackgroundImage();destroy()
Disposes all WebGL resources and DOM bindings related to the SceneManager instance.
This includes renderer, lights, controls, environment maps, resize observers, and background textures. After calling destroy(), the instance becomes unusable. Useful when cleaning up the scene or navigating away from the current view.
sceneManager.destroy();draw()
Renders the scene to the WebGL context.
If a 2D background image is present, it is drawn first using an orthographic camera, followed by the 3D scene render. If no background is set, the 3D scene is rendered normally.
This is called automatically in the internal loop (after updates), but can also be invoked manually.
sceneManager.draw(); // Force a render passhasBackgroundImage() → {boolean}
Checks if a background image is currently active.
- Type:
- boolean
onResize()
Handles resizing of the renderer and cameras when the container changes size.
This method updates the perspective and orthographic projection matrices, re-applies the background quad layout, and triggers a redraw. Automatically bound to a ResizeObserver on initialization.
window.dispatchEvent(new Event('resize')); // indirectly triggers thisremoveLoopObserver()
Removes a previously registered loop observer.
removeTicker(fn)
Removes a function from the internal logic update loop.
If you previously added a ticker using addTicker, you can remove it manually using this method, or use the disposer function returned from addTicker().
| Name | Type | Description |
|---|---|---|
fn | function | The exact function reference to remove. |
const ticker = (dt) => console.log(dt);
sceneManager.addTicker(ticker);
sceneManager.removeTicker(ticker);(async) setBackgroundImage(optsopt)
Sets a 2D image as a background overlay, rendered behind the 3D scene.
Supports sources such as image URLs, File/Blob objects, HTML elements (canvas/image), and ImageBitmap. Allows customization of fitting mode, alignment, and tiling (repeat).
If no source is provided, the background is cleared automatically.
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts | Object | <optional> | Background configuration object. Properties
|
await sceneManager.setBackgroundImage({
source: 'background.jpg',
fit: 'contain',
alignX: 'center',
alignY: 'bottom',
repeat: false
});setCameraPose(optsopt)
Set the camera pose (target and position) with optional tweening.
If position is omitted, the final position is computed from distance and Euler rotations rotX/rotY/rotZ around the target, using a forward vector (0, 0, distance). When duration is greater than 0, a smooth tween runs from the current pose to the requested pose; otherwise the pose is applied immediately.
Notes:
targetandpositioncan be plain objects with x/y/z or any object exposing those fields.- If neither
positionnordistanceis provided, the current camera position is kept. - The tween uses the manager's internal loop and is updated in
_updateCameraTween().
| Name | Type | Attributes | Description | |||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts | Object | <optional> | Options for the camera pose. Properties
|
// Snap to an absolute pose:
scene.setCameraPose({
target: { x: 0, y: 1.6, z: 0 },
position: { x: 0.5, y: 1.9, z: 3.2 }
});// Smoothly orbit to a derived pose 8 units from the target:
scene.setCameraPose({
target: { x: 0, y: 1.6, z: 0 },
distance: 8,
rotX: 0.1,
rotY: 0.3,
rotZ: 0.0,
duration: 400
});setEnvironment(env, optsopt)
Sets the environment map for the scene — either a preset ('room'), a texture, a cube texture, or a full scene. Optionally applies it as a visual background in addition to the lighting environment.
Previously set environments are properly disposed.
| Name | Type | Attributes | Default | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
env | string | | room | Environment map or type. Allowed strings: "room", "none". | |||||||||||
opts | Object | <optional> | Properties
|
// Use default room environment
sceneManager.setEnvironment('room');
// Set environment from HDR texture
const texture = await new RGBELoader().loadAsync('path/to/env.hdr');
sceneManager.setEnvironment(texture, { useAsBackground: true }); // (you need to import RGBELoader separately)setFPS(fps)
Sets the internal update frequency (logic FPS) for animations and tickers.
This affects how often update() is called within the render loop. A higher FPS means smoother updates but more CPU load.
| Name | Type | Description |
|---|---|---|
fps | number | Target frames per second (minimum: 1). |
sceneManager.setFPS(30); // 30 updates per secondsetLighting(lighting)
Updates the scene's lighting configuration with new ambient, directional, and/or spot light settings.
You can update any subset of the lighting configuration — only provided fields will be modified.
| Name | Type | Description |
|---|---|---|
lighting | SceneManager. | Lighting configuration object. If omitted, falls back to initial |
sceneManager.setLighting({
ambient: { intensity: 1.5 },
directional: { color: 0xffffff, intensity: 20 },
spot: { intensity: 5, angle: 0.8 }
});start()
Starts the internal render/update loop.
This loop runs at the logical FPS defined in opts.fps and updates camera animations, tickers (via addTicker()), and rendering. Calling this method multiple times has no effect.
sceneManager.start();stop()
Stops the internal render/update loop.
After stopping, no update() or draw() calls will be made automatically until start() is called again.
sceneManager.stop();update(dtMs)
Updates internal state of the scene for a single frame.
Called automatically in the internal loop at fixed time intervals. Updates camera tweening and invokes all functions registered via addTicker(). Can be called manually if needed.
| Name | Type | Description |
|---|---|---|
dtMs | number | Delta time in milliseconds since the last update. |
sceneManager.update(16.6); // simulate one frame (~60fps)Type Definitions
AmbientLightOptions
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
color | number | | <optional> | 0xffffff | Ambient light color. |
intensity | number | <optional> | 2 | Intensity of ambient light. |
BackgroundImageOptions
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
source | string | | Background image source. | ||
fit | string | <optional> | "cover" | Background image fit mode. Allowed: "cover" | "contain" | "fill" | "none". |
alignX | string | <optional> | "center" | Horizontal alignment. Allowed: "left" | "center" | "right". |
alignY | string | <optional> | "center" | Vertical alignment. Allowed: "top" | "center" | "bottom". |
repeat | boolean | <optional> | false | Whether to repeat/tile the image. |
CameraOptions
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
distance | number | <optional> | 0 | Camera distance from target. |
offsetX | number | <optional> | 0 | Horizontal offset for camera target. |
offsetY | number | <optional> | 0 | Vertical offset for camera target. |
rotX | number | <optional> | 0 | Camera rotation around X axis (radians). |
rotY | number | <optional> | 0 | Camera rotation around Y axis (radians). |
rotZ | number | <optional> | 0 | Camera rotation around Z axis (radians). |
cameraRotateEnable | boolean | <optional> | false | Whether user can rotate the camera. |
cameraPanEnable | boolean | <optional> | false | Whether user can pan the camera. |
cameraZoomEnable | boolean | <optional> | false | Whether user can zoom the camera. |
DevtoolsOptions
- Object
| Name | Type | Attributes | Description |
|---|---|---|---|
stats | DevtoolsStatsOptions | <optional> | Performance overlay options. |
DevtoolsStatsOptions
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
enabled | boolean | <optional> | false | Enables the built-in performance overlay. |
container | HTMLElement | | <optional> | null | DOM node to mount the overlay into. If null, mounts into SceneManager.container. |
style | string | | <optional> | null | Inline CSS for the overlay root element (e.g. 'position:absolute;top:0;right:0;'). |
DirectionalLightOptions
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
color | number | | <optional> | 0x8888aa | Light color. |
intensity | number | <optional> | 30 | Light intensity. |
phi | number | <optional> | 1 | Vertical spherical coordinate. |
theta | number | <optional> | 2 | Horizontal spherical coordinate. |
LightingOptions
- Object
| Name | Type | Attributes | Description |
|---|---|---|---|
ambient | SceneManager. | <optional> | Ambient light settings. |
directional | SceneManager. | <optional> | Directional light settings. |
spot | SceneManager. | <optional> | Spot light settings. |
LoopObserver
Loop observer interface. Implement any subset of these callbacks and register via addLoopObserver().
- Object
| Name | Type | Attributes | Description |
|---|---|---|---|
onBeforeTick | function | <optional> | (dtMs, now) Called once per visual frame before fixed-step updates. |
onAfterTick | function | <optional> | (dtMs, now) Called once per visual frame after all fixed-step updates. |
onBeforeRender | function | <optional> | Called immediately before rendering (each visual frame). |
onAfterRender | function | <optional> | Called immediately after rendering (each visual frame). |
SceneManagerOptions
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fps | number | <optional> | 30 | Logical frames per second for internal update loop. |
modelPixelRatio | number | <optional> | 1 | Multiplier for renderer pixel ratio (combined with devicePixelRatio). |
environment | string | | <optional> | "room" | Environment map or preset. Allowed strings: "room", "none". |
useEnvAsBackground | boolean | <optional> | false | Whether to also use the environment as the scene background. |
transparentBackground | boolean | <optional> | false | Whether to render with a transparent background. |
camera | SceneManager. | <optional> | Camera configuration options. | |
lighting | SceneManager. | <optional> | Lighting configuration. | |
backgroundImage | SceneManager. | <optional> | Optional 2D background image. | |
devtools | SceneManager. | <optional> | Developer tools (performance overlay, etc.). |
SpotLightOptions
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
color | number | | <optional> | 0x3388ff | Light color. |
intensity | number | <optional> | 0 | Light intensity. |
angle | number | <optional> | 1 | Maximum extent of the spotlight beam, in radians. |
phi | number | <optional> | 0.1 | Vertical spherical coordinate. |
theta | number | <optional> | 4 | Horizontal spherical coordinate. |