ts
import { Instances } from "@apricotjs/react-native";
import type { InstanceItem, InstancePressEvent, InstancesHandle, InstancesProps } from "@apricotjs/react-native";Draws many copies of one shape in one draw call per part. Use it for anything repeated more than a few dozen times.
Props#
| Prop | Type | Default | Description |
|---|---|---|---|
items |
T[] where T extends InstanceItem |
required | The copies. Extra fields of your own come back in onPress |
geometry |
THREE.BufferGeometry |
The shape to copy | |
object |
THREE.Object3D | null |
An object whose meshes are copied, such as useModel(src).object |
|
children |
shapes | Shapes copied together as one. Their positions place them within each copy | |
material |
MaterialInput |
"standard" |
For every part. With object or children, parts keep their own materials unless this or color is given |
color |
colour | white when items have colours | The material's colour |
onPress |
(event: InstancePressEvent<T>) => void |
Called when a copy is tapped | |
name |
string | The group's name | |
visible |
boolean | true |
Hides every copy |
castShadow, receiveShadow |
boolean | automatic | With <Scene shadows> |
shadowOnly |
boolean | false |
Draws only the copies' shadows |
ref |
Ref<InstancesHandle> |
For per-frame updates |
Plus transform props, which move all copies together. Give exactly one of geometry, object or children.
InstanceItem#
| Field | Type | Default | Description |
|---|---|---|---|
position |
Vec3 |
[0, 0, 0] |
|
rotation |
Vec3 |
[0, 0, 0] |
Radians |
scale |
number | Vec3 |
1 |
|
color |
colour | none | Tints this copy. Multiplies the material's colour |
visible |
boolean | true |
A new items array updates every copy on the next frame. Passing more items than before adds copies. Pass a new array for selections and state changes, and use the handle for animation.
InstancePressEvent<T>#
{ index, item, hit }: the copy's index in items, the item with your own fields, and the PressHit.
InstancesHandle#
| Member | Description |
|---|---|
group |
The three.js group holding the copies |
set(index, item) |
Changes one copy immediately, without a React render. Call it from useFrame |