import { Scene } from "@apricotjs/react-native";<Scene> is a React Native view that draws a 3D scene with its own camera and lights. Every other Apricot component goes inside one. It also accepts every ViewProps prop (style, pointerEvents, onLayout, testID…).
<Scene style={{ flex: 1 }} look="studio" camera={{ position: [0, 1, 5] }} shadows>
{/* shapes, models, lights, cameras */}
</Scene>Props#
| Prop | Type | Default | Description |
|---|---|---|---|
frameloop |
"demand" | "always" |
"demand" |
"demand" renders only when something changes; "always" renders every frame. Can change at any time |
look |
"soft" | "studio" | "flat" | "none" |
"soft" |
Lighting preset. "none" adds no lights and no background colour |
camera |
CameraOptions |
see below | Camera position, target, field of view and clipping |
fog |
boolean | { near?, far?, color? } |
none | Fades distant objects into the background. true is 30 to 90 units, in the background colour (white if there is none) |
environment |
string | StudioOptions |
"none" |
What metals and glossy materials reflect: "studio", "soft", "sunset", "night", a name from defineEnvironment, or a studio object |
transparent |
boolean |
false |
Transparent canvas over the views behind it, for overlays. Combine with pointerEvents="none" to let touches through |
background |
colour | the look's | Background colour, shown exactly as given. Can change at any time (day and night) |
toneMapping |
"agx" | "neutral" | "filmic" | "none" |
the look's ("agx") |
How light becomes screen colour. "neutral" keeps colours closer to as written (pastels stay rich); "filmic" is more contrasty |
exposure |
number |
1 |
Brightens (above 1) or darkens (below 1) the whole picture |
linger |
number (seconds) |
0.25 |
With "demand", keep rendering this long after the last change, so drags and sensors animate smoothly |
shadows |
boolean | ShadowOptions |
false |
Shadows from the main directional light |
occlusion |
boolean | OcclusionOptions |
false |
Ambient occlusion: soft darkening where surfaces meet or come close (room corners, under a sofa) |
lamps |
number |
0 |
How many <Lamp>s may light the scene at once. 0: lamps only glow |
lighting |
"standard" | "dynamic" |
"standard" |
With "dynamic", each lamp costs GPU time only while on, so lamps can be large |
maxPixelRatio |
number |
2 |
Caps the device pixel ratio. Lower it to trade sharpness for speed |
paused |
boolean |
false |
Stops drawing and keeps the last frame until false again. Use it for a scene that is mounted but not seen (preloaded, under another screen, on another carousel page). A paused scene still loads its content, so it shows at once. Can change at any time |
debug |
boolean |
false |
Shows fps, render and present time (and GPU time where the device can measure it), and a Share button for the performance report |
loading |
ReactNode |
none | Shown over the scene until it first draws its content |
onCreated |
(scene: ApricotScene) => void |
Called once the renderer is ready | |
onReady |
(scene: ApricotScene) => void |
Called once the scene first draws its content | |
onError |
(error: Error) => void |
logs | Called if the scene fails to start, for example with [apricot] WebGPU isn't available on this device (see On the web) |
android |
AndroidCanvasProps |
Passed to react-native-webgpu's canvas on Android | |
children |
ReactNode |
The scene's content |
camera: CameraOptions#
| Field | Type | Default |
|---|---|---|
position |
[x, y, z] |
[0, 0, 6] |
target |
[x, y, z]: the point looked at |
[0, 0, 0] |
fov |
degrees, vertical | 50 |
near |
units | 0.1 |
far |
units. Raise it for big scenes | 200 |
A camera component (<FitCamera>, <CameraRig>, <FollowCamera>) takes over after mount. See Cameras.
look#
| Look | Background | Lights | For |
|---|---|---|---|
soft |
#f6efe6 |
Warm hemisphere, warm key, cool fill | Toys, emoji-style objects |
studio |
#eceff3 |
Neutral hemisphere, key and rim | Product viewers |
flat |
#ffffff |
Even ambient light, no tone mapping | Cartoon or UI-like objects |
none |
black, unless you pass background |
None | Lighting it yourself: <Environment>, <Daylight> or light components |
Shiny objects (metal, chrome, glass) look best with look="none" and an environment.
shadows: ShadowOptions#
| Field | Default | Description |
|---|---|---|
size |
1024 |
Shadow map resolution in texels. 2048 is sharper and costs four times the memory |
softness |
2 |
Edge softness, in texels. With blur, how far edges are blurred (for example 8) |
blur |
false |
Wide, blurred edges, as under a room's soft light or an overcast sky |
darkness |
0.85 |
How dark shadows are, 0 to 1 |
radius |
fits all casters | Half the width of the shadowed area, centred on what the camera looks at. Set it for big scenes |
The shadows come from the look's key light, <Daylight>'s sun, or a <DirectionalLight castShadow>.
Everything casts and receives shadows except floors (which only receive) and see-through objects. castShadow and receiveShadow on an object override that. shadowOnly hides an object but keeps its shadow (a roof over a cut-away room). Things being moved by hand in a <Placement> cast no shadow until they stop.
occlusion: OcclusionOptions#
| Field | Default | Description |
|---|---|---|
radius |
0.3 |
How far, in metres, a surface darkens what is near it |
strength |
0.9 |
How dark it gets, 0 to 1 |
resolution |
0.5 |
Resolution as a share of the screen's. 0.5 is a quarter of the pixels |
samples |
16 |
Directions sampled round each point. Fewer is faster and noisier |
Occlusion adds GPU work to every frame. Devices that can't do it draw the scene without it.
lamps and lighting#
lamps sets how many <Lamp>s may light the scene at once. Lamps can be switched, added and carried at any time without a pause. When more lamps are on than lamps allows, the brightest light the scene and the others only glow. Each light costs a little GPU time on everything it lights, so keep lamps small.
With lighting="dynamic", a lamp costs GPU time only while it is on, so lamps can be large (16 or more). Each light costs a little more than with "standard", so use "dynamic" for many lamps and "standard" for a few. See Lighting a room.
Lifecycle#
- The view lays out and the GPU starts (the canvas is blank).
onCreatedfires. - The scene prepares everything in it, including objects out of view. Frames show only the background, for up to 12 s.
- The scene draws its content.
onReadyfires andloadingis removed.
Children render after onCreated, so hooks inside them (useScene, useFrame) always have a scene.
Several scenes, and scenes off screen#
Any number of scenes can be mounted at once (a carousel of rooms, a scene under a modal). Scenes whose lighting="dynamic" and lamps, kind of shadows (blurred, sharp or none) and debug match show what another scene has already drawn with little or no wait. look doesn't affect this. In a browser drawing with WebGL 2, each scene has a renderer of its own and compiles its shaders itself.
- Pause scenes that aren't seen with
paused. They stop drawing, keep their last frame and show at once when un-paused. - Don't hide a scene by fading its view to opacity 0. Such a view still draws, and on iOS it can hold up the app. To fade a scene in, start from 2% opacity.
- Scenes pause by themselves while the app is in the background.
- Scenes that start together load in turn, the visible ones first. A paused scene that hasn't been shown yet loads after the visible ones have drawn, a little at a time.
- A scene that unmounts stops drawing at once.
import { Box, Scene } from "@apricotjs/react-native";
import { useState } from "react";
import { FlatList, useWindowDimensions } from "react-native";
const ROOMS = ["#ee9c9b", "#97ba80", "#a2bbcc"];
export function Carousel() {
const { width } = useWindowDimensions();
const [page, setPage] = useState(0);
return (
<FlatList
horizontal
pagingEnabled
data={ROOMS}
keyExtractor={(color) => color}
onMomentumScrollEnd={(e) => setPage(Math.round(e.nativeEvent.contentOffset.x / width))}
renderItem={({ item, index }) => (
<Scene style={{ width, height: 400 }} paused={index !== page}>
<Box radius={0.1} color={item} />
</Scene>
)}
/>
);
}Taps#
A tap is one finger lifted within 10 points and 500 ms of where it went down. The nearest object under the finger with an onPress (or onPartPress, or <Instances onPress>) receives it. A tap on nothing that listens puts down whatever a <Placement> has picked up. Gesture handlers around the scene keep receiving their events.
The scene takes only touches that start on it. Drags go to <CameraRig orbit>, a <Placement>, or your own useSceneTouches listener. A listener can claim a whole gesture, so a finger on a picked-up object moves the object and the camera stays still.
gpuSupport()#
import { gpuSupport, type GpuSupport } from "@apricotjs/react-native";gpuSupport(): Promise<GpuSupport> resolves to what scenes draw with here. Call it once, before showing scenes, to show something else where they can't draw.
| Value | Meaning |
|---|---|
"webgpu" |
Scenes draw with WebGPU |
"webgl" |
A browser without a WebGPU adapter: scenes draw with WebGL 2 |
"none" |
Neither: scenes fail with [apricot] WebGPU isn't available on this device. On iOS and Android, a device without a WebGPU adapter |
ApricotScene#
useScene() (inside the scene), onCreated and onReady give the ApricotScene:
| Member | Description |
|---|---|
invalidate() |
Requests a frame. Needed in "demand" mode after changing three.js objects outside useFrame |
camera |
The three.js PerspectiveCamera |
scene |
The three.js Scene root |
size |
{ width, height } in points |
onResize(listener) |
Calls listener(size) after every resize. Returns the remover |
onFrame(callback) |
Like useFrame, outside React. Returns the remover |
ready |
true once the scene draws its content |
onReady(listener) |
Calls once when ready (at once if it already is). Returns the remover |
drawn({ timeout? }) |
Resolves once a frame shows everything just added (at most timeout ms, default 3000). Wait for it after a swap before fading in or taking a picture |
press(x, y) |
Simulates a tap at a point in the canvas. Returns the hit |
report(extra?) |
A performance report as plain JSON (frame times, GPU time with debug, slowest frames, events, contents, GPU errors) |
gpuMs |
The GPU time of the last measured frames in ms, or null (measured with debug) |
threeRenderer |
The three.js WebGPURenderer (with its WebGL 2 backend in a browser without WebGPU), for three.js code of your own |
diagnostics, stats |
Performance recorders used by debug and report |
Related#
- Scenes and the frame loop.
- Lighting.
- Hooks reference.
- Capture:
captureScene.