This page builds one screen in six steps. Each step is complete code for an Expo screen. Complete Installation first.
1. A scene#
<Scene> is a React Native view that draws 3D. Give it a size like any view.
import { Scene } from "@apricotjs/react-native";
export default function Home() {
return <Scene style={{ flex: 1 }} />;
}An empty scene shows its background (a soft cream, from the default look="soft"). The camera sits at [0, 0, 6] and looks at the origin.
2. A shape with a material#
Shapes are centred on their position. Sizes are in scene units, and one unit is meant as one metre.
import { Box, Scene, Sphere } from "@apricotjs/react-native";
export default function Home() {
return (
<Scene style={{ flex: 1 }}>
<Box size={1.4} radius={0.15} position={[-1, 0, 0]} material="car-paint" color="#1f4fd1" />
<Sphere radius={0.7} position={[1.1, 0, 0]} material="chrome" />
</Scene>
);
}materialis a preset name. There are 16, such as"chrome","gold","glass","car-paint","rubber". See Shapes and materials.coloroverrides the preset's colour.
3. Better reflections#
Metals reflect their surroundings. environment="studio" adds a photo studio for them to reflect, with no image to load. look="none" removes the default lights, so the studio is the only light. Use this pair for shiny objects.
import { Box, Scene, Sphere } from "@apricotjs/react-native";
export default function Home() {
return (
<Scene style={{ flex: 1 }} look="none" environment="studio" background="#0e0f12">
<Box size={1.4} radius={0.15} position={[-1, 0, 0]} material="car-paint" color="#1f4fd1" />
<Sphere radius={0.7} position={[1.1, 0, 0]} material="chrome" />
</Scene>
);
}4. Motion#
Behaviors are components that move their children. <Spin> turns them around an axis, in radians per second.
import { Box, Scene, Spin } from "@apricotjs/react-native";
export default function Home() {
return (
<Scene style={{ flex: 1 }} look="none" environment="studio" background="#0e0f12">
<Spin speed={0.6}>
<Box size={1.4} radius={0.15} material="car-paint" color="#1f4fd1" />
</Spin>
</Scene>
);
}5. A swipe that spins it#
Animatable props (position, rotation, scale, speed…) take a plain value or a Reanimated shared value. A gesture that writes a shared value moves the object without re-rendering React. This step needs Reanimated and react-native-gesture-handler. Install them with npx expo install react-native-reanimated react-native-worklets react-native-gesture-handler.
import { Box, Group, Scene, Spin } from "@apricotjs/react-native";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
import { cancelAnimation, useDerivedValue, useSharedValue, withDecay } from "react-native-reanimated";
export default function Home() {
const angle = useSharedValue(0);
const rotation = useDerivedValue<readonly [number, number, number]>(() => [0, angle.value, 0]);
const swipe = Gesture.Pan()
.onBegin(() => cancelAnimation(angle))
.onChange((e) => {
angle.value += e.changeX * 0.012; // radians per point
})
.onEnd((e) => {
angle.value = withDecay({ velocity: e.velocityX * 0.012, deceleration: 0.997 });
});
return (
<GestureDetector gesture={swipe}>
<Scene style={{ flex: 1 }} look="none" environment="studio" background="#0e0f12">
<Spin speed={0.6}>
<Group rotation={rotation}>
<Box size={1.4} radius={0.15} material="car-paint" color="#1f4fd1" />
</Group>
</Spin>
</Scene>
</GestureDetector>
);
}<Spin> adds a steady turn and the <Group> adds the swipe on top. Nested motions add up. The swipe-to-spin recipe packages this pattern as a hook.
6. Taps, colour changes and a loading state#
Every shape takes onPress. When color changes, the material moves to the new colour over 0.4 s. loading shows your own view until the scene has drawn its first frame.
import { Box, Group, Scene, Spin } from "@apricotjs/react-native";
import { useState } from "react";
import { ActivityIndicator, Text, View } from "react-native";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
import { cancelAnimation, useDerivedValue, useSharedValue, withDecay } from "react-native-reanimated";
const COLORS = ["#1f4fd1", "#a8102a", "#e3a21a", "#2f6b3a"];
export default function Home() {
const [color, setColor] = useState(0);
const angle = useSharedValue(0);
const rotation = useDerivedValue<readonly [number, number, number]>(() => [0, angle.value, 0]);
const swipe = Gesture.Pan()
.onBegin(() => cancelAnimation(angle))
.onChange((e) => {
angle.value += e.changeX * 0.012;
})
.onEnd((e) => {
angle.value = withDecay({ velocity: e.velocityX * 0.012, deceleration: 0.997 });
});
return (
<View style={{ flex: 1, backgroundColor: "#0e0f12" }}>
<GestureDetector gesture={swipe}>
<Scene
style={{ flex: 1 }}
look="none"
environment="studio"
background="#0e0f12"
loading={<ActivityIndicator color="#ffffff" style={{ flex: 1 }} />}
>
<Spin speed={0.6}>
<Group rotation={rotation}>
<Box
size={1.4}
radius={0.15}
material="car-paint"
color={COLORS[color]}
onPress={() => setColor((c) => (c + 1) % COLORS.length)}
/>
</Group>
</Spin>
</Scene>
</GestureDetector>
<Text style={{ color: "#8b9099", textAlign: "center", padding: 24 }}>
Swipe to spin · tap to repaint
</Text>
</View>
);
}The four ideas behind it#
- Mount, unmount and choose things with React state. Animate with shared values,
useFrameor behaviors. Never set React state every frame. See Animation. - The scene renders only when something changes. Props, shared values and taps wake it. Code that changes objects directly calls
useScene().invalidate()or returnstruefromuseFrame. See Scenes. - Distances are metres. Object rotations are radians, and camera views are degrees. See Coordinates and units.
- Each separate mesh is one draw call. Use
<Instances>for many copies, and keep materials shared. See Performance.
Next#
- Core concepts explain each idea in depth.
- Guides cover one task each: models, cameras, lighting, gestures.
- Recipes are complete screens to copy.
- Placement lets people arrange furniture or props themselves.