The scene tree#
Components inside <Scene> form a tree, like views. Each object is attached to its closest parent object, and its position, rotation and scale are relative to that parent.
import { Box, Group, Scene, Sphere } from "@apricotjs/react-native";
export function Tree() {
return (
<Scene style={{ flex: 1 }}>
<Group position={[0, -0.5, 0]} rotation={[0, Math.PI / 6, 0]}>
<Box size={[2, 0.2, 1]} />
<Sphere radius={0.3} position={[0, 0.45, 0]} material="chrome" />
</Group>
</Scene>
);
}These components are objects and accept children: Group, Mesh and every shape, Model, Primitive, Vehicle, FollowPath, and behaviors such as Spin.
Ordinary React components can sit anywhere in the tree. An object inside them still attaches to the closest object above it.
Refs: SceneObject#
A ref on an object holds a SceneObject, the underlying three.js Object3D. Use it to read or change the object directly, or to hand it to a component that follows it.
import { Box, FollowCamera, Group, Scene, type SceneObject, useFrame } from "@apricotjs/react-native";
import { type RefObject, useRef } from "react";
function Runner({ object }: { object: RefObject<SceneObject | null> }) {
useFrame(({ time }) => {
if (!object.current) return true;
object.current.position.z = -time * 2; // runs away from the camera
return true;
});
return (
<Group ref={object}>
<Box size={[0.5, 1, 0.5]} position={[0, 0.5, 0]} />
</Group>
);
}
export function Refs() {
const runner = useRef<SceneObject>(null);
return (
<Scene style={{ flex: 1 }}>
<Runner object={runner} />
{/* Must come after Runner to read this frame's position */}
<FollowCamera target={runner} />
</Scene>
);
}What you can do with a SceneObject:
| Use | How |
|---|---|
| Move, turn, scale | ref.current.position.set(x, y, z), .rotation.y = a, .scale.setScalar(s) |
| Hide | ref.current.visible = false |
| Read where it is in the world | ref.current.getWorldPosition(vector) |
| Follow it with the camera | <FollowCamera target={ref}>, <CameraRig follow={ref}> |
| Pin a label to it | <Label attach={ref}> |
<Instances> refs are different: they hold an InstancesHandle with group and set(index, item). <CameraRig> refs hold a CameraRigHandle with flyTo and jumpTo.
Groups#
<Group> is an empty object that transforms its children together. Use it to:
- place several shapes as one object;
- apply a second motion on top of another (a swipe
rotationinside a<Spin>); - set a pivot point: put the child off-centre inside a group, then rotate the group.
import { Box, Group, Scene, Spin } from "@apricotjs/react-native";
export function Pivot() {
return (
<Scene style={{ flex: 1 }}>
<Spin speed={1} axis={[0, 0, 1]}>
{/* turns around the arm's left end */}
<Group>
<Box size={[2, 0.2, 0.2]} position={[1, 0, 0]} />
</Group>
</Spin>
</Scene>
);
}Visibility, names and order#
visible={false}on a shape hides it and keeps its material, without unmounting it.namesets the object's name, for model selectors and debugging.- Unmounting a component removes its object and frees what it created.
- Depth decides what is in front. The order in JSX does not matter.
Who frees what#
| Thing | Created by | Freed when |
|---|---|---|
Shape geometry (<Box size>…) |
Apricot, shared by every shape of the same kind and size | The last shape using it unmounts or changes size |
Preset materials (material="chrome") |
Apricot, shared between identical uses | The last component using it unmounts |
A geometry you pass to <Mesh> or <Instances> |
You | Never by Apricot: dispose it yourself (geometry.dispose() in an effect cleanup) |
A three.js material you pass |
You | Never by Apricot |
Models (<Model src>) and textures (useTexture) |
Apricot, cached per app | The last user unmounts |
Objects you create with @apricotjs/assets factories (createPeach()…) |
You | Call disposeObject(object) from @apricotjs/core |
Escape hatch: three.js objects#
<Primitive object={…}> adds any existing three.js Object3D to the scene, with the usual position, rotation, scale and children. Use it for objects built by @apricotjs/assets factories or your own three.js code. See Custom geometry and behaviors.
Related#
- Objects reference:
Group,Primitive,Meshand shapes. - Coordinates and units.