AvaCapoSDK~ SceneManager

Manager for a Three.js-based avatar scene: sets up renderer, camera, lights, environment, background image, controls and the render/update loop.

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.

Parameters:
NameTypeAttributesDescription
containerHTMLElement

DOM element to render into.

optsSceneManagerOptions<optional>

Optional configuration object to override default scene settings SceneManager.SceneManagerOptions.

Throws:

If the container is not provided.

Type
Error
Example
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
 }
}
Type:
  • SceneManagerOptions

Methods

addLoopObserver(observer) → {void}

Registers a loop observer (plugin) to receive cycle hooks. Returns a disposer function that removes the observer.

Parameters:
NameTypeDescription
observerSceneManager.LoopObserver
Returns:
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.

Parameters:
NameTypeDescription
fnfunction

Callback called every logic frame with delta time (ms).

Returns:

Disposer function to remove this ticker.

Type: 
function
Example
const stop = sceneManager.addTicker((dtMs) => {
  myObject.rotation.y += dtMs * 0.001;
});

// Later, to remove:
stop(); // removes the ticker

clearBackgroundImage()

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.

Example
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.

Example
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.

Example
sceneManager.draw(); // Force a render pass

hasBackgroundImage() → {boolean}

Checks if a background image is currently active.

Returns:
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.

Example
window.dispatchEvent(new Event('resize')); // indirectly triggers this

removeLoopObserver()

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().

Parameters:
NameTypeDescription
fnfunction

The exact function reference to remove.

Example
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.

Parameters:
NameTypeAttributesDescription
optsObject<optional>

Background configuration object.

Properties
NameTypeAttributesDefaultDescription
sourcestring | File | Blob | HTMLImageElement | ImageBitmap | HTMLCanvasElement | OffscreenCanvas<optional>

Source of the background image.

fitstring<optional>
"cover"

Scaling mode. Allowed: "cover" | "contain" | "fill" | "none".

alignXstring<optional>
"center"

Horizontal alignment. Allowed: "left" | "center" | "right".

alignYstring<optional>
"center"

Vertical alignment. Allowed: "top" | "center" | "bottom".

repeatboolean<optional>
false

Whether to tile the image (repeat mode).

Example
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:

  • target and position can be plain objects with x/y/z or any object exposing those fields.
  • If neither position nor distance is provided, the current camera position is kept.
  • The tween uses the manager's internal loop and is updated in _updateCameraTween().
Parameters:
NameTypeAttributesDescription
optsObject<optional>

Options for the camera pose.

Properties
NameTypeAttributesDefaultDescription
targetObject<optional>

World-space point the camera should look at.

positionObject<optional>

Absolute world-space camera position. If provided, distance/rotations are ignored.

distancenumber<optional>

Distance from target when deriving position via rotations.

rotXnumber<optional>
0

Rotation around X axis (radians) applied to the forward vector when deriving position.

rotYnumber<optional>
0

Rotation around Y axis (radians) applied to the forward vector when deriving position.

rotZnumber<optional>
0

Rotation around Z axis (radians) applied to the forward vector when deriving position.

durationnumber<optional>
0

Tween duration in milliseconds. Use 0 for an instant snap.

easingfunction<optional>

Easing function mapping [0..1] -> [0..1]. Defaults to the manager's easing.

Examples
// 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.

Parameters:
NameTypeAttributesDefaultDescription
envstring | THREE.Texture | THREE.CubeTexture | THREE.Sceneroom

Environment map or type. Allowed strings: "room", "none".

optsObject<optional>
Properties
NameTypeAttributesDefaultDescription
useAsBackgroundboolean<optional>
false

Whether to apply the environment as the scene's background as well.

Example
// 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.

Parameters:
NameTypeDescription
fpsnumber

Target frames per second (minimum: 1).

Example
sceneManager.setFPS(30); // 30 updates per second

