Skip to content

JavaScript API ​

For a comparison of optional optimizations, defaults and tradeoffs, see performance options.

Scripts execute as QuickJS globals, without Node.js/browser modules, filesystem APIs, or timers. Define an optional update(dt) function at load time. The host caches it and supplies 1 / 90 seconds at its 90 Hz target; use Date.now() when actual elapsed time matters. Native animation uses elapsed wall time. print(...values) writes to the engine log. Relative asset paths resolve from the process working directory; the launcher uses the repository root.

Start with a complete model-loading scene: avocado.js.

Engine statistics ​

getEngineStats() returns the latest cached, process-wide sample and the calling script's scene counts and physics timings. Process counters are sampled roughly once per second; querying does not read /proc or synchronize the GPU. Scene counts are current at the time of the query.

FieldMeaning
fpsMeasured engine-loop frames per elapsed second, including rendering and pacing; not the HMD/compositor presentation rate.
cpuPercentProcess user+system CPU time divided by elapsed time. 100% means one fully occupied core; multithreaded usage can exceed 100%.
ramBytesProcess resident RAM (VmRSS), in bytes.
vramBytesDriver-reported process GPU memory total from DRM fdinfo (drm-total-memory), in bytes, or null when the driver does not report it. Duplicate DRM clients are counted once.
gpuAllocatedEstimateBytesEstimated GPU allocation size of the whole engine, in bytes: allocated stereo color/depth buffers, model textures and vertex/index buffers (including retained LOD indices), deformed and skinned buffers, instancing buffers, panel textures, shadow maps, lighting environments and the sheen table. stats breaks it down by script.
sampleIdIncreasing sample number; zero before the first sample.
physicsAdvanceMsWall time of the latest physics-world advance in milliseconds, including solver, contact handling, and joint breaking; excludes host pose synchronization.
physicsSolverMsSolver wall time summed over the latest advance's fixed steps, including worker waits.
physicsStepsFixed steps in the latest advance (0–8).
physicsActiveBodiesActive rigid bodies after the latest advance.
physicsWorkersAdditional worker threads used by the latest advance; zero when no step ran.
sampleSecondsElapsed seconds covered by the latest sample; zero before sampling.
objectCount, panelCountThe calling script's scene counts, including hidden instances.

FPS, CPU, RAM, and memory estimates are null until sampled. Unavailable measured counters remain null. The GPU estimate excludes driver/compositor overhead, allocator padding except measured color-buffer stride, and small utility textures. Spotlight shadow diagnostics are available as getEngineStats().shadow, including active status, map storage and measured GPU time.

It assumes four bytes per depth pixel, counts shared depth attachments once, and is not measured GPU residency. RAM and GPU values can overlap on systems with shared memory; do not add them together. Steam Frame GPU memory uses shared system RAM rather than a separate VRAM pool.

Run stress.js for colored, grabbable physics boxes and a statistics panel including solver time, active bodies, and worker count. Either bumper adds one object; A or X adds five; B or Y clears the spawned objects. The scene starts with four boxes and caps at 51 spawned boxes. Spawning stops on a creation error and displays the reason on the HUD; clear or remove objects to free resources before trying again. The 256-object limit per scene and SteamVR's system-wide overlay budget still apply. Each model uses one overlay, in addition to the HUD and two controller lasers. Boxes below scene Y = −1.2 meters or more than 3 meters from the scene origin are removed every 100 ms, reclaiming their physics body, overlay buffers, and spawn capacity. Shared model assets remain cached for subsequent spawns. It uses the normal 768-pixel-per-eye object surfaces. The HUD refreshes twice per second; the underlying statistics sample once per second. When measured GPU memory is unavailable it explicitly displays the allocation estimate instead.

Scene ownership ​

Scene containers group objects, panels, lights, child containers and an optionally owned input callback for recursive cleanup. Ownership is independent of render grouping and transforms. Use createSceneContainer({parent}) and setSceneContainerParent(id, parentId) to build or reparent a hierarchy.

Transfer object and panel ownership between cleanup scopes: container-transfer.js. For child ownership and unused model release, see nested-containers.js and asset-lifetime.js.

Coordinates and models ​

Scene coordinates are meters: +X right, +Y up, +Z forward. The fixed standing-space anchor starts one meter ahead and 0.2 meters below the initial HMD position. Imported geometry is centered and normalized to a unit maximum default-pose extent. Object scale specifies that extent in meters along each of the object's own X, Y and Z axes; equal components keep the model's proportions. Scale applies before rotation, so a stretched object keeps its shape when it turns. Animated poses can extend beyond it. Positions and rotations below are relative to the scene anchor.

getFloorHeight() returns the scene Y of the real floor tracked by SteamVR's standing space, in meters, or null before the anchor is placed and in tests without a headset. It is measured along scene +Y through the scene origin, so it is exact when your head was level at launch. Use it to stand props on the floor, for example createObject(asset, 0, getFloorHeight() + height / 2, .5, height).

