Taps#
Every shape, <Mesh>, <Model> (per part) and <Instances> (per copy) can be tapped. A tap is one finger lifted within 10 points and half a second of where it went down. The nearest object under the finger that listens gets it.
Shapes#
import { Scene, Sphere } from "@apricotjs/react-native";
import { useState } from "react";
export function TapShape() {
const [on, setOn] = useState(false);
return (
<Scene style={{ flex: 1 }}>
<Sphere material="light" color={on ? "#ffcc66" : "#444444"} onPress={() => setOn((v) => !v)} />
</Scene>
);
}onPress receives a PressHit:
| Field | Meaning |
|---|---|
point |
World-space point that was hit (a three.js Vector3) |
distance |
Distance from the camera |
mesh |
The mesh that was hit |
material, materialIndex |
The material of the face that was hit |
instanceId |
Which copy, for instanced meshes |
Model parts#
onPartPress names the part that was tapped, using your model kit's names. See Models.
import { Scene } from "@apricotjs/react-native";
import { defineModelKit, Model } from "@apricotjs/react-native/model";
import { useState } from "react";
const car = defineModelKit(require("./car.glb"), {
doorLeft: { select: "door_left", hinge: "front", axis: "y", range: [0, -65] },
});
export function TapDoor() {
const [open, setOpen] = useState(false);
return (
<Scene style={{ flex: 1 }} look="studio">
<Model kit={car} joints={{ doorLeft: open ? 1 : 0 }} onPartPress={({ part }) => part === "doorLeft" && setOpen((o) => !o)} />
</Scene>
);
}Copies#
<Instances onPress> tells you which copy was tapped: { index, item, hit }. See Instances.
Gestures with shared values#
For drags, swipes, pinches and flings, wrap the scene in a react-native-gesture-handler gesture and write shared values that the scene reads. Nothing re-renders, and the gesture runs on the UI thread.
These libraries are optional. Install them with npx expo install react-native-gesture-handler react-native-reanimated react-native-worklets.
import { Box, Group, Scene } from "@apricotjs/react-native";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
import { useDerivedValue, useSharedValue, withSpring } from "react-native-reanimated";
export function DragToTilt() {
const x = useSharedValue(0);
const y = useSharedValue(0);
const drag = Gesture.Pan()
.onChange((e) => {
x.value += e.changeY * 0.01;
y.value += e.changeX * 0.01;
})
.onEnd(() => {
x.value = withSpring(0);
y.value = withSpring(0);
});
const rotation = useDerivedValue<readonly [number, number, number]>(() => [x.value, y.value, 0]);
return (
<GestureDetector gesture={drag}>
<Scene style={{ flex: 1 }} look="studio" environment="studio">
<Group rotation={rotation}>
<Box size={[2, 1.2, 0.2]} radius={0.08} material="aluminium" />
</Group>
</Scene>
</GestureDetector>
);
}Ready-made versions: swipe-to-spin (fling with decay), card-tilt (drag plus the phone's tilt sensor), drive-joystick (a thumbstick).
Orbit the camera#
<CameraRig orbit> gives drag-to-turn and pinch-to-zoom without writing a gesture. See Camera.
Custom drags inside the scene: useSceneTouches#
useSceneTouches passes the fingers on the scene to a component inside it, for drags of your own. While it is active, the scene claims touches that start on it.
import { Box, Group, Scene, type SceneObject, useScene, useSceneTouches } from "@apricotjs/react-native";
import { useRef } from "react";
function Spinner() {
const scene = useScene();
const group = useRef<SceneObject>(null);
const last = useRef<number | null>(null);
useSceneTouches({
touch: (points) => {
const x = points[0].x;
if (last.current !== null && group.current) {
group.current.rotation.y += (x - last.current) * 0.01;
scene.invalidate();
}
last.current = x;
},
release: () => {
last.current = null;
return 0;
},
});
return (
<Group ref={group}>
<Box size={1.4} />
</Group>
);
}
export function DragToSpin() {
return (
<Scene style={{ flex: 1 }}>
<Spinner />
</Scene>
);
}A listener has these callbacks:
| Callback | Called |
|---|---|
touch(points, time) |
On every start and move. points are the fingers, in points from the scene's top-left |
release(time) |
When all fingers are lifted. Return how far they moved, in points, so taps can be told apart from drags |
capture(points, time) |
Optional. Return true to take the whole gesture from the other listeners, such as <CameraRig orbit> |
wheel(deltaY) |
Optional. The mouse wheel, on the web |
Pass false as the second argument to stop listening.
Picking things up and moving them#
For furniture, props or anything people arrange by hand, use <Placement> and <Placeable>. The user taps to pick something up, drags it, twists two fingers to turn it and taps empty space to put it down. Snapping, collisions and saved layouts are included. It works alongside <CameraRig orbit> and needs no gesture library. See Placement.
Buttons and UI over a scene#
Views placed over a scene keep their own touches, so buttons, sheets and toolbars work as usual. Lay out the scene with StyleSheet.absoluteFill and put UI on top, then frame the subject around it with <CameraRig inset>.
Overlays that let touches through#
To draw a scene over a screen without blocking it, set transparent and pointerEvents="none". Every touch then goes to the views below.
import { Coin } from "@apricotjs/assets";
import { Pile, Scene } from "@apricotjs/react-native";
import { StyleSheet } from "react-native";
export function CoinShower() {
return (
<Scene transparent pointerEvents="none" style={StyleSheet.absoluteFill} camera={{ position: [0, 0, 20], fov: 30 }}>
<Pile count={30} duration={3}>
<Coin radius={0.5} />
</Pile>
</Scene>
);
}Related#
- Animation and shared values.
- Camera.
- Hooks reference:
useSceneTouches.