These rules keep a scene at 60 fps on an iPhone 12 or Pixel 6a class phone, and at 120 fps on high-refresh screens.
The rules#
Pause scenes that aren't seen#
Set <Scene paused> on a scene kept under another screen or on a carousel page that is off screen. It keeps its last frame and uses no time. Scenes pause by themselves while the app is in the background.
Let idle scenes sleep#
Keep frameloop="demand", the default. Return false from useFrame when an animation ends. A scene with nothing changing does not render.
Avoid React renders per frame#
Animate with shared values, useFrame or behaviors (Animation). Keep components that re-render often, such as a countdown or a status line, out of the component that renders the scene.
Cut draw calls#
Each separate mesh costs about 80 µs per frame on a mid-range Android phone, so 200 separate meshes fill a whole 60 fps frame. Use <Instances> for repeated things (seats, trees, posts), <Merged> for one thing built from many shapes (a sofa of twenty boxes) and <Bundle> for a still place full of things (a furnished room). See Draw calls.
Share materials#
Identical presets are shared already: forty material="matte" pillars use one material. Avoid giving every object a slightly different custom material. Each distinct material takes tens of milliseconds to build on a phone.
Change colours instead of materials#
Changing color, roughness or metalness is cheap. Changing the preset, an image or the engraving builds a new material. Switching, adding or moving lamps from <Scene lamps> builds nothing new.
Keep models in budget#
A hero model on a mid-range phone should stay within about 150,000 triangles and 8 MB of textures, at most 2048 px per side. apricot-models warns when a model is heavier (Models). Several components showing the same model load it once.
Keep shadow casters few#
With shadows, each casting object is drawn a second time whenever the shadow map is redrawn. That happens only while something that casts moves. Turning the camera around a still scene does not redraw it. Instance repeated casters, and keep size at 1024 unless shadows look blocky. Things being moved by hand in a <Placement>, or bobbing on water, cast no shadow until they stop.
Measure in release builds on a real device#
Debug builds and simulators are several times slower.
Draw calls#
| Tool | For | What it saves |
|---|---|---|
<Instances> |
Many copies of one shape or model | One draw call per part, for any number of copies |
<Merged> |
One thing built from many plain shapes | One draw call for all its plain, opaque shapes (their colours baked in) |
<Bundle> |
A still place, such as a room or an island, with what is in it | Much less CPU time per frame, most of all on Android. Things inside can still move |
They combine. A furnished room of thirty pieces, each a <Merged>, all inside one <Bundle>, draws in a few dozen calls a frame.
Everything inside a <Bundle> is drawn even when it is off screen, so keep a bundle to one place. Keep <Particles> outside it. See Objects reference.
Measuring#
The debug HUD#
import { Box, Scene } from "@apricotjs/react-native";
export function Measure() {
return (
<Scene style={{ flex: 1 }} debug>
<Box />
</Scene>
);
}debug shows frames per second, render time and present time, refreshed twice a second. Where the device can measure it, it also shows the GPU's time per frame. While the scene sleeps, its fps drops to 0. The Share button opens the share sheet with the full performance report.
| High time | What to cut |
|---|---|
| CPU | Draw calls: use <Merged>, <Bundle>, <Instances> |
| GPU | Pixels and effects: maxPixelRatio (default 2), occlusion, shadow size, lamps |
The performance report#
scene.report() returns a JSON report. It holds:
- frame times, with GPU time when
debugis on; - the slowest frames and what happened in them (shader builds, shadow redraws, bundles recorded again, environment changes);
- scene contents (meshes, draw calls, triangles);
- GPU details and GPU errors.
Get the scene from useScene(), onCreated or onReady. shareDiagnostics(scene, extra?) opens the share sheet with the report, plus the fields in extra.
import { Box, Scene, shareDiagnostics } from "@apricotjs/react-native";
import type { ApricotScene } from "@apricotjs/react-native";
import { useRef } from "react";
import { Button, View } from "react-native";
export function Report() {
const scene = useRef<ApricotScene | null>(null);
return (
<View style={{ flex: 1 }}>
<Scene style={{ flex: 1 }} onCreated={(s) => (scene.current = s)}>
<Box />
</Scene>
<Button title="Share report" onPress={() => scene.current && shareDiagnostics(scene.current, { screen: "Report" })} />
</View>
);
}Budgets at a glance#
| Item | Comfortable on a mid-range phone |
|---|---|
| Separate meshes | Roughly 100. Beyond that, use <Instances> |
Copies in one <Instances> |
Thousands |
| One hero model | ~150,000 triangles, ≤ 8 MB textures, ≤ 2048 px per side |
| Distinct materials | A few dozen (each one takes time to build) |
Bodies in a <Pile> |
About 50 (a fraction of a millisecond per frame while falling, nothing once settled) |
Shadow map size |
1024 (2048 is sharper and uses four times the memory) |
| Lamps lighting at once | A few with lamps, many with lighting="dynamic" |
| Ambient occlusion | One per scene. It adds GPU time to every frame |