FunctionBehavior
setModelLighting(assetId, options)Set directional light, color, intensity, ambient and exposure for a PBR asset; see lighting.
loadModel(path, eyePixels = 768, options = {})Load a GLB and return an asset ID. Reference resolution must be a multiple of 64 from 256 through 2048; render groups use it as a maximum and sample distant members at display density. Boolean option pbr enables model lighting and defaults to false. Boolean option optimizeVertexCache defaults to false; gpuInstancing and lod default to true; see shared geometry, ordered GPU instancing and automatic LOD below. Runtime buffers may crop transparent margins at the same model pixel density. The same exact path is cached; a conflicting explicit resolution or option throws. Up to 32 simultaneously loaded assets per scene; unloadModel releases unused assets.
unloadModel(assetId)Release a loaded model only when no live object uses it; released IDs remain invalid. See asset lifetime and asset-lifetime.js.
setObjectMaterial(id, material) / getObjectMaterial(id)Override or read a PBR object's color and finish, or pass null to remove the override; see material overrides.
createShapeAsset(parts, options = {})Build a PBR model from generated shapes and return {assetId, extent, center}; see shape assets. Counts toward the 32-asset limit and is released with unloadModel.
createObject(assetId, x, y, z, scale, options = {})Return an object ID; finite coordinates are required. scale is a number for equal axes or {x, y, z}, with each component greater than zero and at most 2 meters. Up to 256 objects. Optional container assigns lifetime ownership. Optional renderGroup is an integer from 0 through 256; zero gives an independent overlay, positive IDs share a stereo surface.
setRenderGroupAutoSplit(groupId, options = {})Opt a logical render group into stable automatic subdivision. options accepts maxSurfaces (integer 1–16, default 4) and targetEyePixels (multiple of 64 from 256–4096, default 2048), plus boolean merge and rebalance (both default false). Pass false to stop future adaptation; existing tiles remain.
setObjectPosition(id, x, y, z)Set position.
setObjectYaw(id, radians)Set yaw.
setObjectRotation(id, pitch, yaw, roll)Set rotation in radians.
setObjectScale(id, x, y = x, z = x)Set the scale along the object's X, Y and Z axes; omitted components repeat x. Each is greater than zero and at most 2 meters.
setObjectVisible(id, visible)Show/hide rendering and selection.
getObjectTransform(id)Return {x, y, z, pitch, yaw, roll, scale, visible}: the current position, rotation in radians, scale as {x, y, z} and visibility. It includes movement by grabs and physics, with or without a physics body.
removeObject(id)Remove the instance and its interaction state.

Invalid IDs and invalid arguments throw. During live scene updates, object creation reserves the native handles its surface needs and throws if SteamVR cannot allocate them; a failed reservation leaves no object behind. Removal of the last surface member releases its handles immediately so subsequent spawns can reuse the available capacity. Assets remain loaded until explicitly released with unloadModel or the scene is replaced. Instances share source materials/textures; object transforms and animation playback remain independent. Render resolution trades clarity for GPU time and memory. By default each object has its own surface. Opaque and alpha-masked models occlude one another using per-eye geometry depth before surface submission. Occluder drawing is limited to conservative target bounds; near-plane cases retain the full viewport. Each surface keeps three independent color buffers and reuses one depth attachment while rendering them. Masked holes remain transparent. Blended materials are not opaque occluders; ordering between blended surfaces remains compositor-dependent.

Object surfaces crop centered, transparent borders using conservative bounds from both eyes. Pixel count and physical overlay width shrink together, preserving the reference pixel spacing and centered sample grid; loadModel still defaults to a 768-pixel reference. This removes unused pixels rather than scaling the model into fewer samples. Near-plane crossings or invalid bounds retain the full reference surface. Buffers grow immediately when required, to 25% above the cropped size in 64-pixel steps and at most the reference. They shrink when the crop stays below 60% of the allocation for 45 consecutive frames, with at most two shrinks per frame, so a rotating object's changing crop does not reallocate. Resize keeps the overlay handle and interaction state; the native intersection mask retains its reference physical extent within the cropped surface. Allocation estimates count the actual current buffers, including crops.

Load several models and update their transforms: sample-gallery.js.

Shape assets ​

createShapeAsset(parts, options = {}) builds a model from generated shapes, with no file. parts is an array of 1 to 256 objects:

FieldDefaultMeaning
shapeRequiredcube, sphere, cylinder, cone, wedge, pyramid, torus or arch.
position[0, 0, 0][x, y, z] meters, each within 10 meters, with object conventions (+Z forward).
rotation[0, 0, 0][pitch, yaw, roll] radians, applied like setObjectRotation.
scale[.1, .1, .1][x, y, z] size in meters along the part's own axes, each greater than 0 and at most 2.
color[1, 1, 1]Linear [r, g, b], each from 0 to 1.
finish'matte'matte, glossy, metal or glow; glow emits its color without light.

Each shape fills its scale box. Cylinders, cones and pyramids stand along Y, and a wedge slopes down toward its local −Z, with its tall side at +Z. The torus lies flat: X and Z set its outer size and Y its tube's thickness, which must be less than both. The arch rises along Y over a span along X, with a square cross-section whose side is Z; Z must be less than half of X and less than Y. A part means what an object with the same position, rotation and scale would mean, relative to the Thing's origin.

The result is {assetId, extent, center}. Like an imported model, the asset is centered and normalized to a unit largest side, so createObject(assetId, center.x, center.y, center.z, extent) shows the Thing at the size and place its parts describe, relative to the same origin. Parts with the same color and finish share one material; at most 64 such pairs are allowed. Shape assets always use PBR lighting and accept setModelLighting. options accepts eyePixels (as for loadModel, default 768) and boolean lod, gpuInstancing and optimizeVertexCache with loadModel's defaults.

