Appearance
Examples
Complete patterns for common scripting tasks. Each example is self-contained – attach to the indicated event and it works.
Entity hierarchy and lookup
Every object in the scene is an Entity (the anchor container). Each entity has one or more Representations (the visuals inside it – models, shapes, images, audio, etc.).
scene
└─ Entity ("MyObject") ← scene.findEntity({ name: "MyObject" })
├─ representation (primary) ← entity.representation
├─ Representation "Audio1" ← entity.findRepresentation({ name: "Audio1" })
└─ Representation "SubModel" ← entity.findRepresentation({ name: "SubModel" })Common Gotcha
scene.findEntity only finds top-level entities – not nested representations inside them. Find the parent entity first, then use findRepresentation to navigate.
Find the parent entity first, then navigate to the representation:
javascript
// WRONG – Audio1 is a representation, not a top-level entity
var audio = scene.findEntity({ name: "Audio1" }); // null!
// RIGHT – find the parent entity, then the nested representation
var parent = scene.findEntity({ name: "MyObject" });
var audio = parent.findRepresentation({ name: "Audio1" });
audio.isEnabled = true;Look up by ID or name:
javascript
// Entity lookup
var entity = scene.findEntity("ABC123"); // by ID
var entity = scene.findEntity({ name: "MyBox" }); // by name
// Representation lookup (from entity)
var rep = entity.representation; // primary (first) representation
var rep = entity.findRepresentation("DEF456"); // by ID
var rep = entity.findRepresentation({ name: "Wheel" }); // by nameExplore a model's internal hierarchy:
javascript
var entity = scene.findEntity({ name: "Robot" });
var rep = entity.representation;
// List all nested children (bones, sub-meshes, etc.)
var names = rep.getChildNames();
console.log(names); // ["torso", "arm_left", "arm_right", ...]
// Access a specific child
var arm = rep.findChild("arm_left");
arm.transform = Transform({ rotation: Rotation(0, 0, Math.PI / 4) });Event context: objectEntity vs representationEntity
When a script runs from an event (tap, collision, etc.), the event data includes both:
javascript
// objectEntity = the top-level entity (anchor container)
var entity = scriptContext.sourceEvent.objectEntity;
// representationEntity = the specific representation that triggered the event
var rep = scriptContext.sourceEvent.representationEntity;
// Common pattern: use representationEntity for visual changes
rep.opacity = 0.5;
rep.animateTo({ scale: Vector3(1.2, 1.2, 1.2) }, 0.3);
// Use objectEntity to find sibling representations
var audio = entity.findRepresentation({ name: "Audio1" });
audio.isEnabled = true;Sequential audio with collision counter
A practical example combining entity navigation with state:
javascript
// Attach to: On Object Collision (on each trigger object)
// Setup: Parent entity "AudioPlayer" contains representations "Audio1", "Audio2", "Audio3"
var count = experience.getVariable("hitCount") ?? 0;
count = count + 1;
experience.setVariable("hitCount", count);
if (count <= 3) {
var player = scene.findEntity({ name: "AudioPlayer" });
var audio = player.findRepresentation({ name: "Audio" + count });
if (audio) {
audio.isEnabled = true;
}
}Create objects dynamically
Spawn a box in front of the camera:
javascript
// Attach to: On Screen Tap
var box = await scene.createEntity(
createBox(0.3, 0.3, 0.3) // width, height, depth in meters
.anchor(Anchor.currentPOV(0, 0, -1)) // x, y, z offset from camera
);
console.log("Created: " + box.id);Create a sphere at a fixed world position with traits:
javascript
var sphere = await scene.createEntity(
createSphere(0.1) // radius in meters
.name("Ball")
.anchor(Anchor.position(0, 1.5, -2)) // x, y, z world position
.opacity(0.8)
);Choose how an object animates in and out:
javascript
var card = await scene.createEntity(
createImage("https://picsum.photos/800/600", 0.4, 1.5)
.anchor(Anchor.currentPOV(0, 0, -1))
.revealAnimation({ style: 'fadeAndScale', duration: 0.5, scaleFactor: 0.8 })
.removalAnimation({ style: 'fade', duration: 0.2 })
);The styles are the editor's: 'default', 'disabled', 'fade', 'fadeAndScale'. A script that animates the object in itself wants the built-in one out of the way – .revealAnimation(false).
Prepare an object before anyone sees it:
javascript
var ball = await scene.createEntity(
createSphere(0.05)
.physics({ mode: 'dynamic', shape: 'sphere', mass: 0.15 })
.anchor(Anchor.position(0, 1, -1.5))
.hideByDefault()
);
await ball.waitUntilReady();
await scene.toggle([ball], true); // shown now, physics starts nowhideByDefault still loads the object, it just isn't shown and its physics stays paused. Build a batch that way and one scene.toggle brings them all up on the same frame, so a stack doesn't settle against its half-built self.
Place an object on a detected floor:
javascript
var marker = await scene.createEntity(
createSphere(0.05)
.name("Marker")
.anchor(Anchor.horizontalPlane("floor"))
);Load a 3D model from a remote URL:
javascript
// Attach to: On Experience Start
var model = await scene.createEntity(
createModel("https://developer.apple.com/quick-look-gallery/models/soccerball/ball_soccerball_realistic.usdz")
.name("RemoteModel")
.anchor(Anchor.position(0, 1, -2))
.fittingBox(0.5) // fit into 0.5m bounding box
);
// Spin on tap
model.on('tap', function() {
model.play(EntityAnimation.spin(1, 0.8, { axis: [0, 1, 0] }));
});Display an image:
javascript
var photo = await scene.createEntity(
createImage("https://picsum.photos/800/600", 0.5, 1.5) // url, width (m), aspect ratio (w/h)
.anchor(Anchor.currentPOV(0, 0, -1.5))
);Adjust an authored object before it appears
findEntity gives you an object that's already in the scene. scene.getObjectDescriptor gives you the recipe of an object you built in the editor – so a script can adjust it before it's displayed, then write it back with scene.setObjectDescriptor. Use it to adapt an authored object to the device, add an interaction the editor can't express per platform, or spawn variations from it.
The edit takes effect the next time the object is displayed, so run the script before the object's "Add to Scene" action, in the same event – at the top level or in a Sequence, not grouped with it (a Group runs its actions at the same time).
Enable a throw gesture only where there's no touchscreen. On Apple Vision Pro there are no screen gestures, so drag‑to‑throw has to live on the object itself – author the ball once, then add the gesture at runtime only where it's needed:
javascript
// Attach to: On Start, before the ball's Add to Scene action
if (!environment.features.has('screenGestures')) {
const ball = scene.getObjectDescriptor({ name: 'ball' });
ball.gestures({ drag: true, releaseBehavior: 'momentum' });
scene.setObjectDescriptor(ball);
}Swap the source asset per device – a lighter model on a phone, a heavier one on a headset. .asset(...) replaces a model's or video's source, routed to the right place for its kind:
javascript
const hero = scene.getObjectDescriptor({ name: 'hero' });
hero.asset(environment.deviceCategory === 'spatial' ? 'hero_hi.usdz' : 'hero_lo.usdz');
scene.setObjectDescriptor(hero);Spawn variations from one authored template. .clone() copies the descriptor with fresh ids, so you can add many:
javascript
const template = scene.getObjectDescriptor({ name: 'can' });
for (const p of positions) {
await scene.createEntity(template.clone().transform({ position: p }));
}Passing the authored descriptor to createEntity without .clone() is rejected – its id already belongs to the authored object, so clone first for a distinct copy.
Replace the whole representation. .representation(...) swaps the visual entirely – the way to change kind, e.g. a simple shape instead of a model on a low‑end device – keeping the object's placement and events:
javascript
const crate = scene.getObjectDescriptor({ name: 'crate' });
crate.representation(createBox(0.3, 0.3, 0.3).material('crate-mat'));
scene.setObjectDescriptor(crate);TIP
Inspect what you fetched with descriptor.kind ('model', 'primitive', 'visualMedia', …) and branch on it, and keep descriptor.id to look the object up later with findEntity.
Session only
These edits live in memory for the running session – they change how the object appears now, not the saved experience.
Light a scene
Lights are objects you create like any other. A spot is aimed by its rotation, a point light radiates in every direction, and a directional light is a sun whose position does nothing.
javascript
// Attach to: On Experience Start
var lamp = await scene.createEntity(
createSpotLight({
name: 'Lamp',
color: '#FFD9A0',
intensity: 4000, // lumens
outerAngle: 45, // full cone aperture, in degrees
attenuationRadius: 3, // metres to nothing
shadow: { edges: 'soft', lightSize: 0.2 },
anchor: Anchor.position(0, 2, -1),
transform: { rotation: Rotation(-Math.PI / 2, 0, 0) } // straight down
})
);Everything worth animating lives on entity.representation.light:
javascript
var light = lamp.representation.light;
animateValue({
from: 3000,
to: 5000,
duration: 1.4,
curve: 'easeInOut',
repeatCount: -1,
reverseOnRepeat: true,
onUpdate: function(value) {
light.intensity = value;
}
});A candle, whose colour drifts as it burns:
javascript
var flame = await scene.createEntity(
createPointLight({ color: '#FF9A3C', intensity: 800, attenuationRadius: 1.5 })
);
animateValue({
from: 0, to: 1, duration: 2.5, repeatCount: -1, reverseOnRepeat: true,
onUpdate: function(t) {
flame.representation.light.color = Color(1, 0.5 + 0.2 * t, 0.2 * t);
}
});A sun, where cascades buys sharper shadows far from the camera:
javascript
await scene.createEntity(
createDirectionalLight({
intensity: 2500, // lux, not lumens
shadow: { maximumDistance: 20, cascades: 3 },
transform: { rotation: Rotation(-0.6, 0.7, 0) }
})
);Check before you rely on it
Soft edges, projected images and the surroundings light need a recent OS, and the surroundings light is Apple Vision Pro only. Gate on them so a portable script degrades instead of looking broken:
javascript
var shadow = environment.features.has('softShadows')
? { edges: 'soft', lightSize: 0.2 }
: true;
await scene.createEntity(createSpotLight({ intensity: 4000, shadow: shadow }));A spot can project a texture through its cone, which is how you get a pattern rather than a plain circle. It must be opaque with a black background: black blocks the light and bright areas pass it, so a transparent PNG projects as an even circle.
projectedTexture takes an asset id or URL, or a handle from Texture, so the same cone can project a video, the camera feed, or a compute kernel's output.
javascript
if (environment.features.has('projectedLightTexture')) {
var projector = await scene.createEntity(
createSpotLight({
projectedTexture: 'my-gobo-asset-id',
intensity: 6000,
outerAngle: 50,
anchor: Anchor.position(0, 2.5, 0),
transform: { rotation: Rotation(-Math.PI / 2, 0, 0) }
})
);
// The one part worth animating: turn the pattern, not the light.
animateValue({
from: 0, to: Math.PI * 2, duration: 12, repeatCount: -1,
onUpdate: function(radians) {
projector.representation.light.projectedTextureRotation = radians;
}
});
}Point lights cast no shadow
That is an engine limitation on every OS, not a setting. Use a spot or a directional light where you need one.
Burst and recolour particles
Build the scene in the editor: a particle emitter named Sparks, and a UI panel named Controls with a button named Burst. The script only wires the button. Each tap releases a burst and gives the particles the next colour.
javascript
// Attach to: On Experience Start
var sparks = scene.findEntity({ name: 'Sparks' }).representation.particles;
var button = scene.findEntity({ name: 'Controls' }).representation.findView({ name: 'Burst' });
var colors = ['#FF9300', '#40C8FF', '#B4FF3C'];
var next = 0;
button.on('tap', function() {
sparks.color = Color.hex(colors[next]);
sparks.burst();
next = (next + 1) % colors.length;
});color is the colour particles are born with. Where the emitter fades from one colour to another over each particle's life, the fade to the end colour stays. Set sparks.isEmitting = false to stop new particles and keep only the bursts.
birthRate, speed, size, lifeSpan, color, endColor and acceleration need an emitter built in the editor. On an imported particle file they read as null, while burst() and isEmitting work on both.
Animate objects
Simple property animation:
javascript
var box = scene.findEntity({ name: "MyBox" });
// Animate to target values (fire-and-forget, no await needed)
box.animateTo(
{ position: Vector3(0, 2, -1), opacity: 0.5 },
1.0,
{ timingFunction: "easeInOut" }
);
// Available: "linear", "easeIn", "easeOut", "easeInOut"Chained animations with async/await:
javascript
var box = scene.findEntity({ name: "MyBox" });
await box.animateTo({ position: Vector3(0, 2, 0) }, 1.0);
await box.animateTo({ scale: Vector3(2, 2, 2) }, 0.5);
await box.animateTo({ opacity: 0 }, 0.3);Using EntityAnimation with play():
javascript
var box = scene.findEntity({ name: "MyBox" });
// Spin 2 full revolutions over 3 seconds
await box.play(EntityAnimation.spin(2, 3.0, { axis: [0, 1, 0] }));
// Keyframe animation
await box.play(EntityAnimation.keyframes([
{ position: Vector3(0, 1, -2) },
{ position: Vector3(1, 2, -2) },
{ position: Vector3(0, 3, -2) }
], 2.0, { tweenMode: "linear" }));
// Group multiple animations together
await box.play(EntityAnimation.group([
EntityAnimation.to({ opacity: 0.5 }, 1.0),
EntityAnimation.spin(1, 1.0)
]));Value animations
Spring animations
animateTo supports "linear", "easeIn", "easeOut", "easeInOut" timing. For spring dynamics, use animateValue with a spring config instead.
Animate a number and apply it each frame:
javascript
var box = scene.findEntity({ name: "MyBox" });
animateValue({
from: 0,
to: Math.PI * 2,
duration: 3.0,
curve: "easeInOut",
repeatCount: -1,
onUpdate: function(value) {
// position is an offset from where the box was placed in the editor, so this circles that spot.
box.representation.position = Vector3(
Math.sin(value) * 2,
1,
Math.cos(value) * 2
);
}
});Spring-based value animation:
javascript
animateValue({
from: 0.6,
to: 0.08,
spring: { duration: 0.6, bounce: 0 },
onUpdate: function(h) {
scene.setVariable("baseColor", Color.hsl(h, 0.8, 0.55));
}
});Repeating pulse animation:
javascript
animateValue({
from: 0.6,
to: 0.65,
duration: 4,
curve: 'easeInOut',
repeatCount: -1,
reverseOnRepeat: true,
onUpdate: function(h) {
scene.setVariable("baseColor", Color.hsl(h, 0.5, 0.5));
}
});Fetch data from an API
GET request – fetch live weather data:
javascript
// Attach to: On Experience Start
var response = await http.get(
"https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t=temperature_2m"
);
var temp = response.data.current.temperature_2m;
console.log("Berlin: " + temp + "°C");POST request with JSON:
javascript
var response = await http.post(
"https://api.example.com/submit",
JSON.stringify({ score: 100, name: "Player" }),
{ headers: { "Content-Type": "application/json" } }
);
console.log("Status: " + response.status);Custom request with an API key:
javascript
var response = await http.request("https://api.example.com/data", {
method: "POST",
secret: "my-api",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: "test" })
});
console.log(response.data.results);Keep your API key out of the script
Add the key in the editor under Secrets, then name it with secret. Scenery makes the request and adds the key on the way out, so it stays on your account rather than being shipped with the experience.
A secret is tied to the API's address, so the key is only ever sent there, and you can replace it without touching your scripts. Works with any API that takes its key in a header.
Binary response as base64 (e.g. text-to-speech):
javascript
var response = await http.request("https://api.openai.com/v1/audio/speech", {
method: "POST",
secret: "openai",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "tts-1",
input: "Hello from augmented reality!",
voice: "nova",
response_format: "pcm"
}),
responseType: "base64"
});
if (response.status === 200) {
var entity = scene.findEntity({ name: "Speaker" });
await entity.playAudioBuffer(response.data, { sampleRate: 24000 });
}How long an API call takes
Nearly all of it is the API's own time, so budget for the one you chose rather than for Scenery.
A call spends about 100ms inside Scenery: reading which key the name points at, decrypting it, and counting it against the daily limit. That part is steady. The API's own reply is usually several times that, and on a measured run against OpenAI the swing between identical calls was larger than everything Scenery does.
Two things follow. Reach for an API when something happens, not on every frame, and never inside an update loop. And when a call is slow, look at the endpoint before looking here: a refused key comes back in about 400ms because the API rejects it at once, while a listing or a completion takes as long as it takes.
If a viewer waits on the answer, say so in the scene. A label that reads "Thinking…" until the reply lands is the difference between a pause and a broken experience.
When an API call fails
Two different things can go wrong, and they arrive differently. This catches people out, so it is worth reading once.
The API's own refusal comes back as an ordinary response carrying its status. A spent quota at the vendor, a model name it doesn't know, a malformed body: all of these are a normal result with status set to 429 or 400, not an error. A script that only wraps the call in try never notices them.
Scenery declining to make the call at all rejects the promise. The name in secret isn't bound to this experience, the address doesn't match the one the key was stored for, or the secret has spent its daily limit. These arrive as an error carrying code and reason you can branch on.
Neither is written for the person standing in your scene. Put the detail in the console, where only you read it, and give the viewer a line that belongs in the experience:
javascript
var speaker = scene.findEntity({ name: "Speaker" });
var caption = speaker.representation.findView({ name: "caption" });
try {
var response = await http.request("https://api.openai.com/v1/audio/speech", {
method: "POST",
secret: "openai",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "tts-1",
input: "Hello from augmented reality!",
voice: "nova",
response_format: "pcm"
}),
responseType: "base64"
});
if (response.status === 200) {
await speaker.playAudioBuffer(response.data, { sampleRate: 24000 });
} else {
console.error("Speech request failed with status " + response.status);
caption.setProperty("text", "The guide has gone quiet for a moment.");
}
} catch (error) {
console.error("Speech request refused: " + error.reason + " (" + error.code + ")");
caption.setProperty("text", "The guide has gone quiet for a moment.");
}Out of calls for today
A secret has a daily limit, and it is shared by everyone who opens an experience using it. Two refusals share the code resource-exhausted and want opposite advice, so branch on reason:
javascript
} catch (error) {
if (error.reason === "owner_busy") {
// Too many calls at once. Retrying shortly usually works.
caption.setProperty("text", "One moment…");
} else if (error.reason === "ceiling_reached") {
// The day's limit is spent. error.details.resetAt is when it returns.
console.error("Daily limit of " + error.details.limit + " reached, resets " + error.details.resetAt);
caption.setProperty("text", "The guide is resting until tomorrow.");
} else {
console.error("Refused: " + error.reason + " (" + error.code + ")");
caption.setProperty("text", "The guide has gone quiet for a moment.");
}
}Write the viewer's line yourself
error.message and error.reason describe your credential and your account, in wording that can change. Keep them in the console and write what the viewer sees in the voice of the experience.
WebSocket real-time connection
javascript
// Attach to: On Experience Start
var ws = websocket.connect("wss://echo.websocket.org", {
headers: { "Authorization": "Bearer YOUR_TOKEN" }
});
ws.onOpen = function() {
console.log("Connected");
ws.send(JSON.stringify({ type: "hello" }));
};
ws.onMessage = function(event) {
var data = JSON.parse(event.data);
console.log("Received: " + data.type);
};
ws.onClose = function() {
console.log("Disconnected");
};
ws.onError = function(event) {
console.log("Error: " + event.error);
};Shader material parameters
Bind a shader parameter to a variable, then toggle it:
javascript
var box = scene.findEntity({ name: "Box" });
// Bind the ShaderGraphMaterial parameter "isOn" to scene variable "isOn"
box.representation.bindMaterialParameter("isOn", "isOn", { update: "onChange" });
// Initialize the variable
scene.setVariable("isOn", false);
// Toggle on tap
box.on("tap", function() {
var isOn = scene.getVariable("isOn") ?? false;
scene.setVariable("isOn", !isOn);
});Animate a material parameter via variables:
javascript
var entity = scriptContext.sourceEvent.representationEntity;
// Bind material parameters to variables
entity.bindMaterialParameter("baseColor", "baseColor", { update: "onChange" });
entity.bindMaterialParameter("intensity", "intensity", { update: "onChange" });
// Animate color via variable (material auto-updates)
scene.setVariable("baseColor", Color.hsl(0.6, 0.5, 0.5));
scene.setVariable("intensity", 0.15);
// Smoothly ramp intensity to a target – animateValue paces by
// wall-clock so the ramp takes the same time regardless of frame rate.
// Hand-rolled `lerp on render tick` looks similar but converges twice
// as fast at 120 Hz as at 60 Hz.
animateValue({
from: 0.15,
to: 0.5,
duration: 0.6,
curve: "easeOut",
onUpdate: function (v) { scene.setVariable("intensity", v); }
});Change a Library texture or an effect while it runs
A texture from Library > Textures that a shader draws, and a shader effect of the scene, take the same setParameter as a material. The change reaches the running copy only: the texture and the effect in the editor keep their values.
javascript
// Attach to: onStart
const ripples = await Texture.fromAsset('Ripples');
const glow = scene.getEffect('glowEffectId');
// A trail texture a kernel of your own writes, read by the effect's "trail" slot.
const trail = await Texture.compute({ pixelFormat: 'rgba16Float', width: 512, height: 512 });
glow.shaderTexture('trail', trail);
scene.on('render', function () {
const pulse = 0.5 + 0.5 * Math.sin(scene.time * 2);
ripples.setParameter('speed', 1 + pulse);
glow.setParameter('strength', pulse);
});
const pond = scene.findEntity({ name: 'Pond' });
pond.on('tap', function () {
glow.setParameter('tint', '#33CCFF', { type: 'color' });
});- A name the shader has no setting or texture slot for is reported in the console, and so is a value that does not fit the setting.
- A texture whose shader runs once runs once more after
setParameter. shaderTextureon an effect takes what it takes on a material: a URL, an asset id or a texture handle.- Effects currently do not run on Apple Vision Pro, where
scene.getEffectreturnsnull.
Toggle behavior on tap
Use a variable to alternate between two actions on each tap:
javascript
// Attach to: On Experience Start
experience.setVariable("isOpen", false);
var lid = scene.findEntity({ name: "Lid" });
lid.on('tap', function() {
var isOpen = experience.getVariable("isOpen");
if (isOpen) {
lid.animateTo({ rotation: Rotation(0, 0, 0) }, 0.4, { timingFunction: "easeInOut" });
} else {
lid.animateTo({ rotation: Rotation(-Math.PI / 2, 0, 0) }, 0.4, { timingFunction: "easeOut" });
}
experience.setVariable("isOpen", !isOpen);
});Or use on/off to swap between different event handlers:
javascript
// Attach to: On Experience Start
var box = scene.findEntity({ name: "MyBox" });
var currentTap = null;
function listenShrink() {
currentTap = box.on('tap', function() {
box.off(currentTap);
box.animateTo({ scale: Vector3(0.5, 0.5, 0.5) }, 0.3, { timingFunction: "easeOut" });
listenGrow();
});
}
function listenGrow() {
currentTap = box.on('tap', function() {
box.off(currentTap);
box.animateTo({ scale: Vector3(1, 1, 1) }, 0.3, { timingFunction: "easeOut" });
listenShrink();
});
}
listenShrink();State management with variables
Variables live on two scopes:
experience– persists across all scenes. Use for global state like scores, settings, or progress.scene– cleared on scene transition. Use for local state like counters, toggles, or material bindings.
Experience-scoped (persists across scenes):
javascript
// Script A (On Experience Start): initialize state
experience.setVariable("score", 0);
experience.setVariable("level", 1);
// Script B (On Tap Gesture): update state
var score = experience.getVariable("score");
experience.setVariable("score", score + 10);
// Script C (On Variable Change): react to changes
var newScore = experience.getVariable("score");
console.log("Score: " + newScore);Scene-scoped (cleared on scene transition):
javascript
scene.setVariable("localCounter", 0);
var count = scene.getVariable("localCounter");
scene.setVariable("localCounter", count + 1);Raycasting
Cast a ray from the screen center to find objects:
javascript
// Attach to: On Render or On Screen Tap
var center = Vector2(0.5, 0.5);
var ray = scene.screenToRay(center);
if (!ray) return;
var hits = scene.raycast(ray.origin, ray.direction);
if (hits.length > 0) {
var hit = hits[0];
console.log("Hit at: " + hit.position.x.toFixed(2) + ", " + hit.position.y.toFixed(2) + ", " + hit.position.z.toFixed(2));
}Place an object aligned to the surface normal:
javascript
function transformFromNormal(position, normal) {
var n = normal.normalize();
var up = Vector3(0, 1, 0);
var dot = up.dot(n);
if (dot > 0.999) return Transform({ position: position });
if (dot < -0.999) return Transform({ position: position, rotation: Rotation(Math.PI, 0, 0) });
var axis = up.cross(n).normalize();
var angle = Math.acos(dot);
var halfAngle = angle / 2;
var s = Math.sin(halfAngle);
return Transform({
position: position,
rotation: Rotation.quaternion(axis.x * s, axis.y * s, axis.z * s, Math.cos(halfAngle))
});
}
var ray = scene.screenToRay(Vector2(0.5, 0.5));
if (ray) {
var hits = scene.raycast(ray.origin, ray.direction);
if (hits.length > 0) {
var entity = scene.findEntity({ name: "Marker" });
entity.representation.worldTransform = transformFromNormal(hits[0].position, hits[0].normal);
}
}Cast against AR planes (real-world surfaces):
javascript
var ray = scene.screenToRay(Vector2(0.5, 0.5));
var arHits = scene.raycastAR(ray.origin, ray.direction, "existingPlaneGeometry", "horizontal");
if (arHits.length > 0) {
var hit = arHits[0];
console.log("Surface: " + hit.targetAlignment);
scene.createEntity(
createSphere(0.05)
.anchor(Anchor.position(hit.worldTransform.position))
);
}Audio
Play spatial audio on an object:
javascript
var speaker = scene.findEntity({ name: "Speaker" });
await speaker.playAudio("https://example.com/sound.mp3", {
gain: 0.8,
loops: true,
inputMode: "spatial"
});Play audio from a generated buffer:
javascript
// Generate a 440Hz sine wave tone (1 second)
var sampleRate = 24000;
var numSamples = Math.floor(sampleRate * 1.0);
var samples = new Int16Array(numSamples);
for (var i = 0; i < numSamples; i++) {
var t = i / sampleRate;
samples[i] = Math.floor(Math.sin(2 * Math.PI * 440 * t) * 32767 * 0.5);
}
// Convert to base64
var bytes = new Uint8Array(samples.buffer);
var binary = '';
for (var j = 0; j < bytes.length; j++) {
binary += String.fromCharCode(bytes[j]);
}
var base64 = btoa(binary);
var entity = scene.findEntity({ name: "Speaker" });
await entity.playAudioBuffer(base64, { sampleRate: sampleRate });Stream audio in real-time:
javascript
var entity = scene.findEntity({ name: "Speaker" });
var stream = entity.createAudioStream({ sampleRate: 24000 });
// Append base64-encoded PCM16 audio chunks as they arrive
stream.append(base64AudioData);
// Check if buffer has finished playing
if (stream.isDrained) {
console.log("Playback finished");
}
// Stop when done
stream.stop();Microphone input
javascript
// Attach to: On Experience Start
var mic = scene.microphone;
mic.configure({
sampleRate: 24000,
silenceThreshold: 0,
highpassFrequency: 0
});
mic.onData = function(base64Chunk) {
// Process audio chunk (e.g. send to server or WebSocket)
ws.send(JSON.stringify({
type: "input_audio_buffer.append",
audio: base64Chunk
}));
};
mic.onError = function(error) {
console.log("Mic error: " + error);
};
mic.start();
// Later: mic.stop();Face tracking (blend shapes)
Drive a 3D face model with live face tracking data:
javascript
// Attach to: On Render
// Requires a model with blend shape support (e.g. a face-tracking-capable head model)
var headModel = scene.findEntity({ name: "Head" }).representation;
var faces = scene.getAnchors("face");
if (faces.length > 0) {
headModel.setBlendShapeWeights(faces[0].blendShapes);
}Hand tracking (visionOS)
Hand tracking is available on Apple Vision Pro. Gate portable scripts with environment.features.has('handTracking').
Attach an object to the user's hand:
javascript
scene.createEntity(
createSphere(0.02)
.name("HandMarker")
.anchor(Anchor.hand("right", "indexFingerTip", "full", 0.5, true))
);Read hand joint data without placing an object – opt into the provider, then poll the live hands each frame:
javascript
// Attach to: On Experience Start
await scene.tracking.hands.enable(); // resolves when tracking is live; rejects if denied / unsupportedjavascript
// Attach to: On Render
var right = scene.tracking.hands.right; // null when that hand isn't tracked
if (right) {
var tip = right.joints.indexFingerTip.worldTransform.position;
console.log("Right index tip: " + tip);
}scene.tracking.hands owns the provider lifecycle (enable() / disable(), isSupported, active) and the live reads (left / right / anchors).
Joint positions are in world space. To use them against an object you built (a mesh, a placed entity), convert into that object's local frame first – see Coordinate spaces.
scene.getAnchors("hand") remains the low-level equivalent:
javascript
// Attach to: On Render
var anchors = scene.getAnchors("hand");
for (var i = 0; i < anchors.length; i++) {
var hand = anchors[i];
if (hand.chirality === "right") {
console.log("Right hand at: " + hand.worldTransform.position);
}
}Inspect model hierarchy for hand mesh rigging:
javascript
var entity = scene.findEntity({ name: "Glove" });
var rep = entity.representation;
var names = rep.getChildNames();
names.forEach(function(name) {
var child = rep.findChild(name);
console.log(name + " - joints: " + child.jointNames.length);
});Cross-platform surface input
scene.createSurfaceInput gives you interaction points on a surface using whatever input the platform offers – fingertips on Apple Vision Pro (hand tracking), screen taps and drags on iPhone, iPad and Mac. Your code reads the same points either way and never branches on platform.
Read points each frame. Each point is in the surface's own local frame and carries a stable id, a phase (began / moved / ended) and a source (hand / screen):
javascript
// Attach to: On Experience Start
var surface = scene.findEntity({ name: "Surface" });
var input = scene.createSurfaceInput({
target: surface.representation, // points are returned in this representation's local frame
plane: { axis: "y", offset: 0 }, // how a screen ray maps to the surface (a flat plane here)
maxDistance: 0.08 // a fingertip counts as touching within 8 cm of the plane
});
scene.on('render', function () {
input.points.forEach(function (p) {
if (p.phase === 'began') {
console.log('touch started via ' + p.source + ' at ' + p.x.toFixed(2) + ', ' + p.z.toFixed(2));
}
// p.x / p.y / p.z are local to the surface — feed them into your own logic
// (deform a mesh, spawn something, move an object …).
});
});
// When you no longer need it (for example before leaving the scene):
// input.stop();TIP
targetis the representation whose local frame you want, usuallyentity.representation. For a composed object with nested representations, pass the specific one that holds the surface.project: 'plane'(the default) needs no collision – ideal for water, tables, or a deforming mesh. Useproject: 'collision'to raycast a solid model's collision geometry instead.- On Apple Vision Pro the points come from fingertips; on iPhone / iPad / Mac they come from taps and drags. Nothing else in your code changes.
Image tracking
Track a physical image and attach content:
javascript
scene.createEntity(
createBox(0.1, 0.1, 0.01)
.name("ImageOverlay")
.anchor(Anchor.image(
"https://example.com/marker.jpg",
0.15, // physical width in meters
"myMarker",
"any",
true, // track continuously
true, // hide if tracking lost
true // show discovery hint
))
);Render loop patterns
Smooth oscillation:
javascript
// Attach to: On Render
var box = scene.findEntity({ name: "MyBox" });
var t = scriptContext.sourceEvent.time;
// An offset from where the box was placed, so it sways around that spot.
box.representation.position = Vector3(
Math.sin(t) * 0.5,
Math.sin(t * 2) * 0.2,
0
);Frame-rate independent movement:
javascript
// Attach to: On Render
var box = scene.findEntity({ name: "MyBox" });
var dt = scriptContext.sourceEvent.deltaTime;
var speed = 2.0;
var pos = box.representation.position;
box.representation.position = Vector3(pos.x + speed * dt, pos.y, pos.z);Throttled updates for expensive operations:
javascript
scene.on('render', { throttle: 0.1 }, function(e) {
// Runs at 10 FPS instead of every frame
var anchors = scene.getAnchors("plane");
console.log("Planes: " + anchors.length);
});Look at camera (billboard)
javascript
// Attach to: On Render
var label = scene.findEntity({ name: "Label" });
var cam = scene.cameraTransform;
var dir = cam.position.subtract(label.representation.worldPosition).normalize();
var yaw = Math.atan2(dir.x, dir.z);
label.representation.rotation = Rotation(0, yaw, 0);Scene transitions
javascript
// Pass data to the next scene via experience variables
experience.setVariable("selectedLevel", 3);
// Transition (use scene ID from editor)
experience.transitionToScene("SCENE_ID");Ground objects with ambient occlusion
Ambient occlusion darkens creases and the ground under objects, so objects look like they stand in the room. Here a tap anywhere on the screen turns it on or off, so you can see the difference.
javascript
// Attach to: On Experience Start
if (environment.features.has('ambientOcclusion')) {
var isOn = false;
scene.on('screenTap', function () {
isOn = !isOn;
scene.setAmbientOcclusion(isOn ? { radius: 0.3, strength: 3 } : null);
});
}Open a screen and fill it in
Make the screen in the editor's Screens section and name its views. A script opens it like the Open Screen action does, then writes into it:
javascript
// Attach to: an object's tap event
const details = await experience.openScreen('Details');
details.representation.findView({ name: 'Title' }).setProperty('text', 'House 25');A Close button on the screen needs no name: experience.closeScreen() closes the screen whose event ran the script, the same as the Close Screen action with This Screen.
Room picker carousel
Build the carousel in the editor, one child per room, and name it. A script tilts the side rooms like a card stack and shows the room the visitor settles on:
javascript
// Attach to: On Experience Start
const rooms = scene.findEntity({ name: 'Room Picker' }).representation.findView({ name: 'rooms' });
rooms.scrollTransition({ tilt: Math.toRadians(35), scale: 0.85, opacity: 0.6 });
rooms.on('valueChanged', function(e) {
scene.setVariable('room', e.uiValue);
});rooms.selectedIndex = 2 scrolls the third room to the middle. A carousel made in a script takes the same options as the editor: UI.carousel({ children, spacing, neighborScale, ... }).
Guide to a room
Point the direction indicator at one part of a model, such as a room inside a building, in the room's colour. The arrow aims at the middle of the part, and the pin at the screen edge shows the way while the room is out of view:
javascript
// Attach to: On Experience Start
const building = scene.findEntity({ name: 'Building' }).representation;
const rooms = [
{ part: 'Kitchen', color: Color.hex('#FF9500') },
{ part: 'Studio', color: Color.hex('#30B0C7') }
];
scene.findEntity({ name: 'Room Picker' }).representation.findView({ name: 'rooms' }).on('valueChanged', function(e) {
const room = rooms[e.uiValue];
building.guideTo(building.findChild(room.part), { color: room.color });
});building.guideTo(building) aims at the whole object, and building.guideTo(null) hides the indicator. building.getChildNames() lists the parts a model has.
Stop the previous room's tour
Name each room's tour Sequence "Tour" in the editor. A script that starts a room first stops whatever tour is running:
javascript
// Attach to: On Experience Start
const picker = scene.findEntity({ name: 'Room Picker' }).representation.findView({ name: 'rooms' });
picker.on('valueChanged', function(e) {
scene.cancelNamedActions('Tour');
scene.triggerCustomEvent('room-' + e.uiValue);
});Groups and Sequences without a name keep running, so background music outside the tours is not affected.
Zoomable room photo
Let a photo on a screen panel zoom with two fingers and spring back when they lift, as in a photo viewer. Author a screen-space panel with an image named "Room photo":
javascript
// Attach to: On Experience Start
const photo = scene.findEntity({ name: 'Room Photo Panel' }).representation.findView({ name: 'Room photo' });
photo.enablePinchZoom({ maxScale: 3 });releaseBehavior: 'stay' keeps the zoom instead. For other mechanics, listen to on('pinch' | 'pan' | 'rotate') on the view and set its scale, rotation, offsetX and offsetY yourself.
One hotspot open at a time
Pin an annotation to each feature of a product, and close the others when the visitor opens one. Author three objects named "Port", "Lens" and "Speaker", each a UI panel holding an annotation named "Hotspot" with its card:
javascript
// Attach to: On Experience Start
const hotspots = ['Port', 'Lens', 'Speaker'].map(function(name) {
return scene.findEntity({ name: name }).representation.findView({ name: 'Hotspot' });
});
hotspots.forEach(function(hotspot) {
hotspot.on('toggled', function(e) {
if (!e.uiValue) return;
hotspots.forEach(function(other) {
if (other !== hotspot) other.isExpanded = false;
});
});
});Set a panel's Presentation to Projected to keep its marker the same size on the screen at any distance. In 3D, a Dynamic Scale constraint on the object does the same. An annotation made in a script takes the editor's options: UI.annotation({ title: '1', card: UI.vStack({ children: [...] }) }).
Count badge on a button
Show how many items a visitor picked in a red badge on the button's corner, and take it away when the count goes back to zero. Author a screen panel named "Shop" with a button named "Cart":
javascript
// Attach to: On Experience Start
const cart = scene.findEntity({ name: 'Shop' }).representation.findView({ name: 'Cart' });
let badge = null;
function showCount(count) {
if (count === 0) {
if (badge) badge.remove();
badge = null;
} else if (badge) {
badge.text = String(count);
} else {
badge = cart.add(UI.label({
text: String(count),
padding: { vertical: 2, horizontal: 6 },
placement: { alignment: 'topTrailing' },
style: { foregroundColor: '#FFFFFF', backgroundColor: '#FF3B30', shape: 'capsule' }
}), { to: 'overlay' });
}
}The badge sits centred on the corner and isn't cut by the button's rounded shape. A badge that is always there is simpler to add in the editor with Add Overlay… and to hide with isHidden.
List from one sample row
Author one row in the editor inside a stack named "Destinations": an image, a label named "Row Name" and a button named "Go" the size of the row. Name the row "Row". The script adds a copy per destination and hides the sample, so the editor shows one row however long the list gets:
javascript
const panel = scene.findEntity({ name: 'Destination Picker' }).representation;
const list = panel.findView({ name: 'Destinations' });
const sample = list.findView({ name: 'Row' });
for (const destination of ['Fuse Box', 'Toilets', 'Workshop', 'Meeting Room']) {
const row = list.add(sample.copy());
row.findView({ name: 'Row Name' }).text = destination;
row.findView({ name: 'Go' }).on('tap', () => {
console.log('Guide to ' + destination);
});
}
sample.isHidden = true;Every copy keeps the sample's names, so row.findView searches inside that copy. Run it from the panel's On Will Appear event.
Platform-specific behavior
Use environment to check the current platform and adapt:
javascript
// Attach to: On Experience Start
var platform = environment.hostingPlatform; // "iOS", "macOS", "visionOS", or "web"
if (platform === "visionOS") {
// Add hand-tracked content
scene.createEntity(
createSphere(0.02)
.name("HandMarker")
.anchor(Anchor.hand("right", "indexFingerTip", "full", 0.5, true))
);
} else {
console.log("Running on " + platform + " (" + environment.systemOSVersion + ")");
console.log("Locale: " + environment.locale);
}Text in the viewer's language
A label that only shows a variable needs no script: write ${count} into its text and its translations, and set the variable (see Texts that change). When a script builds the text itself, it can follow the experience's translations too. Make a shared text for it in the editor (a text field's link menu, Make Shared Text…), translate it in Library › Translations, and look it up by name with experience.localize. ${name} placeholders are filled from the object you pass. Format numbers with experience.language, so a German viewer reads "1.234,5" and an English one "1,234.5".
javascript
// Attach to: On Experience Start
// A shared text named "Score" reads "You found ${count} of ${total}".
var panel = scene.findEntity({ name: "Score Panel" });
var label = panel.representation.findView({ name: "score" });
var found = 0;
function showScore() {
var count = new Intl.NumberFormat(experience.language).format(found);
label.text = experience.localize("Score", { count: count, total: 5 });
}
showScore();
scene.on("languageChange", showScore);A text set this way stays in the language it was written in, so the script writes it again when the language switches. To let visitors switch, give a panel button a script that sets the language:
javascript
// Attach to: the "Deutsch" button's UI Interaction event
experience.language = "de";Arrange objects in a line
On screen tap, find multiple entities and animate them into a row:
javascript
// Attach to: On Screen Tap
var names = ["BoxA", "BoxB", "BoxC", "BoxD", "BoxE"];
var spacing = 0.4;
var startX = -((names.length - 1) * spacing) / 2;
for (var i = 0; i < names.length; i++) {
var entity = scene.findEntity({ name: names[i] });
if (!entity) continue;
var targetPos = Vector3(startX + i * spacing, 1.2, -2);
entity.animateTo(
{ position: targetPos, rotation: Rotation(0, 0, 0), scale: Vector3(1, 1, 1) },
0.6 + i * 0.1,
{ timingFunction: "easeOut" }
);
}Arrange in a circle:
javascript
// Attach to: On Screen Tap
var names = ["Obj1", "Obj2", "Obj3", "Obj4", "Obj5", "Obj6"];
var radius = 1.0;
var center = Vector3(0, 1.2, -2.5);
for (var i = 0; i < names.length; i++) {
var entity = scene.findEntity({ name: names[i] });
if (!entity) continue;
var angle = (i / names.length) * Math.PI * 2;
var targetPos = Vector3(
center.x + Math.sin(angle) * radius,
center.y,
center.z + Math.cos(angle) * radius
);
// Face center
var yaw = Math.atan2(
center.x - targetPos.x,
center.z - targetPos.z
);
entity.animateTo(
{ position: targetPos, rotation: Rotation(0, yaw, 0) },
0.8,
{
timingFunction: "easeOut",
delay: i * 0.08
}
);
}Procedural spiral with tap bounce
Create boxes in a rainbow spiral. Each box gets an inline unlit material with a unique HSL color. Tap any box to bounce it:
javascript
// Attach to: On Experience Start
var count = 24;
var baseRadius = 0.8;
var heightStep = 0.06;
var turns = 2.5;
async function buildSpiral() {
for (var i = 0; i < count; i++) {
var t = i / count;
var angle = t * Math.PI * 2 * turns;
var radius = baseRadius * (0.3 + t * 0.7);
var size = 0.06 + t * 0.06;
var x = Math.sin(angle) * radius;
var y = 1.0 + i * heightStep;
var z = -2.5 + Math.cos(angle) * radius;
var box = await scene.createEntity(
createBox(size, size, size)
.name("Spiral_" + i)
.anchor(Anchor.position(x, y, z))
);
// Face outward from center
var yaw = Math.atan2(x, z - (-2.5));
box.animateTo(
{ rotation: Rotation(0, yaw, 0) },
0.3,
{ delay: i * 0.03 }
);
// Bounce on tap
box.on('tap', function(e) {
e.representationEntity.animateTo(
{ scale: Vector3(1.5, 1.5, 1.5) },
0.3,
{ timingFunction: "easeOut" }
);
});
}
}
buildSpiral();UI panel with bound slider
Build a small control panel, bind a slider to a scene variable, and read the live value back in a label via ${var|fixed(2)} – the label re-renders automatically whenever the variable changes. See Live labels for the full filter reference.
javascript
// Attach to: On Experience Start
scene.setVariable("brightness", 0.5);
var panel = createPanel(
UI.vStack({
spacing: 16,
padding: 24,
children: [
UI.label({ text: "Brightness: ${brightness|fixed(2)}" }),
// Sliders fill the stack's width by default – no `sizing` needed.
UI.slider({
name: "brightness",
range: [0, 1],
value: 0.5
})
]
}),
// Panel size in points (~1360 pt/m). Use 'auto', 'fill', or [w, h].
{ panelSize: [400, 200] }
)
.name("controls")
// Static world anchor in front of the user at eye height.
.anchor(Anchor.position(0.5, 1.4, -0.8));
var entity = await scene.createEntity(panel);
entity.representation.findView({ name: "brightness" }).bind("value", "brightness");Dynamic mesh ribbon
Generate a growing ribbon procedurally: each frame, append two new vertices to the strip, then publish the updated draw range. Vertex writes go through a single memcpy from a reused TypedArray.
javascript
// Attach to: On Experience Start
var MAX_SEGMENTS = 256;
var vertexCapacity = MAX_SEGMENTS * 2; // two verts per segment
var ribbon = await scene.createEntity(
createMesh({
vertexCapacity: vertexCapacity,
indexCapacity: vertexCapacity,
attributes: {
position: { format: "float3", storage: "dynamic" },
color: { format: "uchar4Normalized", storage: "dynamic" }
},
parts: [{
indexCount: 0,
topology: "triangleStrip",
bounds: { min: [-1, -1, -1], max: [1, 1, 1] }
}]
})
);
var mesh = ribbon.representation.mesh;
// triangleStrip walks the vertex buffer in order, so the index buffer
// is just identity. Written once; drawCount expands per-frame.
var indices = new Uint32Array(vertexCapacity);
for (var i = 0; i < vertexCapacity; i++) { indices[i] = i; }
mesh.writeIndices(0, indices);
// Reusable scratch buffers – allocated once, extended lazily as the
// ribbon grows.
var positions = new Float32Array(vertexCapacity * 3);
var colors = new Uint8Array(vertexCapacity * 4);
var written = 0; // high-water mark – each segment's geometry is deterministic
function writeSegment(i) {
var t = i * 0.05;
var x = Math.sin(t) * 0.4;
var y = i * 0.01;
var z = Math.cos(t) * 0.4;
positions[(i * 2) * 3 + 0] = x; positions[(i * 2) * 3 + 1] = y; positions[(i * 2) * 3 + 2] = z;
positions[(i * 2 + 1) * 3 + 0] = x; positions[(i * 2 + 1) * 3 + 1] = y + 0.03; positions[(i * 2 + 1) * 3 + 2] = z;
var hue = (i / MAX_SEGMENTS) * 255;
colors[(i * 2) * 4 + 0] = hue; colors[(i * 2) * 4 + 1] = 200; colors[(i * 2) * 4 + 2] = 255 - hue; colors[(i * 2) * 4 + 3] = 255;
colors[(i * 2 + 1) * 4 + 0] = hue; colors[(i * 2 + 1) * 4 + 1] = 200; colors[(i * 2 + 1) * 4 + 2] = 255 - hue; colors[(i * 2 + 1) * 4 + 3] = 255;
}
// Wall-clock paced reveal via animateValue – same duration at 60, 120,
// 240 Hz. Avoid hand-rolled `segments++` per render tick: that ties the
// reveal speed to the frame rate.
animateValue({
from: 0,
to: MAX_SEGMENTS,
duration: 4.0,
curve: "linear",
onUpdate: function (s) {
var target = Math.floor(s);
// Extend the buffer to cover any newly-visible segments.
for (var i = written; i < target; i++) writeSegment(i);
if (target > written) {
mesh.writeVertices("position", 0, positions);
mesh.writeVertices("color", 0, colors);
written = target;
}
mesh.drawCount = target * 2;
mesh.setBounds({ min: [-0.5, 0, -0.5], max: [0.5, Math.max(0.05, target * 0.01), 0.5] });
}
});Follow a path
Build a spline with scene.createPath and drive an object along it with followPath:
javascript
// Attach to: On Experience Start
var pts = [
new Vector3(-0.6, 0, -1.2),
new Vector3(-0.2, 0.4, -1.2),
new Vector3(0.2, -0.2, -1.4),
new Vector3(0.6, 0.3, -1.2)
];
var path = scene.createPath({ points: pts });
var box = await scene.createEntity(
createBox(0.05, 0.05, 0.05).anchor(Anchor.position(0, 0, 0))
);
box.followPath(path, {
duration: 4,
constantSpeed: true, // even speed regardless of control-point spacing
alignsToTangent: true, // nose points along the direction of travel
timing: 'easeInOut',
repeat: true
});Mesh instancing
Render thousands of copies of one object cheaply with setInstances – far lighter than cloning. Here, a sunflower spiral:
javascript
// Attach to: On Experience Start
var base = await scene.createEntity(
createBox(0.03, 0.03, 0.03).anchor(Anchor.position(0, 0, -1.5))
);
var spots = [];
var count = 1000;
var goldenAngle = Math.PI * (3 - Math.sqrt(5));
for (var i = 0; i < count; i++) {
var r = 0.4 * Math.sqrt(i / count);
var a = i * goldenAngle;
spots.push(new Vector3(r * Math.cos(a), 0, r * Math.sin(a)));
}
base.setInstances(Instances.transforms(spots));Softened pictures
A picture can be resized and blurred before a material shows it. Place a plane named Glow Poster in the editor, then:
javascript
// Attach to: On Experience Start
// Half size, blurred, with the dark parts dropped, for a soft glow
var poster = scene.findEntity({ name: 'Glow Poster' });
var glow = await Texture.image('poster.png', {
scale: 0.5,
steps: [{ blur: 12 }, { thresholdToZero: 0.2 }]
});
await poster.representation.setMaterial(Material.unlit({ map: glow }));A step outside its range, such as { blur: 100 }, rejects with a message that names it. Blur takes 0 to 64 pixels; Threshold to Zero and Radial Zoom Blur take 0 to 1.
Roll a ball with a game controller
A small game: the left stick rolls a ball across a floor, relative to where the camera looks. Cross (A) jumps with a light haptic tap, clicking the left stick sprints until the stick is let go, and touching an orb collects it. Outside AR the camera follows the ball; in AR the same controls work from wherever the viewer stands.
Build the scene in the editor, then add the script:
- Floor – a flat box, about 2 × 2 m, with a Static physics body.
- Ball – a small sphere above the floor with a Dynamic physics body: mass 0.1 kg, linear damping 4, angular damping 0, restitution 0.
- Orb 1 to Orb 4 – small spheres floating just above the floor, no physics.
- On Experience Start, an Add to Scene action for each, then a Run Script action with this script.
javascript
// Attach to: On Experience Start
var GRAVITY = 9.81; // m/s²
var BALL_MASS = 0.1; // kg, as set on the Ball
var BALL_DAMPING = 4; // per second, the Ball's linear damping
var ROLL_SPEED = 0.6; // m/s at full stick, where the push and the damping cancel
var ROLL_FORCE = BALL_MASS * BALL_DAMPING * ROLL_SPEED; // N at full stick
var SPRINT_FACTOR = 2; // a sprint settles at twice ROLL_SPEED
var JUMP_HEIGHT = 0.25; // metres without damping; the damping shortens it
var JUMP_IMPULSE = BALL_MASS * Math.sqrt(2 * GRAVITY * JUMP_HEIGHT); // N·s
var GROUND_TOLERANCE = 0.02; // metres above the floor that still count as standing
var FALL_DEPTH = 0.5; // metres below the floor that count as fallen off
var PICKUP_DISTANCE = 0.1; // metres
var ball = scene.findEntity({ name: 'Ball' });
var floor = scene.findEntity({ name: 'Floor' });
var orbs = [1, 2, 3, 4].map(function (i) {
return scene.findEntity({ name: 'Orb ' + i });
});
var start = ball.position;
var pad = null;
var sprinting = false;
var collected = 0;
scene.camera.follow(ball, { distance: 1.6, elevation: 30 });
function rumble(intensity, seconds) {
if (pad) {
pad.pulse(intensity, seconds);
}
}
function isOnGround() {
return ball.worldBoundingBox.min.y - floor.worldBoundingBox.max.y < GROUND_TOLERANCE;
}
function jump() {
if (isOnGround()) {
ball.applyImpulse(Vector3(0, JUMP_IMPULSE, 0), { space: 'world' });
rumble(0.2, 0.05);
}
}
// Stick up rolls the ball away from the viewer, whichever way the camera faces.
function stickToWorld(stick) {
var view = scene.cameraTransform.transformVector(Vector3(0, 0, -1));
var forward = Vector3(view.x, 0, view.z);
if (forward.length() === 0) {
return null; // looking straight down: no horizontal forward
}
forward = forward.normalize();
var right = Vector3(-forward.z, 0, forward.x);
return forward.multiply(stick.y).add(right.multiply(stick.x));
}
scene.gameController.on('connect', function (controller) {
pad = controller;
controller.on('button', { name: 'buttonA', phase: 'began' }, jump);
controller.on('button', { name: 'leftThumbstickButton', phase: 'began' }, function () {
sprinting = true;
});
});
scene.on('render', function (e) {
var position = ball.worldPosition;
if (position.y < floor.worldBoundingBox.max.y - FALL_DEPTH) {
rumble(0.8, 0.25);
ball.setVelocity(Vector3(0, 0, 0));
ball.setAngularVelocity(Vector3(0, 0, 0));
ball.position = start;
return;
}
orbs.forEach(function (orb) {
if (orb.isEnabled && orb.worldPosition.distanceTo(position) < PICKUP_DISTANCE) {
orb.isEnabled = false;
collected += 1;
rumble(0.4, 0.08);
console.log('Orb ' + collected + ' of ' + orbs.length);
}
});
// A force, not a velocity: the Ball's damping caps its speed at ROLL_SPEED and stops it when the stick is let go.
var stick = pad && pad.sticks.leftThumbstick;
if (!stick || (stick.x === 0 && stick.y === 0)) {
sprinting = false;
return;
}
var push = stickToWorld(stick);
if (push) {
var force = sprinting ? ROLL_FORCE * SPRINT_FACTOR : ROLL_FORCE;
ball.applyImpulse(push.multiply(force * e.deltaTime), { space: 'world' });
}
});TIP
- While the camera follows the ball, the left stick no longer moves the camera, so the script has it to itself. The right stick still orbits, and clicking it recentres the view.
BALL_MASSandBALL_DAMPINGmust match the Ball's physics in the editor, or the top speed drifts fromROLL_SPEED.
See Game Controllers and Camera for the full API.
Draw in the air with a stylus (Apple Vision Pro)
Leave a trail of dots behind a stylus tip while it is pressed against a surface, harder for bigger dots. Author a small sphere named "Pen Tip" with a Spatial Accessory anchor set to Stylus, so the viewer sees where the line starts:
javascript
// Attach to: On Experience Start
if (environment.features.has('accessoryTracking')) {
var SPACING = 0.005; // metres between dots
var last = null;
scene.gameController.on('connect', function (controller) {
if (controller.kind !== 'stylus') return;
scene.on('render', function () {
var tip = controller.buttons.tip;
var aim = controller.aim;
if (!controller.isConnected || !tip || !tip.pressed || !aim) {
last = null;
return;
}
if (last && aim.position.distanceTo(last) < SPACING) return;
last = aim.position;
var radius = 0.001 + 0.003 * tip.value;
scene.createEntity(createSphere(radius)).then(function (dot) {
dot.worldTransform = Transform({ position: aim.position, rotation: Rotation(0, 0, 0), scale: Vector3(1, 1, 1) });
});
});
});
}A script can attach an object itself with createSphere(0.004).anchor(Anchor.accessory({ kind: 'stylus' })), or to a controller's grip with Anchor.accessory({ kind: 'spatialController', hand: 'right', location: 'grip' }). For long strokes, draw into one dynamic mesh instead of one object per dot, as in Dynamic mesh ribbon.
Rings that hook into each other
A convex hull would fill a ring's hole, so give it a collision shape of capsules around the rim instead. Author two ring models named "Ring 1" and "Ring 2", interlocked, with their rims in the object's XY plane; the script swaps each for a copy with physics, so they hang from each other like chain links:
javascript
// Attach to: On Experience Start
var RADIUS = 0.03; // metres, to the middle of the rim
var THICKNESS = 0.004;
var capsules = [];
for (var i = 0; i < 8; i++) {
var a = (i / 8) * Math.PI * 2, b = ((i + 1) / 8) * Math.PI * 2;
capsules.push({
from: Vector3(Math.cos(a) * RADIUS, Math.sin(a) * RADIUS, 0),
to: Vector3(Math.cos(b) * RADIUS, Math.sin(b) * RADIUS, 0),
radius: THICKNESS
});
}
['Ring 1', 'Ring 2'].forEach(function (name, index) {
var copy = scene.getObjectDescriptor({ name: name }).clone().physics({
mode: index === 0 ? 'static' : 'dynamic',
shape: { capsules: capsules },
mass: 0.02
});
scene.findEntity({ name: name }).toggle(false);
scene.createEntity(copy);
});Capsules are in the object's own space, in metres, and may overlap.
Less motion when the viewer asks for it
Viewers who turned on Reduce Motion get a calm marker instead of a pulsing one, and it changes as soon as they change the setting:
javascript
// Attach to: On Experience Start
var ring = scene.findEntity({ name: 'Pulse Ring' }).representation;
function updatePulse() {
ring.stopAnimations();
if (!environment.accessibility.reduceMotion) {
ring.animateTo({ scale: 2.2, opacity: 0 }, 1.6, { repeatCount: -1 });
}
}
updatePulse();
environment.accessibility.on('change', updatePulse);Scenery plays authored animations as they are, so check reduceMotion before starting a loop, a shake or a fast zoom.
Read the device location
Ride the base location grant, read a fix, and measure distance to a point of interest:
javascript
// Attach to: On Experience Start
await environment.location.requestAccess();
var here = await environment.location.current();
console.log('You are at', here.latitude, here.longitude, '±' + here.horizontalAccuracy + 'm');
var munich = new GeoCoordinate(48.1372, 11.5756);
console.log('Distance to Munich:', Math.round(here.distanceTo(munich)), 'm');
console.log('Bearing to Munich:', Math.round(here.bearingTo(munich)) + '°');AI voice assistant (OpenAI Realtime)
Full conversational AI with voice input/output and tool calling:
javascript
// Attach to: On Did Appear (on the assistant entity)
var entity = scriptContext.sourceEvent.representationEntity;
var stream = entity.createAudioStream({ sampleRate: 24000 });
var isResponding = false;
var ws = websocket.connect("wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview", {
headers: {
"Authorization": "Bearer YOUR_KEY",
"OpenAI-Beta": "realtime=v1"
}
});
ws.onOpen = function() {
ws.send(JSON.stringify({
type: "session.update",
session: {
modalities: ["text", "audio"],
input_audio_format: "pcm16",
output_audio_format: "pcm16",
voice: "ash",
turn_detection: {
type: "semantic_vad",
eagerness: "medium",
create_response: true,
interrupt_response: true
},
instructions: "You are a helpful AR assistant. Keep responses brief."
}
}));
startListening();
};
ws.onMessage = function(event) {
var msg = JSON.parse(event.data);
if (msg.type === "response.created") {
isResponding = true;
scene.microphone.stop();
}
if (msg.type === "response.audio.delta") {
stream.append(msg.delta);
}
if (msg.type === "response.done") {
isResponding = false;
// Resume mic after audio finishes
waitForDrain(function() { startListening(); });
}
};
function startListening() {
if (isResponding) return;
scene.microphone.configure({ sampleRate: 24000, silenceThreshold: 0, highpassFrequency: 0 });
scene.microphone.onData = function(base64) {
if (!isResponding) {
ws.send(JSON.stringify({ type: "input_audio_buffer.append", audio: base64 }));
}
};
scene.microphone.start();
}
function waitForDrain(callback) {
if (stream.isDrained) { callback(); return; }
setTimeout(function() { waitForDrain(callback); }, 100);
}