@apricotjs/core is Apricot without React. @apricotjs/react-native re-exports the parts most apps need: Path, Trip, CAMERA_VIEWS, DAY, definePreset, defineEnvironment, createMaterial, PresetMaterial, smoothOutline and the types. Import from @apricotjs/core for the utilities below.
import { disposeObject, mergeColoredParts, seededRandom, setShadow } from "@apricotjs/core";Utilities#
| Export | Signature | Description |
|---|---|---|
gpuSupport |
() => Promise<"webgpu" | "webgl" | "none"> |
What scenes draw with here. Also exported from @apricotjs/react-native. See Scene reference |
seededRandom |
(seed: number) => () => number |
Returns a generator of pseudo-random numbers in [0, 1). The same seed gives the same sequence on every launch |
disposeObject |
(root: Object3D) => void |
Frees the geometries, materials and textures of root and everything under it. Call it on unmount for objects you built |
mergeColoredParts |
(parts: ColoredPart[]) => BufferGeometry |
Merges several geometries, each with a colour, into one geometry with vertex colours, drawn in one draw call. UVs are dropped |
setShadow |
(object, { cast?, receive?, only? } | undefined) => void |
Whether a three.js object of your own casts and receives shadows. only: true hides it but keeps its shadow. On a directional light, cast: true makes it the one that casts |
leaveOutOfFootprint |
(object, leave = true) => void |
Leaves an object out of <Placeable> footprints, such as a glow or fronds over the sand. false counts it again. Also exported from @apricotjs/react-native |
visibleBox |
(camera, depth = 0, z = 0) => Bounds |
The box of world space the camera sees, depth deep on each side of the plane at z. Use it to fill the screen |
fitDistance |
(camera, width, height, margin = 0.1) => number |
How far the camera must be for an area to fill the view |
fitCamera |
(camera, width, height, { margin, target }) |
Moves the camera along its line of sight so an area fits, as <FitCamera> does |
ColoredPart: { geometry, color, matrix? }, where color is a colour, a function of each vertex's position, or "vertex" to keep the geometry's own colours.
Bounds: { min: Vec3, max: Vec3 }.
Outlines and water#
| Export | Description |
|---|---|
insideOutline(x, z, outline) |
Whether a ground point is inside an outline (any shape) |
outlineArea(outline) |
The area an outline encloses, m² |
smoothOutline(outline, segments?) |
Corners rounded into a smooth closed curve |
Waves |
What useWaves returns: heightAt(x, z), slopeAt(x, z), set(options) |
Ground and water#
Build what <Terrain>, <Slab>, <Water> and useShore make in your own three.js code.
| Export | Description |
|---|---|
createTerrainGeometry(width, depth, heightAt, { spacing, base, color, sides }) |
A BufferGeometry of ground whose height at each point is heightAt(x, z) |
createSlabGeometry(outline, { height, holes, bevel }) |
A BufferGeometry of a flat slab in the shape of an outline, with holes, height thick |
createWaterGeometry(width, depth, spacing) |
The flat grid a water surface is drawn on |
createWaterMaterial(options) |
Takes the options <Water> takes, plus waves and shore. Returns { material, set }. set changes its colours |
createShoreField(land, bounds, { size, reach }) |
A ShoreField: how far each point of bounds is from the land. land is outlines or a function of x, z |
Daylight#
daylightAt(hour, keys = DAY, northOffset = 0, { sunrise, sunset }) returns a DaylightState for an hour. It holds the sun's direction, colours, intensities and dark, from 0 by day to 1 at night. northOffset is in radians, as <Daylight north>.
Use it in your own code to follow the time of day without a <Daylight>.
Constants#
| Export | Description |
|---|---|
CAMERA_VIEWS |
The built-in camera views (see Cameras) |
DAY |
The default DaylightKey[] for <Daylight> |
BURSTS, EMITTERS |
The ready-made particle bursts and streams (see Particles) |
@apricotjs/core/layout#
import { freeId, readLayout, writeLayout, type KeptLayout } from "@apricotjs/core/layout";Reads and writes layouts as text, without three.js. Use it on a server, in a migration script or for your own storage. The text format is the one useLayout uses.
| Export | Description |
|---|---|
writeLayout(items, version = 1) |
The layout as JSON text: { "version": 1, "items": [...] } |
readLayout(text, version = 1, migrate?) |
The items, or null if the text is empty, damaged, or another version that migrate can't bring up to date |
freeId(prefix, items) |
The first unused prefix-1, prefix-2… |
LayoutHistory |
Undo and redo history, as used by useLayout |
KeptLayout |
{ version, items }, as passed to migrate |
@apricotjs/core/models#
import { instance, parseModel } from "@apricotjs/core/models";Loads models without React.
| Export | Description |
|---|---|
parseModel(bytes) |
A Promise of { scene, animations } from .glb bytes. Prepare files with apricot-models first. Draco, meshopt and KTX2 aren't supported |
instance(model) |
A copy of a loaded model to add to a scene. Copies share geometry, materials and textures. Rigged models get their own skeleton |
Apps normally use <Model> and useModel from @apricotjs/react-native/model instead.