Building is synchronous. A Thing of tens of parts builds in under a millisecond; the 256-part worst case of curved shapes takes about 250 ms including its first upload (measurement). Invalid parts or options throw before anything is built. Identical part lists are not shared, and loadModel never returns a shape asset. Saving a container that uses a shape asset throws until shape-asset snapshots are supported.

Build a small table from parts: shapes.js.

Choosing an object rendering approach ​

Scripts choose rendering per object through the final createObject options. The choice is independent of the model asset: a group may contain boxes, detailed models, animated instances, and static scenery together.

ApproachrenderGroupVisible 3D surfacesNative handles
Independent objects (default)Omit it or use 0One per visible objectOne per allocated object surface.
Single scene surfaceGive all 3D objects the same positive IDOne for the whole sceneTwo for the shared surface, including hidden staging.
Multiple grouped surfacesAssign different positive IDs to subsetsOne per nonempty visible groupTwo per allocated group, including hidden staging.
Adaptive grouped surfacesPositive IDs plus setRenderGroupAutoSplitOne or more tiles per opted-in groupTwo per allocated tile; extra handles during transitions.

For example, these are alternative ways to create the same content:

js
// Independent surfaces: native rectangular input can be enabled per object.
createObject(floorAsset, 0, -.55, 0, 1.2);
createObject(boxAsset, -.2, 0, 0, .14);
createObject(carAsset,  .2, 0, 0, .14, {renderGroup: 0});
js
// One visible stereo surface: include the floor and every 3D model in group 1.
createObject(floorAsset, 0, -.55, 0, 1.2, {renderGroup: 1});
createObject(boxAsset, -.2, 0, 0, .14, {renderGroup: 1});
createObject(carAsset,  .2, 0, 0, .14, {renderGroup: 1});
js
// Separate groups can concentrate pixels around different parts of the scene.
createObject(floorAsset, 0, -.55, 0, 1.2, {renderGroup: 3});
createObject(boxAsset, -.2, 0, 0, .14, {renderGroup: 1});
createObject(carAsset,  .2, 0, 0, .14, {renderGroup: 2});

Configure physics and grabbing per instance with setObjectPhysics and enableObjectInteraction as usual. Group IDs are scene-local integer labels from 1 through 256, not object IDs. Membership is fixed at creation. Changing an object's group requires removing and recreating it; the new object has a new ID, and physics, interaction, and animation settings must be applied again. Existing objects are never migrated when another object uses a different group ID.

A single surface reduces repeated scene traversal and depth passes, but must cover all its members at one sampling density. Large or widely separated groups can allocate more GPU memory or reach the resolution ceiling. Several compact groups may be faster. Use getEngineStats and visual checks for the intended scene instead of assuming one approach always wins. Panels created with createPanel and controller lasers remain separate overlays in all three modes; "single scene surface" refers to 3D geometry, not every overlay owned by the app. Automatic subdivision is a separate opt-in; it preserves logical group membership and individual object IDs while allowing several render tiles.

Shared render group behavior ​

createObject(asset, x, y, z, scale, {renderGroup: 1}) opts an object into a shared stereo surface by default. Each allocated tile uses two overlay handles, alternating a visible overlay and a hidden staging overlay. Transform, width, and texture are prepared on the hidden overlay before it is shown; the previously visible overlay is then hidden. Group membership is fixed at creation and scoped to the scene. Physics, transforms, animation, visibility, mesh picking, and grabbing remain per object. Native rectangular pointer state/events are unavailable for grouped objects; use mesh hover/controller events and the interaction helper. The shared surface passes native overlay input through to other applications.

A live creation reserves both handles when its group needs them; failure rolls back the reservation before publishing the object. Removing the last member of a tile releases both of its handles. Hidden members do not render; an entirely hidden group releases its surface during synchronization and recreates it when shown again. Both eyes share conservative group bounds and geometry depth, including occlusion by objects outside the group. Grouped surfaces use different texture regions on one common projection plane so late head-pose reprojection moves box silhouettes and their depth cutouts together. Include mutually occluding geometry such as floors in render groups too; independent surfaces use their own planes and can reproject differently during head movement. Alpha blending retains scene-order limitations. Object framebuffer colors use premultiplied alpha, including masked and blended materials, and the native overlay is configured to match.

Surface dimensions grow with projected bounds and the most demanding member's reference angular pixel density, capped at 1.25 times the headset's pixels per meter at the group's depth. Distant groups therefore use fewer pixels and regain them as you approach. This can use more memory when members spread apart or approach the headset. Resolution is bounded by 4096 pixels per eye and half the GPU's maximum renderbuffer width; reaching that ceiling logs a warning and reduces sampling density. Keep groups compact to avoid this limit. Buffer growth uses 25% allocation headroom rounded to 256 pixels. Shrinking requires the requested size to stay below 60% of the allocation for 45 frames, with at most two shrinks per frame. A resize renders and shows a replacement overlay before detaching the previous one; it temporarily needs another overlay pair and buffer set. Failed allocation keeps the previous surface available. Each surface retains three color buffers and one shared depth attachment.

grouped-stress.js uses four compact groups for up to 240 colored boxes plus a floor in its own render group and a statistics panel. Its body groups opt into at most two tiles each, with merging and rebalancing enabled. Bumper adds 10, A/X adds 40, and B/Y clears. Fallen/distant bodies are removed. The original stress.js keeps its independent surfaces and 51-box cap.

Automatic render group subdivision ​

