A <Scene> is a React Native view with a 3D world inside. It has its own camera, lights (from its look) and frame loop. Everything 3D you render goes inside one.
import { Box, Scene } from "@apricotjs/react-native";
import { View } from "react-native";
export function ProductCard() {
return (
<View style={{ height: 320, borderRadius: 24, overflow: "hidden" }}>
<Scene style={{ flex: 1 }} look="studio">
<Box size={1.5} radius={0.2} material="gold" />
</Scene>
</View>
);
}Size and layout#
A scene is sized by its style, like a View: flex: 1, a fixed height, or StyleSheet.absoluteFill. It resizes with layout changes such as rotation, split screen or a sheet animating in. A scene with no size draws nothing.
You can put several scenes on one screen. Each has its own camera and loop. When objects share a camera, put them in one scene.
Lifecycle#
| Stage | What happens |
|---|---|
| Mount | The scene waits for its layout. |
| Attach | onCreated fires with the ApricotScene. Children mount at this stage, so useFrame and useScene work in them. |
| Warm-up | The scene prepares its materials. Frames show only the background, for up to 12 s. |
| Ready | The first frame with content draws and onReady fires. |
| Unmount | The scene, its materials and geometry are freed. Models and textures are freed when their last user unmounts. |
Loading and the first frame#
Content appears after a short wait, about a second on a mid-range phone. Show your own view meanwhile with loading:
import { Box, Scene } from "@apricotjs/react-native";
import { ActivityIndicator } from "react-native";
export function LoadingScene() {
return (
<Scene
style={{ flex: 1 }}
loading={<ActivityIndicator style={{ flex: 1 }} />}
onReady={() => console.log("scene shown")}
onError={(error) => console.warn("3D failed to start", error)}
>
<Box />
</Scene>
);
}loading covers the scene until its first content frame. <Label> views stay hidden until then too.
On the web#
Scenes also run in web browsers (Expo web). They draw with WebGPU, or with WebGL 2 in a browser that gives no WebGPU adapter (some Android phones) or has no WebGPU at all. Your code stays the same, and scenes look the same. With WebGL 2 each scene has a renderer of its own, so a page with many scenes compiles shaders once per scene.
Where neither is available, a scene fails with [apricot] WebGPU isn't available on this device, passed to onError (or logged). A phone app on a device without a WebGPU adapter fails the same way: there is no WebGL on iOS or Android.
To decide once, before showing scenes, call gpuSupport(). It resolves to "webgpu", "webgl" or "none":
import { Box, gpuSupport, type GpuSupport, Scene, Spin } from "@apricotjs/react-native";
import { useEffect, useState } from "react";
import { Image } from "react-native";
export function Hero() {
const [support, setSupport] = useState<GpuSupport | null>(null);
useEffect(() => {
gpuSupport().then(setSupport);
}, []);
if (support === null) return null;
if (support === "none") return <Image source={require("./hero.png")} style={{ flex: 1 }} />;
return (
<Scene style={{ flex: 1 }}>
<Spin speed={1}>
<Box size={1.6} color="#ff9f59" />
</Spin>
</Scene>
);
}Props fixed at mount#
These props are read once, when the scene mounts. Changing them later has no effect.
look, camera, fog, environment, transparent, toneMapping, exposure, shadows, occlusion, lamps, lighting, maxPixelRatio, linger.
To change them at runtime:
| To change | Use |
|---|---|
| Reflections | <Environment name> (fades between environments) |
| Time of day, sun and sky | <Daylight time> (with look="none") |
| Camera position or framing | <CameraRig view>, <FitCamera>, <FollowCamera>, or the camera through useScene().camera |
| Lights | Light components such as <DirectionalLight intensity> |
| Lamps on and off | <Lamp on> (within the lamps the scene was made with) |
| Anything else in the list | Remount the scene with a new key |
frameloop, background and paused can change at any time.
When the scene renders#
By default (frameloop="demand") a scene renders only when something changes, then sleeps. A sleeping scene uses no CPU or GPU time. These things wake it:
- a prop changing (a new
position,color,view…); - a shared value bound to a prop changing (a gesture, a spring, a sensor);
- a
useFramecallback returningtrue(still animating); - a behavior that is moving (
<Spin>with a non-zero speed, a driving<Vehicle>, a falling<Pile>); - a tap, a drag on
<CameraRig orbit>, a resize; useScene().invalidate().
After the last change it keeps rendering for linger seconds (default 0.25).
Use frameloop="always" only for scenes that never stop moving and do not wake the loop themselves.
Three ways to place a scene#
Full screen#
import { Scene, Sphere } from "@apricotjs/react-native";
export function FullScreen() {
return (
<Scene style={{ flex: 1 }}>
<Sphere material="chrome" />
</Scene>
);
}Behind your UI#
Put the scene in StyleSheet.absoluteFill and your views above it. Use <CameraRig inset> so the subject is framed in the part of the screen your UI does not cover.
import { Box, CameraRig, Scene } from "@apricotjs/react-native";
import { StyleSheet, Text, View } from "react-native";
export function WithPanel() {
return (
<View style={{ flex: 1 }}>
<Scene style={StyleSheet.absoluteFill} look="studio">
<Box size={1.5} radius={0.2} material="car-paint">
<CameraRig view="three-quarter" inset={{ bottom: 220 }} />
</Box>
</Scene>
<View style={{ position: "absolute", left: 0, right: 0, bottom: 0, height: 220, padding: 24 }}>
<Text>Your panel</Text>
</View>
</View>
);
}Over your UI (overlay)#
A transparent scene draws only its objects, and pointerEvents="none" lets every touch reach the screen below. This is how objects fall over an existing screen (Falling objects).
import { Apple } from "@apricotjs/assets";
import { Pile, Scene } from "@apricotjs/react-native";
import type { ReactNode } from "react";
import { StyleSheet, View } from "react-native";
export function Overlay({ children }: { children: ReactNode }) {
return (
<View style={{ flex: 1 }}>
{children}
<Scene transparent pointerEvents="none" style={StyleSheet.absoluteFill} camera={{ position: [0, 0, 20], fov: 30 }}>
<Pile count={40}>
<Apple />
</Pile>
</Scene>
</View>
);
}Background colours are exact#
background and fog colours appear exactly as you give them, so a scene can match the UI around it. Without background, the scene uses its look's colour.
look |
Background |
|---|---|
soft |
#f6efe6 |
studio |
#eceff3 |
flat |
#ffffff |
The scene object#
useScene() (inside the scene) and onCreated / onReady give you the ApricotScene:
| Member | What it is |
|---|---|
invalidate() |
Requests a frame (needed after changing objects outside useFrame). |
camera |
The three.js PerspectiveCamera. |
scene |
The three.js Scene (the root of your objects). |
size |
Layout size in points, { width, height }. |
onResize(listener) |
Calls listener after every resize. Returns a remover. |
ready, onReady(listener) |
Whether the first content frame has drawn, and a way to subscribe to it. |
report(extra?) |
A JSON performance report (frame times, slowest frames, GPU details). |
threeRenderer |
The three.js renderer, once attached. |
Related#
- Scene reference: every prop with its default.
- Animation and shared values: the ways to animate.
- Performance: keeping scenes fast.