Developers4 of 7
Three.js scenes
One scene.js exporting a function. three.js supplied.
One folder, one scene.js exporting a function; a visual.json beside
it for controls.
my-scene/
scene.js
visual.json
Three.js has no canonical scene export, so Operatour defines one and holds it frozen, like the audio contract. Operatour supplies Three.js, the canvas, the renderer, the loop and resizing. Your module builds the scene and says what changes each frame. No renderer, no loop, no CDN fetches: it runs offline.
The smallest one that works
import * as THREE from 'three';
export default function (stage) {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(55, stage.width / stage.height, 0.1, 100);
camera.position.z = 6;
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.5, 2),
new THREE.MeshStandardMaterial({ color: 0xd8c6b1, flatShading: true }),
);
scene.add(mesh, new THREE.PointLight(0xffffff, 40, 0, 1).translateY(5));
return {
scene,
camera,
update({ audio, transport }) {
mesh.scale.setScalar(1 + audio.bass * 0.6);
mesh.rotation.y = transport.elapsed * 0.3;
},
};
}
Drag the folder on. Done.
What the function is given, and what it returns
stage carries THREE, renderer, canvas, width, height and sialk.
Usually only the size matters (camera aspect); the rest is for post-processing
and anything that needs the renderer.
Return an object:
| Field | Required | What it does |
|---|---|---|
scene, camera |
yes, unless render is given |
What Operatour draws each frame |
update(frame) |
no | Called every frame before the draw. frame is { audio, transport, parameters, width, height } |
resize(width, height) |
no | Called when the layer changes size. Without it, a perspective camera's aspect is kept for you |
render(frame) |
no | Take over drawing, for an EffectComposer built on stage.renderer |
dispose() |
no | Called if the scene stops after an error |
The function may be async if a model must load first; the layer stays
transparent until it resolves.
Making it react
frame.audio and frame.transport are the
audio contract: the objects a p5 sketch reads
on window.sialk, mutated in place once per frame. Read inside update; never
cache.
update({ audio, transport }) {
mesh.scale.setScalar(1 + audio.bass * 0.6 + audio.onBeat * 0.2);
material.emissiveIntensity = 1 - audio.beatPhase; // falls in time with the music
light.intensity = 30 + audio.level * 140;
for (let i = 0; i < 64; i++) bar(i, 0.25 + audio.spectrum[i] * 5);
}
Controls the performer can play
In visual.json, same folder, under parameters, shaped as in
declaring parameters. Every type
allowed; a number needs min and max; everything wants a label. They
arrive on frame.parameters under their own names:
{
"parameters": {
"speed": { "type": "number", "min": 0, "max": 3, "default": 1, "label": "Speed" },
"glow": { "type": "colour", "default": "#d9a437", "label": "Glow" }
}
}
update({ transport, parameters }) {
mesh.rotation.y = transport.elapsed * 0.3 * parameters.speed;
material.emissive.set(parameters.glow);
}
Addons
Import from three/addons/, as the Three.js docs spell it. Operatour ships
three's examples/jsm under that name.
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
Models live in your folder and load by relative path (the folder is the
scene's origin root): new GLTFLoader().loadAsync('./horse.glb') finds it.
Not supplied: the WebGPU renderer and the TSL node material addons. The layer
is a WebGL2 canvas.
What is different from a page
Every tutorial does three things your module must not:
- create a
WebGLRenderer - call
setAnimationLooporrequestAnimationFrame - append a canvas to the page
Operatour does all three, once; a scene doing them again draws twice. The
three you import is Operatour's copy, currently r185. A CDN address in a
scene is refused before it loads.
Unpainted areas are transparent. For opaque, set scene.background.
Cost
Three.js cost is count, not resolution. A quarter of a million points is
slow at any size; four times the pixels is nearly free. One InstancedMesh
over a thousand meshes; keep draw calls low. This runs at 4K60 in front of a
room.
Check it before the show
npx @sialk/agent check my-scene reports what Operatour would not play as
intended: a missing default export, an import from anywhere but three, your
own renderer or loop, a field the contract does not have. Zero problems is the
floor; running it in Operatour is the proof.