A render group draws its objects into one stereo image that SteamVR displays as an overlay. If two clusters move far apart, that image must also cover the empty space between them. Maintaining the requested detail then needs a larger texture, which consumes more GPU memory and can reach the resolution ceiling.

Automatic subdivision lets the engine replace that image with smaller tiles around subsets of the objects. It reduces allocated empty space while preserving the requested sampling density; it does not lower model detail or texture quality. It is useful for sparse or spreading groups. Extra tiles also mean more overlays, scene traversal, and depth passes, so lower allocation does not guarantee higher FPS. Keep tightly packed scenes on one surface when measurements favor it. getEngineStats().gpuAllocatedEstimateBytes, fps, and cpuPercent can help compare the intended layout after its allocations settle.

js
setRenderGroupAutoSplit(1, {
  maxSurfaces: 4, targetEyePixels: 2048, merge: true, rebalance: true
});
createObject(boxAsset, -.6, 0, 0, .1, {renderGroup: 1});
createObject(carAsset,  .6, 0, 0, .1, {renderGroup: 1});
// Stop further adaptation without changing existing tiles or object IDs:
// setRenderGroupAutoSplit(1, false);

The function returns undefined. Invalid group IDs or numeric options throw RangeError; options must be an object, undefined, or false. The policy is scene-local and can be configured before or after creating members. Groups without a policy retain the requested single surface. The engine checks at most every 500 milliseconds and starts at most one split at a time. A tile must exceed targetEyePixels or reach the resolution ceiling. Bounded candidates along both projected axes must preserve member sampling density, avoid the ceiling in both children, and reduce estimated total allocated texels by at least 20%, including normal allocation headroom. targetEyePixels is a trigger, not a hard per-tile size limit or a promise that a useful split exists.

After an allocation failure the engine waits at least five seconds before trying another split, avoiding repeated allocation pressure.

A split retains the complete parent surface until both replacements can be presented. Allocation failure keeps the current membership and tile; presentation failure keeps the parent available and retries. Splitting temporarily needs another two overlay pairs and two complete buffer sets alongside the retained parent. Overlay limits or allocation failures can prevent a split. Statistics include retained generations until they are released.

Grouped native color/depth buffers share an engine-wide 512 MiB allocation budget. All three stereo color buffers, the shared depth attachment, and retained replacement generations count against it. This is a conservative surface-storage estimate, not a limit on total GPU memory: models, panels, independent surfaces, driver overhead, and compositor resources are outside this budget. A structural change must fit both complete new outputs alongside the old snapshots before allocation starts; otherwise membership stays unchanged and adaptation waits five seconds before retrying. A new group starts at the largest size the budget admits and grows like any group as buffers become free. When no buffers fit, its members stay unrendered until another group releases buffers; the script keeps running.

Ordinary grouped buffer resizing admits at most one attempt per 100 ms and does not run during a structural handoff. Allocation failure or budget refusal retains the current buffers and delays that tile's next attempt for five seconds. Growth can use a smaller 256-pixel allocation bucket when the full request cannot fit; the engine keeps the current allocation if no larger bucket can be admitted. A shrink must admit its full requested allocation; otherwise it keeps the existing sharper buffers rather than allocating a lower-density replacement. While growth is delayed, denied, or awaiting presentation, the current buffer renders the entire moving group at reduced pixel density instead of clipping its bounds. Fast-moving objects therefore need not wait for regrouping to remain visible. These limits bound allocation pressure; they do not guarantee a target FPS or full reference sharpness for arbitrary object counts or motion.

With the default split-only policy, existing objects keep their tile membership after a split. Enabling merge or rebalance allows the engine to change render tile membership under the constraints below, while logical group and object IDs remain unchanged. New objects join the nearest existing member's tile. Physics, animation, grabbing, and script IDs are unchanged. Tiles share the same projection plane and per-eye opaque/masked occlusion rules. Hidden objects retain logical tile membership; maxSurfaces counts those memberships too. Removing all members releases their tile, and entirely hidden tiles follow the normal surface synchronization rules.

merge and rebalance default to false, preserving split-only behavior. Disabling the policy stops future splits, merges, and rebalances; it leaves current tiles intact. Reducing maxSurfaces prevents additional splits and does not force a merge. With merging disabled, reload the scene or remove and recreate its group members to start from one tile again. A single large object, invalid near-plane bounds, overlapping clusters, or insufficient allocation savings can leave a group unsplit. Moving an isolated object close to the headset can still reach the resolution ceiling. Use several explicit compact groups for layouts that need direct control over surface count.

Merging and rebalancing ​

Set merge: true to let two tiles in the same logical group combine when their members come back together. The combined image must fit within targetEyePixels, avoid the resolution ceiling, and reduce estimated steady allocation by at least 20%. Requiring a compact result avoids immediately splitting the merged tile again. This can reduce overlay count and repeated rendering; it is not a guarantee of higher FPS in every scene.

Set rebalance: true to let two existing tiles exchange members when objects have moved between spatial clusters. The engine tries bounded cuts along both projected axes, keeps the tile count unchanged, and requires at least 20% less estimated steady allocation. Both outputs preserve member sampling density and avoid the resolution ceiling. Output labels are chosen to minimize membership changes.

