import {
defineModelKit, download, forgetDownloads, isDownloaded, measureModel, Model, useModel, useTexture, useTextureState,
} from "@apricotjs/react-native/model";
import type { ClipInput, JointInput, JointPart, KitPart, ModelKit, PartPressEvent } from "@apricotjs/react-native/model";Everything on this page is imported from @apricotjs/react-native/model.
Model#
Shows a .glb file. Renders nothing, children included, until it has loaded.
| Prop | Type | Default | Description |
|---|---|---|---|
src |
number | string |
A bundled asset (require("./car.glb")) or a URL. Bundled assets need expo-asset and expo-file-system. A URL is downloaded once and kept on the phone, with expo-file-system. Or give kit |
|
kit |
ModelKit |
A model plus names for its parts (defineModelKit). Its names autocomplete in materials, joints and onPartPress |
|
materials |
{ [part or selector]: MaterialInput } |
Material per part: a preset name, a preset with options, or a three.js material. Colour changes animate | |
joints |
{ [joint or selector]: JointInput } |
Moving parts, 0 (rest) to 1 (open). Plain numbers animate; shared values are followed exactly | |
animations |
{ [clip name]: ClipInput } |
Blender animation clips: progress 0–1, "once" or "loop" |
|
morphs |
{ [shape key]: number or shared value } |
Shape keys, 0 (off) to 1 (full). Plain numbers animate over 0.4 s | |
onPartPress |
(event: PartPressEvent) => void |
A part was tapped | |
inspect |
boolean | false |
For development: tap parts to highlight them and see their names and materials |
ground |
boolean | false |
Stands the model on the floor: its lowest point at y = 0, measured once at rest |
castShadow, receiveShadow |
boolean | automatic | Overrides for every mesh, with <Scene shadows> |
shadowOnly |
boolean | false |
Not drawn: only its shadow shows |
ref |
Ref<SceneObject> |
The model's root group | |
onLoad |
(object: THREE.Group) => void |
Loaded (with keepPrevious, before it shows) |
|
onReady |
(object: THREE.Group, src) => void |
The model is on screen. Called again for each new src, with that src. Quick changes can skip some |
|
keepPrevious |
boolean |
true |
When src changes, the current model stays until the next is ready. Then they swap in one frame and the children move onto the new one. false: the current model goes at once |
onError |
(error: Error) => void |
logs | Failed to load |
children |
ReactNode |
Placed in the model's space; shown once it has loaded (a <CameraRig>, a <Label>) |
Plus transform props (position, rotation, scale; plain or shared values).
A file used by several <Model>s loads once. Materials, joints and animations apply to each <Model> on its own, rigged models included. materials also reaches parts that arrive later, such as a <Model> inside it once it loads. A materials key that matches nothing logs a warning once the model has parts to match. See Catalogs for swapping src.
defineModelKit(src, parts)#
const sedan = defineModelKit(require("./sedan.glb"), {
paint: "material:vernice",
wheels: "ruota_*",
doorLeft: { select: "porta_ant_sx", hinge: "front", axis: "y", range: [0, -65] },
});Returns a ModelKit<P> ({ src, parts }) where P is the union of your part names. A part (KitPart) is a selector or a JointPart.
Selectors (PartSelector)#
| Selector | Picks |
|---|---|
"wheel_*" |
Parts (nodes) or materials whose name matches. * any characters, ? one. Case-insensitive |
"node:Lid" |
Nodes with that name, and everything under them |
"material:car_paint*" |
Only material slots with that name (the name in the file, even after a preset replaced it) |
"tag:door" |
Nodes with the Blender custom property apricot set to door (userData.apricot in three.js) |
["glass", "window_*"] |
Any of several |
Find names with npx apricot-models inspect file.glb or <Model inspect>.
JointPart#
| Field | Type | Default | Description |
|---|---|---|---|
select |
PartSelector |
required | The part(s) that move together |
hinge |
words or Vec3 |
"center" |
Where it turns: an edge or corner of the part's bounds at rest (front, back, top, bottom, left, right, center, combined as "bottom-back"), or an exact point in the part's parent space |
axis |
"x" | "y" | "z" | Vec3 |
"y" |
Rotation or slide axis, in the parent's space |
range |
[at0, at1] degrees |
[0, 90] |
Angle at value 0 and at value 1 |
slide |
[at0, at1] units |
none | Slide along the axis instead of turning |
transition |
seconds | 0.6 |
How long a plain value change takes |
The model faces +z: front is +z, top is +y, left is +x.
JointInput#
A value (number or shared value), or an InlineJoint that describes the joint where it is used: { value, select?, hinge?, axis?, range?, slide?, transition? }. select defaults to the key.
<Model src={box} joints={{ "node:Lid": { value: open ? 1 : 0, hinge: "bottom-back", axis: "x", range: [0, -110] } }} />ClipInput#
number | SharedValue<number> | "once" | "loop". A number from 0 to 1 shows that point of the clip. Plain numbers animate there over 0.6 s. Shared values scrub exactly. "once" plays the clip once and "loop" plays it on repeat.
PartPressEvent#
| Field | Type | Description |
|---|---|---|
part |
P | undefined |
The kit name: a joint that includes the mesh, else the most specific kit name. undefined without a kit or for unnamed parts |
parts |
P[] |
Every kit name whose selector includes the mesh |
name |
string |
The tapped mesh's name in the file |
material |
string |
The tapped material's name in the file |
wearing |
string | undefined |
The preset it wears now, if replaced |
mesh |
THREE.Mesh |
The tapped mesh |
point |
THREE.Vector3 |
World-space point hit |
useModel(src)#
Loads a model without showing it. Returns { object, animations, error }. object is a THREE.Group, or null while loading. animations are its clips. object is this component's own copy, but its geometries and materials are shared: do not dispose them. The model is freed when its last user unmounts. Pass object to <Instances object> for many copies, or to <Primitive object>.
useTexture(src, options?) and useTextureState(src, options?)#
useTexture loads a PNG or JPEG (a require or a URL) and returns a THREE.Texture. It returns null while it loads and on error, which it logs. useTextureState returns { texture, error }. Textures are shared between components and freed when the last user unmounts.
TextureOptions#
| Option | Type | Default | Description |
|---|---|---|---|
colorSpace |
THREE.ColorSpace |
SRGBColorSpace |
NoColorSpace (from three) for data such as masks and normal maps |
flipY |
boolean | true |
Image rows top to bottom map to v = 1 … 0, as three expects |
repeat |
boolean | false |
Tile instead of clamping at the edges |
premultiplyAlpha |
boolean | true |
Keep colours premultiplied by alpha. Pass false only for your own three.js materials that expect straight colour |
Downloads#
| Function | Description |
|---|---|
download(urls, { onProgress? }) |
Returns Promise<void>. Downloads the files not on the phone yet and calls onProgress(done, total) after each. Rejects if one fails, keeping those already done. Needs expo-file-system. Downloads nothing on the web |
isDownloaded(url) |
boolean: whether the file is on the phone |
forgetDownloads(urls?) |
Removes those downloaded files, or all of them |
Any URL given to <Model src>, useModel or useTexture is also kept once loaded. Put a version in URLs (/v2/boat.glb): a URL is downloaded only once.
measureModel(src, options?)#
Returns a Promise<Extent> without showing the model: its extent round its origin in metres, { minX, maxX, minY, maxY, minZ, maxZ }, as <Model> would show it. Use it with <Placement>'s findFreePlace.
| Option | Type | Default | Description |
|---|---|---|---|
scale |
number | 1 |
The scale it will be shown at |
ground |
boolean | false |
Measure it standing on its lowest point, as <Model ground> |
Lower level#
| Export | Description |
|---|---|
loadModelBytes(src) |
Promise<ArrayBuffer> of a bundled asset or URL |
modelCache |
The shared ModelCache behind useModel and <Model> |
textureCache |
The shared cache behind useTexture |
ModelSource |
number | string |