setLighting(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.

Parameters:
NameTypeDescription
lightingSceneManager.LightingOptions

Lighting configuration object. If omitted, falls back to initial opts.lighting.

Example
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.

Example
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.

Example
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.

Parameters:
NameTypeDescription
dtMsnumber

Delta time in milliseconds since the last update.

Example
sceneManager.update(16.6); // simulate one frame (~60fps)

Type Definitions

AmbientLightOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
colornumber | string<optional>
0xffffff

Ambient light color.

intensitynumber<optional>
2

Intensity of ambient light.

BackgroundImageOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
sourcestring | File | Blob | HTMLImageElement | ImageBitmap | HTMLCanvasElement | OffscreenCanvas

Background image source.

fitstring<optional>
"cover"

Background image fit mode. Allowed: "cover" | "contain" | "fill" | "none".

alignXstring<optional>
"center"

Horizontal alignment. Allowed: "left" | "center" | "right".

alignYstring<optional>
"center"

Vertical alignment. Allowed: "top" | "center" | "bottom".

repeatboolean<optional>
false

Whether to repeat/tile the image.

CameraOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
distancenumber<optional>
0

Camera distance from target.

offsetXnumber<optional>
0

Horizontal offset for camera target.

offsetYnumber<optional>
0

Vertical offset for camera target.

rotXnumber<optional>
0

Camera rotation around X axis (radians).

rotYnumber<optional>
0

Camera rotation around Y axis (radians).

rotZnumber<optional>
0

Camera rotation around Z axis (radians).

cameraRotateEnableboolean<optional>
false

Whether user can rotate the camera.

cameraPanEnableboolean<optional>
false

Whether user can pan the camera.

cameraZoomEnableboolean<optional>
false

Whether user can zoom the camera.

DevtoolsOptions

Type:
  • Object
Properties
NameTypeAttributesDescription
statsDevtoolsStatsOptions<optional>

Performance overlay options.

DevtoolsStatsOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
enabledboolean<optional>
false

Enables the built-in performance overlay.

containerHTMLElement | null<optional>
null

DOM node to mount the overlay into. If null, mounts into SceneManager.container.

stylestring | null<optional>
null

Inline CSS for the overlay root element (e.g. 'position:absolute;top:0;right:0;').

DirectionalLightOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
colornumber | string<optional>
0x8888aa

Light color.

intensitynumber<optional>
30

Light intensity.

phinumber<optional>
1

Vertical spherical coordinate.

thetanumber<optional>
2

Horizontal spherical coordinate.

LightingOptions

Type:
  • Object
Properties
NameTypeAttributesDescription
ambientSceneManager.AmbientLightOptions<optional>

Ambient light settings.

directionalSceneManager.DirectionalLightOptions<optional>

Directional light settings.

spotSceneManager.SpotLightOptions<optional>

Spot light settings.

LoopObserver

Loop observer interface. Implement any subset of these callbacks and register via addLoopObserver().

Type:
  • Object
Properties
NameTypeAttributesDescription
onBeforeTickfunction<optional>

(dtMs, now) Called once per visual frame before fixed-step updates.

onAfterTickfunction<optional>

(dtMs, now) Called once per visual frame after all fixed-step updates.

onBeforeRenderfunction<optional>

Called immediately before rendering (each visual frame).

onAfterRenderfunction<optional>

Called immediately after rendering (each visual frame).

SceneManagerOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
fpsnumber<optional>
30

Logical frames per second for internal update loop.

modelPixelRationumber<optional>
1

Multiplier for renderer pixel ratio (combined with devicePixelRatio).

environmentstring | THREE.Texture | THREE.CubeTexture | THREE.Scene<optional>
"room"

Environment map or preset. Allowed strings: "room", "none".

useEnvAsBackgroundboolean<optional>
false

Whether to also use the environment as the scene background.

transparentBackgroundboolean<optional>
false

Whether to render with a transparent background.

cameraSceneManager.CameraOptions<optional>

Camera configuration options.

lightingSceneManager.LightingOptions<optional>

Lighting configuration.

backgroundImageSceneManager.BackgroundImageOptions<optional>

Optional 2D background image.

devtoolsSceneManager.DevtoolsOptions<optional>

Developer tools (performance overlay, etc.).

SpotLightOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
colornumber | string<optional>
0x3388ff

Light color.

intensitynumber<optional>
0

Light intensity.

anglenumber<optional>
1

Maximum extent of the spotlight beam, in radians.

phinumber<optional>
0.1

Vertical spherical coordinate.

thetanumber<optional>
4

Horizontal spherical coordinate.