Merging takes priority when it qualifies. Allocation comparisons use current projected demand with normal headroom, rather than oversized buffers awaiting ordinary shrinking. A proposal must have the same object-to-output assignment on at least three checks spanning one second. Regrouping waits at least two seconds after the group's previous structural change completes. At most four candidate pairs are evaluated per check, using a rotating search while a persistent proposal is rechecked first. These constraints limit churn from brief movements. Tiles containing hidden members defer merging/rebalancing until those members are visible again. Pairs containing blended materials are excluded to preserve their existing compositor order; opaque and masked materials can regroup. Unchanged groups and invalid near-plane bounds stay as they are.

Both old tiles remain available until every visible replacement is ready to show; view-culled replacements can complete the handoff without being shown. A rebalance retains both old buffer sets on either output's presentation failure. It hides replacement fronts before reusing their buffers, retries failed hiding, and keeps the fallbacks. Removing or hiding a pending output transfers its fallback snapshots to the surviving output until the handoff can finish. Object IDs, interaction, physics, and animation are preserved; this moves render membership only.

A merge temporarily adds one overlay pair and buffer set before releasing the two old tiles. A rebalance temporarily adds two pairs and buffer sets. Retained snapshots are included in GPU allocation estimates. Allocation failure leaves current membership intact and uses the existing five-second retry backoff. Already-started handoffs finish even if a script subsequently disables the policy. Each configuration call replaces that group's options; omitted options return to their defaults, including merge: false and rebalance: false.

Try adaptive groups with merging and rebalancing enabled: grouped-stress.js.

Shared model geometry ​

Unlit shared GPU meshes reuse identical position/UV vertices within each material primitive when vertex and index buffers together use less storage. By default, full-detail triangle order, UV seams, and material boundaries are preserved. Automatic LOD retains that full mesh and selects reduced index streams for smaller objects; opt out with lod: false to always render the full mesh. Indices use 16 bits when possible, otherwise 32 bits. Picking, collision construction, and per-instance CPU animation retain their expanded geometry; deformed instances draw from their own dynamic vertex buffers.

For static opaque or masked models, opt into GPU vertex-cache optimization at load time:

js
const car = loadModel('assets/samples/ToyCar/ToyCar.glb', 768, {
  optimizeVertexCache: true
});

This reorders whole triangles inside each indexed material primitive so nearby draws can reuse transformed vertices, then packs GPU vertices in first-use order for vertex fetch locality. It runs once during GPU upload and keeps every triangle, its winding, exact vertex/UV data, textures, and reference resolution. It does not simplify geometry or alter picking/collision meshes. Objects can still move, rotate, be grabbed, and use physics; the static-geometry restriction concerns vertex deformation, not object motion.

For PBR, this option also enables full-input indexing of static opaque/masked primitives when compact GPU storage is smaller. Identity includes both UV inputs, normal and tangent/handedness as well as position. Compact full-detail buffers coexist with reduced PBR LOD streams, which also receive compact indices when smaller under the same cache option. Failed optional staging keeps expanded full detail; the option does not enable plain indexing for blends. The retained CPU geometry and picking remain unchanged. Blended primitives and assets with animation or skinning retain their original GPU triangle order, even when the option is enabled. Non-indexed primitives also retain their original stream.

The option defaults to false because reordering can change ownership of exactly coplanar overlapping triangles within an opaque/masked primitive. Leave it off for assets that rely on that order, and also set lod: false to retain the full geometry at every size. Material and object draw order remain unchanged. A cached path keeps its original setting when the option is omitted; explicitly requesting a different setting throws. Choose the setting on the first load or reload the script to change it. The value must be a boolean.

GLB triangle primitives across mesh nodes, node transforms, embedded base-color textures, material factors, alpha modes, UV transforms, and approximate sheen are supported. Both lit and unlit rendering honor glTF doubleSided: single-sided materials cull their back faces, with winding corrected for mirrored node transforms. The rule also applies to depth occluders, animated objects and ordered instancing. Double-sided materials retain both faces. Sidedness controls rendering; picking and physics continue using the complete mesh. Animation covers skinning, morph targets and clip limits.

Opt-in pbr: true adds physical model lighting, material maps, normals, tangents and fabric sheen. Legacy rendering remains the default. PBR currently uses full expanded meshes and CPU animation instead of the unlit geometry fast paths.

Ordered GPU instancing ​

Repeated static models use ordered GPU instancing by default:

js
const asset = loadModel('assets/Avocado.glb', 768);
for (let i = 0; i < 16; i++) {
  createObject(asset, (i % 4 - 1.5) * .16, (Math.floor(i / 4) - 1.5) * .16,
    0, .12, {renderGroup: 1});
}

The renderer combines consecutive eligible copies of the same asset at the same selected LOD level into one GPU draw per eye/pass. Sharing a renderGroup lets their visible geometry use the same surface. Single, grouped, and adaptive rendering keep their existing surface membership, projection, and occlusion rules. Instancing changes neither triangle detail nor texture resolution, and works with moving, rotated, grabbed, and physics-driven objects.

Eligible assets have one opaque or masked material primitive and no animation, skinning, morph targets, or per-instance deformation. Multi-part models such as ToyCar, blended materials, isolated instances, and incompatible runs keep the original drawing path. Instancing preserves object order and the selected geometry index stream; it does not sort objects by asset to manufacture batches. Interleaving different assets can therefore prevent batching. Masked holes use the same material shader.

