import { Box, Bundle, Capsule, Cone, Cylinder, Group, Merged, Mesh, Plane, Primitive, Slab, Sphere, Torus } from "@apricotjs/react-native";
import type { SceneObject, Vec3 } from "@apricotjs/react-native";Transform props#
Every object (shapes, Mesh, Group, Primitive, Instances, Model) takes:
| Prop | Type | Default | Description |
|---|---|---|---|
position |
Vec3 or shared value |
[0, 0, 0] |
Relative to the parent, in units (metres) |
rotation |
Vec3 or shared value |
[0, 0, 0] |
Euler angles in radians, XYZ order |
scale |
number | Vec3 or shared value |
1 |
One number for all axes, or per axis |
type Vec3 = readonly [number, number, number]. Shared values need Reanimated. They are read every frame without React renders. Type them with useDerivedValue<readonly [number, number, number]>(…).
Shared shape props#
Every shape and Mesh takes:
| Prop | Type | Default | Description |
|---|---|---|---|
material |
MaterialInput |
"standard" |
A preset name, { preset, ...options }, or a three.js material (not disposed for you). See Materials |
color |
colour | the preset's | Overrides the preset's colour. Changes animate |
onPress |
(hit: PressHit) => void |
Called when the object is tapped | |
name |
string |
"" |
The three.js object's name |
visible |
boolean |
true |
Hides the object (and its children) |
castShadow, receiveShadow |
boolean |
decided by the scene | Overrides with <Scene shadows> |
shadowOnly |
boolean |
false |
Hides the object but keeps its shadow. For a roof over a cut-away room, or something off screen whose shadow falls into view |
ref |
Ref<SceneObject> |
The three.js mesh | |
children |
ReactNode |
Nested objects, placed relative to this one |
PressHit: { mesh, material, materialIndex, point, distance, instanceId }. point is the world-space Vector3 hit; distance is from the camera.
Shapes#
All shapes are centred on their position.
| Component | Props (default) | Orientation |
|---|---|---|
Box |
size number or [w, h, d] (1), radius edge rounding (0) |
Axis-aligned |
Sphere |
radius (0.5), segments (32) |
|
Cylinder |
radius (0.5), radiusTop (= radius), height (1), segments (32) |
Stands along y |
Cone |
radius (0.5), height (1), segments (32) |
Points up (+y) |
Plane |
size number or [w, h] (1) |
Faces +z. For a floor: rotation={[-Math.PI / 2, 0, 0]} |
Torus |
radius centre to tube middle (0.5), tube thickness (0.15), segments (48) |
Ring in the xy plane (faces +z) |
Capsule |
radius (0.3), length straight middle (0.6), segments (16) |
Along y. Total height = length + 2 * radius |
Slab |
outline [x, z] corners (required), height (0.2), holes (none), bevel (0.03) |
Flat ground of any shape, standing up from its position (not centred). See Ground and water |
Changing a size prop rebuilds the shape. To animate size, animate scale.
Mesh#
| Prop | Type | Description |
|---|---|---|
geometry |
THREE.BufferGeometry (required) |
The shape. Not disposed by the mesh |
Plus the shared shape props and transform props. See Custom geometry.
Group#
An empty node that transforms its children together.
| Prop | Type | Description |
|---|---|---|
name |
string |
|
visible |
boolean |
Default true. A hidden group is still prepared with the scene, so showing it later is instant. Use it to keep items ready and show one at a time |
footprint |
boolean |
Default true. false: what is in it takes up no floor in a <Placeable> (a palm's fronds, a canopy). It still shows, and things can stand under it |
ref |
Ref<SceneObject> |
The three.js group, for <FollowCamera target>, <Label attach>, <CameraRig follow> |
children |
ReactNode |
Plus transform props.
Primitive#
Adds an existing three.js object as it is.
| Prop | Type | Description |
|---|---|---|
object |
THREE.Object3D (required) |
Any three.js object. Dispose it yourself (disposeObject) |
ref |
Ref<SceneObject> |
The same object |
children |
ReactNode |
Nested under the object |
Plus transform props. Transform props overwrite the object's own position, rotation and scale only when given.
Merged#
Draws the shapes inside as one mesh, so a piece of furniture made of twenty boxes and cylinders is one draw call.
| Prop | Type | Default | Description |
|---|---|---|---|
roughness |
0–1 | 0.7 |
How rough the merged surfaces are |
castShadow, receiveShadow, shadowOnly |
boolean |
automatic | As on shapes |
ref |
Ref<SceneObject> |
Its group | |
children |
ReactNode |
Shapes, groups, and anything else |
Plus transform props.
- Plain, opaque, coloured shapes are baked in. Glowing (
light), see-through (glass), metal (metalnessabove 0.1), clearcoated and textured ones stay as they are, drawn separately. - It bakes again when it re-renders with different shapes. A shape inside that animates on its own does not make it bake again, so use it for things that keep their shape.
- Taps on any baked part reach whatever holds the
<Merged>(a<Placeable>), not the shapes' ownonPress.
<Merged>
<Box size={[1.6, 0.25, 0.75]} position={[0, 0.2, 0]} color="#e8939b" />
<Box size={[1.6, 0.5, 0.18]} position={[0, 0.55, -0.3]} color="#e8939b" />
</Merged>Bundle#
Cuts the CPU time per frame of what is inside it, most of all on Android. Use it for a furnished room or another group of things that stays in one place.
| Prop | Type | Description |
|---|---|---|
ref |
Ref<SceneObject> |
Its group |
children |
ReactNode |
What to record |
Plus transform props.
- Things inside may still move and turn (a piece being dragged). Adding, removing or hiding something, or changing its shape or material, makes the bundle update once.
- Everything inside is drawn even when out of view. Keep a bundle to one place (a room, an island) and don't put a whole city in one.
- See-through things (glass,
<LightBeam>) can go inside or outside it. Outside, they are drawn after it. - Keep
<Particles>outside a bundle.
<Bundle>
<Room />
<Placement>…</Placement>
</Bundle>SceneObject#
type SceneObject = THREE.Object3D: what a ref on Box, Group, Model, Vehicle, FollowPath or a light holds. Read or change its position, rotation, scale and visible directly, then call useScene().invalidate() if you changed it outside useFrame.
const box = useRef<SceneObject>(null);
<Box ref={box} />