roamwright 0.57.0 → 0.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/land.html +2 -1
- package/examples/land.js +92 -2
- package/geometry/creased-sheet.js +20 -0
- package/geometry/drystone-wall.js +143 -0
- package/geometry/farm-boundaries.js +158 -0
- package/geometry/farm-life.js +183 -0
- package/geometry/field-survey.js +461 -0
- package/geometry/irrigated-fields.js +308 -0
- package/geometry/land-trails.js +108 -0
- package/geometry/prop-plough.js +40 -0
- package/geometry/prop-stook.js +47 -0
- package/geometry/road-ribbon.js +99 -0
- package/geometry/shore-reeds.js +105 -0
- package/geometry/strata-talus.js +71 -0
- package/geometry/stream-banks.js +164 -0
- package/index.js +20 -0
- package/motion/road-traffic.js +207 -0
- package/package.json +1 -1
- package/rendering/farm-cells.js +88 -0
- package/rendering/irrigated-field-surface.js +294 -0
- package/rendering/road-ribbons.js +37 -0
- package/rendering/sea-shaders.js +30 -0
- package/rendering/strata-cliffs.js +333 -0
- package/rendering/strata-talus.js +58 -0
- package/rendering/stream-banks.js +76 -0
- package/rendering/water-material.js +32 -33
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { instancedMesh } from '../geometry/instances.js';
|
|
3
|
+
import { TRS } from '../geometry/primitives.js';
|
|
4
|
+
|
|
5
|
+
const _tint = new THREE.Color(); // scratch: each scattered copy's instance colour
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Put a farm's cells in the world (`planFieldBoundaries`'s): to each cell with wall stones, one instanced mesh of the one
|
|
9
|
+
* stone at every copy's place and tint, and the box its walls are drawn as from afar (sharing nothing but the cell); to each cell
|
|
10
|
+
* with drains, one mesh of their strips. The near meshes are drawn within `near` metres of the eye and the far ones beyond it,
|
|
11
|
+
* for a level of detail (`cover`); a line of field walls is culled against the tighter enclosure of its stones rather than the
|
|
12
|
+
* cell's.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} options
|
|
15
|
+
* @param {Map<string, { stones: number[], tints: number[], far: object, ditches: object }>} options.cells - The plan's cells.
|
|
16
|
+
* @param {import('three').BufferGeometry} options.stone - The one stone (`drystoneStone`).
|
|
17
|
+
* @param {import('three').Material} options.stoneMaterial - What draws the stones, with room for each copy's own colour.
|
|
18
|
+
* @param {import('three').Material} options.farMaterial - What draws a cell's walls from afar.
|
|
19
|
+
* @param {import('three').Material} options.ditchMaterial - What draws the drains' strips.
|
|
20
|
+
* @param {import('three').Object3D} options.parent - What the meshes join.
|
|
21
|
+
* @param {object[]} options.cover - The list a level of detail reads; a cell `{ mesh, lo, cx, cz, r, near }` is added to it.
|
|
22
|
+
* @param {number} [options.near=84] - Metres within which the stones are drawn and beyond which the far boxes are.
|
|
23
|
+
*/
|
|
24
|
+
export function meshFarmCells({ cells, stone, stoneMaterial, farMaterial, ditchMaterial, parent, cover, near = 84 }) {
|
|
25
|
+
const wallBounds = new THREE.Sphere(); // scratch: the tighter enclosure of a line of walls
|
|
26
|
+
for (const { stones, tints, far, ditches } of cells.values()) {
|
|
27
|
+
if (stones.length) {
|
|
28
|
+
const mesh = instancedMesh(stone, stoneMaterial, { matrices: new Float32Array(stones), colours: new Float32Array(tints) }),
|
|
29
|
+
lo = far.mesh(farMaterial, parent, false);
|
|
30
|
+
mesh.receiveShadow = true;
|
|
31
|
+
mesh.matrixAutoUpdate = false;
|
|
32
|
+
mesh.updateMatrix();
|
|
33
|
+
mesh.computeBoundingSphere();
|
|
34
|
+
parent.add(mesh);
|
|
35
|
+
const { center, radius } = mesh.boundingSphere;
|
|
36
|
+
cover.push({ mesh, lo, cx: center.x, cz: center.z, r: radius, near });
|
|
37
|
+
// The distance switch uses the original cell. Cull its stones against the tighter enclosure of a line of field walls.
|
|
38
|
+
mesh.computeBoundingBox();
|
|
39
|
+
mesh.boundingBox.getBoundingSphere(wallBounds);
|
|
40
|
+
if (wallBounds.radius < radius) mesh.boundingSphere.copy(wallBounds);
|
|
41
|
+
}
|
|
42
|
+
if (ditches.p.length) ditches.mesh(ditchMaterial, parent, false).layers.set(1);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Put the planned lots of a farm's seasonal life into the world (`planFarmLife`'s): to each lot, a near and a far
|
|
48
|
+
* instanced mesh sharing their matrices, colours and bounds, drawn in full within the lot's `near` metres and as its far copy
|
|
49
|
+
* beyond.
|
|
50
|
+
*
|
|
51
|
+
* @param {object} options
|
|
52
|
+
* @param {Map<string, { id: string, near: number, sites: number[][] }>} options.lots - Planned lots from `planFarmLife`.
|
|
53
|
+
* @param {Record<string, { near: import('three').BufferGeometry, far: import('three').BufferGeometry, name?: string }>} options.shapes -
|
|
54
|
+
* Near and far geometries for each lot `id` (with an optional mesh name, defaulting to `id`).
|
|
55
|
+
* @param {import('three').Material | ((id: string) => import('three').Material)} options.material - Shared material or factory per mesh.
|
|
56
|
+
* @param {(x: number, z: number) => number} options.ground - Terrain height at `(x, z)`, metres.
|
|
57
|
+
* @param {import('three').Object3D} options.parent - Group the meshes join.
|
|
58
|
+
* @param {object[]} options.cover - Level-of-detail list; each lot adds `{ mesh, lo, cx, cz, r, near }`.
|
|
59
|
+
* @param {number} [options.sink=0.02] - Metres each copy is sunk into the ground so sloped footings do not float.
|
|
60
|
+
* @param {number} [options.layer=1] - Render layer set on both meshes.
|
|
61
|
+
*/
|
|
62
|
+
export function scatterFarmLots({ lots, shapes, material, ground, parent, cover, sink = 0.02, layer = 1 }) {
|
|
63
|
+
const matOf = typeof material === 'function' ? material : () => material;
|
|
64
|
+
for (const { id, near, sites } of lots.values()) {
|
|
65
|
+
const shape = shapes[id],
|
|
66
|
+
mesh = new THREE.InstancedMesh(shape.near, matOf(id), sites.length),
|
|
67
|
+
lo = new THREE.InstancedMesh(shape.far, matOf(id), sites.length);
|
|
68
|
+
mesh.name = lo.name = shape.name ?? id;
|
|
69
|
+
for (let i = 0; i < sites.length; i++) {
|
|
70
|
+
const [x, z, yaw, size, tint] = sites[i];
|
|
71
|
+
mesh.setMatrixAt(i, TRS(x, ground(x, z) - sink, z, size, size, size, yaw));
|
|
72
|
+
mesh.setColorAt(i, _tint.setHex(tint));
|
|
73
|
+
}
|
|
74
|
+
mesh.computeBoundingSphere();
|
|
75
|
+
lo.instanceMatrix = mesh.instanceMatrix;
|
|
76
|
+
lo.instanceColor = mesh.instanceColor;
|
|
77
|
+
lo.boundingSphere = mesh.boundingSphere;
|
|
78
|
+
lo.visible = false;
|
|
79
|
+
mesh.castShadow = true;
|
|
80
|
+
mesh.receiveShadow = lo.receiveShadow = true;
|
|
81
|
+
mesh.matrixAutoUpdate = lo.matrixAutoUpdate = false;
|
|
82
|
+
mesh.layers.set(layer);
|
|
83
|
+
lo.layers.set(layer);
|
|
84
|
+
parent.add(mesh, lo);
|
|
85
|
+
const { center, radius } = mesh.boundingSphere;
|
|
86
|
+
cover.push({ mesh, lo, cx: center.x, cz: center.z, r: radius, near });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { SRGB2LIN } from '../math/color.js';
|
|
3
|
+
import { IRRIGATED_FIELDS } from '../geometry/irrigated-fields.js';
|
|
4
|
+
import { fieldSurfaceGLSL } from './field-surface.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The look of an irrigated field's ground, beyond {@link IRRIGATED_FIELDS}: a parcel's edge is soft over this many pixels, or
|
|
8
|
+
* this many metres where that is more; the metres a pixel spans across the parcels (and along them) from which they give way to
|
|
9
|
+
* the ground's own baked colour, and at which they are gone; the metres farther than any canal; the radius of the watered basin a
|
|
10
|
+
* palm stands in; and how many times the horizon's colour the sky a canal mirrors at a grazing look.
|
|
11
|
+
*/
|
|
12
|
+
export const IRRIGATED_FIELD_SURFACE = Object.freeze({
|
|
13
|
+
noCanal: 1e4,
|
|
14
|
+
basin: 1.5,
|
|
15
|
+
edgeSoft: [2.5, 0.12],
|
|
16
|
+
seenAcross: [4, 9],
|
|
17
|
+
seenAlong: [25, 55],
|
|
18
|
+
skyInWater: 1.7,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What each use of {@link IRRIGATED_FIELDS}'s `uses` is to the shared picture of a field (`fieldSurfaceGLSL`'s kinds: plough,
|
|
23
|
+
* grain, grass, fallow, orchard, yard, then sown and sprouting): green corn standing, garden beds and a date grove's floor are
|
|
24
|
+
* kinds of their own beyond it.
|
|
25
|
+
*/
|
|
26
|
+
export const IRRIGATED_FIELD_KIND = Object.freeze({ crop: 7, ripe: 1, tilled: 0, garden: 8, grove: 9, stubble: 1 });
|
|
27
|
+
|
|
28
|
+
const number = n => (Number.isInteger(n) ? n.toFixed(1) : String(n));
|
|
29
|
+
// An sRGB colour `k` times as bright, as a GLSL vec3 in linear light.
|
|
30
|
+
const flat = (rgb, k = 1) => `vec3(${rgb.map(c => (SRGB2LIN[c] * k).toFixed(4)).join(', ')})`;
|
|
31
|
+
|
|
32
|
+
const DEFAULT_IRRIGATED_TONES = Object.freeze({
|
|
33
|
+
crop: [92, 128, 64],
|
|
34
|
+
ripe: [186, 152, 86],
|
|
35
|
+
tilled: [112, 88, 64],
|
|
36
|
+
garden: [84, 118, 66],
|
|
37
|
+
grove: [122, 114, 82],
|
|
38
|
+
stubble: [176, 150, 102],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const defaultTone = (use, k) => flat(DEFAULT_IRRIGATED_TONES[use] ?? DEFAULT_IRRIGATED_TONES.crop, k);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The worked ground of an irrigated plain as a ground shader draws it: the parcels of `irrigated-fields.js` worked out again for
|
|
45
|
+
* each pixel from the same numbers (the cities' frames, the same hash and noise in whole numbers as the page has them), so a
|
|
46
|
+
* parcel's side is a line at any distance of the eye where a map of the fields over a land six kilometres across would hold a texel
|
|
47
|
+
* to several metres. Of the land's sheet it reads only how far a point could be worked and how far it lies from a river. Each
|
|
48
|
+
* parcel is painted by the shared picture of a field (`fieldSurfaceGLSL`) in the colours the caller gives, with the uses of its
|
|
49
|
+
* own, and over the parcels lie the dry bunds between them, the canals with their banks and weedy verges, and the feeders that
|
|
50
|
+
* fill them.
|
|
51
|
+
*
|
|
52
|
+
* @param {object} [options]
|
|
53
|
+
* @param {number} [options.cities=4] - The cities of one land: as many as the land with the most has (the shader's arrays).
|
|
54
|
+
* @param {number} [options.worldSeed=0] - The world's seed, which its hash adds to every sample (the shader's hash must match it).
|
|
55
|
+
* @param {number} [options.riverFields=600] - Metres from a river within which its fields are looked for (the slope map holds the
|
|
56
|
+
* distance as a share of it).
|
|
57
|
+
* @param {(use: string, k: number) => string} [options.tone] - A use's colour as the ground carries it from afar, `k` times as
|
|
58
|
+
* bright, as a GLSL `vec3` in linear light.
|
|
59
|
+
* @param {number[]} [options.earth] - The plain's dry earth, sRGB: the bunds are trodden pale, the dug bank dark.
|
|
60
|
+
* @param {number[]} [options.weeds] - The weeds along a canal's verge, sRGB.
|
|
61
|
+
* @param {number[]} [options.canal] - A canal's water, sRGB.
|
|
62
|
+
* @param {object} [options.look] - The shared field surface's palette and pitches (`fieldSurfaceGLSL`), a world's own.
|
|
63
|
+
* @param {Partial<typeof IRRIGATED_FIELDS>} [options.shape] - The fields' numbers, the same the page's `createIrrigatedFields`
|
|
64
|
+
* was given.
|
|
65
|
+
* @param {Partial<typeof IRRIGATED_FIELD_SURFACE>} [options.surface] - Changes to {@link IRRIGATED_FIELD_SURFACE}.
|
|
66
|
+
* @returns {{ pars: string, fields: string, water: string, paint: string, uniforms: Function }} `pars`: the declarations and
|
|
67
|
+
* helpers the fields need in the fragment shader's head; `fields`: the fields under a pixel, for the ground's `map_fragment`,
|
|
68
|
+
* which expects `landUv`, `slopes` (the slope map's texel), `spanX` and `spanY` (the ground the pixel spans to its right and
|
|
69
|
+
* upward, taken outside every branch), `cityAt`, the detail samples `d2` and `d3`, `fleck` and `grit`, and `gTH`; `water`: for
|
|
70
|
+
* once the normal and roughness are set, the sky in a canal's still water; `uniforms(frames, { windDrift, sky })`: the uniform
|
|
71
|
+
* values the cities' frames fill.
|
|
72
|
+
*/
|
|
73
|
+
export function createIrrigatedFieldSurface({
|
|
74
|
+
cities: CITIES = 4,
|
|
75
|
+
worldSeed: WORLD_SEED = 0,
|
|
76
|
+
riverFields: RIVER_FIELDS = 600,
|
|
77
|
+
tone = defaultTone,
|
|
78
|
+
earth = [178, 154, 118],
|
|
79
|
+
weeds = [108, 122, 68],
|
|
80
|
+
canal = [68, 92, 96],
|
|
81
|
+
look = {},
|
|
82
|
+
shape = {},
|
|
83
|
+
surface = {},
|
|
84
|
+
} = {}) {
|
|
85
|
+
const {
|
|
86
|
+
from: FROM,
|
|
87
|
+
reach: REACH,
|
|
88
|
+
wanderReach: WANDER_REACH,
|
|
89
|
+
wanderSpan: WANDER_SPAN,
|
|
90
|
+
wanderSeed: WANDER_SEED,
|
|
91
|
+
river: RIVER,
|
|
92
|
+
riverYaw: RIVER_YAW,
|
|
93
|
+
riverSeed: RIVER_SEED,
|
|
94
|
+
citySeed: CITY_SEED,
|
|
95
|
+
cell: CELL,
|
|
96
|
+
canalRun: CANAL_RUN,
|
|
97
|
+
keep: KEEP,
|
|
98
|
+
bend: BEND,
|
|
99
|
+
edge: EDGE,
|
|
100
|
+
net: NET,
|
|
101
|
+
bund: BUND,
|
|
102
|
+
water: WATER,
|
|
103
|
+
bank: BANK,
|
|
104
|
+
verge: VERGE,
|
|
105
|
+
palmPitch: PALM_PITCH,
|
|
106
|
+
palmRoom: PALM_ROOM,
|
|
107
|
+
bed: BED,
|
|
108
|
+
bedRun: BED_RUN,
|
|
109
|
+
furrow: FURROW,
|
|
110
|
+
bedBare: BED_BARE,
|
|
111
|
+
bedSeed: BED_SEED,
|
|
112
|
+
uses: USES,
|
|
113
|
+
progress: PROGRESS,
|
|
114
|
+
done: DONE,
|
|
115
|
+
} = { ...IRRIGATED_FIELDS, ...shape };
|
|
116
|
+
const {
|
|
117
|
+
noCanal: NO_CANAL,
|
|
118
|
+
basin: BASIN,
|
|
119
|
+
edgeSoft: EDGE_SOFT,
|
|
120
|
+
seenAcross: SEEN_A,
|
|
121
|
+
seenAlong: SEEN_B,
|
|
122
|
+
skyInWater: SKY_IN_WATER,
|
|
123
|
+
} = {
|
|
124
|
+
...IRRIGATED_FIELD_SURFACE,
|
|
125
|
+
...surface,
|
|
126
|
+
};
|
|
127
|
+
const f = number,
|
|
128
|
+
KIND = IRRIGATED_FIELD_KIND,
|
|
129
|
+
NOWHERE = 1e9; // a slot no city fills: its frame lies far off the land
|
|
130
|
+
|
|
131
|
+
// The uses of the plain's own, for the field surface shader: green corn standing, with the wind over it; a garden's beds, each
|
|
132
|
+
// run of a bed sown with one thing, a watered furrow between two beds; and the floor of a date grove, littered with dry fronds,
|
|
133
|
+
// each palm in the basin that is flooded for it.
|
|
134
|
+
const KINDS = `/* glsl */ else if (kind > 6.5 && kind < 7.5) {
|
|
135
|
+
float gust = smoothstep(0.8, 0.92, textureLod(map, (vWP.xz - uWindDrift * 0.8) * 0.013, max(0.0, log2((spanAlong + spanAcross) * 3.3))).r);
|
|
136
|
+
crop = mix(${tone('crop', 0.82)}, ${tone('crop', 1.14)}, tone) * (0.9 + 0.2 * gust) * (0.93 + 0.14 * lined) * (0.9 + 0.2 * patchy);
|
|
137
|
+
rough += 0.5 * speck;
|
|
138
|
+
} else if (kind > 7.5 && kind < 8.5) {
|
|
139
|
+
float bedAt = across / ${f(BED)} + tint * 7.0, bed = floor(bedAt), inBed = fract(bedAt), bedSpan = spanAcross / ${f(BED)};
|
|
140
|
+
float bedSeen = 1.0 - smoothstep(0.2, 0.55, bedSpan); // the beds while they can be told apart
|
|
141
|
+
float furrow = lineCover(min(inBed, 1.0 - inBed), ${f(FURROW)}, bedSpan) * bedSeen;
|
|
142
|
+
float sown = fieldHash(int(bed), int(floor(along / ${f(BED_RUN)} + fieldHash(int(bed), 0, fSeed + ${BED_SEED[0]}) * 7.0)), fSeed + ${BED_SEED[1]}); // the page's gardenBed
|
|
143
|
+
sown = mix(0.6, sown, bedSeen * (1.0 - smoothstep(1.5, 4.0, spanAlong)));
|
|
144
|
+
float plantAt = along * 2.3 + sin(across * 4.1) * 0.3, clumps = mix(0.55, smoothstep(0.2, 0.75, sin(plantAt * 6.2831853) * 0.5 + 0.5), 1.0 - smoothstep(0.09, 0.3, spanAlong * 2.3));
|
|
145
|
+
vec3 leaf = mix(${tone('garden', 0.62)}, ${tone('crop', 1.05)}, fract(sown * 7.3)) * mix(vec3(0.86, 1.0, 0.92), vec3(1.12, 1.04, 0.8), fract(sown * 3.1));
|
|
146
|
+
crop = mix(dirt * 1.25, leaf, step(${f(BED_BARE)}, sown) * clumps * (0.55 + 0.45 * patchy) * 0.92);
|
|
147
|
+
crop = mix(crop, dirt * 0.62, furrow);
|
|
148
|
+
rough += 0.35 * clumps * bedSeen - 0.5 * bedSeen * (1.0 - smoothstep(${f(FURROW)}, ${f(FURROW * 2.5)}, min(inBed, 1.0 - inBed))); arable = 0.0;
|
|
149
|
+
} else if (kind > 8.5) {
|
|
150
|
+
vec2 palmAt = (floor(plan / ${f(PALM_PITCH)}) + 0.5) * ${f(PALM_PITCH)};
|
|
151
|
+
float basin = (1.0 - smoothstep(${f(BASIN)} - 0.25, ${f(BASIN)}, length(plan - palmAt))) * fPalm * (1.0 - smoothstep(0.3, 1.2, spanAlong + spanAcross));
|
|
152
|
+
float rim = (1.0 - smoothstep(0.0, 0.3, abs(length(plan - palmAt) - ${f(BASIN)} - 0.1))) * fPalm * (1.0 - smoothstep(0.15, 0.5, spanAlong + spanAcross));
|
|
153
|
+
vec3 litter = mix(dirt * 1.5, straw * 0.62, smoothstep(0.3, 0.75, patchy * 0.6 + tone * 0.4));
|
|
154
|
+
litter = mix(litter, pasture * 0.9, smoothstep(0.55, 0.85, wide) * 0.45); // weeds where the shade is thin
|
|
155
|
+
crop = mix(mix(litter, dirt * 0.72, basin), dirt * 1.7, rim);
|
|
156
|
+
rough += 0.4 * rim - 0.3 * basin + 0.3 * speck; arable = 0.0;
|
|
157
|
+
}`;
|
|
158
|
+
const PAINT = fieldSurfaceGLSL({
|
|
159
|
+
...look,
|
|
160
|
+
read: 'vec4 lot = fLot; vec2 plan = fPlan;',
|
|
161
|
+
dirt: [tone('tilled', 0.72), tone('tilled', 1.16)],
|
|
162
|
+
straw: [tone('stubble', 0.74), tone('stubble', 1.2)],
|
|
163
|
+
pasture: [tone('crop', 0.7), tone('crop', 1.08)],
|
|
164
|
+
standing: [tone('ripe', 0.86), tone('ripe', 1.2)],
|
|
165
|
+
baked: 'vec3(1.0)',
|
|
166
|
+
kinds: KINDS,
|
|
167
|
+
});
|
|
168
|
+
// The use (its index in `uses`) of a parcel whose draw is `fPick`: each use that comes before the last adds a step, so the
|
|
169
|
+
// count of steps passed is the index.
|
|
170
|
+
const USE_OF = [];
|
|
171
|
+
let sum = 0;
|
|
172
|
+
for (const [, share] of USES.slice(0, -1)) USE_OF.push(`step(${f((sum += share))}, fPick)`);
|
|
173
|
+
const useOf = USE_OF.join(' + '),
|
|
174
|
+
KIND_OF = USES.map(([name]) => f(KIND[name])).join(', '),
|
|
175
|
+
index = name => USES.findIndex(([use]) => use === name);
|
|
176
|
+
// Metres inside the season's work, of a parcel whose use is being worked across it (`progress`), by its draw `fDone`.
|
|
177
|
+
const workOf = ([whole, least, most], rest) =>
|
|
178
|
+
`(fDone < ${f(whole)} ? ${f(rest)} : (${f(least)} + ${f((most - least) / (1 - whole))} * (fDone - ${f(whole)})) * fWidth - fAcross)`;
|
|
179
|
+
|
|
180
|
+
// The declarations and helpers the fields need in the fragment shader's head: the cities' frames, a coordinate hash and value
|
|
181
|
+
// noise in whole numbers as the page has them, and the share of a pixel a thin band covers.
|
|
182
|
+
const FIELD_PARS = `/* glsl */
|
|
183
|
+
uniform vec4 uFieldCity[${CITIES}]; uniform vec2 uFieldPlan[${CITIES}]; uniform vec2 uWindDrift; uniform vec3 uSky;
|
|
184
|
+
float gWater = 0.0; // how far the pixel is a canal's water
|
|
185
|
+
float fieldHash(int ix, int iz, int seed) { // a coordinate hash, to the bit, as the page's
|
|
186
|
+
uint h = uint(ix) * 374761393u + uint(iz) * 668265263u + uint(seed + ${WORLD_SEED}) * 1442695041u;
|
|
187
|
+
h = (h ^ (h >> 13)) * 1274126177u; h ^= h >> 16;
|
|
188
|
+
return float(h) / 4294967296.0;
|
|
189
|
+
}
|
|
190
|
+
float fieldNoise(vec2 p, int seed) { // value noise, as the page's
|
|
191
|
+
vec2 i = floor(p), u = p - i; u = u * u * (3.0 - 2.0 * u); ivec2 c = ivec2(i);
|
|
192
|
+
float a = fieldHash(c.x, c.y, seed), b = fieldHash(c.x + 1, c.y, seed), d = fieldHash(c.x, c.y + 1, seed), e = fieldHash(c.x + 1, c.y + 1, seed);
|
|
193
|
+
return a + (b - a) * u.x + (d - a) * u.y + (a - b - d + e) * u.x * u.y;
|
|
194
|
+
}
|
|
195
|
+
float lineCover(float d, float reach, float px) { return clamp((min(d + 0.5 * px, reach) - max(d - 0.5 * px, -reach)) / max(px, 1e-3), 0.0, 1.0); }`;
|
|
196
|
+
|
|
197
|
+
// The fields under a pixel, in the ground's map_fragment: nothing in it asks the GPU for a derivative.
|
|
198
|
+
const FIELDS = `/* glsl */
|
|
199
|
+
if (slopes.b > 0.004) {
|
|
200
|
+
float fWander = (fieldNoise(vWP.xz / ${f(WANDER_SPAN)}, ${WANDER_SEED}) - 0.5) * ${f(2 * WANDER_REACH)};
|
|
201
|
+
float fBest = 0.0; vec2 fAB = vec2(0.0), fAlong = vec2(1.0, 0.0), fOut = vec2(0.0, 1.0); int fSeed = 0;
|
|
202
|
+
for (int i = 0; i < ${CITIES}; i++) { // the city whose fields these are: square to its nearest wall
|
|
203
|
+
vec4 F = uFieldCity[i]; vec2 c = cityAt(F, vWP.xz);
|
|
204
|
+
float beside = abs(c.x) - uFieldPlan[i].x, behind = c.y - uFieldPlan[i].y, outside = max(max(beside, behind), -c.y);
|
|
205
|
+
float k = smoothstep(${f(FROM[0])}, ${f(FROM[1])}, outside) * (1.0 - smoothstep(${f(REACH[0])} + fWander, ${f(REACH[1])} + fWander, outside));
|
|
206
|
+
if (k > fBest) {
|
|
207
|
+
fBest = k; fSeed = ${CITY_SEED[0]} + i * ${CITY_SEED[1]};
|
|
208
|
+
if (beside > behind) { fAB = vec2(c.y, abs(c.x)); fAlong = vec2(-F.w, -F.z); fOut = sign(c.x) * vec2(F.z, -F.w); }
|
|
209
|
+
else { fAB = c; fAlong = vec2(F.z, -F.w); fOut = vec2(-F.w, -F.z); }
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
float fRiverD = slopes.a * ${f(RIVER_FIELDS)}, fRiver = fRiverD < ${f(RIVER[1] + WANDER_REACH)} ? 1.0 - smoothstep(${f(RIVER[0])}, ${f(RIVER[1])} + fWander * 0.5, fRiverD) : 0.0;
|
|
213
|
+
if (fRiver > fBest) { // or a river's, to the land's own grid
|
|
214
|
+
fBest = fRiver; fSeed = ${RIVER_SEED}; fAlong = vec2(${f(Math.cos(RIVER_YAW))}, ${f(-Math.sin(RIVER_YAW))}); fOut = vec2(${f(Math.sin(RIVER_YAW))}, ${f(Math.cos(RIVER_YAW))});
|
|
215
|
+
fAB = vec2(dot(vWP.xz, fAlong), dot(vWP.xz, fOut));
|
|
216
|
+
}
|
|
217
|
+
if (fBest > 0.0) {
|
|
218
|
+
vec2 fBendAt = fAB / ${f(BEND[1])}, fPlan = fAB + (vec2(fieldNoise(fBendAt, fSeed), fieldNoise(fBendAt, fSeed + 1)) - 0.5) * ${f(2 * BEND[0])};
|
|
219
|
+
vec2 fCell = floor(fPlan / vec2(${f(CELL[0])}, ${f(CELL[1])})), fIn = fPlan / vec2(${f(CELL[0])}, ${f(CELL[1])}) - fCell; ivec2 fC = ivec2(fCell);
|
|
220
|
+
float fStrips = 1.0 + min(2.0, floor(fieldHash(fC.x, fC.y, fSeed + 2) * 3.0)), fStrip = min(fStrips - 1.0, floor(fIn.x * fStrips));
|
|
221
|
+
int fId = fC.x * 3 + int(fStrip);
|
|
222
|
+
float fPick = fieldHash(fId, fC.y, fSeed + 3), fTint = fieldHash(fId, fC.y, fSeed + 4), fKeep = ${f(KEEP[0])} + ${f(KEEP[1])} * fieldHash(fId, fC.y, fSeed + 5), fDone = fieldHash(fId, fC.y, fSeed + 6);
|
|
223
|
+
float fWidth = ${f(CELL[0])} / fStrips, fAcross = (fIn.x * fStrips - fStrip) * fWidth;
|
|
224
|
+
float fEdge = min(fAcross, fWidth - fAcross), fEnd = min(fIn.y, 1.0 - fIn.y) * ${f(CELL[1])};
|
|
225
|
+
float fRun = floor(fCell.x / ${f(CANAL_RUN)}), fInRun = fCell.x - fRun * ${f(CANAL_RUN)};
|
|
226
|
+
float fFeeder = fInRun < 0.5 ? fIn.x * ${f(CELL[0])} : fInRun > ${f(CANAL_RUN - 1.5)} ? (1.0 - fIn.x) * ${f(CELL[0])} : ${f(NO_CANAL)};
|
|
227
|
+
float fLateral = 1.0 - mod(fRun + fCell.y + step(0.5, fIn.y), 2.0); // a canal along the nearer end, at every other row's
|
|
228
|
+
int fUse = int(${useOf} + 0.5);
|
|
229
|
+
const float FIELD_KIND[${USES.length}] = float[${USES.length}](${KIND_OF});
|
|
230
|
+
float fKind = FIELD_KIND[fUse];
|
|
231
|
+
float fWork = fUse == ${index('ripe')} ? ${workOf(PROGRESS.ripe, -DONE)} : fUse == ${index('tilled')} ? ${workOf(PROGRESS.tilled, DONE)} : fUse == ${index('stubble')} ? ${f(DONE)} : ${f(-DONE)};
|
|
232
|
+
float fReach = fBest * slopes.b, fWhole = smoothstep(0.0, ${f(EDGE)}, slopes.b); // the page's workOn
|
|
233
|
+
float fK = smoothstep(fKeep - ${f(KEEP[2])}, fKeep + ${f(KEEP[2])}, fReach) * fWhole; // how far this parcel is worked,
|
|
234
|
+
float fNet = max(fK, smoothstep(${f(NET[0])}, ${f(NET[1])}, fReach) * fWhole); // and how far the bunds and canals run here, worked or fallow
|
|
235
|
+
vec2 groundX = vec2(dot(spanX, fAlong), dot(spanX, fOut)), groundY = vec2(dot(spanY, fAlong), dot(spanY, fOut)); // the pixel's span on the plan
|
|
236
|
+
float fSpanA = abs(groundX.x) + abs(groundY.x), fSpanB = abs(groundX.y) + abs(groundY.y);
|
|
237
|
+
float fSeen = (1.0 - smoothstep(${f(SEEN_A[0])}, ${f(SEEN_A[1])}, fSpanA)) * (1.0 - smoothstep(${f(SEEN_B[0])}, ${f(SEEN_B[1])}, fSpanB));
|
|
238
|
+
// the parcel within its bunds and the canals' verges, its edge soft over a hand's breadth or a few pixels (the relief
|
|
239
|
+
// follows the parcel's cover: a step in it a pixel wide lay along every bund as a dotted line)
|
|
240
|
+
float fSide = min(fEdge - ${f(BUND)}, fFeeder - ${f(VERGE)}), fRowEnd = fEnd - mix(${f(BUND)}, ${f(VERGE)}, fLateral);
|
|
241
|
+
float fCover = fK * fSeen * clamp(fSide / max(fSpanA * ${f(EDGE_SOFT[0])}, ${f(EDGE_SOFT[1])}) + 0.5, 0.0, 1.0) * clamp(fRowEnd / max(fSpanB * ${f(EDGE_SOFT[0])}, ${f(EDGE_SOFT[1])}) + 0.5, 0.0, 1.0);
|
|
242
|
+
// a grove's palm stands in the middle of its square, where that keeps clear of the parcel's sides and ends
|
|
243
|
+
vec2 fPalmAt = (floor(fPlan / ${f(PALM_PITCH)}) + 0.5) * ${f(PALM_PITCH)}, fPalmIn = fPalmAt / vec2(${f(CELL[0])}, ${f(CELL[1])}) - fCell;
|
|
244
|
+
float fPalmAcross = (fPalmIn.x * fStrips - fStrip) * fWidth;
|
|
245
|
+
float fPalm = step(${f(PALM_ROOM)}, min(min(fPalmAcross, fWidth - fPalmAcross), min(fPalmIn.y, 1.0 - fPalmIn.y) * ${f(CELL[1])} - ${f(VERGE)}));
|
|
246
|
+
if (fCover > 0.004) {
|
|
247
|
+
vec4 farm = vec4(0.5, fKind * ${f(42 / 255)}, 0.0, fCover);
|
|
248
|
+
vec4 fLot = vec4(fTint, clamp(fRowEnd, 0.0, 12.75) / 12.75, clamp(127.5 + fWork * 20.0, 0.0, 255.0) / 255.0, clamp(min(fSide, fRowEnd), 0.0, 12.75) / 12.75);
|
|
249
|
+
${PAINT}
|
|
250
|
+
}
|
|
251
|
+
// the dry bund between two parcels, trodden pale along its top: a rounded ridge in the relief
|
|
252
|
+
float fBund = max(lineCover(fEdge, ${f(BUND)}, fSpanA), lineCover(fEnd, ${f(BUND)}, fSpanB) * (1.0 - fLateral)) * fNet * fSeen;
|
|
253
|
+
float fRidge = max(1.0 - smoothstep(0.0, ${f(BUND * 1.8)}, fEdge), (1.0 - smoothstep(0.0, ${f(BUND * 1.8)}, fEnd)) * (1.0 - fLateral)) * fNet * fSeen;
|
|
254
|
+
vec3 fDry = ${flat(earth, 0.86)} * (0.8 + 0.3 * d2.r) * (0.84 + 0.3 * grit) * (1.0 + 0.16 * fRidge * fRidge);
|
|
255
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, fDry, fBund * 0.9); gTH += fRidge * 0.7;
|
|
256
|
+
// a canal: weeds along its verge, the dug bank dark where the water wets it, the water itself
|
|
257
|
+
float fCanalD = min(mix(${f(NO_CANAL)}, fEnd, fLateral), fFeeder), fCanalPx = fFeeder < mix(${f(NO_CANAL)}, fEnd, fLateral) ? fSpanA : fSpanB;
|
|
258
|
+
float fWater = lineCover(fCanalD, ${f(WATER)}, fCanalPx), fBank = lineCover(fCanalD, ${f(BANK)}, fCanalPx), fVerge = lineCover(fCanalD, ${f(VERGE)}, fCanalPx);
|
|
259
|
+
float fWeeds = smoothstep(0.35, 0.75, 0.6 * smoothstep(0.74, 0.96, d2.g) + 0.4 * smoothstep(0.76, 0.96, fleck));
|
|
260
|
+
vec3 fGreen = mix(${flat(earth, 0.9)}, ${flat(weeds, 0.9)}, 0.45 + 0.5 * fWeeds) * (0.85 + 0.3 * grit);
|
|
261
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, fGreen, fVerge * fNet * fSeen * 0.9);
|
|
262
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, ${flat(earth, 0.5)} * (0.8 + 0.4 * d2.r) * mix(0.62, 1.0, smoothstep(${f(WATER)}, ${f(BANK)}, fCanalD)), fBank * fNet * fSeen);
|
|
263
|
+
gWater = fWater * fNet * fSeen;
|
|
264
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, ${flat(canal, 0.5)} * (0.85 + 0.3 * d3.g), gWater);
|
|
265
|
+
// in the relief the bank falls to the water, which lies level: smoothly, since a step a pixel wide shows as a dotted line
|
|
266
|
+
float fCanal = fNet * fSeen, fLevel = fCanal * (1.0 - smoothstep(${f(WATER * 0.75)}, ${f(WATER * 1.2)}, fCanalD));
|
|
267
|
+
gTH = mix(gTH + fVerge * fCanal * 0.5 * fWeeds, 0.0, fLevel) - 0.8 * fCanal * (1.0 - smoothstep(${f(WATER)}, ${f(BANK + 0.4)}, fCanalD));
|
|
268
|
+
}
|
|
269
|
+
}`;
|
|
270
|
+
// A canal's water lies level and still, and the sky is in it, more the flatter the look across it: for the ground's shader once
|
|
271
|
+
// its normal and roughness are set (the ground has no map of the sky to mirror: `uSky` is the horizon's colour).
|
|
272
|
+
const FIELD_WATER = `/* glsl */
|
|
273
|
+
if (gWater > 0.0) {
|
|
274
|
+
normal = normalize(mix(normal, normalize((viewMatrix * vec4(0.0, 1.0, 0.0, 0.0)).xyz), gWater));
|
|
275
|
+
float fresnel = 0.03 + 0.97 * pow(1.0 - clamp(dot(normalize(vViewPosition), normal), 0.0, 1.0), 5.0);
|
|
276
|
+
totalEmissiveRadiance += uSky * (${f(SKY_IN_WATER)} * fresnel * gWater);
|
|
277
|
+
diffuseColor.rgb *= 1.0 - fresnel * gWater;
|
|
278
|
+
roughnessFactor = mix(roughnessFactor, 0.05, gWater);
|
|
279
|
+
}`;
|
|
280
|
+
|
|
281
|
+
// The uniforms the shader reads of the land laid out: its cities' frames (a slot no city fills lies far off the land), the
|
|
282
|
+
// wind that runs over the standing corn and the sky a canal mirrors.
|
|
283
|
+
function uniforms(frames, { windDrift, sky }) {
|
|
284
|
+
const city = Array.from({ length: CITIES }, () => new THREE.Vector4(NOWHERE, NOWHERE, 1, 0)),
|
|
285
|
+
plan = Array.from({ length: CITIES }, () => new THREE.Vector2(0, 0));
|
|
286
|
+
frames.forEach((frame, i) => {
|
|
287
|
+
city[i].set(frame.x, frame.z, Math.cos(frame.yaw), Math.sin(frame.yaw));
|
|
288
|
+
plan[i].set(frame.plan.half, frame.plan.depth);
|
|
289
|
+
});
|
|
290
|
+
return { uFieldCity: { value: city }, uFieldPlan: { value: plan }, uWindDrift: windDrift, uSky: sky };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return { pars: FIELD_PARS, fields: FIELDS, water: FIELD_WATER, paint: PAINT, uniforms };
|
|
294
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { roadRibbon } from '../geometry/road-ribbon.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Lay earth roads over the ground: a ribbon to each (`roadRibbon`), draped on the terrain and drawn by the earth's
|
|
6
|
+
* material unless the road carries a laid surface of its own.
|
|
7
|
+
*
|
|
8
|
+
* @param {object} options
|
|
9
|
+
* @param {object[]} options.roads - The roads, as `roadRibbon` takes them.
|
|
10
|
+
* @param {import('three').Object3D} options.parent - What the meshes join.
|
|
11
|
+
* @param {(x: number, z: number) => number} options.ground - The terrain's height at a place, in metres.
|
|
12
|
+
* @param {(x: number, z: number) => { r: number, g: number, b: number }} options.soil - The soil's colour at a place, linear.
|
|
13
|
+
* @param {(x: number, z: number) => { r: number, g: number, b: number }} options.verge - The verge's colour at a place, linear.
|
|
14
|
+
* @param {() => import('three').Material} options.material - Makes the earth roads' material, once a laying.
|
|
15
|
+
* @returns {import('three').Mesh[]} The roads' meshes, in the order of the roads.
|
|
16
|
+
*/
|
|
17
|
+
export function layRoads({ roads, parent, ground, soil, verge, material }) {
|
|
18
|
+
const earth = material(),
|
|
19
|
+
meshes = [];
|
|
20
|
+
for (const r of roads) {
|
|
21
|
+
const { position, normal, color, road, verge: vergeColour, index } = roadRibbon(r, { ground, soil, verge });
|
|
22
|
+
const g = new THREE.BufferGeometry();
|
|
23
|
+
g.setAttribute('position', new THREE.BufferAttribute(position, 3));
|
|
24
|
+
g.setAttribute('normal', new THREE.BufferAttribute(normal, 3));
|
|
25
|
+
g.setAttribute('color', new THREE.BufferAttribute(color, 4));
|
|
26
|
+
g.setAttribute('road', new THREE.BufferAttribute(road, 4));
|
|
27
|
+
g.setAttribute('verge', new THREE.BufferAttribute(vergeColour, 3));
|
|
28
|
+
g.setIndex(index);
|
|
29
|
+
const mesh = new THREE.Mesh(g, r.surface ?? earth);
|
|
30
|
+
mesh.receiveShadow = true;
|
|
31
|
+
mesh.renderOrder = 2;
|
|
32
|
+
mesh.matrixAutoUpdate = false;
|
|
33
|
+
parent.add(mesh);
|
|
34
|
+
meshes.push(mesh);
|
|
35
|
+
}
|
|
36
|
+
return meshes;
|
|
37
|
+
}
|
package/rendering/sea-shaders.js
CHANGED
|
@@ -285,3 +285,33 @@ export const seaBody = (water = {}) => {
|
|
|
285
285
|
export const seaOpacity = (water = {}) => `/* glsl */
|
|
286
286
|
opacity = mix(0.06, 0.95, 1.0 - dot(exp(-${glslVec3({ ...SEA_WATER, ...water }.absorption)} * opticalPath), vec3(0.2, 0.5, 0.3)));
|
|
287
287
|
opacity = mix(opacity, 1.0, smoothstep(6.0, 7.8, waterline));`;
|
|
288
|
+
|
|
289
|
+
const glslStretch = ([along, across]) =>
|
|
290
|
+
along === across ? `vec2(${glslFloat(along)})` : `vec2(${glslFloat(along)}, ${glslFloat(across)})`;
|
|
291
|
+
const glslAcross = across => (across < 0 ? `-pileRow.z * ${glslFloat(-across)}` : `pileRow.z * ${glslFloat(across)}`);
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Generates the GLSL snippet for water ripples around rows of pier/jetty piles (`uPileRows`), returning an empty string
|
|
295
|
+
* when no pile rows are configured so materials without piles compile no loop.
|
|
296
|
+
* @param {object|null} options Pile-row shader options (`PileRowsOptions`).
|
|
297
|
+
* @returns {string}
|
|
298
|
+
*/
|
|
299
|
+
export function pileRowsGlsl(options) {
|
|
300
|
+
if (!options) return '';
|
|
301
|
+
const { rows, along, halfWidth, firstPile, pitch, pairs, gap, pileRange, spots = [], reach = 160, strength = 0.19 } = options;
|
|
302
|
+
const spotLines = spots.map(
|
|
303
|
+
spot =>
|
|
304
|
+
` pileRipples += ripple(q - vec2(${glslFloat(spot.along)}, ${glslAcross(spot.across)}), ${glslStretch(spot.stretch)}, ${glslFloat(spot.range)}, ${glslFloat(spot.phase)});\n`,
|
|
305
|
+
);
|
|
306
|
+
return ` if (uHasDepth > 0.5 && dist < ${glslFloat(reach)}) {
|
|
307
|
+
vec2 pileRipples = vec2(0.0);
|
|
308
|
+
for (int i = 0; i < ${rows}; i++) {
|
|
309
|
+
vec4 pileRow = uPileRows[i]; vec2 d = vec2(cos(pileRow.w - 10.0), sin(pileRow.w - 10.0)), r = vW.xz - pileRow.xy, q = vec2(dot(r, d), d.x * r.y - d.y * r.x);
|
|
310
|
+
if (pileRow.w < 0.5 || abs(q.y) > ${glslFloat(halfWidth)} || q.x < ${glslFloat(along[0])} || q.x > ${glslFloat(along[1])}) continue;
|
|
311
|
+
float pile = clamp(floor((q.x - ${glslFloat(firstPile)}) / ${glslFloat(pitch)} + 0.5), 0.0, ${glslFloat(pairs - 1)});
|
|
312
|
+
pileRipples += ripple(q - vec2(${glslFloat(firstPile)} + ${glslFloat(pitch)} * pile, sign(q.y) * ${glslFloat(gap)}), vec2(1.0), ${glslFloat(pileRange)}, pile);
|
|
313
|
+
${spotLines.join('')} }
|
|
314
|
+
g += pileRipples * ${glslFloat(strength)} * OW(8.0);
|
|
315
|
+
}
|
|
316
|
+
`;
|
|
317
|
+
}
|