gpuInstancing must be a boolean and defaults to true. Pass {gpuInstancing: false} on first load to use individual draws. A cached path inherits its first-load setting when the option is omitted; an explicitly conflicting setting throws. Reload the script to change the setting. Unsupported geometry falls back normally rather than causing a load error. optimizeVertexCache is independent and can be combined with this option.

Instancing primarily reduces CPU draw submission; GPU-bound workloads may see little FPS improvement. Measure the actual scene when evaluating its performance benefit. The renderer lazily creates a shared shader and a reusable 7 KiB instance buffer; the buffer is included in gpuAllocatedEstimateBytes. Driver streaming copies remain outside that estimate. Initialization failure retains original draws, with another initialization attempt after scene reload. Instancing resources are released on successful scene replacement and engine shutdown.

See instances.js for a grabbable example.

Automatic model LOD ​

loadModel enables automatic level of detail by default:

js
const car = loadModel('assets/samples/ToyCar/ToyCar.glb');
// Keep the full mesh at every distance for this separately loaded asset.
const exact = loadModel('assets/Avocado.glb', 768, {lod: false});

The engine retains full geometry and generates up to two shared reduced levels per eligible static opaque primitive, targeting half and quarter triangle counts. Unlit meshes use reduced index buffers with a relative position/UV error limit of 0.001, UV weights of 0.1 and locked mesh borders. PBR meshes include normals, tangents, handedness and material UVs in simplification, lock surface seams and borders, and upload reduced position/surface buffers. With optimizeVertexCache: true, reduced PBR streams also receive compact cache-ordered indices when smaller. Each level owns its index width and compact vertex count; its geometry error and selected triangle count remain unchanged. PBR uses the same combined error limit with additional surface-attribute weights. Each PBR level must save at least 20% of triangles, and a second must save at least 20% beyond the first. Targets may not be reached; this is lossy geometry, not a promise of identical pixels or a forced triangle budget.

Animated, skinned, morph, CPU-deformed, masked and blended geometry keeps full detail. Small meshes and ineffective reductions also retain the original path.

Each object gets one detail decision per frame after input, animation and physics updates. The engine estimates its projected bounding-sphere diameter from both eyes, headset projection/resolution and off-axis placement. Both eyes, all grouped/independent surfaces, and color/depth occlusion passes use that same choice. Instancing batches only consecutive instances with matching asset and selected detail. Textures, reference resolution, picking and physics/collision meshes remain unchanged.

Grabbed objects always use the full mesh. The nearest sphere point inside 35 cm also restores full detail; after restoration it stays full until beyond 40 cm. For other objects, the conservative diameter estimate starts quarter detail below 102 pixels, half detail below 255 pixels, and full detail above that. Hysteresis retains quarter detail through 138 pixels and half detail through 345 pixels. Invalid/near-plane projections keep full detail. If a requested level is unavailable, the renderer uses a finer available level or the full mesh. These thresholds concern estimated headset pixels, independently of eyePixels.

lod must be a boolean. A cached exact path inherits its first-load setting when the option is omitted; an explicitly conflicting setting throws. Reload the script to change it. Use {lod: false} for assets requiring unchanged geometry or for visual comparisons. optimizeVertexCache remains independently opt-in.

Reduced geometry is generated once during GPU upload and shared by every copy of the asset. Full geometry stays allocated, so LOD adds storage while reducing rendering work. Optional PBR position/surface pairs share a 32 MiB engine-wide buffer-payload budget, separate from textures, full meshes, unlit LOD indices and overlay buffers. Reservations include both buffers before either is published. If a pair cannot fit or its GPU allocation fails, that primitive keeps a finer available level or full detail; loading and scene restore do not fail because optional detail is unavailable. Some primitives of an asset may reduce while others remain full.

Budget admission follows asset/primitive upload order. There is no eviction or background retry for an already uploaded asset; reloading the script rebuilds availability. Held objects use full geometry while their shared reduced buffers remain resident. Removing an object does not unload its cached asset. Scene reload/shutdown or actual asset destruction releases the buffers and budget; gpuAllocatedEstimateBytes includes allocated pairs once per shared asset. The budget limits payload bytes, not driver overhead or total engine VRAM. Generation can add loading time. Scene saves retain lod, not generated levels or reservations; restored availability depends on the current budget. See lod.js for a detailed grabbable-model example and pbr.js for lit geometry with the same automatic policy.

Scene lighting ​

Load receivers with {pbr: true} to use scene-wide lighting. Lights add to each asset's setModelLighting settings; they do not affect unlit models, panels or lasers. All three types share eight live slots, including disabled lights. One point, spotlight or directional light can opt into shadows through a shared slot; see spotlight shadows, directional coverage, point shadows, shadows.js, point-shadows.js and directional-shadows.js for controls and diagnostics.

Light typeControlsGuide and complete example
Point: position, range and distance falloffcreatePointLight, setPointLight, getPointLight, removePointLightPoint-light API, lights.js
Spot: position, direction, range and conecreateSpotLight, setSpotLight, getSpotLight, removeSpotLightSpotlight API, spotlights.js
Directional: parallel rays without distance falloffcreateDirectionalLight, setDirectionalLight, getDirectionalLight, removeDirectionalLightDirectional API, directional-lights.js

Each type supports color/intensity, enabled state, optional object attachment and container ownership. Typed functions reject another type's ID. Attachments are resolved after interaction/physics; removing their object removes its lights. Directional attachments follow rotation only. Container disposal removes owned lights, and snapshots preserve all three types with remapped attachment IDs.

