import { useAnimatable, useAttach, useFrame, useParent, useScene, useSceneTouches, useTransform } from "@apricotjs/react-native";Index#
| Hook | Where | Returns |
|---|---|---|
useScene() |
inside Scene | ApricotScene |
useFrame(callback) |
inside Scene | void |
useAnimatable(value) |
inside Scene | () => T |
useSceneTouches(listener, active?) |
inside Scene | void |
useParent() / useAttach(object) |
inside Scene | Object3D / void |
useTransform(object, props) |
inside Scene | void |
useMaterial, useMaterialInput, useMaterialOverrides |
inside Scene | see Materials |
useModel, useTexture, useTextureState |
anywhere | see Model |
usePath, useTrip, useTripStatus |
anywhere | see Motion |
useDaylight() |
inside Scene | () => DaylightNow | null: see Lights |
useLayout(initial, options) |
anywhere | Layout<T>: see Placement |
useBounds(object) |
inside Scene | ObjectBounds | null |
useWaves(options), useShore(land, bounds) |
anywhere | Waves, ShoreField: see Ground and water |
useScene#
Returns the closest scene's ApricotScene: invalidate(), camera, scene, size, onResize(), ready, onReady(), report(), threeRenderer. Throws outside <Scene>. See Scene reference.
Call invalidate() after changing three.js objects outside useFrame (in an event handler, a timer) when frameloop="demand".
useFrame#
useFrame((state: { time: number; delta: number; frame: number }) => boolean | undefined)Runs the callback before every rendered frame.
| Field | Description |
|---|---|
time |
Seconds since the loop started |
delta |
Seconds since the previous frame, capped at 0.1, so objects don't jump after a pause |
frame |
The number of frames rendered |
Return true while animating to keep the scene rendering. Return false or nothing to let it sleep until something else changes. You can pass a new callback on every render.
useAnimatable#
const read = useAnimatable(value); // value: T | SharedValue<T>
useFrame(() => { const now = read(); … });Returns a reader for a prop that is a plain value or a Reanimated shared value (including useDerivedValue). A change to the shared value, or a new plain value, wakes the scene. Use it to make your own components' props accept shared values, as Apricot's do. Without Reanimated installed, it reads plain values only.
type Animatable<T> = T | SharedLike<T>, where SharedLike<T> is anything with a readonly value: T (a Reanimated shared or derived value).
useSceneTouches#
useSceneTouches(listener: SceneTouchListener, active = true)Receives the fingers on the enclosing scene while active. The scene then claims touches that start on it, so drags no longer reach views behind it. Taps on objects keep working.
| Listener method | Description |
|---|---|
capture(points, time) |
Optional. Called when a gesture starts. Return true to take the whole gesture so no other listener gets it, for example to move a picked-up object instead of the camera. The first listener to claim it wins |
touch(points, time) |
Called on every start and move. points are the fingers down now, { x, y } in points from the scene's top-left. time is in ms |
release(time) |
Called when all fingers lift. Return how far they moved, in points (taps barely move) |
wheel(deltaY) |
Optional. Mouse wheel on the web. deltaY > 0 is away from the user |
useParent and useAttach#
useParent() returns the three.js object new children attach to: the scene root, or the closest <Group>, shape, model or behavior.
useAttach(object) adds object to that parent while the component is mounted, and renders a frame.
To make your object the parent of your component's children, provide it with ParentContext.
useTransform#
useTransform(object, { position, rotation, scale })Keeps a three.js object's transform in sync with transform props. Plain values are applied before the next frame after they change. Shared values are read before every frame. Unset values are left alone.
applyTransform(object, values) applies them once, outside React.
useBounds#
const bounds = useBounds(object, { measure? });Returns the box round an object (a group, a model, or a ref to one). Use it to size or place something by the object, such as a shadow under it or a label above it. It is null until there is something to measure.
The box is measured again when something is added to the object or removed from it (a model loading), when the component re-renders, and when measure changes.
ObjectBounds: { min, max, size, center }, each [x, y, z] in metres in the scene's space (size is width, height, depth).
Hidden objects and shadow-only objects are left out. To leave out something else (a shadow inside the object it is under), set object.userData[NO_BOUNDS] = true (NO_BOUNDS from @apricotjs/react-native).
const [piece, setPiece] = useState<SceneObject | null>(null);
const bounds = useBounds(piece);
<Group ref={setPiece}>…</Group>
{bounds && <Label position={[bounds.center[0], bounds.max[1] + 0.1, bounds.center[2]]}>…</Label>}Diagnostics#
| Export | Description |
|---|---|
<Scene debug> |
fps, render and present time, and a Share button |
DebugHud |
The overlay itself: <DebugHud stats={scene.stats} scene={scene} /> |
diagnosticsJson(scene, extra?) |
The scene's performance report with device details, as JSON text |
shareDiagnostics(scene, extra?) |
Opens the share sheet with that report (logs it where sharing is unavailable) |