webgpu-grass 0.0.0-stage → 0.1.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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +647 -2
  3. package/lib/distance.d.ts +48 -0
  4. package/lib/distance.d.ts.map +1 -0
  5. package/lib/distance.js +134 -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 +3247 -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/README.md CHANGED
@@ -1,3 +1,648 @@
1
- # Temporary Holding Version
1
+ # Grass
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Grass for three.js on WebGPU, written in TSL: fields of real leaves, from a
4
+ 3.5 mm putting green to a knee-deep meadow, by species, with a lab to set
5
+ them in. It began as the grass of Couch Golf Club, a browser golf game, and
6
+ was pulled out so any three.js project can use it.
7
+
8
+ It is early. It is not on npm yet: take `src/` as it is, or run the lab.
9
+ WebGPU only (three.js r187, `three/webgpu`).
10
+
11
+ ```
12
+ src/ the package: imports three and nothing else
13
+ lab/ the lab: a plot of ground and a panel to edit the grass on it
14
+ test/ the tests
15
+ ```
16
+
17
+ `pnpm test` holds the two walls: the package imports only three, and the lab
18
+ reaches the package only through `src/index.ts`, so the lab can do only what
19
+ your project could. It also checks the placement's twin in plain numbers
20
+ against the plants' ranks and hashes (`test/placement.test.mjs`).
21
+
22
+ ## The lab
23
+
24
+ From the workspace root:
25
+
26
+ ```bash
27
+ pnpm install
28
+ pnpm lab:grass
29
+ ```
30
+
31
+ The lab opens at <http://localhost:5174/lab/>. Drag to look, W A S D to walk, the wheel for eye
32
+ height. `?plot=3` fills the plot with one species (its row), `?eye=0.15`
33
+ sets the eye height, `?fresh` ignores what the lab kept from last time.
34
+
35
+ A first visit opens on the *Gallery* (G): fourteen scenes, each one press, each
36
+ setting everything the lab has (the species, what grows on the plot, the
37
+ eye, the hour, the wind, the mowing, the style). The scene that is on is
38
+ named at the foot of the picture, with the ones either side a press away
39
+ (`[` and `]`); `?scene=striped-fairway` opens on one. They are data
40
+ (`lab/scenes.ts`), and their pictures in the gallery were taken from this
41
+ lab (`lab/thumbs`). *Dawn*, *Morning*, *Noon* and *Evening* on the bar set
42
+ the sun, and the sky and the light's colour follow its height.
43
+
44
+ The bar along the top chooses what is looked at and from how high. The rail
45
+ on the left chooses a species; the inspector on the right changes it
46
+ (*Species*) or what holds for all of them (*Scene*: view, style, light and
47
+ wind, field). *Preset* shows all of it as JSON, and loads JSON back. H puts
48
+ the panels away.
49
+
50
+ *Blade*, *Plant*, *Tuft* and *Clump* on the bar put the chosen species on a
51
+ bench in place of the plot: one blade, one plant, one tuft as the far field
52
+ draws it, or one clump (`?show=blade`). It is drawn by the field's own leaf
53
+ shader (`grass.createSpecimen`), so it is what the field draws, seen close.
54
+
55
+ *Leaf picture* puts a picture on a species' leaves: its light and dark over
56
+ the leaf's own colour, or its colour as the leaf's, and optionally its
57
+ transparency as the leaf's outline. The lab draws nine of its own, by arithmetic and from no photograph or
58
+ bought texture, so they can live here: five leaf surfaces (fresh, flecked,
59
+ olive, spent, a stalk) and four simpler ones
60
+ (`lab/leafart.ts`) and takes more from files (several at once). A picture from a file is not
61
+ kept in a preset: only its name is.
62
+
63
+ ## The contract
64
+
65
+ ```ts
66
+ const grass = createGrass({
67
+ height: heightGrid({ heights, nx, nz, x0, z0, cell }),
68
+ map: speciesMap({ ids, nx, nz, x0, z0, cell }),
69
+ species: STARTER_SPECIES,
70
+ });
71
+ scene.add(grass.object);
72
+ // each frame, before rendering:
73
+ grass.update(renderer, camera, timeSeconds, lookAt);
74
+ ```
75
+
76
+ - **Height** and **map** are sources: objects the shaders sample at any
77
+ place. `heightGrid` and `speciesMap` make them from plain grids. A host
78
+ with its own terrain writes its own (`HeightSource`, `MapSource`).
79
+ - **Species** are rows of plain data (`species.ts`): height, leaf width, how
80
+ the tip ends, splay, stiffness, clumping, colour and so on. The shaders
81
+ name no species; they read the row. Nor need a row be a grass: `stem`
82
+ makes its leaves round, stiff stems and `tussock` stands it in tussocks
83
+ with open ground between, which together are a rush (`MORE_PLANTS.rush`,
84
+ soft rush, for a marsh; the lab's "Soft rush" scene), and `flower` puts a
85
+ rush's brown tuft of flowers on the side of some of its stems. Up to
86
+ sixteen species (`MAX_SPECIES`). What a species costs is mostly its
87
+ ground picture, 4 MB here and a third more on the graphics card, and the
88
+ grass keeps as many as it is given species; `speciesMap` keeps a channel
89
+ to each row its map may hold (`rows`), four to a layer of one array texture.
90
+ - **Style** (`grass.style`) is how it is drawn, apart from what it is.
91
+ - **Distance** (`options.distance` / `grass.distance`) lets a host switch each distance technique on or off (`frustumCull`, `horizonCull`, `grazingLod`, `horizonLod`, `hexGround`, `windSheen`—all on by default; `distance: false` turns all off):
92
+ - `frustumCull`: Gribb-Hartmann clip-space sphere culling in compute (keeping near shadow-casters within 2.6 m).
93
+ - `horizonCull`: 6-step heightfield ray occlusion culling behind hills and dunes.
94
+ - `grazingLod`: denser silhouettes along ridges and faster ground fade when looking steeply down.
95
+ - `horizonLod`: procedural 16-triangle horizon blade ring out to 84 m with constant-density handoff and sub-pixel tuft feathering.
96
+ - `hexGround`: variance-preserving 3-tap simplex hex-tiling on distant ground (7–84 m) with zero extra texture bindings.
97
+ - `windSheen`: analytical wind-tilted canopy reflectance and sheen waves on distant tall grass.
98
+ - **Transitions** (`options.transitions` / `grass.transitions`) exposes runtime shader uniforms for every distance border, sub-pixel fade, fine-to-tuft handoff (`tuftStart`, `tuftFull`, `tuftMinDist`, `unseenTufts`, `tuftWidthScale`, `tuftMinWidth`, `tuftShadeMatch`), radial falloff (`reachScale`, `falloffPower`), ground relief/shade curve (`groundShadeLift`, `groundDetailTo`, `groundGrainTo`), horizon crossfade (`horizonStart`, `horizonEnd`), and stand border ramp (`edgeRamp`, `edgeBlendScale`).
99
+ - **Light and wind** (`grass.environment`) are the host's to set.
100
+ - **The ground** is the host's mesh. `grass.groundColor(xz)` and
101
+ `grass.light(normal)` give it the colour and light the grass takes, so the
102
+ two are one colour and nothing shows where the leaves thin out.
103
+
104
+ ## Heights of cut
105
+
106
+ `TURF_SPECIES` are a golf course's surfaces at heights in real use:
107
+
108
+ | Surface | Here | In use |
109
+ | --- | --- | --- |
110
+ | Green | 3.2 mm | 2.3 to 3.2 mm at US championships, 3.5 to 4.5 mm at Opens, 4 to 7 mm UK summer play |
111
+ | Collar | 8 mm | 6 to 9 mm at championships, 10 to 12 mm everyday; about 1.2 m wide |
112
+ | Fairway | 11 mm | 8 to 12 mm at championships, 9 to 13 mm US, 14 to 19 mm UK |
113
+ | First cut | 32 mm | 25 to 35 mm, one mower's pass (about 2 m) to 4 m wide |
114
+ | Rough | 64 mm | about 64 mm for member play, 76 to 89 mm at a US Open, 100 mm UK |
115
+ | Deep rough | 125 mm | 100 to 150 mm |
116
+ | Fescue in seed | 30 cm | 15 to 30 cm standing, unmown or cut once a year |
117
+ | Hay meadow | 60 cm | 50 cm to 1 m before the cut |
118
+
119
+ From STRI and European Tour guidance, the USGA's fairway article, and
120
+ championship set-up sheets (GCSAA, Oakmont, the Open venues). Each mown band
121
+ is a separate mower at a fixed height, so its edges are steps
122
+ (`edgeBlend` 0); rough into native grass is not kept, and is not sharp.
123
+
124
+ ## Other courses' grasses
125
+
126
+ `GRASSES` holds every species here by a name, and a host makes its own list
127
+ from it, as a course has the grasses it has: `TURF_SPECIES` (a cool, wet
128
+ parkland's: bent greens, rye fairways), `MORE_TURF` (a bermuda fairway, a
129
+ bluegrass lawn), `MORE_PLANTS` (soft rush) and `MORE_GRASSES`:
130
+
131
+ | Name | What | Height |
132
+ | --- | --- | --- |
133
+ | `poaGreen` | A green gone over to annual meadow-grass: lighter, broader-leaved, seeding at a green's height, the bent left in it in darker patches | 3.2 mm |
134
+ | `kikuyuFairway`, `kikuyuRough` | Kikuyu, a warm coast's: a dense springy mat of broad stiff folded leaves | 13 mm, 50 mm |
135
+ | `fescueGreen`, `fescueFairway` | A links': fine fescue, needle-leaved, the densest turf, grey-green and browning on the crowns where it is not watered | 4.5 mm, 10 mm |
136
+ | `marram` | The dunes behind a links: rolled grey-green leaves in tussocks, many dead | 80 cm |
137
+
138
+ The lab's *A links*, *Marram on the dunes* and *Kikuyu on a warm coast*
139
+ show them. Their habit was set by eye, as the others' was. Kikuyu has its
140
+ own flowers, its stamens: white threads out of the mat after a mowing.
141
+ Marram carries its pale seed spikes on a few of its stems (`seedShare`,
142
+ the share of a species' stems in seed, which then stand a fifth over the
143
+ leaves) and stands in tussocks further apart than the field's clumps
144
+ (`clumpScale`, a multiple of `look.clumpSize`), with open ground between.
145
+
146
+ ## Native grass, as mixes
147
+
148
+ Ground nobody mows but once a year is never one grass. `NATIVE_GRASSES`
149
+ holds the grasses such ground is made of, each as it stands in high
150
+ summer, gone to seed, with leaves and stems from the floras (Kew's
151
+ GrassBase, the Flora of New Zealand, Flora of Victoria, Flora of North
152
+ America):
153
+
154
+ | Name | What | Leaf |
155
+ | --- | --- | --- |
156
+ | `sheepsFescue` | Dry, thin, sandy and acid ground: bristle leaves in dense grey tufts with ground between | 0.3–0.6 mm |
157
+ | `commonBent` | Flat green leaves and a loose panicle of purplish-brown spikelets, a reddish haze in seed | 1–2.5 mm |
158
+ | `yorkshireFog` | A damp, rich meadow's: soft, hairy, pale grey-green leaves, pinkish panicles | 3–10 mm |
159
+ | `sweetVernal` | The first meadow grass to flower: a gold, spike-like panicle | 2–6.5 mm |
160
+ | `wavyHairGrass` | Acid heath: thread leaves in tufts, silvery-purple panicles on wavy branches | 0.3–0.6 mm |
161
+ | `creepingBent` | Left unmown on a wet or grazed coastal meadow, with red fescue: runners, flat leaves, a purplish panicle | 0.5–5 mm |
162
+ | `saltmarshRush` | A rush of coastal meadows the sea reaches in winter: wiry dark stems in loose tufts | stems |
163
+
164
+ A stand of several is a mix: one grass is the stand's body (its lead)
165
+ and the others grow in it in patches of their own sizes.
166
+ `mixedMap(source, mixes)` wraps a host's map: where the map says a mix's
167
+ lead, each member takes its share of the place in colonies about its
168
+ `patchSize` across, and the lead keeps the rest.
169
+
170
+ ```ts
171
+ const map = mixedMap(fieldMap({ ... }), [
172
+ // Row 8 is red fescue in seed; 9 sheep's fescue, 10 common bent: a links' rough.
173
+ { species: 8, members: [{ species: 9, share: 0.3, patchSize: 1.5 }, { species: 10, share: 0.15, patchSize: 2 }] },
174
+ ]);
175
+ ```
176
+
177
+ The ground between a stand's stems is not the same everywhere: sand under
178
+ a links' thin fescue, peat on a heath, mud in a wet meadow.
179
+ `Species.soil` is what the ground's colour is multiplied by under that
180
+ species (the host's ground, under `groundShade`), so a mix's thin patches
181
+ can show their own soil.
182
+
183
+ A member may be `NO_GRASS` instead of a row: bare ground in patches,
184
+ where the host's own ground shows (sandy scrapes on a links, thin ground
185
+ on a heath).
186
+
187
+ The members are rows of their own in the species list, so a mix's flowers
188
+ are its members' (`Species.blooms`), up to four to each. A patch is part
189
+ of its stand: the place's distance to another stand stays the map's, so
190
+ tufts, flowers and a stand's straying edge are as they were, and two
191
+ grasses of a mix run into each other at a patch's edge as far as each
192
+ lets its edge stray (`edgeBlend`). A member costs nine looks at a hash for
193
+ every place the grass asks about.
194
+
195
+ A member's `chance` (that a square of its has a colony) may be given
196
+ instead of worked out from its share: a host whose physics plays the same
197
+ patches (bare ground a ball lies on) gives the physics' own number, so the
198
+ two have the very same colonies.
199
+
200
+ The colonies are worked out by plain arithmetic in `src/mixshare.ts`,
201
+ which the shaders' half follows step for step: `mixAt(mix, chances, x, z)`
202
+ says which grass grows at a place, and `test/mix.test.mjs` holds each
203
+ member to its share within two points. `mixChances` throws on a mix that
204
+ cannot be had: members taking more than the whole stand, or one wanting
205
+ more of what the members before it leave than its colonies can cover
206
+ (`mostCover`, about three fifths).
207
+
208
+ The habit and the colours were set by eye from the floras' words. A
209
+ grass's seed heads stand a fifth over its leaves, whatever the floras say
210
+ (wavy hair-grass's stems stand far over its leaves); how big the patches
211
+ of each grass are in a real stand was not found, and the sizes in the
212
+ example are guesses from how each spreads.
213
+
214
+ ## Where the numbers come from
215
+
216
+ A species' leaf breadth and, for the grass nobody mows, its height are set
217
+ against these floras, which can be read and reused freely:
218
+
219
+ | Source | What | Terms |
220
+ | --- | --- | --- |
221
+ | [Flora of Australia](https://profiles.ala.org.au/opus/foa) | The turf grasses as they grow wild there: creeping bent, perennial rye, Kentucky bluegrass, annual meadow-grass, red fescue, bermuda; JSON for each | CC BY, © Commonwealth of Australia |
222
+ | A. S. Hitchcock, *Manual of the Grasses of the United States*, 2nd ed. (USDA, 1950), [Internet Archive](https://archive.org/details/manualofgrasseso200hitc_0) | Creeping bent, annual meadow-grass, American and European beachgrass | Public domain (a US government work) |
223
+ | *Flora Iberica* XVII and XIX(II), through [Plazi](https://tb.plazi.org) | Marram, kikuyu, soft rush | Plazi publishes its treatments as CC0 |
224
+ | Wessapak et al., Cenchrus in Thailand, *PhytoKeys* 234 (2023) | Kikuyu | CC BY |
225
+
226
+ Each species' note in `species.ts` gives the figures it was set against.
227
+ A mown grass is drawn at the narrow end of its range, as a mown leaf is.
228
+ The floras give no petal counts or flower sizes in a form that can be
229
+ reused, so the flowers' are from the floras' prose (Stace; Clapham, Tutin
230
+ and Moore). How a leaf leans, droops and twists is not measured anywhere
231
+ here: it was set by eye.
232
+
233
+ ## Where its light and wind come from
234
+
235
+ - **Light lost in the stand.** Roots are as dark as exp(-K L) makes them
236
+ (Monsi and Saeki), with the leaf area index from measured mown turf (Lee
237
+ et al. 2011) and K from the sun's height (Campbell and Norman 1998).
238
+ `look.depth` sets how far that is followed over a species' own
239
+ `rootShade`; it is half by default, by eye.
240
+ - **A leaf's shine** is white, Fresnel-weighted and narrower along the leaf
241
+ than across (roughness 0.18 and 0.46, from wheat leaves: Comar et al.).
242
+ **Light through it** follows Barre-Brisebois and Bouchard (GDC 2011).
243
+ - **Wind.** A two-scale gust field scrolled downwind, as Sucker Punch
244
+ describe for Ghost of Tsushima (GDC 2021); over it every leaf swings in a small figure of eight, which
245
+ Guerrilla found is most of what reads as wind (Sanders, GDC 2018).
246
+ - **Patches.** A species can have part of its ground in patches of another
247
+ colour and height (`patch`, `patchSize`, `patchTint`, `patchTall`):
248
+ annual meadow-grass in a bent green, green left in a parched lawn. They
249
+ are a tint and a height, not other plants: no clover leaf is drawn.
250
+ `patchKind` says which of three things they are. Colonies: discs grown
251
+ out from where a plant landed, many small and few large, alike within
252
+ and each a little unlike the next, gathered in drifts, with seedlings
253
+ about them (annual meadow-grass on greens was measured at 10 cm to 2.5 m
254
+ across, 50 cm on average: Poole and Johnston, WSU). Runners: the same for
255
+ a plant that spreads by its runners and hardly by seed, so no seedlings
256
+ (creeping bent in a meadow-grass green, white clover). Blotches: odd
257
+ shapes with hazy edges, as drought makes. A colony's edge is ragged at a
258
+ few runners' breadth, and no colony is cut off by the grid it is placed
259
+ on.
260
+ Where patches get in follows the lie of the ground (`patchSite`, below)
261
+ and `patchEdge`: towards the stand's edge and into the trees' shade, as
262
+ annual meadow-grass comes into a bent green from the collar and takes the
263
+ shaded ground first (USGA), or away from them, as the bent left in a
264
+ meadow-grass green holds out in the open middle.
265
+ - **Strains.** A green is not one plant: a bent sown from seed bred of a
266
+ few parents, or a green grown over by many strains of annual
267
+ meadow-grass, comes to be a quilt of touching patches each a shade unlike
268
+ the next (USGA: "a patchwork quilt"), most coming out of winter and going
269
+ into it and hardly at all in summer. `mosaic` says how unlike (a share of
270
+ the colour: two or three per cent, set by eye; five read as camouflage)
271
+ and `mosaicSize` how broad; `look.strains` how much the season shows it.
272
+ Where two strains meet the line is soft.
273
+ - **A dry spell.** `look.drought` browns the mown grass that is prone to
274
+ it (`scorch`): small spots a hand to a foot across, gathered on the
275
+ crowns, and a ring a mower's width in from the stand's edge, where the
276
+ clean-up pass turns, as greens go first (USGA; K-State on localized dry
277
+ spot). Straw, never bare. How dry a course is, is the host's to say.
278
+ - **The lie of the ground.** Where the map gives its `bounds`, each place
279
+ is known as a crown (above the ground 2.5 m round it) or a hollow
280
+ (below the ground 9 m round it). The grass is lusher in the hollows and
281
+ paler on the crowns (`look.site`), and a species' patches take to one or
282
+ the other (`patchSite`): meadow-grass to the wet, drought to the crowns.
283
+ It is the ground's shape only: not shade, not which way a slope faces,
284
+ not where the sprinklers reach.
285
+ - **Wild turf from a way off.** From about 8 m out, a species that is not
286
+ mown (its tip not cut or square, and not laid by the roller while the
287
+ field is mown) gets tussock clumps, drifts of dry thatch and hummock
288
+ relief, so a far meadow does not read as lawn. `look.wild` (0..1,
289
+ default 1) sets how strongly; a host whose ground already carries its
290
+ own drift of colour turns it down or off (0), since the two patterns stack.
291
+ - **Mowing stripes** are off unless `look.mown` is set: then leaves are
292
+ laid one way and back in passes (`mowStripes`). Nothing is painted: a
293
+ stand of leaves that all face one way sends back light by how much of
294
+ each face the sun strikes and the eye sees (single scattering in an
295
+ oriented canopy, after Ross 1981), with a leaf's under face darker than
296
+ its upper. So the stripes are strong from the air, faint from a stance
297
+ on the fairway, and change with the sun and as the eye goes round.
298
+
299
+ - **Colour.** A species' colour is a leaf's, worked out with the PROSPECT-D
300
+ leaf model (Feret et al. 2017): what a leaf holding so much chlorophyll
301
+ sends back of sun and sky, as linear sRGB, every species at one
302
+ brightness. A grass leaf sends back about half as much red as green and
303
+ a quarter as much blue, which is a good deal more red than a green
304
+ picked by eye, and what the photographs show too. Through a leaf comes
305
+ as much green, nearly as much red and almost no blue, and the light
306
+ through the leaves is tinted so. The chlorophyll per square centimetre
307
+ is an estimate for each grass (38 to 60 ug): no source gives it species
308
+ against species. A leaf's wax, which greys fescue and bluegrass, is not
309
+ in the model: it is added as a little more light sent back, most of it
310
+ blue, by estimate.
311
+
312
+ ## Flowers
313
+
314
+ A species can have up to four flowers in it (`blooms`). Each names a
315
+ flower (`"daisy"`, `"buttercup"`, `"dandelion"`, `"clover"`,
316
+ `"speedwell"`, `"selfheal"`, `"kikuyu"`, `"oxeye"`, `"redClover"`,
317
+ `"catsear"`, `"knapweed"`, `"yarrow"`, `"harebell"`, `"birdsfoot"`,
318
+ `"plantain"`, the coast's `"thrift"`, `"seaAster"` and `"seaPlantain"`,
319
+ or one of the host's own, given as `flowers` when the grass
320
+ is made) and says how many to the square metre in the heart of one of its
321
+ colonies (`perM2`), the share of the stand's ground its colonies take
322
+ (`cover`), and where on the ground they take (`site`: creeping buttercup in
323
+ the wet hollows, cat's-ear on the dry banks, by the lie of the ground as a
324
+ species' patches go). A flower with `where: "patches"` grows only in the
325
+ species' patches, as white clover flowers where the clover is. Up to
326
+ sixteen kinds of flower are drawn at once.
327
+
328
+ Flowers grow as the plants of open grassland do, not evenly. Each flower
329
+ comes in colonies, a plant's runners or its own seed about where it took:
330
+ thickest at a centre and thinning out from it, some small and a few large
331
+ (`Flower.colony`, the radius most of one's flowers stand in, broad for a
332
+ creeper such as yarrow or white clover, small for one that only seeds near
333
+ itself). Ecologists draw clumped plants the same way (a Thomas cluster
334
+ process); no one has fitted one to these flowers, so the sizes are set from
335
+ how far each creeps in a year and how far seed falls (see `FLOWERS`). The colonies stand only where the ground suits that flower: a
336
+ slow noise of its own over some thirty metres, so one bank has it and the
337
+ next has none; the lie of the ground (`site`); and out of the trees' shade,
338
+ where a host says how much of the day's light a place goes without
339
+ (`world.shelter`; `Flower.light`, the least light it flowers in). A few
340
+ strays grow between colonies, and a flower's heads come up a few together,
341
+ from one plant. So most of a stand's
342
+ ground has none of a given flower and some has it thick. A flower stands
343
+ at its own height; in grass as tall as it is or taller it reaches up by up
344
+ to two fifths again, and no more, so a short flower in long grass is down
345
+ among the leaves.
346
+
347
+ A grass's seed heads (`seedShare`, the share of its stems in seed) come in
348
+ patches too: some ground a few metres across thick with heads and the next
349
+ with hardly one, and a slower swell over tens of metres; `seedShare` is
350
+ the share over the stand as a whole.
351
+
352
+ A `Flower` is plain data, as a species is: its form, how many petals, how
353
+ long and broad, how far they rise from flat (or, below nothing, hang), the
354
+ centre's breadth and how tall it is, how tall it stands, two colours. The
355
+ forms: a disc, petals about a centre (a daisy; with lift, a clover's ball;
356
+ with a tall centre, a knapweed's brush over its dark knob; hanging, a
357
+ grass's stamens); a spike, florets up a core (selfheal, plantain); an
358
+ umbel, florets over a low domed top (yarrow; a few large ones, a trefoil's
359
+ pea flowers); a bell, a cup that hangs from its stalk (harebell). Each is
360
+ as near as so few surfaces come: a pea flower is a cupped petal, a stalk has
361
+ no branches or leaves, and heather, a shrub, is not a flower in grass.
362
+
363
+ Within a few strides of the eye a flower is its petals, its centre and its
364
+ stalk; from there to about nineteen metres it is a dot of its colour, and
365
+ beyond that nothing. It nods in the wind and is lit and shaded as the
366
+ grass about it is, but not tinted as the grass is to match its ground: a
367
+ host that puts its ground's colours in `world.tint` gives the light alone
368
+ as `world.light`, which flowers take. `look.blooms = false` draws none.
369
+ Where each place has a flower, and which, is worked out once a frame in a
370
+ compute pass, which passes over the places that cannot have one or could
371
+ not be seen (no flowers in the species, too near or too far for the
372
+ square, behind the eye) before the colonies are looked for, and the
373
+ flowers are drawn without the empty places: `stats.flowersDrawn` says how
374
+ many, when `grass.countDrawn` is asked. In `TURF_SPECIES` the mown surfaces, the rough among them,
375
+ have none: a course keeps them clear. The deep rough has cat's-ear,
376
+ bird's-foot trefoil, ox-eye daisies and creeping buttercup; the fescue
377
+ cat's-ear, ox-eye daisies, harebells and yarrow; the meadow buttercups, red
378
+ clover, ox-eye daisies and knapweed. A lawn (`MORE_TURF.bluegrass`) has
379
+ daisies, clover and speedwell. Which grow where is from the floras; how
380
+ many was set by eye.
381
+
382
+ Not done: a ball or a foot does not flatten or hide a flower, flowers cast
383
+ no shadow, nothing flowers by the season (a host that has a date can
384
+ change `blooms`), and a daisy's thirty-odd rays are drawn as sixteen.
385
+
386
+ The way a head is built, each petal a small curved surface with nearer
387
+ and further drawings of it, follows
388
+ [Inkwell's WebGPU flower field](https://github.com/siliconjungle/inkwell-webgpu-flowers)
389
+ (MIT). None of its code or art is used.
390
+
391
+ ## Things in the grass
392
+
393
+ `grass.setThings([{ x, y, z, radius, clear, keepSeen }])` tells the grass
394
+ what lies in it: up to four spheres (a ball, a stone). The grass under one
395
+ is pressed down beneath it, no leaf goes through it, and the leaves against
396
+ it are held still in the wind. `clear` is a nest pressed round it. With
397
+ `keepSeen` the leaves between the eye and the thing are kept under
398
+ the line of sight to its middle, so its top half shows and how deep it sits
399
+ can be read off: grass taller than a ball otherwise hides the ball. Give
400
+ `keepSeen` a share, 0 to 1, and that much of the thing shows from its top
401
+ down (`true` is a half). The thing itself is the host's to draw, and it
402
+ casts no shadow on the grass yet.
403
+
404
+ A thing with `flat: true` is not a sphere but a place something stands
405
+ on, a foot: the grass there is pressed to a third of its height.
406
+
407
+ `grass.setTrail(points)` is where something has gone through the grass: the
408
+ grass is laid over along the track as the thing passes and is back up in
409
+ about a second, since dry grass keeps no track. Wet grass (`look.wet`, 0 to
410
+ 1) is slower up and its leaves along the track are a little darker for some
411
+ minutes: a faint line, on the leaves only. A dew track as a green shows it
412
+ (dark through pale) is not drawn yet.
413
+
414
+ The grass remembers what went through it: a grid of 4 cm cells over the 30 m
415
+ about the eye, wrapping round as the eye moves (after bevy-grass's trample
416
+ buffer). Each frame one pass fades every cell at the grass's own pace and
417
+ stamps in the trail and anything standing on the grass (`flat`), so a track
418
+ and a footprint stay after the points and the foot are gone: crushed and
419
+ laid over until the grass is back up, wiped of its wet for some minutes. A
420
+ plant reads four cells, however many things went through; the pass costs
421
+ about 0.15 ms a frame. A track further than 15 m or so from the eye is let
422
+ go. `grass.setStir({ x, z, dx, dz })` is a
423
+ stir of the air at a place, a club swung through: the grass within a
424
+ stride is pushed out and along.
425
+
426
+ A ball's lie in rough is how much of it stands above the grass: the grass
427
+ holds it up. `thingMiddleFor(grassTall, radius, showing)` gives the height
428
+ of its middle above the ground for a share `showing` of it to stand above
429
+ grass that tall, taking the top of a stand as its plants' mean height. No
430
+ two plants are the same height, so at any one place it is that give or
431
+ take a few millimetres. The lab's "A ball in the grass" panel sets it.
432
+
433
+ ## In a host
434
+
435
+ It is not on a registry yet. Pack it from the workspace and install the
436
+ tarball, alongside the host's own `three` (a peer, r187):
437
+
438
+ ```bash
439
+ pnpm --filter webgpu-grass pack --pack-destination <dir>
440
+ pnpm add <dir>/webgpu-grass-0.0.0.tgz three@^0.187.0
441
+ ```
442
+
443
+ The package holds compiled JavaScript and type declarations (`lib/`), and
444
+ its exports name `three/webgpu` and `three/tsl` only, so any bundler takes
445
+ it. Add `@types/three` for its types. Inside the workspace, the package
446
+ still resolves to its TypeScript source. The smallest working host is
447
+ [examples/grass-patch](../../examples/grass-patch), and `pnpm consumer:grass`
448
+ checks it against the packed tarball.
449
+
450
+ What stands over the grass and is not grass is the host's to tell it, as
451
+ `createGrass({ ..., world })`: functions of a place, any of them left
452
+ out.
453
+
454
+ - `world.sun(xz)`: how much of the sun reaches the place, 0 to 1. A
455
+ cloud's shadow, a tree's. The sky still lights it.
456
+ - `world.sky(xz)`: how much of the sky it sees, 0 to 1. Under a crown, in
457
+ a hollow.
458
+ - `world.tint(xz)`: what the grass's colour is multiplied by. Wet grass.
459
+ - `world.light(xz)`: what a flower's colour is multiplied by: the light's
460
+ part of `tint` (shade, the wet), without what makes a leaf the colour of
461
+ its ground. Not given, flowers take `tint`.
462
+ - `world.shelter(xz)`: how much of the day's light the place goes without,
463
+ 0 open to 1 under a closed crown, for what grows there: a flower flowers
464
+ less the less light it has (by 0.72 for each tenth lost, as meadow herbs
465
+ at a forest's edge do) and not at all below its least (`Flower.light`).
466
+ It should not move with the sun or the leaves' sway.
467
+ - `world.air(colour, at)`: the air between a leaf and the eye, for a host
468
+ with haze of its own: the colour that reaches the eye.
469
+
470
+ The shaders call them in compute passes, vertex shaders and fragment shaders, so
471
+ a texture in one is looked up at a set level. The lab's "clouds' shadows"
472
+ is one: twenty lines in `lab/main.ts`.
473
+
474
+ A host that draws its own ground under the grass, in its own colours,
475
+ multiplies that colour by `grass.groundShade(xz)`: what the stand does to
476
+ the ground under it (dark between the leaves near the eye, the stand's
477
+ mean where the leaves are too far off to draw, its picture from above,
478
+ its patches, the grass's own shade). It takes the host's colour to be the
479
+ stand's as a whole, so give each species that colour over
480
+ `standTone(species)` for its leaves. The lab's `?hostground` draws its
481
+ ground this way.
482
+
483
+ ### What `groundShade` binds
484
+
485
+ A shader stage has sixteen sampled textures (and sixteen samplers) on any
486
+ machine, and `groundShade` takes some of the host's ground shader's. It
487
+ binds four textures and four samplers, however many species there are and
488
+ whichever distance techniques are on (`test/wgsl.test.ts` holds it to that):
489
+
490
+ - the species map: `speciesMap` keeps all its rows in one array texture, so
491
+ 16 species are one binding as 3 are. (A `fieldMap` binds the host's own
492
+ field textures, one each; `mixedMap` adds none.)
493
+ - the grass's tiling noise: none of its own if the host gives the noise its
494
+ ground already looks up, as `createGrass({ noise })`, which makes it three.
495
+ - the canopy's picture (the stand's height about the place, for its shade).
496
+ - the ground's pictures: one array texture, a layer to a species.
497
+
498
+ The distance techniques add none. `hexGround`, the hex-tiling of the far
499
+ ground, reads the ground's pictures again, which is the same binding, and
500
+ `windSheen` reads only the noise. They are switched by a uniform, so the
501
+ shader and its bindings are the same with them on or off; what turning them
502
+ off saves is their work, not a binding. A host switches them when it makes
503
+ the grass, `createGrass({ distance: { hexGround: false, windSheen: false } })`
504
+ (or `distance: false` for every distance technique), or later on
505
+ `grass.distance` (`grass.distance.hexGround = false`). The host's `world`
506
+ functions are not called in `groundShade` (the light on its ground is the
507
+ host's), but a `mow` source of its own is, with whatever it looks up
508
+ (`mowStripes` looks up none). `groundColor`, the grass's own ground, binds
509
+ two more: the leaves' shadow picture and the near canopy's.
510
+
511
+ The leaves are thickest about the eye and thin out from it. A picture of
512
+ something on the ground some way off (a ball at a golfer's feet, seen from
513
+ four metres behind) wants them about the thing instead:
514
+ `grass.update(renderer, camera, time, { x, y, z, focus: true })`.
515
+
516
+ A course that already keeps a field for each of its surfaces has its map
517
+ already. `fieldMap({ fields, species, x0, z0, width, depth, edgePerLead })`
518
+ takes those textures, a channel to a surface, and says which species each
519
+ channel is: a place belongs to the surface whose field is highest there.
520
+ `speciesMap`, for a plain grid of species numbers, is the same thing with
521
+ the fields made for you.
522
+
523
+ ## What it does not do yet
524
+
525
+ - **Light.** Leaves are unlit materials, coloured in the compute pass by a
526
+ sun tint and a shade tint. three's own lights and shadows do nothing to
527
+ them, and they cast no shadow.
528
+ - **Borders from the host's own shapes.** `speciesMap` smooths a grid of
529
+ species numbers into a field for each species and puts the border where
530
+ two are equal, so a painted grid gives clean lines that cut its corners.
531
+ It rounds away anything narrower than three or four cells, and a corner
532
+ is as round as the smoothing. A host with exact outlines (a course's
533
+ distance fields) still wants a source of its own.
534
+ - **Height across a border.** Each stand is full height up to its line; the
535
+ game raises rough out of semi-rough over a stride.
536
+ - **Things in the grass.** A ball, feet and a trail press it, and the grass
537
+ remembers them (see "Things in the grass"), but nothing in it casts a
538
+ shadow on it, and a dew track, dark through pale, is not drawn: the grid
539
+ is where its wiped dew is kept, and the dew itself is the host's to draw.
540
+ - **Mowing beyond straight passes.** `mowStripes` is passes side by side,
541
+ there and back. A species says how far the mower lays it over
542
+ (`Species.lay`); laid grass leans that way, and it and the ground under it
543
+ are lighter seen along the lay and darker against it, so the stripes
544
+ change places as the eye goes round them. Not yet: the laps round the
545
+ edge, a second cut across the first, the step up to longer grass at a
546
+ mown line and its shadow, wear, and a stripe that follows the sun as
547
+ well as the eye.
548
+ - **A ground with relief.** Each species has a picture of what lies on its
549
+ ground, seen from straight above, which shows between the leaves and
550
+ fades to the species' plain colour with distance. `grass.bakeGround`
551
+ makes it from the species' own leaves (the lab does, again whenever a
552
+ species changes); `grass.setGroundPicture` takes a photograph instead.
553
+ Only its light and dark are used. A baked picture carries how high each
554
+ of its leaves stands, and from that is seen with parallax and lit by its
555
+ slope to the sun (`look.relief`); a photograph brings no heights, so
556
+ what is lighter in it is taken to stand higher. It has no shadows of its
557
+ own, it repeats every `groundTile`, and at 1,024 pixels to the tile it
558
+ is a blur from a few centimetres off.
559
+ - **A leaf's cross-section.** A leaf bends in an arc and keeps its length.
560
+ The leaves nearest the eye (the three near squares, and a specimen) are
561
+ folded along the rib in their geometry, each half facing its own way
562
+ (`look.fold`); further off a leaf is a flat strip, where it is too
563
+ small in the picture for the fold to show.
564
+ - **How far leaves are drawn.** A leaf is drawn while it is most of a pixel
565
+ wide in the picture and not once it is a third of one
566
+ (`look.leafPixels`). The squares' reach follows that in one step only:
567
+ a wider square of leaves (`layers.wide`, to 18 m) is drawn as far as
568
+ the broadest species' leaf is still half a pixel wide beyond the
569
+ others' 9 m, and not at all on a small picture or for a fine-leaved
570
+ grass. It is chosen by the broadest species in the table, not by what
571
+ grows in view, and beyond 18 m the leaves end whatever the display.
572
+ `grass.countDrawn` says how many plants a frame drew.
573
+ - **The far tufts.** Beyond its leaves a tufted stand is drawn as tufts
574
+ (`layers.far`, to 48 m), and an unmown one from 4 cm tall also where
575
+ its own leaves are too thin to be seen nearer. A tuft is eight broad
576
+ blades standing for a bunch of leaves: three of its species' leaves
577
+ broad, and never narrower than 5.5 mm, so a fine fescue's tufts stand
578
+ for its many leaves by breadth, not by number. That is coarser than the
579
+ stand it stands for; fewer are kept further off and from higher up, each
580
+ broadened for those left out (to three times); and over the last part of
581
+ their range they take the ground's flat colour as the ground takes the
582
+ stand's. From high overhead a stand of them shows as small stars.
583
+ - **Rushes, brush.** Left in the game.
584
+ - **Thin leaves on a see-through canvas.** A leaf thinner than a pixel is
585
+ drawn a pixel wide and that much see-through (alpha to coverage, where
586
+ the picture has several samples to a pixel), so far grass thins and does
587
+ not fatten. That writes alpha below one into the picture: on a canvas
588
+ that keeps its alpha the page behind shows through as pale flecks,
589
+ worst in a band where a square of leaves thins out. The host's renderer
590
+ must be opaque (`alpha: false`), as the lab's is. With four samples the
591
+ thinning is coarse, and soft on a small picture.
592
+ - **Leaf pictures at a distance.** They have no smaller copies: their detail
593
+ fades out by nine metres, and a cut-out's outline flickers far off. There
594
+ are no normal maps, and the pictures share sixteen layers of 64 by 256.
595
+ - **A map that changes.** The grass keeps a picture of how tall it stands
596
+ over the map's ground, for its shadows and the lie of the ground. It is
597
+ drawn again when the species change, and when the host says the map has
598
+ (`grass.mapChanged()`); otherwise only every 600 frames. Its texels are
599
+ about a metre over a golf hole's ground, so a second picture 64 m across
600
+ (12 cm to a texel) follows the eye, for the leaves' and flowers' shadows
601
+ within it; a host's ground keeps to the whole one (its shader has no
602
+ texture to spare).
603
+ - **Shadows from anything but grass.** The grass keeps a picture of how
604
+ tall it stands over the map's ground (if the map says what ground it
605
+ covers), and leaves and ground look a little way towards the sun in it:
606
+ taller grass shades shorter grass and bare ground on the side away from
607
+ the sun, and takes some of the sky from what stands beside it. It knows
608
+ nothing of the terrain's own slopes, of trees or of a golfer: there is no
609
+ way yet for a host to hand in its own shadows, and three's lights and
610
+ shadow maps still do nothing here. The shadow is five steps long (1.2 m),
611
+ so the tallest grass at the lowest sun is cut short.
612
+ - **A specimen's wind.** It sways on a plain sine, not the field's gusts.
613
+ - **Cost on anything but one machine.** *Measure* in the lab times the
614
+ graphics card's own passes over five fixed views. On an Apple M-series
615
+ laptop at 2,048 by 1,536, a frame of grass and ground is 7 to 13 ms:
616
+ under 1 ms of compute, about 2 ms of ground, the rest leaves. Two runs
617
+ differ by up to a fifth. Only plants that grow and are in view are drawn
618
+ (the compute pass compacts them and counts them into the draw's own
619
+ arguments), which took a fifth to a third off the heavier views; what
620
+ is left is mostly the drawing of leaves that are seen. No phone and no
621
+ other card has been measured.
622
+ - **The near squares' leaves.** Close to the ground three finer squares of
623
+ leaves switch on (2, 5 and 10 mm apart, a mown sward's own density) and
624
+ off again as the eye rises. A species' density above 1 (more leaves to
625
+ a plant) shows only in them.
626
+ - **Any eye.** The squares of fine leaves were tuned for an eye a metre and
627
+ a half up. Lower, the host must aim them at the foreground (`lookAt`), as
628
+ the lab does; inside grass taller than the eye it is a thicket of broad
629
+ leaves.
630
+ - **The game.** Couch Golf Club draws its grass with this on its WebGPU
631
+ renderer, pinned to a commit. Its standard renderer, and phones, still
632
+ draw the game's own.
633
+
634
+ `STARTER_SPECIES` are the game's surfaces turned into rows, and a hay
635
+ meadow. `TURF_SPECIES`, which the lab starts from, are a golf course's eight surfaces
636
+ (creeping bent, perennial rye as fairway and as rough, red fescue, fescue
637
+ in seed, a hay meadow, bermuda, Kentucky bluegrass): their mowing heights
638
+ and leaf widths are the species' own, their colour is a leaf's by the leaf
639
+ model above, and their habit was set by eye beside photographs of the real
640
+ grass. The habit is a likeness, not a measurement: no leaf was measured
641
+ from a photograph. The photographs are
642
+ not here (their licences are not this one's): put your own in
643
+ `lab/reference/` with a `list.ts` (see `lab/reference.ts`; the folder is
644
+ ignored by git) and *Photo* on the bar shows each beside its species.
645
+
646
+ ## Licence
647
+
648
+ MIT (`LICENSE`).