Grabbing and selection ​

js
const asset = loadModel('assets/samples/Box/Box.glb');
const box = createObject(asset, 0, 0, 0, .3);
enableObjectInteraction(box, {grabButton: 'both'});
setObjectInteraction(box, {
  laserYaw: true, depthAdjust: true,
  handRotationAxes: {pitch: true, yaw: true, roll: true}
});
setLaserVisible('left', true);
setLaserVisible('right', true);

setObjectInteraction(id, options) updates only supplied fields:

OptionDefault on a new objectMeaning
grabbablefalseEnable native grab/movement handling.
grabButton'trigger''trigger', 'grip', or 'both'; either allowed button can acquire/hold.
laserPassThroughfalseLet the engine laser pass through the object. Grabbable objects are always hit.
directGrabtrueUse near-hand holding when acquired close to the controller.
laserYawfalseDistant grab: stick left/right changes yaw.
laserPitchWithStickClickfalseDistant grab: while the stick is clicked, left/right changes pitch instead.
depthAdjustfalseDistant grab: stick up moves farther, down moves nearer.
handRotationAxesAll false{pitch, yaw, roll} chooses hand rotation axes for direct grabs.

Acquisition requires a current transformed triangle hit. Empty mesh gaps never fall back to a bounding sphere. Near-hand mode is selected after that hit when the controller-to-object-center distance is below 0.10 + scale * 0.5 meters, using the largest scale component; there is no proximity-only pickup. Static picking uses a shared per-asset BVH; animated instances refit their own picking geometry. Each hand can hold its own object. A held object releases after all allowed buttons are up for five frames. Distant-grab stick control reads the raw thumbstick past a 0.15 dead zone: full deflection turns 3 radians per second and moves 1.5 meters per second.

enableObjectInteraction(id, {grabButton}) makes an object grabbable with the other fields at their defaults. Its options object recognizes grabButton; configure other fields with setObjectInteraction. For custom grab or press behavior, leave the object nongrabbable and combine mesh hover with controller.button events.

Each hand has one engine laser. It hits visible objects that are grabbable or not marked laserPassThrough, and interactive panels. Hover, grabs and panel clicks work whether or not its beam is drawn. While an object is held, its hit distance follows the held distance along the beam.

getMeshHover(hand) returns that hand's current model hit in this script's scene: {hit, objectId, distance, point: {x, y, z}}. point is in scene coordinates and distance in meters along the beam; both are zero without a hit. It reads the same state as the overlay.meshHover* events, so a script can read it when a button event arrives to find what was pressed.

setLaserVisible(hand, visible) requests that the engine draw or hide the beam for 'left' or 'right', and getLaserVisible(hand) reads it. The beam state belongs to the engine, not to a script: with several scripts running, the latest request wins. Stopping or reloading a script leaves it unchanged. Beams start hidden.

Use the engine laser for custom presses with a 3D hit point: mesh-pointer.js.

Controllers ​

getController(hand) returns {valid, connected, x, y, z, rotation}. rotation is a row-major 3×3 matrix. Positions use scene coordinates. connected can remain true during temporary pose loss; check valid before using pose data. An invalid pose returns zero position/rotation values.

getControllerInput(hand) reads the cached raw controller stream:

FieldContents
valid, handStream validity and requested hand.
rawComponent paths mapped to boolean or finite numeric values.
buttons{down, touched} for trigger, grip, bumper, thumbstick, a/b/x/y, menu, system, view, and dpad_up/down/left/right.
analogNumeric trigger, grip, and thumbstick: {x, y}.
capacitiveSensor projections, including ring, pinky, thumbrest, and dpad.

Missing convenience values are false/zero. Unsupported structured values are invalidated. Use getFingerTracking when missing values and freshness must be distinguished from real zero.

getInput(hand) reads optional OpenVR action bindings. It returns valid, gripClick/Touch/Value, triggerClick/Touch/Value, thumbstick: {x,y}, thumbstickClick/Touch, bumperClick/Touch, systemClick/Touch, menuClick, viewClick, aClick, bClick, xClick, yClick, and dpadUp/Down/Left/RightClick. Action availability depends on SteamVR focus and bindings; unavailable values are false/zero.

pulse(hand, seconds = .05, amplitude = .5) requests action-based haptics and returns success. Both numbers must be finite within [0, 1]; availability still depends on native action bindings. All hand arguments use 'left' or 'right'.

Read controller poses and raw input, and request haptics: input.js.

Events ​

onInputEvent(callback, options = {}) replaces the single callback. Optional {container: id} gives it container lifetime; omission keeps root lifetime. pollInputEvents() drains an independent queue, so polling does not consume callback delivery. Delivery happens in the host loop. The bounded queue evicts the oldest 1024 entries when it exceeds 4096; consumers should drain it regularly.

Events include type, hand, control, objectId, x, y, down, value, dx, dy, dz, dpitch, dyaw, and droll; interpret fields by event type.

