import { Slab, smoothOutline, Terrain, useShore, useWaves, Water } from "@apricotjs/react-native";
import type { HeightAt, Outline, ShoreField, SlabProps, TerrainProps, WaterProps, WaveOptions, Waves } from "@apricotjs/react-native";All distances are metres. Shapes on the ground are outlines in the x–z plane.
Types#
| Type | Definition |
|---|---|
Outline |
readonly (readonly [x: number, z: number])[]: the corners of a shape in order. The last corner joins the first. Any shape works, bays included |
HeightAt |
(x: number, z: number) => number: the ground's height at a point |
smoothOutline(outline, segments = 96)#
Returns a smooth closed curve through the corners of outline, as segments points. Use it to draw an island's shore from a handful of points.
Slab#
Flat ground in any shape, such as an island, a lawn or a deck round a pool. It stands up from its position, so its top is at position.y + height.
| Prop | Type | Default | Description |
|---|---|---|---|
outline |
Outline |
required | Its shape on the ground |
height |
metres | 0.2 |
Thickness |
holes |
Outline[] |
none | Shapes cut through it |
bevel |
metres | 0.03 |
Rounding of its edges |
Plus every shape prop: material, color, onPress, name, visible, shadows, transforms, ref (Objects). Changing the outline rebuilds it.
Terrain#
A rectangular tile of ground with a height and a colour at each point. Its edges are cut straight down, so its sides show.
| Prop | Type | Default | Description |
|---|---|---|---|
size |
[width, depth] |
required | Along x and z, centred on its position |
height |
HeightAt |
required | The ground's height at each point |
color |
(x, z, y) => colour |
white | The top's colour at each point |
sides |
(y) => colour |
the top's edge colour | The sides' colour at each height, for layers |
spacing |
metres | 0.1 |
Distance between its points |
base |
metres | 0.2 below its lowest point | How far down its sides reach |
roughness |
0–1 | 0.9 |
0 is polished, 1 is rough |
water |
object | none | Water in it up to level. See below |
name |
string |
The object's name | |
ref |
Ref<SceneObject> |
The terrain's scene object |
Plus transform and shadow props.
water:
| Field | Type | Default | Description |
|---|---|---|---|
level |
metres | required | Height of the water's surface |
top |
colour | #7fe3e0 |
The water's colour at the surface, seen in the sides |
deep |
colour | #1a7fc0 |
The colour further down |
fade |
metres | 1 |
Depth at which it is all deep |
clarity |
0–1 | 0.25 |
How much of what is behind shows through |
water only draws the water's depth in the sides. For the surface, add a <Water> of the same size at level.
Water#
A water surface, such as the sea, a pool or a pond. It always lies level, so it takes position but no rotation. Its scale prop sizes the shore details and is not a transform.
| Prop | Type | Default | Description |
|---|---|---|---|
size |
[width, depth] |
required | Centred on its position |
position |
Vec3 or shared value |
[0, 0, 0] |
Its middle. y is the water level |
waves |
Waves |
still | From useWaves. Share it with what floats on the water |
shore |
ShoreField |
none | From useShore: shallows, deep water and foam |
shallow |
colour | bright turquoise | Over the shallows, or looked into from above |
deep |
colour | azure blue | Out at sea, or seen at a slant |
shoreline |
colour | pale aqua (#b9f3e6) |
Where it meets the land (with shore) |
foam |
colour or false |
white | Foam along the shore |
caustics |
0–1 | 0.6 |
Light dancing on the bottom of the shallows (with shore) |
glow |
number | 0.35 |
How much it glows with its own colour |
clarity |
0–1 | 0 |
How much you see through it |
sparkle |
0–1 | 0.5 |
A pale glint on the crests |
scale |
number | 1 |
Size of the shore details (with shore). The foam line is about 13 cm wide at 1. Use 1 for a pool or small beach seen close up, 5 or more for a lake or coast seen from afar. Changing it rebuilds the water's material, so don't animate it |
spacing |
metres | 0.25 |
Distance between its points. Smaller values carry shorter waves |
ref |
Ref<SceneObject> |
The water's scene object |
While it has waves, the scene keeps rendering.
useWaves(options?)#
Returns a Waves object. It keeps the same object and updates it when the options change.
WaveOptions:
| Option | Type | Default | Description |
|---|---|---|---|
height |
metres | 0.06 |
Crest height above the middle. 0 is still water |
length |
metres | 2.5 |
Crest to crest |
speed |
metres per second | 0.8 |
|
direction |
degrees | 0 |
Round the vertical. 0 travels towards +x |
Waves methods, for your own floating objects in useFrame:
| Method | Returns |
|---|---|
heightAt(x, z) |
The water's height above its level at that point, now |
slopeAt(x, z) |
[alongX, alongZ]: how steeply it rises there. Use it to tilt what floats |
set(options) |
Changes the waves. Options left out (or undefined) keep their values |
useShore(land, bounds, options?)#
Measures how far each point in bounds is from land, and measures again when the inputs change. Returns a ShoreField for <Water shore>.
| Argument | Type | Default | Description |
|---|---|---|---|
land |
Outline[] or (x, z) => boolean |
required | The land's outlines, or a test that returns true on land. Keep a test function stable |
bounds |
[minX, minZ, maxX, maxZ] |
required | The area measured. Outside it, water counts as far from shore |
options.reach |
metres | 4 |
How far out the water deepens from the shore |
options.size |
texels | 128 |
The field's resolution |
ShoreField: { texture, bounds, reach }.
Placement on ground and water#
<Placement areas zones snaps> and <Placeable on="water">: see Placement reference.
Related#
- Ground and water guide.
- Core reference:
createTerrainGeometry,createWaterMaterial,createShoreField,createSlabGeometry, without React.