webgpu-grass 0.0.0-stage → 0.1.1

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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +645 -2
  3. package/lib/distance.d.ts +63 -0
  4. package/lib/distance.d.ts.map +1 -0
  5. package/lib/distance.js +148 -0
  6. package/lib/distance.js.map +1 -0
  7. package/lib/engine.d.ts +402 -0
  8. package/lib/engine.d.ts.map +1 -0
  9. package/lib/engine.js +3253 -0
  10. package/lib/engine.js.map +1 -0
  11. package/lib/field.d.ts +133 -0
  12. package/lib/field.d.ts.map +1 -0
  13. package/lib/field.js +244 -0
  14. package/lib/field.js.map +1 -0
  15. package/lib/flowers.d.ts +138 -0
  16. package/lib/flowers.d.ts.map +1 -0
  17. package/lib/flowers.js +117 -0
  18. package/lib/flowers.js.map +1 -0
  19. package/lib/index.d.ts +11 -0
  20. package/lib/index.d.ts.map +1 -0
  21. package/lib/index.js +14 -0
  22. package/lib/index.js.map +1 -0
  23. package/lib/mix.d.ts +17 -0
  24. package/lib/mix.d.ts.map +1 -0
  25. package/lib/mix.js +95 -0
  26. package/lib/mix.js.map +1 -0
  27. package/lib/mixshare.d.ts +42 -0
  28. package/lib/mixshare.d.ts.map +1 -0
  29. package/lib/mixshare.js +144 -0
  30. package/lib/mixshare.js.map +1 -0
  31. package/lib/noise.d.ts +4 -0
  32. package/lib/noise.d.ts.map +1 -0
  33. package/lib/noise.js +49 -0
  34. package/lib/noise.js.map +1 -0
  35. package/lib/placement.d.ts +39 -0
  36. package/lib/placement.d.ts.map +1 -0
  37. package/lib/placement.js +67 -0
  38. package/lib/placement.js.map +1 -0
  39. package/lib/rank.d.ts +36 -0
  40. package/lib/rank.d.ts.map +1 -0
  41. package/lib/rank.js +158 -0
  42. package/lib/rank.js.map +1 -0
  43. package/lib/species.d.ts +408 -0
  44. package/lib/species.d.ts.map +1 -0
  45. package/lib/species.js +672 -0
  46. package/lib/species.js.map +1 -0
  47. package/lib/transitions.d.ts +72 -0
  48. package/lib/transitions.d.ts.map +1 -0
  49. package/lib/transitions.js +123 -0
  50. package/lib/transitions.js.map +1 -0
  51. package/package.json +60 -3