TypeMeaning
controller.buttonRaw boolean path in control, state in down.
controller.valueRaw numeric path in control, number in value.
physics.jointBreakAutomatic joint release with joint/object IDs and estimated force/torque; see breakable joints.
physics.collisionStart, physics.collisionStay, physics.collisionEndOpt-in pair contacts with object IDs, point/normal, penetration, and closing speed; see physics events.
overlay.meshHoverStart, overlay.meshHover, overlay.meshHoverEndEngine laser model hits, with point and distance. overlay.meshHover follows a hit point that moves more than 1 mm.
overlay.grabStart, overlay.grabMove, overlay.grabEndObject grab lifecycle; overlay.grabStart carries the hit point and distance.
overlay.move, overlay.rotateObject transform changes with position/rotation deltas.
panel.hoverStart, panel.hover, panel.hoverEndPanel hover with top-left-origin pixel coordinates.
panel.pointerDown, panel.pointerUpPanel button transition; control is left, right, or other.

Raw controller events reach every running script and carry objectId zero; use getMeshHover for the hovered model. Mesh hover events briefly hold the last hit across a miss shorter than 100 ms, so triangle edges do not flicker. Panel IDs and object IDs are separate numeric namespaces; distinguish them by type. Animation does not emit completion events; inspect playback state.

Combine a scoped pointer callback with the independent polled queue: containers.js.

Messages between scripts ​

js
const id = subscribe('space.scoreChanged', event => print(event.payload.score));
publish('space.scoreChanged', {score: 3});
subscribe('*', event => print(`${event.topic} from ${event.sourceInstanceId}`));
unsubscribe(id);

Running scripts exchange messages through an in-memory bus. There is no persistence and no replay: a script started later never sees earlier messages.

FunctionBehavior
publish(topic, payload = null)Queue a copy of payload for topic and return its sequence number, or null when the queue is full and the message is dropped. Invalid topics or payloads throw.
subscribe(topic, callback)Call callback(event) for matching messages and return a subscription ID. topic is an exact topic or '*'.
unsubscribe(subscriptionId)Remove one of this script's own subscriptions; returns whether it existed.

Scripts publish only space.* topics. engine.* topics come from the engine and can be subscribed to exactly or through '*'. Topics are at most 128 bytes, contain a name after the dot, and never contain * or control characters.

An exact subscription receives every message on its topic, including the subscribing script's own. '*' receives every topic from other scripts and from the engine, but never the subscribing script's own publications. A script can hold several subscriptions, and each matching one is called.

event contains topic, payload, sourceInstanceId and sequence. sourceInstanceId is the publishing script's instance ID, 0 for the scratch scene, or null for engine events. sequence increases across the engine.

Payloads are JSON-compatible data: null, booleans, finite numbers, strings, arrays and plain objects, at most 32 levels deep and 64 KiB as JSON. Functions, undefined, symbols, BigInts, non-finite numbers, cycles and class instances such as Date are rejected. Each subscriber receives its own copy.

Delivery happens once per frame, after every script's input callbacks and update, never during publish. Messages arrive in publish order and, for each message, in subscription order. A message published by a subscriber is delivered on the next frame. Up to 1024 callbacks run per frame; the rest wait. Each script can have 256 undelivered messages, and the bus holds at most 4 MiB.

Subscribers are chosen when a message's delivery begins. One that stops or reloads before its turn is skipped. An uncaught exception in a callback stops only that script, like other callbacks. Subscriptions belong to the scene: a successful reload replaces them with the new scene's, and messages published while loading are sent only if the reload succeeds. A failed reload keeps the old subscriptions.

The engine publishes script lifecycle events:

TopicPayload
engine.scriptStarted{instanceId, path}
engine.scriptReloaded{instanceId, path}
engine.scriptStopped{instanceId, path, reason, error}; reason is 'stopped' or 'failed'. A script that fails to launch also reports 'failed'.
engine.scriptReloadFailed{instanceId, path, error}; the script keeps its previous scene.

Share a counter between running scripts: event-bus.js.

Additional APIs ​

  • Panels: createPanel, canvas drawing, panelLoadFont, panelSetTransform, panelSetRotation, panelSetVisible, and removePanel.
  • Physics: setObjectPhysics, getObjectPhysics, removeObjectPhysics, setPhysicsGravity, setObjectVelocity, setObjectAngularVelocity, applyObjectImpulse, setPhysicsCollisionEvents, createPhysicsJoint, getPhysicsJoint, setPhysicsJoint, removePhysicsJoint, setPhysicsJointMotor.
  • Lighting: setModelLighting, createPointLight, setPointLight, getPointLight, removePointLight, createSpotLight, setSpotLight, getSpotLight, removeSpotLight.
  • Animation: getAnimations, playAnimation, pauseAnimation, resumeAnimation, stopAnimation, seekAnimation, getAnimationState.
  • Tracking: getEyeTracking, getCalibratedEyeTracking, getEyeFoveationCenters, getFingerTracking.
  • Calibration: saveEyeTrackingCalibration, getEyeTrackingCalibration.
  • Lifecycle: reload and resource ownership.

Combine model interaction and physics in a complete script: physics-controls.js.

Scene persistence ​

Use captureSceneContainer(id) / restoreSceneContainer(snapshot) for in-memory JSON snapshots and saveSceneContainer(id, name) / loadSceneContainer(name) for named files private to the calling script. importSceneContainer(sourceScript, name) explicitly copies another script's save. saveScriptData(name, value) / loadScriptData(name) keep small JSON-compatible values, such as settings or scores, in the same private namespace; see saved data. See scene save and restore for the complete schema, panels and pixels, animation playback, physics bodies/joints, per-script storage paths, fresh object/joint/group mappings, limits, and unsupported resources.

Capture and restore an in-memory scene with fresh handles: scene-snapshot.js.

Released under the GNU AGPL v3.0 only.