package/lib/field.d.ts ADDED
@@ -0,0 +1,133 @@
1
+ import * as THREE from "three/webgpu";
2
+ type Vec2 = THREE.Node<"vec2">;
3
+ type Float = THREE.Node<"float">;
4
+ type Vec3 = THREE.Node<"vec3">;
5
+ /** A map's number for ground where nothing grows. */
6
+ export declare const NO_GRASS = 255;
7
+ /** The ground's height (m) and its normal at a place. Sampled in the compute passes, in vertex shaders and in the canopy's fragment shader. */
8
+ export interface HeightSource {
9
+ sample(xz: Vec2): {
10
+ height: Float;
11
+ normal: Vec3;
12
+ };
13
+ dispose(): void;
14
+ }
15
+ /**
16
+ * The species at a place (its row in the table, or `NO_GRASS`) and the
17
+ * metres from there to the nearest ground of another species. Sampled in
18
+ * the compute pass and, for the ground's colour, in a fragment shader.
19
+ */
20
+ export interface MapSource {
21
+ sample(xz: Vec2): {
22
+ species: Float;
23
+ edge: Float;
24
+ };
25
+ /**
26
+ * The ground the map covers (m), if it says: the grass keeps a picture of
27
+ * how tall it stands over that ground, for its shadows. Without it nothing
28
+ * shades anything.
29
+ */
30
+ bounds?: {
31
+ x0: number;
32
+ z0: number;
33
+ x1: number;
34
+ z1: number;
35
+ };
36
+ /**
37
+ * The furthest `edge` is ever told (m), if the map has such a limit: a map of cells knows how far off
38
+ * a border is only up to a cell or two, and says that much for everywhere deeper in.
39
+ */
40
+ edgeReach?: number;
41
+ dispose(): void;
42
+ }
43
+ /** Heights (m) on a square grid: `nx` by `nz` posts `cell` metres apart, the first at (`x0`, `z0`), rows along x. */
44
+ export interface HeightGrid {
45
+ heights: Float32Array;
46
+ nx: number;
47
+ nz: number;
48
+ x0: number;
49
+ z0: number;
50
+ cell: number;
51
+ }
52
+ /** The height of a grid at a place, as the shaders have it (for the host's own use: a camera that walks the ground). */
53
+ export declare function heightAt(g: HeightGrid, x: number, z: number): number;
54
+ /** A height source from a grid of heights. The normals are worked out here. */
55
+ export declare function heightGrid(g: HeightGrid): HeightSource;
56
+ /** A species number to a cell: `nx` by `nz` cells of `cell` metres, the first cell's corner at (`x0`, `z0`), rows along x. */
57
+ export interface GrassMap {
58
+ ids: Uint8Array;
59
+ nx: number;
60
+ nz: number;
61
+ x0: number;
62
+ z0: number;
63
+ cell: number;
64
+ }
65
+ export interface SpeciesMapSource extends MapSource {
66
+ /** Replace the map's species numbers (the same size of map), as after painting on it. */
67
+ set(ids: Uint8Array): void;
68
+ }
69
+ /**
70
+ * A map source from a grid of species numbers. It keeps a channel to each species' row the map may hold
71
+ * and one for ground where nothing grows, four to a layer of one array texture: `rows` says how many rows
72
+ * that is (the highest number in the grid and one, if not said). A number from `rows` up is ground where
73
+ * nothing grows, and so it stays if it is painted in later: a map that will be painted with more species
74
+ * says so. However many rows, it is one texture to a shader that asks it (a stage may have sixteen in all,
75
+ * and a host's ground shader that asks `groundShade` has its own to look up too).
76
+ *
77
+ * A grid's borders are steps a cell wide. So the grid is not looked up as
78
+ * it is: each species' cells are spread into a smooth field of how much of
79
+ * the ground about a place is that species', and a place belongs to
80
+ * whichever species has most of it. Where two have as much as each other is
81
+ * the border, which is then a clean line that cuts the grid's corners, the
82
+ * same from any distance; and how far ahead the leader is says how far off
83
+ * the border is. Features narrower than three or four cells are smoothed
84
+ * away with the steps.
85
+ */
86
+ export declare function speciesMap(m: GrassMap, rows?: number): SpeciesMapSource;
87
+ /** A host's own fields, one to a surface, as textures over a rectangle of ground. */
88
+ export interface FieldMap {
89
+ /** The textures, each with up to four surfaces' fields in its channels. Looked up with whatever filter they are set to. */
90
+ fields: readonly THREE.Texture[];
91
+ /** The species' row (or `NO_GRASS`) for each channel, r, g, b, a through the textures in turn. Two surfaces may be one species. */
92
+ species: readonly number[];
93
+ /** The ground the textures cover (m): their first corner, and how far they go in x and in z. */
94
+ x0: number;
95
+ z0: number;
96
+ width: number;
97
+ depth: number;
98
+ /** Metres to the border for a lead of 1 between the leading field and the next. */
99
+ edgePerLead: number;
100
+ }
101
+ /**
102
+ * A map source from a host's own fields: a course that already keeps a field for each of its surfaces
103
+ * (how much of the ground about a place is green, fairway, rough, sand) has its map already. The place
104
+ * belongs to the surface whose field is highest there.
105
+ */
106
+ export declare function fieldMap(m: FieldMap): MapSource;
107
+ /**
108
+ * How the mower left the grass at a place: the way its leaves were laid
109
+ * over, as a vector along the ground (x, z), its length how fully (0 where
110
+ * nothing was mown, 1 in the middle of a pass). Sampled in the compute pass
111
+ * and, for the ground's colour, in a fragment shader.
112
+ */
113
+ export interface MowSource {
114
+ sample(xz: Vec2): Vec2;
115
+ }
116
+ export interface MowStripes extends MowSource {
117
+ /** A pass's width (m): the mower's. */
118
+ width: number;
119
+ /** The way the first pass was driven, in degrees from +z towards +x. Each pass beside it is driven back the other way. */
120
+ heading: number;
121
+ /** A place on the line between two passes. */
122
+ x: number;
123
+ z: number;
124
+ }
125
+ /**
126
+ * Straight passes side by side, there and back: a fairway mown up and down.
127
+ * The lay turns over across a hand's width where two passes meet. (Not yet:
128
+ * the laps a mower makes round the edge first, or a second cut across the
129
+ * first.)
130
+ */
131
+ export declare function mowStripes(init?: Partial<Pick<MowStripes, "width" | "heading" | "x" | "z">>): MowStripes;
132
+ export {};
133
+ //# sourceMappingURL=field.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"field.d.ts","sourceRoot":"","sources":["../src/field.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAItC,KAAK,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAC/B,KAAK,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACjC,KAAK,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAE/B,qDAAqD;AACrD,eAAO,MAAM,QAAQ,MAAM,CAAC;AAE5B,+IAA+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AKD,MAAM,WAAW,gBAAiB,SAAQ,SAAS;IACjD,yFAAyF;IACzF,GAAG,CAAC,GAAG,EAAE,UAAU,GAAG,IAAI,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,gBAAgB,CA6EvE;AA+BD,qFAAqF;AACrF,MAAM,WAAW,QAAQ;IACvB,2HAA2H;IAC3H,MAAM,EAAE,SAAS,KAAK,CAAC,OAAO,EAAE,CAAC;IACjC,mIAAmI;IACnI,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3B,gGAAgG;IAChG,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,mFAAmF;IACnF,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,QAAQ,GAAG,SAAS,CAU/C;AAED;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACxB,MAAM,CAAC,EAAE,EAAE,IAAI,GAAG,IAAI,CAAC;CACxB;AAED,MAAM,WAAW,UAAW,SAAQ,SAAS;IAC3C,uCAAuC;IACvC,KAAK,EAAE,MAAM,CAAC;IACd,0HAA0H;IAC1H,OAAO,EAAE,MAAM,CAAC;IAChB,8CAA8C;IAC9C,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,IAAI,GAAE,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,OAAO,GAAG,SAAS,GAAG,GAAG,GAAG,GAAG,CAAC,CAAM,GAAG,UAAU,CAwB5G"}
package/lib/field.js ADDED
@@ -0,0 +1,244 @@
1
+ // What the grass grows on, as two things it can ask at any place: how high
2
+ // the ground is there, and which species grows there (and how far that
3
+ // species' ground goes on before something else starts).
4
+ //
5
+ // Each is a source: an object the shaders sample. The two made here read
6
+ // plain grids. A host with its own terrain (a noise function, its own
7
+ // distance fields) writes a source of its own with the same `sample`.
8
+ import * as THREE from "three/webgpu";
9
+ import { MAX_SPECIES } from "./species.js";
10
+ import { abs, clamp, cos, dot, float, floor, fract, int, max, min, mix, normalize, sin, smoothstep, step, storage, texture, uniform, vec2, vec3 } from "three/tsl";
11
+ /** A map's number for ground where nothing grows. */
12
+ export const NO_GRASS = 255;
13
+ /** The height of a grid at a place, as the shaders have it (for the host's own use: a camera that walks the ground). */
14
+ export function heightAt(g, x, z) {
15
+ const fx = Math.min(g.nx - 1.001, Math.max(0, (x - g.x0) / g.cell));
16
+ const fz = Math.min(g.nz - 1.001, Math.max(0, (z - g.z0) / g.cell));
17
+ const i = Math.floor(fx);
18
+ const j = Math.floor(fz);
19
+ const tx = fx - i;
20
+ const tz = fz - j;
21
+ const h = g.heights;
22
+ const a = h[j * g.nx + i];
23
+ const b = h[j * g.nx + i + 1];
24
+ const c = h[(j + 1) * g.nx + i];
25
+ const d = h[(j + 1) * g.nx + i + 1];
26
+ return (a + (b - a) * tx) * (1 - tz) + (c + (d - c) * tx) * tz;
27
+ }
28
+ /** A height source from a grid of heights. The normals are worked out here. */
29
+ export function heightGrid(g) {
30
+ const { nx, nz, cell } = g;
31
+ if (g.heights.length !== nx * nz)
32
+ throw new Error(`heights holds ${g.heights.length} numbers, not ${nx} x ${nz}`);
33
+ const data = new Float32Array(nx * nz * 4);
34
+ const at = (i, j) => g.heights[Math.min(nz - 1, Math.max(0, j)) * nx + Math.min(nx - 1, Math.max(0, i))];
35
+ for (let j = 0; j < nz; j++) {
36
+ for (let i = 0; i < nx; i++) {
37
+ const dx = (at(i + 1, j) - at(i - 1, j)) / (2 * cell);
38
+ const dz = (at(i, j + 1) - at(i, j - 1)) / (2 * cell);
39
+ const len = Math.hypot(dx, 1, dz);
40
+ data.set([at(i, j), -dx / len, 1 / len, -dz / len], (j * nx + i) * 4);
41
+ }
42
+ }
43
+ const attr = new THREE.StorageBufferAttribute(data, 4);
44
+ const posts = storage(attr, "vec4", nx * nz).toReadOnly();
45
+ return {
46
+ sample(xz) {
47
+ const p = clamp(xz.sub(vec2(g.x0, g.z0)).div(cell), vec2(0), vec2(nx - 1.001, nz - 1.001));
48
+ const pi = floor(p);
49
+ const pf = fract(p);
50
+ const i0 = int(pi.x);
51
+ const j0 = int(pi.y);
52
+ const i1 = int(min(pi.x.add(1), float(nx - 1)));
53
+ const j1 = int(min(pi.y.add(1), float(nz - 1)));
54
+ const a = posts.element(j0.mul(nx).add(i0));
55
+ const b = posts.element(j0.mul(nx).add(i1));
56
+ const c = posts.element(j1.mul(nx).add(i0));
57
+ const d = posts.element(j1.mul(nx).add(i1));
58
+ const height = mix(mix(a.x, b.x, pf.x), mix(c.x, d.x, pf.x), pf.y);
59
+ const n = mix(mix(vec3(a.y, a.z, a.w), vec3(b.y, b.z, b.w), pf.x), mix(vec3(c.y, c.z, c.w), vec3(d.y, d.z, d.w), pf.x), pf.y);
60
+ return { height, normal: normalize(n) };
61
+ },
62
+ dispose() {
63
+ attr.dispose();
64
+ },
65
+ };
66
+ }
67
+ /** A map's cells are blurred over about this many of them to either side, which is what rounds its steps into a line. */
68
+ const SMOOTH_CELLS = 1.5;
69
+ /**
70
+ * A map source from a grid of species numbers. It keeps a channel to each species' row the map may hold
71
+ * and one for ground where nothing grows, four to a layer of one array texture: `rows` says how many rows
72
+ * that is (the highest number in the grid and one, if not said). A number from `rows` up is ground where
73
+ * nothing grows, and so it stays if it is painted in later: a map that will be painted with more species
74
+ * says so. However many rows, it is one texture to a shader that asks it (a stage may have sixteen in all,
75
+ * and a host's ground shader that asks `groundShade` has its own to look up too).
76
+ *
77
+ * A grid's borders are steps a cell wide. So the grid is not looked up as
78
+ * it is: each species' cells are spread into a smooth field of how much of
79
+ * the ground about a place is that species', and a place belongs to
80
+ * whichever species has most of it. Where two have as much as each other is
81
+ * the border, which is then a clean line that cuts the grid's corners, the
82
+ * same from any distance; and how far ahead the leader is says how far off
83
+ * the border is. Features narrower than three or four cells are smoothed
84
+ * away with the steps.
85
+ */
86
+ export function speciesMap(m, rows) {
87
+ const { nx, nz, cell } = m;
88
+ const n = nx * nz;
89
+ let highest = -1;
90
+ for (let k = 0; k < n; k++)
91
+ if (m.ids[k] < MAX_SPECIES && m.ids[k] > highest)
92
+ highest = m.ids[k];
93
+ const kept = Math.min(MAX_SPECIES, Math.max(1, rows ?? highest + 1));
94
+ /** Channels: one to each species' row, and one for ground where nothing grows. */
95
+ const CHANNELS = kept + 1;
96
+ const LAYERS = Math.ceil(CHANNELS / 4);
97
+ // (One texture of `LAYERS` layers, not a texture to a layer: every layer is then one binding.)
98
+ const pixels = new Uint8Array(n * 4 * LAYERS);
99
+ const data = Array.from({ length: LAYERS }, (_, l) => pixels.subarray(l * n * 4, (l + 1) * n * 4));
100
+ const fields = new THREE.DataArrayTexture(pixels, nx, nz, LAYERS);
101
+ fields.format = THREE.RGBAFormat;
102
+ fields.type = THREE.UnsignedByteType;
103
+ fields.magFilter = fields.minFilter = THREE.LinearFilter;
104
+ fields.colorSpace = THREE.NoColorSpace;
105
+ fields.generateMipmaps = false;
106
+ /** Three passes of a box three cells wide: near enough a bell 1.4 cells to either side. */
107
+ const smooth = (field) => {
108
+ const other = new Float32Array(n);
109
+ let from = field;
110
+ let to = other;
111
+ for (let pass = 0; pass < 3; pass++) {
112
+ for (let j = 0; j < nz; j++) {
113
+ for (let i = 0; i < nx; i++) {
114
+ const k = j * nx + i;
115
+ to[k] = (from[j * nx + Math.max(0, i - 1)] + from[k] + from[j * nx + Math.min(nx - 1, i + 1)]) / 3;
116
+ }
117
+ }
118
+ [from, to] = [to, from];
119
+ for (let j = 0; j < nz; j++) {
120
+ for (let i = 0; i < nx; i++) {
121
+ const k = j * nx + i;
122
+ to[k] = (from[Math.max(0, j - 1) * nx + i] + from[k] + from[Math.min(nz - 1, j + 1) * nx + i]) / 3;
123
+ }
124
+ }
125
+ [from, to] = [to, from];
126
+ }
127
+ return from;
128
+ };
129
+ const set = (ids) => {
130
+ if (ids.length !== n)
131
+ throw new Error(`ids holds ${ids.length} numbers, not ${nx} x ${nz}`);
132
+ for (let c = 0; c < CHANNELS; c++) {
133
+ const out = data[c >> 2];
134
+ const at = c & 3;
135
+ // (A species the map has none of costs nothing to smooth.)
136
+ let any = false;
137
+ const field = new Float32Array(n);
138
+ for (let k = 0; k < n; k++) {
139
+ if ((ids[k] < CHANNELS - 1 ? ids[k] : CHANNELS - 1) === c) {
140
+ field[k] = 1;
141
+ any = true;
142
+ }
143
+ }
144
+ const share = any ? smooth(field) : field;
145
+ for (let k = 0; k < n; k++)
146
+ out[k * 4 + at] = Math.round(share[k] * 255);
147
+ }
148
+ fields.needsUpdate = true;
149
+ };
150
+ set(m.ids);
151
+ const size = vec2(nx, nz);
152
+ return {
153
+ set,
154
+ bounds: { x0: m.x0, z0: m.z0, x1: m.x0 + nx * cell, z1: m.z0 + nz * cell },
155
+ edgeReach: 1.25 * SMOOTH_CELLS * cell,
156
+ sample(xz) {
157
+ const t = xz.sub(vec2(m.x0, m.z0)).div(cell);
158
+ // (Across a border the lead grows about as the distance over the blur's width does.)
159
+ const uv = t.div(size);
160
+ const taken = data.map((_, l) => texture(fields, uv).depth(int(l)).level(float(0)));
161
+ return leaderOf(taken, uv, Array.from({ length: CHANNELS }, (_, k) => (k < CHANNELS - 1 ? k : NO_GRASS)), 1.25 * SMOOTH_CELLS * cell);
162
+ },
163
+ dispose() {
164
+ fields.dispose();
165
+ },
166
+ };
167
+ }
168
+ /**
169
+ * Which of several fields leads at a place, and by how much. `taken` is the fields looked up at the place
170
+ * (at `uv`, which says only whether the place is on the map), four to a lookup. Each channel of each is one
171
+ * surface's field: how much of the ground about a place is that surface's (or how far inside it the
172
+ * place is: any measure that is highest for the surface the place belongs to, on one scale for all).
173
+ * `species[k]` is the species (or `NO_GRASS`) of channel `k`, counted r, g, b, a through the textures
174
+ * in turn. The border between two surfaces is where their fields are level, and `edgePerLead` is the
175
+ * metres to it for a lead of 1 over the next.
176
+ */
177
+ const leaderOf = (taken, uv, species, edgePerLead) => {
178
+ const channel = (k) => [taken[k >> 2].r, taken[k >> 2].g, taken[k >> 2].b, taken[k >> 2].a][k & 3];
179
+ // (Two surfaces may be one species: a species' share is its surfaces' most. The line between two surfaces of one
180
+ // species is no edge, or every place along it is told it stands on one, and loses what grows only off edges.)
181
+ const kinds = [...new Set(species)];
182
+ const shares = kinds.map((sp) => species.map((s, k) => (s === sp ? k : -1)).filter((k) => k >= 0).map(channel).reduce((a, b) => max(a, b)));
183
+ // The species with most of the ground here, and how far ahead of the next it is.
184
+ let most = shares[0];
185
+ let next = float(-1e6);
186
+ let which = float(kinds[0]);
187
+ for (let k = 1; k < shares.length; k++) {
188
+ const leads = step(most, shares[k]);
189
+ next = mix(max(next, shares[k]), most, leads);
190
+ which = mix(which, float(kinds[k]), leads);
191
+ most = max(most, shares[k]);
192
+ }
193
+ const inside = step(float(0), uv.x).mul(step(float(0), uv.y)).mul(step(uv.x, float(1))).mul(step(uv.y, float(1)));
194
+ return { species: mix(float(NO_GRASS), which, inside), edge: most.sub(next).mul(edgePerLead) };
195
+ };
196
+ /**
197
+ * A map source from a host's own fields: a course that already keeps a field for each of its surfaces
198
+ * (how much of the ground about a place is green, fairway, rough, sand) has its map already. The place
199
+ * belongs to the surface whose field is highest there.
200
+ */
201
+ export function fieldMap(m) {
202
+ if (m.species.length === 0 || m.species.length > m.fields.length * 4)
203
+ throw new Error(`${m.species.length} species for ${m.fields.length} textures of four channels`);
204
+ return {
205
+ bounds: { x0: m.x0, z0: m.z0, x1: m.x0 + m.width, z1: m.z0 + m.depth },
206
+ sample(xz) {
207
+ const uv = xz.sub(vec2(m.x0, m.z0)).div(vec2(m.width, m.depth));
208
+ return leaderOf(m.fields.map((tex) => texture(tex, uv).level(float(0))), uv, m.species, m.edgePerLead);
209
+ },
210
+ dispose() { },
211
+ };
212
+ }
213
+ /**
214
+ * Straight passes side by side, there and back: a fairway mown up and down.
215
+ * The lay turns over across a hand's width where two passes meet. (Not yet:
216
+ * the laps a mower makes round the edge first, or a second cut across the
217
+ * first.)
218
+ */
219
+ export function mowStripes(init = {}) {
220
+ const pattern = { width: init.width ?? 2.5, heading: init.heading ?? 0, x: init.x ?? 0, z: init.z ?? 0 };
221
+ /** x, z, width, heading (radians). */
222
+ const u = uniform(new THREE.Vector4());
223
+ const put = () => u.value.set(pattern.x, pattern.z, Math.max(0.05, pattern.width), (pattern.heading * Math.PI) / 180);
224
+ put();
225
+ const source = {
226
+ sample(xz) {
227
+ const along = vec2(sin(u.w), cos(u.w));
228
+ const across = dot(xz.sub(vec2(u.x, u.y)), vec2(along.y, along.x.negate())).div(u.z);
229
+ // (+1 down one pass, -1 back up the next, turning over between them.)
230
+ const there = smoothstep(float(0.46), float(0.54), abs(fract(across.mul(0.5)).sub(0.5)).mul(2)).mul(2).sub(1);
231
+ return along.mul(there);
232
+ },
233
+ };
234
+ const settable = (key) => ({
235
+ get: () => pattern[key],
236
+ set: (v) => {
237
+ pattern[key] = v;
238
+ put();
239
+ },
240
+ enumerable: true,
241
+ });
242
+ return Object.defineProperties(source, { width: settable("width"), heading: settable("heading"), x: settable("x"), z: settable("z") });
243
+ }
244
+ //# sourceMappingURL=field.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"field.js","sourceRoot":"","sources":["../src/field.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,uEAAuE;AACvE,yDAAyD;AACzD,EAAE;AACF,yEAAyE;AACzE,sEAAsE;AACtE,sEAAsE;AAEtE,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAA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CAAC;YACrF,sEAAsE;YACtE,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC9G,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAC1B,CAAC;KACF,CAAC;IACF,MAAM,QAAQ,GAAG,CAAC,GAAyB,EAAE,EAAE,CAAC,CAAC;QAC/C,GAAG,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC;QACvB,GAAG,EAAE,CAAC,CAAS,EAAE,EAAE;YACjB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACjB,GAAG,EAAE,CAAC;QACR,CAAC;QACD,UAAU,EAAE,IAAI;KACjB,CAAC,CAAC;IACH,OAAO,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAe,CAAC;AACvJ,CAAC","sourcesContent":["// What the grass grows on, as two things it can ask at any place: how high\n// the ground is there, and which species grows there (and how far that\n// species' ground goes on before something else starts).\n//\n// Each is a source: an object the shaders sample. The two made here read\n// plain grids. A host with its own terrain (a noise function, its own\n// distance fields) writes a source of its own with the same `sample`.\n\nimport * as THREE from \"three/webgpu\";\nimport { MAX_SPECIES } from \"./species.ts\";\nimport { abs, clamp, cos, dot, float, floor, fract, int, max, min, mix, normalize, sin, smoothstep, step, storage, texture, uniform, vec2, vec3 } from \"three/tsl\";\n\ntype Vec2 = THREE.Node<\"vec2\">;\ntype Float = THREE.Node<\"float\">;\ntype Vec3 = THREE.Node<\"vec3\">;\n\n/** A map's number for ground where nothing grows. */\nexport const NO_GRASS = 255;\n\n/** The ground's height (m) and its normal at a place. Sampled in the compute passes, in vertex shaders and in the canopy's fragment shader. */\nexport interface HeightSource {\n sample(xz: Vec2): { height: Float; normal: Vec3 };\n dispose(): void;\n}\n\n/**\n * The species at a place (its row in the table, or `NO_GRASS`) and the\n * metres from there to the nearest ground of another species. Sampled in\n * the compute pass and, for the ground's colour, in a fragment shader.\n */\nexport interface MapSource {\n sample(xz: Vec2): { species: Float; edge: Float };\n /**\n * The ground the map covers (m), if it says: the grass keeps a picture of\n * how tall it stands over that ground, for its shadows. Without it nothing\n * shades anything.\n */\n bounds?: { x0: number; z0: number; x1: number; z1: number };\n /**\n * The furthest `edge` is ever told (m), if the map has such a limit: a map of cells knows how far off\n * a border is only up to a cell or two, and says that much for everywhere deeper in.\n */\n edgeReach?: number;\n dispose(): void;\n}\n\n/** Heights (m) on a square grid: `nx` by `nz` posts `cell` metres apart, the first at (`x0`, `z0`), rows along x. */\nexport interface HeightGrid {\n heights: Float32Array;\n nx: number;\n nz: number;\n x0: number;\n z0: number;\n cell: number;\n}\n\n/** The height of a grid at a place, as the shaders have it (for the host's own use: a camera that walks the ground). */\nexport function heightAt(g: HeightGrid, x: number, z: number): number {\n const fx = Math.min(g.nx - 1.001, Math.max(0, (x - g.x0) / g.cell));\n const fz = Math.min(g.nz - 1.001, Math.max(0, (z - g.z0) / g.cell));\n const i = Math.floor(fx);\n const j = Math.floor(fz);\n const tx = fx - i;\n const tz = fz - j;\n const h = g.heights;\n const a = h[j * g.nx + i];\n const b = h[j * g.nx + i + 1];\n const c = h[(j + 1) * g.nx + i];\n const d = h[(j + 1) * g.nx + i + 1];\n return (a + (b - a) * tx) * (1 - tz) + (c + (d - c) * tx) * tz;\n}\n\n/** A height source from a grid of heights. The normals are worked out here. */\nexport function heightGrid(g: HeightGrid): HeightSource {\n const { nx, nz, cell } = g;\n if (g.heights.length !== nx * nz) throw new Error(`heights holds ${g.heights.length} numbers, not ${nx} x ${nz}`);\n const data = new Float32Array(nx * nz * 4);\n const at = (i: number, j: number) => g.heights[Math.min(nz - 1, Math.max(0, j)) * nx + Math.min(nx - 1, Math.max(0, i))];\n for (let j = 0; j < nz; j++) {\n for (let i = 0; i < nx; i++) {\n const dx = (at(i + 1, j) - at(i - 1, j)) / (2 * cell);\n const dz = (at(i, j + 1) - at(i, j - 1)) / (2 * cell);\n const len = Math.hypot(dx, 1, dz);\n data.set([at(i, j), -dx / len, 1 / len, -dz / len], (j * nx + i) * 4);\n }\n }\n const attr = new THREE.StorageBufferAttribute(data, 4);\n const posts = storage(attr, \"vec4\", nx * nz).toReadOnly();\n return {\n sample(xz) {\n const p = clamp(xz.sub(vec2(g.x0, g.z0)).div(cell), vec2(0), vec2(nx - 1.001, nz - 1.001));\n const pi = floor(p);\n const pf = fract(p);\n const i0 = int(pi.x);\n const j0 = int(pi.y);\n const i1 = int(min(pi.x.add(1), float(nx - 1)));\n const j1 = int(min(pi.y.add(1), float(nz - 1)));\n const a = posts.element(j0.mul(nx).add(i0));\n const b = posts.element(j0.mul(nx).add(i1));\n const c = posts.element(j1.mul(nx).add(i0));\n const d = posts.element(j1.mul(nx).add(i1));\n const height = mix(mix(a.x, b.x, pf.x), mix(c.x, d.x, pf.x), pf.y);\n const n = mix(mix(vec3(a.y, a.z, a.w), vec3(b.y, b.z, b.w), pf.x), mix(vec3(c.y, c.z, c.w), vec3(d.y, d.z, d.w), pf.x), pf.y);\n return { height, normal: normalize(n) };\n },\n dispose() {\n attr.dispose();\n },\n };\n}\n\n/** A species number to a cell: `nx` by `nz` cells of `cell` metres, the first cell's corner at (`x0`, `z0`), rows along x. */\nexport interface GrassMap {\n ids: Uint8Array;\n nx: number;\n nz: number;\n x0: number;\n z0: number;\n cell: number;\n}\n\n/** A map's cells are blurred over about this many of them to either side, which is what rounds its steps into a line. */\nconst SMOOTH_CELLS = 1.5;\n\nexport interface SpeciesMapSource extends MapSource {\n /** Replace the map's species numbers (the same size of map), as after painting on it. */\n set(ids: Uint8Array): void;\n}\n\n/**\n * A map source from a grid of species numbers. It keeps a channel to each species' row the map may hold\n * and one for ground where nothing grows, four to a layer of one array texture: `rows` says how many rows\n * that is (the highest number in the grid and one, if not said). A number from `rows` up is ground where\n * nothing grows, and so it stays if it is painted in later: a map that will be painted with more species\n * says so. However many rows, it is one texture to a shader that asks it (a stage may have sixteen in all,\n * and a host's ground shader that asks `groundShade` has its own to look up too).\n *\n * A grid's borders are steps a cell wide. So the grid is not looked up as\n * it is: each species' cells are spread into a smooth field of how much of\n * the ground about a place is that species', and a place belongs to\n * whichever species has most of it. Where two have as much as each other is\n * the border, which is then a clean line that cuts the grid's corners, the\n * same from any distance; and how far ahead the leader is says how far off\n * the border is. Features narrower than three or four cells are smoothed\n * away with the steps.\n */\nexport function speciesMap(m: GrassMap, rows?: number): SpeciesMapSource {\n const { nx, nz, cell } = m;\n const n = nx * nz;\n let highest = -1;\n for (let k = 0; k < n; k++) if (m.ids[k] < MAX_SPECIES && m.ids[k] > highest) highest = m.ids[k];\n const kept = Math.min(MAX_SPECIES, Math.max(1, rows ?? highest + 1));\n /** Channels: one to each species' row, and one for ground where nothing grows. */\n const CHANNELS = kept + 1;\n const LAYERS = Math.ceil(CHANNELS / 4);\n // (One texture of `LAYERS` layers, not a texture to a layer: every layer is then one binding.)\n const pixels = new Uint8Array(n * 4 * LAYERS);\n const data = Array.from({ length: LAYERS }, (_, l) => pixels.subarray(l * n * 4, (l + 1) * n * 4));\n const fields = new THREE.DataArrayTexture(pixels, nx, nz, LAYERS);\n fields.format = THREE.RGBAFormat;\n fields.type = THREE.UnsignedByteType;\n fields.magFilter = fields.minFilter = THREE.LinearFilter;\n fields.colorSpace = THREE.NoColorSpace;\n fields.generateMipmaps = false;\n /** Three passes of a box three cells wide: near enough a bell 1.4 cells to either side. */\n const smooth = (field: Float32Array) => {\n const other = new Float32Array(n);\n let from: Float32Array = field;\n let to: Float32Array = other;\n for (let pass = 0; pass < 3; pass++) {\n for (let j = 0; j < nz; j++) {\n for (let i = 0; i < nx; i++) {\n const k = j * nx + i;\n to[k] = (from[j * nx + Math.max(0, i - 1)] + from[k] + from[j * nx + Math.min(nx - 1, i + 1)]) / 3;\n }\n }\n [from, to] = [to, from];\n for (let j = 0; j < nz; j++) {\n for (let i = 0; i < nx; i++) {\n const k = j * nx + i;\n to[k] = (from[Math.max(0, j - 1) * nx + i] + from[k] + from[Math.min(nz - 1, j + 1) * nx + i]) / 3;\n }\n }\n [from, to] = [to, from];\n }\n return from;\n };\n const set = (ids: Uint8Array) => {\n if (ids.length !== n) throw new Error(`ids holds ${ids.length} numbers, not ${nx} x ${nz}`);\n for (let c = 0; c < CHANNELS; c++) {\n const out = data[c >> 2];\n const at = c & 3;\n // (A species the map has none of costs nothing to smooth.)\n let any = false;\n const field = new Float32Array(n);\n for (let k = 0; k < n; k++) {\n if ((ids[k] < CHANNELS - 1 ? ids[k] : CHANNELS - 1) === c) {\n field[k] = 1;\n any = true;\n }\n }\n const share = any ? smooth(field) : field;\n for (let k = 0; k < n; k++) out[k * 4 + at] = Math.round(share[k] * 255);\n }\n fields.needsUpdate = true;\n };\n set(m.ids);\n const size = vec2(nx, nz);\n return {\n set,\n bounds: { x0: m.x0, z0: m.z0, x1: m.x0 + nx * cell, z1: m.z0 + nz * cell },\n edgeReach: 1.25 * SMOOTH_CELLS * cell,\n sample(xz) {\n const t = xz.sub(vec2(m.x0, m.z0)).div(cell);\n // (Across a border the lead grows about as the distance over the blur's width does.)\n const uv = t.div(size);\n const taken = data.map((_, l) => texture(fields, uv).depth(int(l)).level(float(0)) as unknown as THREE.Node<\"vec4\">);\n return leaderOf(taken, uv, Array.from({ length: CHANNELS }, (_, k) => (k < CHANNELS - 1 ? k : NO_GRASS)), 1.25 * SMOOTH_CELLS * cell);\n },\n dispose() {\n fields.dispose();\n },\n };\n}\n\n/**\n * Which of several fields leads at a place, and by how much. `taken` is the fields looked up at the place\n * (at `uv`, which says only whether the place is on the map), four to a lookup. Each channel of each is one\n * surface's field: how much of the ground about a place is that surface's (or how far inside it the\n * place is: any measure that is highest for the surface the place belongs to, on one scale for all).\n * `species[k]` is the species (or `NO_GRASS`) of channel `k`, counted r, g, b, a through the textures\n * in turn. The border between two surfaces is where their fields are level, and `edgePerLead` is the\n * metres to it for a lead of 1 over the next.\n */\nconst leaderOf = (taken: readonly THREE.Node<\"vec4\">[], uv: Vec2, species: readonly number[], edgePerLead: number): { species: Float; edge: Float } => {\n const channel = (k: number) => [taken[k >> 2].r, taken[k >> 2].g, taken[k >> 2].b, taken[k >> 2].a][k & 3];\n // (Two surfaces may be one species: a species' share is its surfaces' most. The line between two surfaces of one\n // species is no edge, or every place along it is told it stands on one, and loses what grows only off edges.)\n const kinds = [...new Set(species)];\n const shares = kinds.map((sp) => species.map((s, k) => (s === sp ? k : -1)).filter((k) => k >= 0).map(channel).reduce((a, b) => max(a, b)));\n // The species with most of the ground here, and how far ahead of the next it is.\n let most = shares[0];\n let next = float(-1e6) as typeof most;\n let which = float(kinds[0]) as typeof most;\n for (let k = 1; k < shares.length; k++) {\n const leads = step(most, shares[k]);\n next = mix(max(next, shares[k]), most, leads);\n which = mix(which, float(kinds[k]), leads);\n most = max(most, shares[k]);\n }\n const inside = step(float(0), uv.x).mul(step(float(0), uv.y)).mul(step(uv.x, float(1))).mul(step(uv.y, float(1)));\n return { species: mix(float(NO_GRASS), which, inside), edge: most.sub(next).mul(edgePerLead) };\n};\n\n/** A host's own fields, one to a surface, as textures over a rectangle of ground. */\nexport interface FieldMap {\n /** The textures, each with up to four surfaces' fields in its channels. Looked up with whatever filter they are set to. */\n fields: readonly THREE.Texture[];\n /** The species' row (or `NO_GRASS`) for each channel, r, g, b, a through the textures in turn. Two surfaces may be one species. */\n species: readonly number[];\n /** The ground the textures cover (m): their first corner, and how far they go in x and in z. */\n x0: number;\n z0: number;\n width: number;\n depth: number;\n /** Metres to the border for a lead of 1 between the leading field and the next. */\n edgePerLead: number;\n}\n\n/**\n * A map source from a host's own fields: a course that already keeps a field for each of its surfaces\n * (how much of the ground about a place is green, fairway, rough, sand) has its map already. The place\n * belongs to the surface whose field is highest there.\n */\nexport function fieldMap(m: FieldMap): MapSource {\n if (m.species.length === 0 || m.species.length > m.fields.length * 4) throw new Error(`${m.species.length} species for ${m.fields.length} textures of four channels`);\n return {\n bounds: { x0: m.x0, z0: m.z0, x1: m.x0 + m.width, z1: m.z0 + m.depth },\n sample(xz) {\n const uv = xz.sub(vec2(m.x0, m.z0)).div(vec2(m.width, m.depth));\n return leaderOf(m.fields.map((tex) => texture(tex, uv).level(float(0)) as unknown as THREE.Node<\"vec4\">), uv, m.species, m.edgePerLead);\n },\n dispose() {},\n };\n}\n\n/**\n * How the mower left the grass at a place: the way its leaves were laid\n * over, as a vector along the ground (x, z), its length how fully (0 where\n * nothing was mown, 1 in the middle of a pass). Sampled in the compute pass\n * and, for the ground's colour, in a fragment shader.\n */\nexport interface MowSource {\n sample(xz: Vec2): Vec2;\n}\n\nexport interface MowStripes extends MowSource {\n /** A pass's width (m): the mower's. */\n width: number;\n /** The way the first pass was driven, in degrees from +z towards +x. Each pass beside it is driven back the other way. */\n heading: number;\n /** A place on the line between two passes. */\n x: number;\n z: number;\n}\n\n/**\n * Straight passes side by side, there and back: a fairway mown up and down.\n * The lay turns over across a hand's width where two passes meet. (Not yet:\n * the laps a mower makes round the edge first, or a second cut across the\n * first.)\n */\nexport function mowStripes(init: Partial<Pick<MowStripes, \"width\" | \"heading\" | \"x\" | \"z\">> = {}): MowStripes {\n const pattern = { width: init.width ?? 2.5, heading: init.heading ?? 0, x: init.x ?? 0, z: init.z ?? 0 };\n /** x, z, width, heading (radians). */\n const u = uniform(new THREE.Vector4());\n const put = () => u.value.set(pattern.x, pattern.z, Math.max(0.05, pattern.width), (pattern.heading * Math.PI) / 180);\n put();\n const source: MowSource = {\n sample(xz) {\n const along = vec2(sin(u.w), cos(u.w));\n const across = dot(xz.sub(vec2(u.x, u.y)), vec2(along.y, along.x.negate())).div(u.z);\n // (+1 down one pass, -1 back up the next, turning over between them.)\n const there = smoothstep(float(0.46), float(0.54), abs(fract(across.mul(0.5)).sub(0.5)).mul(2)).mul(2).sub(1);\n return along.mul(there);\n },\n };\n const settable = (key: keyof typeof pattern) => ({\n get: () => pattern[key],\n set: (v: number) => {\n pattern[key] = v;\n put();\n },\n enumerable: true,\n });\n return Object.defineProperties(source, { width: settable(\"width\"), heading: settable(\"heading\"), x: settable(\"x\"), z: settable(\"z\") }) as MowStripes;\n}\n"]}
@@ -0,0 +1,138 @@
1
+ import * as THREE from "three/webgpu";
2
+ /**
3
+ * How a head is built. "disc": petals about a centre, a daisy's (and with lift, a clover's ball or a
4
+ * buttercup's cup; with lift below 0, threads that hang). "spike": florets up a stalk's top, round a core
5
+ * (selfheal, plantain). "umbel": florets spread over a domed flat top (yarrow; a few large ones, a trefoil's
6
+ * pea flowers). "bell": a disc that hangs from its stalk, its petals a cup opening down (harebell).
7
+ */
8
+ export type FlowerForm = "disc" | "spike" | "umbel" | "bell";
9
+ export interface Flower {
10
+ name: string;
11
+ /** How the head is built (none said: "disc"). */
12
+ form?: FlowerForm;
13
+ /** Petals to a head, as drawn, or florets to a spike or an umbel: up to `MAX_PETALS`. (A daisy has thirty-odd rays; half as many, a little broader, read the same from a stride off.) */
14
+ petals: number;
15
+ /** A petal's (or floret's) length and its breadth at its widest (m). */
16
+ petalLength: number;
17
+ petalWidth: number;
18
+ /**
19
+ * How far the petals rise from flat, 0 (a disc: a daisy open in the sun) to 1 (a ball: a clover's
20
+ * head); below 0 they hang (-1: straight down, as a grass's stamens do). A spike's or an umbel's
21
+ * florets, from lying out (0) to standing up (1).
22
+ */
23
+ lift: number;
24
+ /** The breadth of the disc at the head's middle (m), or of a spike's core. */
25
+ centre: number;
26
+ /** A spike's length, or an umbel's breadth from its middle to its edge (m). Not used by a disc or a bell. */
27
+ length?: number;
28
+ /** How tall the centre is, 0 (a low dome: a daisy's) to 1 (a knob under the petals: a knapweed's). */
29
+ centreTall?: number;
30
+ /** How tall it stands (m). In grass taller than that it reaches up a little towards the light, and no more. */
31
+ stem: number;
32
+ /**
33
+ * How far its colonies reach (m, the radius in which most of one's flowers stand): a plant that creeps
34
+ * by runners or rhizomes (yarrow, creeping buttercup, white clover) makes broad ones; one that only
35
+ * seeds near itself, small ones. Its colonies stand some three times this apart where they are many, a
36
+ * few flowers stray between them, and none of it where the ground does not suit it. Left out: 1.
37
+ */
38
+ colony?: number;
39
+ /**
40
+ * The least of open ground's light it flowers in, 0..1 (by its Ellenberg light value: 8, rarely below
41
+ * 0.4; 7, 0.3; 6, 0.2). Between that and the open it flowers less the less light it has. Left out: 0.3.
42
+ */
43
+ light?: number;
44
+ /** Hex colours, sRGB. */
45
+ petalColor: string;
46
+ centreColor: string;
47
+ }
48
+ /**
49
+ * One flower in a species' grass (`Species.blooms`): which, how many, on how much of the ground, and where.
50
+ */
51
+ export interface Bloom {
52
+ /** The flower, by its name in the flowers the grass was given (`FLOWERS`, and the host's own). */
53
+ flower: string;
54
+ /** Flowers to the square metre in the heart of one of its colonies (`Flower.colony`). All a species' blooms together come to at most about 40: further off a flower is a dot, and there are only so many dots. */
55
+ perM2: number;
56
+ /** The share of the stand's ground its colonies take, 0..1, where all the ground suited it (about half does): flowers come in colonies, not evenly, each of a species' flowers in colonies of its own. */
57
+ cover: number;
58
+ /**
59
+ * Where on the ground they take, -1..1: in the hollows, where water gathers and stays (-1: creeping
60
+ * buttercup), on the crowns and banks, which dry first (1: cat's-ear, clover on thin soil), or anywhere
61
+ * (0). By the ground's own shape, as a species' patches are (`Species.patchSite`).
62
+ */
63
+ site: number;
64
+ /**
65
+ * "drifts" (in its own colonies, `cover` of the ground); "patches": it flowers only in the species'
66
+ * patches, as white clover flowers where the clover is (`Species.patch`, which then says how much of
67
+ * the ground that is; `cover` is not used); or "tussocks": in its colonies, but only on the species'
68
+ * tussocks (`Species.tussock`), as a tussock grass's own flowering stems rise from its tussocks.
69
+ */
70
+ where: "drifts" | "patches" | "tussocks";
71
+ }
72
+ /** The most flowers one species has in it. */
73
+ export declare const MAX_BLOOMS = 4;
74
+ /** The most kinds of flower the grass draws at once, all its species' together. */
75
+ export declare const MAX_FLOWERS = 16;
76
+ /** The most petals a head is drawn with. */
77
+ export declare const MAX_PETALS = 16;
78
+ /** Four numbers to a slot, this many slots to a flower. */
79
+ export declare const FLOWER_SLOTS = 5;
80
+ /** A form's number in the flower table. */
81
+ export declare const FORM_CODE: Record<FlowerForm, number>;
82
+ /**
83
+ * Nineteen that grow in grass. Sizes from the floras (Stace; Clapham, Tutin and Moore).
84
+ *
85
+ * In mown grass: a daisy's head 16 to 25 mm across on a stalk 3 to 12 cm; a creeping buttercup's flower
86
+ * 2 to 3 cm, five glossy petals; a dandelion's head 25 to 40 mm of strap-shaped florets; white clover's a
87
+ * ball 15 to 20 mm across; slender speedwell, a pale blue flower 8 to 12 mm across, four petals, on a
88
+ * thread of a stalk; selfheal, a spike 1 to 3 cm of violet two-lipped flowers on dark calyces, on 5 to 30
89
+ * cm; kikuyu's own flowers, which are only their stamens, white threads about a centimetre long hanging
90
+ * out of the mat in the days after a mowing.
91
+ *
92
+ * In grass left long: ox-eye daisy, a head 25 to 50 mm across, on 20 to 70 cm; red clover, a ball 15 to
93
+ * 30 mm, pink-purple, on 10 to 40 cm; cat's-ear, a head 25 to 40 mm, on a branched stalk 20 to 60 cm;
94
+ * common knapweed, a brush of purple florets 2 to 4 cm across over a near-black knob of bracts, on 30 to
95
+ * 80 cm; yarrow, a flat top 4 to 10 cm across of white florets 4 to 6 mm, on 8 to 60 cm; harebell, a
96
+ * nodding blue bell 12 to 20 mm long on a hair-thin stalk 15 to 40 cm, in dry grassland, heath and
97
+ * dunes; bird's-foot trefoil, two to seven yellow pea flowers 10 to 16 mm in a head, on 10 to 40 cm;
98
+ * ribwort plantain, a dark brown spike 1 to 4 cm ringed with pale stamens, on 10 to 40 cm. And a grass's
99
+ * own: marram's, a dense pale spike 7 to 22 cm long and 1.5 to 2 cm thick, over its leaves.
100
+ *
101
+ * On a coastal meadow the sea reaches in winter, among red fescue and saltmarsh rush: thrift, a round
102
+ * pink head of small flowers 1.5 to 3 cm across on a bare stalk, the plant 20 to 30 cm (Flora of Victoria),
103
+ * its stalks up to 40 (drawn at 30); sea aster, a head 1 to 4 cm across (commonly 2) of pale violet rays round a
104
+ * yellow disc, on 15 to 60 cm, from July to September (iFlora; Flora Europaea's subsp. tripolium of the
105
+ * North Sea and the Baltic); sea plantain, a lax spike 3 to 7 cm long and 3 to 4 mm thick of greenish
106
+ * flowers with pale yellow stamens, on a stout stalk 5 to 20 cm (Flora Europaea; Gleason and Cronquist).
107
+ *
108
+ * Where they grow (`colony`, `light`). How far a plant creeps each year from Pladias (Kaplan et al. 2019,
109
+ * clonal traits from CLO-PLA; pladias.cz): white clover 27 cm, yarrow 14, speedwell 13, harebell 11,
110
+ * daisy 10, cat's-ear and meadow buttercup 2, knapweed 1; bird's-foot trefoil, red clover and the
111
+ * dandelion not at all, only by seed. Seed falls within a few metres of its plant: a grassland orchid's
112
+ * seedlings gather within 4 to 5 m of it (Jacquemyn et al. 2007, New Phytologist). No one has fitted the
113
+ * colonies of these flowers themselves, so their sizes are set from those: 1.2 m for one that only seeds,
114
+ * to 2.5 m for a creeper, and marram (its rhizomes run 2 m in half a year) and kikuyu 3 m. The least light
115
+ * from their Ellenberg values (Pladias): 8 for cat's-ear, daisy and knapweed, rarely in less than 0.4 of
116
+ * the open's light; 7 for most, 0.3; 6 for germander speedwell, 0.2 (slender speedwell taken as it).
117
+ * Marram's, kikuyu's, selfheal's and plantain's were not looked up: 0.4, 0.4, 0.3, 0.3; nor the coast's
118
+ * three, which grow in the open and are given 0.4. None of the three creeps far: thrift a cushion, sea aster
119
+ * by seed and a short rootstock, sea plantain a rosette: 1.2 to 1.4 m.
120
+ *
121
+ * Colours by eye. Each form is as near as so few surfaces come: a trefoil's pea flower is drawn as a
122
+ * cupped petal, a spike's florets as petals round a core, and a stalk has no branches or leaves.
123
+ */
124
+ export declare const FLOWERS: Record<string, Flower>;
125
+ /** One uniform-array entry to a slot, a row to a flower: made once, then written into by `packFlowers`. */
126
+ export declare function makeFlowerTable(): THREE.Vector4[];
127
+ /**
128
+ * Write the flowers `used` names into the table the shaders read, in that order, and say which row each
129
+ * is. The slots:
130
+ *
131
+ * 0 a petal's length and width, its lift, the centre's breadth
132
+ * 1 petals, how tall it stands, its form (`FORM_CODE`), length
133
+ * 2 the petals' colour (linear r, g, b), how far its colonies reach (m)
134
+ * 3 the centre's colour (linear r, g, b), how tall the centre is
135
+ * 4 the least light it flowers in, and three to spare
136
+ */
137
+ export declare function packFlowers(used: readonly string[], table: THREE.Vector4[], flowers: Readonly<Record<string, Flower>>): Map<string, number>;
138
+ //# sourceMappingURL=flowers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flowers.d.ts","sourceRoot":"","sources":["../src/flowers.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAEtC;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAC;AAE7D,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,iDAAiD;IACjD,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,yLAAyL;IACzL,MAAM,EAAE,MAAM,CAAC;IACf,wEAAwE;IACxE,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,8EAA8E;IAC9E,MAAM,EAAE,MAAM,CAAC;IACf,6GAA6G;IAC7G,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,sGAAsG;IACtG,UAAU,CAAC,EAAE,MAAM,CAAC;IACp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