webgpu-trees 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 (322) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1541 -2
  3. package/assets/SOURCES.md +255 -0
  4. package/assets/bark/alder.albedo.avif +0 -0
  5. package/assets/bark/alder.normal.avif +0 -0
  6. package/assets/bark/aleppoPine.albedo.avif +0 -0
  7. package/assets/bark/aleppoPine.normal.avif +0 -0
  8. package/assets/bark/ash.albedo.avif +0 -0
  9. package/assets/bark/ash.normal.avif +0 -0
  10. package/assets/bark/aspen.albedo.avif +0 -0
  11. package/assets/bark/aspen.normal.avif +0 -0
  12. package/assets/bark/beech.albedo.avif +0 -0
  13. package/assets/bark/beech.normal.avif +0 -0
  14. package/assets/bark/birch.albedo.avif +0 -0
  15. package/assets/bark/birch.normal.avif +0 -0
  16. package/assets/bark/birchFoot.albedo.avif +0 -0
  17. package/assets/bark/birchFoot.normal.avif +0 -0
  18. package/assets/bark/corsicanPine.albedo.avif +0 -0
  19. package/assets/bark/corsicanPine.normal.avif +0 -0
  20. package/assets/bark/cypress.albedo.avif +0 -0
  21. package/assets/bark/cypress.normal.avif +0 -0
  22. package/assets/bark/douglas.albedo.avif +0 -0
  23. package/assets/bark/douglas.normal.avif +0 -0
  24. package/assets/bark/eucalyptus.albedo.avif +0 -0
  25. package/assets/bark/eucalyptus.normal.avif +0 -0
  26. package/assets/bark/eucalyptusUpper.albedo.avif +0 -0
  27. package/assets/bark/eucalyptusUpper.normal.avif +0 -0
  28. package/assets/bark/fibrous.albedo.avif +0 -0
  29. package/assets/bark/fibrous.normal.avif +0 -0
  30. package/assets/bark/hemlock.albedo.avif +0 -0
  31. package/assets/bark/hemlock.normal.avif +0 -0
  32. package/assets/bark/hornbeam.albedo.avif +0 -0
  33. package/assets/bark/hornbeam.normal.avif +0 -0
  34. package/assets/bark/larch.albedo.avif +0 -0
  35. package/assets/bark/larch.normal.avif +0 -0
  36. package/assets/bark/lime.albedo.avif +0 -0
  37. package/assets/bark/lime.normal.avif +0 -0
  38. package/assets/bark/liveOak.albedo.avif +0 -0
  39. package/assets/bark/liveOak.normal.avif +0 -0
  40. package/assets/bark/maple.albedo.avif +0 -0
  41. package/assets/bark/maple.normal.avif +0 -0
  42. package/assets/bark/montereyPine.albedo.avif +0 -0
  43. package/assets/bark/montereyPine.normal.avif +0 -0
  44. package/assets/bark/norMaple.albedo.avif +0 -0
  45. package/assets/bark/norMaple.normal.avif +0 -0
  46. package/assets/bark/oak.albedo.avif +0 -0
  47. package/assets/bark/oak.normal.avif +0 -0
  48. package/assets/bark/palmLattice.albedo.avif +0 -0
  49. package/assets/bark/palmLattice.normal.avif +0 -0
  50. package/assets/bark/palmPineapple.albedo.avif +0 -0
  51. package/assets/bark/palmPineapple.normal.avif +0 -0
  52. package/assets/bark/palmRings.albedo.avif +0 -0
  53. package/assets/bark/palmRings.normal.avif +0 -0
  54. package/assets/bark/pine.albedo.avif +0 -0
  55. package/assets/bark/pine.normal.avif +0 -0
  56. package/assets/bark/pineFlakes.albedo.avif +0 -0
  57. package/assets/bark/pineFlakes.normal.avif +0 -0
  58. package/assets/bark/plane.albedo.avif +0 -0
  59. package/assets/bark/plane.normal.avif +0 -0
  60. package/assets/bark/poplar.albedo.avif +0 -0
  61. package/assets/bark/poplar.normal.avif +0 -0
  62. package/assets/bark/redAlder.albedo.avif +0 -0
  63. package/assets/bark/redAlder.normal.avif +0 -0
  64. package/assets/bark/redOak.albedo.avif +0 -0
  65. package/assets/bark/redOak.normal.avif +0 -0
  66. package/assets/bark/ridged.albedo.avif +0 -0
  67. package/assets/bark/ridged.normal.avif +0 -0
  68. package/assets/bark/sitkaSpruce.albedo.avif +0 -0
  69. package/assets/bark/sitkaSpruce.normal.avif +0 -0
  70. package/assets/bark/spruce.albedo.avif +0 -0
  71. package/assets/bark/spruce.normal.avif +0 -0
  72. package/assets/bark/stonePine.albedo.avif +0 -0
  73. package/assets/bark/stonePine.normal.avif +0 -0
  74. package/assets/bark/sycMaple.albedo.avif +0 -0
  75. package/assets/bark/sycMaple.normal.avif +0 -0
  76. package/assets/bark/torreyPine.albedo.avif +0 -0
  77. package/assets/bark/torreyPine.normal.avif +0 -0
  78. package/assets/bark/whitePine.albedo.avif +0 -0
  79. package/assets/bark/whitePine.normal.avif +0 -0
  80. package/assets/bark/whiteSpruce.albedo.avif +0 -0
  81. package/assets/bark/whiteSpruce.normal.avif +0 -0
  82. package/assets/bark/whiteWillow.albedo.avif +0 -0
  83. package/assets/bark/whiteWillow.normal.avif +0 -0
  84. package/assets/bark/willow.albedo.avif +0 -0
  85. package/assets/bark/willow.normal.avif +0 -0
  86. package/assets/leaves/alder.albedo.avif +0 -0
  87. package/assets/leaves/alder.normal.avif +0 -0
  88. package/assets/leaves/ash.albedo.avif +0 -0
  89. package/assets/leaves/ash.normal.avif +0 -0
  90. package/assets/leaves/aspen.albedo.avif +0 -0
  91. package/assets/leaves/aspen.normal.avif +0 -0
  92. package/assets/leaves/beech.albedo.avif +0 -0
  93. package/assets/leaves/beech.normal.avif +0 -0
  94. package/assets/leaves/birch.albedo.avif +0 -0
  95. package/assets/leaves/birch.normal.avif +0 -0
  96. package/assets/leaves/blackthorn.albedo.avif +0 -0
  97. package/assets/leaves/blackthorn.normal.avif +0 -0
  98. package/assets/leaves/blade.albedo.avif +0 -0
  99. package/assets/leaves/blade.normal.avif +0 -0
  100. package/assets/leaves/broomSprig.albedo.avif +0 -0
  101. package/assets/leaves/broomSprig.normal.avif +0 -0
  102. package/assets/leaves/corsicanShoot.albedo.avif +0 -0
  103. package/assets/leaves/corsicanShoot.normal.avif +0 -0
  104. package/assets/leaves/cypress.albedo.avif +0 -0
  105. package/assets/leaves/cypress.normal.avif +0 -0
  106. package/assets/leaves/cypressPad.albedo.avif +0 -0
  107. package/assets/leaves/cypressPad.normal.avif +0 -0
  108. package/assets/leaves/deadFronds.albedo.avif +0 -0
  109. package/assets/leaves/deadFronds.normal.avif +0 -0
  110. package/assets/leaves/deltoid.albedo.avif +0 -0
  111. package/assets/leaves/deltoid.normal.avif +0 -0
  112. package/assets/leaves/douglasShoot.albedo.avif +0 -0
  113. package/assets/leaves/douglasShoot.normal.avif +0 -0
  114. package/assets/leaves/elderSprig.albedo.avif +0 -0
  115. package/assets/leaves/elderSprig.normal.avif +0 -0
  116. package/assets/leaves/elliptic.albedo.avif +0 -0
  117. package/assets/leaves/elliptic.normal.avif +0 -0
  118. package/assets/leaves/eucalyptus.albedo.avif +0 -0
  119. package/assets/leaves/eucalyptus.normal.avif +0 -0
  120. package/assets/leaves/fan.albedo.avif +0 -0
  121. package/assets/leaves/fan.normal.avif +0 -0
  122. package/assets/leaves/fanB.albedo.avif +0 -0
  123. package/assets/leaves/fanB.normal.avif +0 -0
  124. package/assets/leaves/fir.albedo.avif +0 -0
  125. package/assets/leaves/fir.normal.avif +0 -0
  126. package/assets/leaves/frond.albedo.avif +0 -0
  127. package/assets/leaves/frond.normal.avif +0 -0
  128. package/assets/leaves/gorse.albedo.avif +0 -0
  129. package/assets/leaves/gorse.normal.avif +0 -0
  130. package/assets/leaves/hawthorn.albedo.avif +0 -0
  131. package/assets/leaves/hawthorn.normal.avif +0 -0
  132. package/assets/leaves/hemlockShoot.albedo.avif +0 -0
  133. package/assets/leaves/hemlockShoot.normal.avif +0 -0
  134. package/assets/leaves/hornbeam.albedo.avif +0 -0
  135. package/assets/leaves/hornbeam.normal.avif +0 -0
  136. package/assets/leaves/lance.albedo.avif +0 -0
  137. package/assets/leaves/lance.normal.avif +0 -0
  138. package/assets/leaves/larchShoot.albedo.avif +0 -0
  139. package/assets/leaves/larchShoot.normal.avif +0 -0
  140. package/assets/leaves/lime.albedo.avif +0 -0
  141. package/assets/leaves/lime.normal.avif +0 -0
  142. package/assets/leaves/liveOak.albedo.avif +0 -0
  143. package/assets/leaves/liveOak.normal.avif +0 -0
  144. package/assets/leaves/londonPlane.albedo.avif +0 -0
  145. package/assets/leaves/londonPlane.normal.avif +0 -0
  146. package/assets/leaves/maple.albedo.avif +0 -0
  147. package/assets/leaves/maple.normal.avif +0 -0
  148. package/assets/leaves/norMaple.albedo.avif +0 -0
  149. package/assets/leaves/norMaple.normal.avif +0 -0
  150. package/assets/leaves/oak.albedo.avif +0 -0
  151. package/assets/leaves/oak.normal.avif +0 -0
  152. package/assets/leaves/paintFan.albedo.avif +0 -0
  153. package/assets/leaves/paintFan.normal.avif +0 -0
  154. package/assets/leaves/paintFrond.albedo.avif +0 -0
  155. package/assets/leaves/paintFrond.normal.avif +0 -0
  156. package/assets/leaves/pine.albedo.avif +0 -0
  157. package/assets/leaves/pine.normal.avif +0 -0
  158. package/assets/leaves/pineClumpDark.albedo.avif +0 -0
  159. package/assets/leaves/pineClumpDark.normal.avif +0 -0
  160. package/assets/leaves/pineClumpLight.albedo.avif +0 -0
  161. package/assets/leaves/pineClumpLight.normal.avif +0 -0
  162. package/assets/leaves/poplar.albedo.avif +0 -0
  163. package/assets/leaves/poplar.normal.avif +0 -0
  164. package/assets/leaves/queenFrond.albedo.avif +0 -0
  165. package/assets/leaves/queenFrond.normal.avif +0 -0
  166. package/assets/leaves/redAlder.albedo.avif +0 -0
  167. package/assets/leaves/redAlder.normal.avif +0 -0
  168. package/assets/leaves/redOak.albedo.avif +0 -0
  169. package/assets/leaves/redOak.normal.avif +0 -0
  170. package/assets/leaves/round.albedo.avif +0 -0
  171. package/assets/leaves/round.normal.avif +0 -0
  172. package/assets/leaves/scale.albedo.avif +0 -0
  173. package/assets/leaves/scale.normal.avif +0 -0
  174. package/assets/leaves/scotsPineClump.albedo.avif +0 -0
  175. package/assets/leaves/scotsPineClump.normal.avif +0 -0
  176. package/assets/leaves/seaBuckthornSprig.albedo.avif +0 -0
  177. package/assets/leaves/seaBuckthornSprig.normal.avif +0 -0
  178. package/assets/leaves/serrate.albedo.avif +0 -0
  179. package/assets/leaves/serrate.normal.avif +0 -0
  180. package/assets/leaves/sitkaShoot.albedo.avif +0 -0
  181. package/assets/leaves/sitkaShoot.normal.avif +0 -0
  182. package/assets/leaves/stonePineCanopy.albedo.avif +0 -0
  183. package/assets/leaves/stonePineCanopy.normal.avif +0 -0
  184. package/assets/leaves/stonePineCushion.albedo.avif +0 -0
  185. package/assets/leaves/stonePineCushion.normal.avif +0 -0
  186. package/assets/leaves/sycamore.albedo.avif +0 -0
  187. package/assets/leaves/sycamore.normal.avif +0 -0
  188. package/assets/leaves/torreyPine.albedo.avif +0 -0
  189. package/assets/leaves/torreyPine.normal.avif +0 -0
  190. package/assets/leaves/whitePine.albedo.avif +0 -0
  191. package/assets/leaves/whitePine.normal.avif +0 -0
  192. package/assets/leaves/whiteSpruceShoot.albedo.avif +0 -0
  193. package/assets/leaves/whiteSpruceShoot.normal.avif +0 -0
  194. package/assets/leaves/whiteWillow.albedo.avif +0 -0
  195. package/assets/leaves/whiteWillow.normal.avif +0 -0
  196. package/assets/leaves/willow.albedo.avif +0 -0
  197. package/assets/leaves/willow.normal.avif +0 -0
  198. package/assets/marks/marks.json +894 -0
  199. package/assets/marks/painterly.avif +0 -0
  200. package/assets/marks/sheets.avif +0 -0
  201. package/assets/scans.json +6066 -0
  202. package/lib/age.d.ts +36 -0
  203. package/lib/age.d.ts.map +1 -0
  204. package/lib/age.js +170 -0
  205. package/lib/age.js.map +1 -0
  206. package/lib/atmosphere.d.ts +64 -0
  207. package/lib/atmosphere.d.ts.map +1 -0
  208. package/lib/atmosphere.js +260 -0
  209. package/lib/atmosphere.js.map +1 -0
  210. package/lib/dabs.d.ts +4 -0
  211. package/lib/dabs.d.ts.map +1 -0
  212. package/lib/dabs.js +77 -0
  213. package/lib/dabs.js.map +1 -0
  214. package/lib/engine.d.ts +211 -0
  215. package/lib/engine.d.ts.map +1 -0
  216. package/lib/engine.js +3808 -0
  217. package/lib/engine.js.map +1 -0
  218. package/lib/genetics.d.ts +129 -0
  219. package/lib/genetics.d.ts.map +1 -0
  220. package/lib/genetics.js +721 -0
  221. package/lib/genetics.js.map +1 -0
  222. package/lib/geometry.d.ts +237 -0
  223. package/lib/geometry.d.ts.map +1 -0
  224. package/lib/geometry.js +1944 -0
  225. package/lib/geometry.js.map +1 -0
  226. package/lib/grow.d.ts +83 -0
  227. package/lib/grow.d.ts.map +1 -0
  228. package/lib/grow.js +3851 -0
  229. package/lib/grow.js.map +1 -0
  230. package/lib/hiz.d.ts +74 -0
  231. package/lib/hiz.d.ts.map +1 -0
  232. package/lib/hiz.js +161 -0
  233. package/lib/hiz.js.map +1 -0
  234. package/lib/impostor.d.ts +106 -0
  235. package/lib/impostor.d.ts.map +1 -0
  236. package/lib/impostor.js +450 -0
  237. package/lib/impostor.js.map +1 -0
  238. package/lib/index.d.ts +17 -0
  239. package/lib/index.d.ts.map +1 -0
  240. package/lib/index.js +16 -0
  241. package/lib/index.js.map +1 -0
  242. package/lib/leaves.d.ts +30 -0
  243. package/lib/leaves.d.ts.map +1 -0
  244. package/lib/leaves.js +829 -0
  245. package/lib/leaves.js.map +1 -0
  246. package/lib/light.d.ts +110 -0
  247. package/lib/light.d.ts.map +1 -0
  248. package/lib/light.js +430 -0
  249. package/lib/light.js.map +1 -0
  250. package/lib/lobes.d.ts +25 -0
  251. package/lib/lobes.d.ts.map +1 -0
  252. package/lib/lobes.js +151 -0
  253. package/lib/lobes.js.map +1 -0
  254. package/lib/marks.d.ts +63 -0
  255. package/lib/marks.d.ts.map +1 -0
  256. package/lib/marks.js +553 -0
  257. package/lib/marks.js.map +1 -0
  258. package/lib/mips.d.ts +42 -0
  259. package/lib/mips.d.ts.map +1 -0
  260. package/lib/mips.js +220 -0
  261. package/lib/mips.js.map +1 -0
  262. package/lib/paintedMarks.d.ts +21 -0
  263. package/lib/paintedMarks.d.ts.map +1 -0
  264. package/lib/paintedMarks.js +53 -0
  265. package/lib/paintedMarks.js.map +1 -0
  266. package/lib/palm.d.ts +162 -0
  267. package/lib/palm.d.ts.map +1 -0
  268. package/lib/palm.js +779 -0
  269. package/lib/palm.js.map +1 -0
  270. package/lib/palmAtlas.d.ts +79 -0
  271. package/lib/palmAtlas.d.ts.map +1 -0
  272. package/lib/palmAtlas.js +335 -0
  273. package/lib/palmAtlas.js.map +1 -0
  274. package/lib/palmGeometry.d.ts +41 -0
  275. package/lib/palmGeometry.d.ts.map +1 -0
  276. package/lib/palmGeometry.js +309 -0
  277. package/lib/palmGeometry.js.map +1 -0
  278. package/lib/phenology.d.ts +21 -0
  279. package/lib/phenology.d.ts.map +1 -0
  280. package/lib/phenology.js +164 -0
  281. package/lib/phenology.js.map +1 -0
  282. package/lib/random.d.ts +34 -0
  283. package/lib/random.d.ts.map +1 -0
  284. package/lib/random.js +92 -0
  285. package/lib/random.js.map +1 -0
  286. package/lib/scans.d.ts +232 -0
  287. package/lib/scans.d.ts.map +1 -0
  288. package/lib/scans.js +391 -0
  289. package/lib/scans.js.map +1 -0
  290. package/lib/scatter.d.ts +70 -0
  291. package/lib/scatter.d.ts.map +1 -0
  292. package/lib/scatter.js +186 -0
  293. package/lib/scatter.js.map +1 -0
  294. package/lib/species.d.ts +369 -0
  295. package/lib/species.d.ts.map +1 -0
  296. package/lib/species.js +2817 -0
  297. package/lib/species.js.map +1 -0
  298. package/lib/sprays.d.ts +112 -0
  299. package/lib/sprays.d.ts.map +1 -0
  300. package/lib/sprays.js +1137 -0
  301. package/lib/sprays.js.map +1 -0
  302. package/lib/stand.d.ts +52 -0
  303. package/lib/stand.d.ts.map +1 -0
  304. package/lib/stand.js +279 -0
  305. package/lib/stand.js.map +1 -0
  306. package/lib/styles.d.ts +135 -0
  307. package/lib/styles.d.ts.map +1 -0
  308. package/lib/styles.js +79 -0
  309. package/lib/styles.js.map +1 -0
  310. package/lib/tree.d.ts +134 -0
  311. package/lib/tree.d.ts.map +1 -0
  312. package/lib/tree.js +57 -0
  313. package/lib/tree.js.map +1 -0
  314. package/lib/variants.d.ts +157 -0
  315. package/lib/variants.d.ts.map +1 -0
  316. package/lib/variants.js +532 -0
  317. package/lib/variants.js.map +1 -0
  318. package/lib/visibility.d.ts +105 -0
  319. package/lib/visibility.d.ts.map +1 -0
  320. package/lib/visibility.js +242 -0
  321. package/lib/visibility.js.map +1 -0
  322. package/package.json +62 -3
package/lib/sprays.js ADDED
@@ -0,0 +1,1137 @@
1
+ // The realistic look's leaf cards, from photographs: each species' own leaves (a sheet of single scanned
2
+ // leaves, scans.ts) laid on its own twigs, at their real size on the card, as its kind carries them: an oak's
3
+ // one to a node with a cluster at the tip, a maple's in pairs, a eucalyptus's few long sickles hanging from
4
+ // the twig's end, a willow's all along a hanging whip; a fir's or a cedar's whole scanned sprays, a pine's
5
+ // whole shoots; a palm's leaflets along its rachis or round its fan.
6
+ //
7
+ // Each species has a row of the atlas: three near cards (a short twig with a few leaves, about a third of a
8
+ // metre across: `nearCardHalf`) and two big cards (a branch's end, a clump wide: the mid tier's cards, a
9
+ // conifer's sheets, the far dabs; a pine's cushions, `drawCushionCard` and `drawnCushionCard`). Each card is
10
+ // drawn from its foot: its first row is where its twig comes in (`CARD_V`). Its texels are as leaves.ts draws them, so the shaders read either alike:
11
+ // red and blue a grey tone (the scan's lightness over its sheet's mean, lit a little by its own relief), green
12
+ // a number of each leaf's own (how thin it is: the scan's translucency, by leaf), alpha coverage.
13
+ //
14
+ // Plain arithmetic, no three: the tests read the cards as they are.
15
+ import { hash01 } from "./variants.js";
16
+ import { nearCardHalf } from "./scans.js";
17
+ /** Columns of a species' row: three near cards, two big ones, and two far ones. */
18
+ export const SPRAY_COLS = 7;
19
+ export const NEAR_CARDS = 3;
20
+ /** The first big card's column, and how many big cards a row has (a near clump's mass, a conifer's sheets). */
21
+ export const BIG_CARD = 3;
22
+ export const BIG_CARDS = 2;
23
+ /**
24
+ * The first far card's column, and how many: a mid tree's cards and a far dab's. A big card again, but for a
25
+ * pine with pictures of its canopy (`ScanLook.cushionFar`, a strip of cushions side by side), that picture.
26
+ */
27
+ export const FAR_CARD = 5;
28
+ export const FAR_CARDS = 2;
29
+ /** A leaf's tone on average (as leaves.ts's), and a twig's. */
30
+ const LEAF = 0.6;
31
+ const TWIG = 0.33;
32
+ /** Where a near card's twig comes in: this share of the cell from its first row (geometry.ts `SPRAY_FOOT`: 0.8 of the half below the middle). */
33
+ export const FOOT = 0.1;
34
+ /**
35
+ * How a card's corner (`aDab.y`, -1 at its twig's foot, 1 at its far end: geometry.ts `cardPieces`) is laid on
36
+ * its cell in the atlas, its rows from its first (engine.ts `vTreeUv`: v = row + 0.5 + this · aDab.y, over the
37
+ * rows): positive, so the foot corner is the cell's first row, where a card is drawn from its foot. three's
38
+ * WebGPU renderer samples a DataTexture's first row at v = 0 (no flip: `flipY` is for pictures), as WebGL
39
+ * does; checked in the lab with a texture of two rows.
40
+ */
41
+ export const CARD_V = 0.5;
42
+ /** A big card's leaves behind its own: from how dense a species (1 - openness), and how dark, in their shade. */
43
+ const DENSE_FROM = 0.6;
44
+ /**
45
+ * A pine's tuft (see `needleTuft`): the stretch of its shoot that bears needles, in needle lengths; the angle its
46
+ * needles leave the shoot at, at the tip (the young, forward) and at the back of that stretch (the old, spread
47
+ * out and falling back), in radians; and how many needles to a needle's length in texels, fewest and most.
48
+ */
49
+ const TUFT_STRETCH = 0.8;
50
+ /** Behind a long brush's tip (`ScanLook.brush`), the angles its needles stand forward from the shoot at (rad). */
51
+ const BRUSH_ANGLE_LO = 0.55;
52
+ const BRUSH_ANGLE_HI = 1.15;
53
+ const TUFT_TIP_ANGLE = 0.45;
54
+ const TUFT_BACK_ANGLE = 1.9;
55
+ const TUFT_NEEDLES = 1.4;
56
+ const TUFT_LEAST = 18;
57
+ const TUFT_MOST = 160;
58
+ /** At most this many bundles to a tuft, and how far a bundle's needles stray from its line (rad). */
59
+ const TUFT_BUNDLES = 28;
60
+ /** At least this many: a tuft of a few big spokes was a palmate leaf a few dozen metres off. */
61
+ const TUFT_BUNDLES_LEAST = 12;
62
+ const TUFT_BUNDLE_SPREAD = 0.1;
63
+ /** The share of a tuft's needles that stand loose between its bundles. */
64
+ const TUFT_LOOSE = 0.45;
65
+ /** How much lighter a tuft's needles pointing up are than those lying level, and darker those hanging; how dark a needle's inner half is beside its outer's. */
66
+ const TUFT_UP_LIGHT = 0.3;
67
+ const TUFT_HEART = 0.5;
68
+ /**
69
+ * A needle drawn for a smaller mip (`Layer.far`): its half width, in that mip's texels, at its foot, and at its tip
70
+ * a share of that; and how many more of them are kept than would cover what the full-size needles do.
71
+ */
72
+ const FAR_NEEDLE = 0.75;
73
+ const FAR_TIP = 0.7;
74
+ const FAR_KEEP = 1;
75
+ /**
76
+ * From this mip (`Layer.far`, a texel this many of the full-size picture's) a pine whose tufts are pictures of its
77
+ * shoots fanned (`ScanLook.tuftFan`) has its drawn needles alone, this many times as many.
78
+ */
79
+ const FAR_BARE = 4;
80
+ const FAR_BARE_MORE = 1.5;
81
+ /** A pine's shoot picture laid at a tuft (`ScanLook.tuftPicture`): how long, in its needles' lengths (a white pine's 30 cm shoot, its needles 8–12 cm). */
82
+ const TUFT_SHOOT = 3;
83
+ /** How far apart the pictures of a fan of shoot pictures laid at one tuft are turned (rad; `ScanLook.tuftFan`). */
84
+ const TUFT_FAN = 0.55;
85
+ /** How long a fan's pictures are beside the needles drawn under them: shorter, so the brush's edge is the drawn needles' tips. */
86
+ const TUFT_FAN_LEN = 0.62;
87
+ /** The shortest a pine's shoot that puts out more, in its needles' lengths. */
88
+ const PINE_SHOOT = 2.2;
89
+ /** A pine's big card: how many branches' shoots it carries side by side, and as many more for a dense one; the fan they spread over (rad). */
90
+ const BRANCH_SHOOTS = 5;
91
+ const BRANCH_SHOOTS_DENSE = 7;
92
+ const BRANCH_FAN = 1.2;
93
+ /** How long a pine's big card's branch is, as a share of the card. */
94
+ const BRANCH_REACH = 0.8;
95
+ /** How thick its wood is beside a near card's: its stems, a texel or two each, were a ladder under the cushion. */
96
+ const BRANCH_WOOD = 0.5;
97
+ /** Needles shorter than this (m) fill a pine's big card with as many more shoots as they are shorter, to twice as many. */
98
+ const BRANCH_NEEDLE = 0.1;
99
+ /** A pine's needle when its look gives none: this share of its brush (`leafCm`). */
100
+ const NEEDLE_OF_BRUSH = 0.45;
101
+ const UNDER_TONE = 0.55;
102
+ /** Texels kept clear round a cell's edge, so its mips do not bleed into the next. */
103
+ const MARGIN = 6;
104
+ const clamp01 = (x) => (x < 0 ? 0 : x > 1 ? 1 : x);
105
+ const lerp = (a, b, t) => a + (b - a) * t;
106
+ const srgbToLinear = (s) => (s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4);
107
+ const LIN = new Float32Array(256).map((_, i) => srgbToLinear(i / 255));
108
+ /** How far a photograph's texel (sRGB, 0..1) is a flower or berry cluster: warm yellow/orange blooms (`Gorse`, `Scotch broom`, `Sea buckthorn`) or creamy-white flower corymbs (`Elder`). */
109
+ export function flowerOf(r, g, b) {
110
+ const warm = clamp01((Math.min(r, g) - b - 0.2) / 0.2) * clamp01((r / Math.max(g, 0.01) - 0.8) / 0.15);
111
+ const cream = clamp01((Math.min(r, g, b) - 0.56) / 0.16) * clamp01((0.08 - (g - Math.max(r, b))) / 0.08);
112
+ return Math.max(warm, cream);
113
+ }
114
+ /**
115
+ * A sheet's mean lightness (linear) of its leaves and of its flowers apart, for a look that keeps its flowers: each
116
+ * brought to a leaf's tone against its own kind's mean, so a sprig's dark spines are not darkened beside its bright
117
+ * flowers, nor its flowers all white.
118
+ */
119
+ const flowerMeans = new WeakMap();
120
+ function meansOf(sh) {
121
+ let m = flowerMeans.get(sh);
122
+ if (m)
123
+ return m;
124
+ const d = sh.albedo.data;
125
+ let leaf = 0;
126
+ let nl = 0;
127
+ let flower = 0;
128
+ let nf = 0;
129
+ for (let q = 0; q < d.length; q += 4) {
130
+ if (d[q + 3] < 128)
131
+ continue;
132
+ const lum = 0.2126 * LIN[d[q]] + 0.7152 * LIN[d[q + 1]] + 0.0722 * LIN[d[q + 2]];
133
+ const f = flowerOf(d[q] / 255, d[q + 1] / 255, d[q + 2] / 255);
134
+ leaf += lum * (1 - f);
135
+ nl += 1 - f;
136
+ flower += lum * f;
137
+ nf += f;
138
+ }
139
+ m = { leaf: Math.max(0.02, leaf / Math.max(1, nl)), flower: Math.max(0.02, nf > 0 ? flower / nf : 0.3) };
140
+ flowerMeans.set(sh, m);
141
+ return m;
142
+ }
143
+ function rng(seed) {
144
+ let k = 0;
145
+ return () => hash01(k++, seed);
146
+ }
147
+ // ---------------------------------------------------------------------------------------------------------------------
148
+ // Drawing: a twig as a tapered stroke, a leaf as its photograph, turned, scaled and bent onto the card.
149
+ function stroke(L, ax, ay, bx, by, ra, rb, tone, own = 0.2) {
150
+ const n = L.n;
151
+ const r = Math.max(ra, rb) + 1.5;
152
+ const i0 = Math.max(MARGIN, Math.floor(Math.min(ax, bx) - r));
153
+ const i1 = Math.min(n - 1 - MARGIN, Math.ceil(Math.max(ax, bx) + r));
154
+ const j0 = Math.max(MARGIN, Math.floor(Math.min(ay, by) - r));
155
+ const j1 = Math.min(n - 1 - MARGIN, Math.ceil(Math.max(ay, by) + r));
156
+ const dx = bx - ax;
157
+ const dy = by - ay;
158
+ const len2 = dx * dx + dy * dy || 1e-9;
159
+ const len = Math.sqrt(len2);
160
+ for (let j = j0; j <= j1; j++) {
161
+ const py = j + 0.5 - ay;
162
+ for (let i = i0; i <= i1; i++) {
163
+ const px = i + 0.5 - ax;
164
+ const t = clamp01((px * dx + py * dy) / len2);
165
+ const ex = px - dx * t;
166
+ const ey = py - dy * t;
167
+ const rad = ra + (rb - ra) * t;
168
+ const d = Math.sqrt(ex * ex + ey * ey);
169
+ const c = clamp01((rad - d) / 1.2 + 0.5);
170
+ if (c <= 0)
171
+ continue;
172
+ // (Round: lighter along one side.)
173
+ const side = (dx * py - dy * px) / len / Math.max(rad, 0.5);
174
+ const t1 = tone * (1 + 0.18 * Math.max(-1, Math.min(1, side)));
175
+ const o = j * n + i;
176
+ L.tone[o] = L.tone[o] * (1 - c) + t1 * c;
177
+ L.own[o] = L.own[o] * (1 - c) + own * c;
178
+ L.cover[o] = L.cover[o] * (1 - c) + c;
179
+ if (L.flower)
180
+ L.flower[o] *= 1 - c;
181
+ }
182
+ }
183
+ }
184
+ /**
185
+ * A pine's tuft drawn needle by needle: the end of a shoot at (x, y) pointing along `ang`, its needles `len`
186
+ * texels long, leaving the last stretch of it (`TUFT_STRETCH`) all round it. Each bundle of needles is set in
187
+ * three dimensions (the young ones near the tip forward, the old ones further back standing out square and
188
+ * falling back) and seen side-on, foreshortened: so a tuft is a starburst, its outline a ring of needle tips,
189
+ * longest forward, and not the fan of a picture laid flat, which made a pine's crown a broad leaf's. Its
190
+ * needles lie together in bundles a little apart, spokes with sky between them, as a pine's tufts are seen a
191
+ * few dozen metres off: needles spread evenly were, once the card was mipped down, a soft round blot. The
192
+ * bundles behind the shoot are drawn first and darker, those in front last and lighter, each needle lighter to
193
+ * its tip.
194
+ */
195
+ function needleTuft(L, x, y, ang, len, rnd, tone, own, brush = TUFT_STRETCH, thicker = 1) {
196
+ // (A longer brush, as many more needles and bundles as it is longer: as thick all along it.)
197
+ const more = Math.max(1, brush / TUFT_STRETCH);
198
+ const n = Math.round(Math.min(TUFT_MOST * more, Math.max(TUFT_LEAST, len * TUFT_NEEDLES * more)));
199
+ const bundles = Math.max(TUFT_BUNDLES_LEAST, Math.min(Math.round(TUFT_BUNDLES * more), Math.round(n / 4)));
200
+ const tip = len * TUFT_STRETCH;
201
+ const stretch = len * Math.max(TUFT_STRETCH, brush);
202
+ // (A needle at least most of a texel wide: thinner, a branch's card of them is a lace, little of it at the cut.)
203
+ const w = Math.max(0.7, len * 0.012);
204
+ const ca = Math.cos(ang);
205
+ const sa = Math.sin(ang);
206
+ const needles = [];
207
+ for (let b = 0; b < bundles; b++) {
208
+ // (Where along the stretch, 0 at its back and 1 at the tip: more of them towards the tip. Behind the tip's
209
+ // starburst, a long brush's needles stand forward along the shoot, as a bottle brush's: older needles
210
+ // than the tip's, still pointing the way the shoot grew.)
211
+ const q = (b + rnd()) / bundles;
212
+ const back = stretch * (1 - q);
213
+ const s = back <= tip ? 1 - back / tip : 0;
214
+ const theta = back <= tip ? lerp(TUFT_BACK_ANGLE, TUFT_TIP_ANGLE, s) * (0.8 + 0.4 * rnd()) : lerp(BRUSH_ANGLE_LO, BRUSH_ANGLE_HI, rnd());
215
+ const phi = rnd() * Math.PI * 2;
216
+ const bt = LEAF * tone * (0.8 + 0.35 * rnd());
217
+ const per = Math.max(2, Math.round(((n * (1 - TUFT_LOOSE)) / bundles) * (0.6 + 0.8 * rnd())));
218
+ for (let q = 0; q < per; q++) {
219
+ // (Each needle of a bundle a little off its line: a spoke fraying at its end.)
220
+ const th = theta + (rnd() - 0.5) * TUFT_BUNDLE_SPREAD;
221
+ const ph = phi + (rnd() - 0.5) * TUFT_BUNDLE_SPREAD * 2;
222
+ // (Along the shoot, across it in the card, and towards the eye.)
223
+ const u = Math.cos(th);
224
+ const v = Math.sin(th) * Math.cos(ph);
225
+ const front = Math.sin(th) * Math.sin(ph);
226
+ const nl = len * (0.82 + 0.25 * rnd());
227
+ const at = back + (rnd() - 0.5) * tip * 0.12;
228
+ needles.push({ bx: x - ca * at, by: y - sa * at, dx: (ca * u - sa * v) * nl, dy: (sa * u + ca * v) * nl, front, t: bt * (0.9 + 0.2 * rnd()) });
229
+ }
230
+ }
231
+ // (And some loose between them, so close to a tuft is a brush and not a few spokes.)
232
+ for (let q = 0; q < n * TUFT_LOOSE; q++) {
233
+ const at = stretch * rnd();
234
+ const s = at <= tip ? 1 - at / tip : 0;
235
+ const th = at <= tip ? lerp(TUFT_BACK_ANGLE, TUFT_TIP_ANGLE, s) * (0.8 + 0.4 * rnd()) : lerp(BRUSH_ANGLE_LO, BRUSH_ANGLE_HI, rnd());
236
+ const ph = rnd() * Math.PI * 2;
237
+ const u = Math.cos(th);
238
+ const v = Math.sin(th) * Math.cos(ph);
239
+ const nl = len * (0.75 + 0.25 * rnd());
240
+ needles.push({ bx: x - ca * at, by: y - sa * at, dx: (ca * u - sa * v) * nl, dy: (sa * u + ca * v) * nl, front: Math.sin(th) * Math.sin(ph), t: LEAF * tone * (0.75 + 0.35 * rnd()) });
241
+ }
242
+ needles.sort((p, q) => p.front - q.front);
243
+ // (Drawn for a smaller mip (`Layer.far`): each needle as wide as that mip's texel and kept by chance as
244
+ // often as it is narrower than that, so the tuft covers about what its needles do, but in spokes a texel
245
+ // wide with sky between, which the mip keeps; its needles' own light and shade are kept too. Averaged down
246
+ // from the full-size picture, a needle a quarter of a texel wide was a quarter of an alpha, under the cut,
247
+ // and only the tuft's dense heart was left: a disc, a broad leaf's blot (Cook et al. 2007, *Stochastic
248
+ // simplification of aggregate detail*: fewer elements, each bigger, the same cover).)
249
+ const far = L.far ?? 1;
250
+ const wide0 = far > 1 ? Math.max(w, FAR_NEEDLE * far) : w;
251
+ const share = far > 1 ? (w / wide0) * FAR_KEEP * thicker : 1;
252
+ // (All of them, and wider, where as many again are asked for as there are: `thicker`.)
253
+ const wide = wide0 * Math.max(1, share);
254
+ const keep = Math.min(1, share);
255
+ const salt = Math.round(x * 13 + y * 7919);
256
+ needles.forEach((nd, i) => {
257
+ if (keep < 1 && hash01(i, salt) >= keep)
258
+ return;
259
+ // (Behind the shoot, in the tuft's own shade; in front, in the light. And lit as a brush is from above: its
260
+ // needles pointing up catch the sky and the sun, those hanging down are in the brush's own shade, so a tuft
261
+ // is a ball of needles, its heart dark, and not a flat leaf of one tone.)
262
+ const up = nd.dy / Math.max(1e-6, Math.hypot(nd.dx, nd.dy));
263
+ const lit = (0.7 + 0.45 * (nd.front * 0.5 + 0.5)) * (1 + TUFT_UP_LIGHT * up);
264
+ const mx = nd.bx + nd.dx * 0.45;
265
+ const my = nd.by + nd.dy * 0.45;
266
+ stroke(L, nd.bx, nd.by, mx, my, wide, wide * 0.9, nd.t * lit * TUFT_HEART, own);
267
+ stroke(L, mx, my, nd.bx + nd.dx, nd.by + nd.dy, wide * 0.9, far > 1 ? wide * FAR_TIP : w * 0.45, nd.t * lit * 1.3, own);
268
+ });
269
+ }
270
+ /** A pine's needle, metres: its look's, or a share of its brush. */
271
+ export function needleOf(look) {
272
+ return (look.needleCm ?? look.leafCm * NEEDLE_OF_BRUSH) / 100;
273
+ }
274
+ /** Bilinear sample of a sheet's channel k at (x, y) texels; 0 off the sheet. */
275
+ function sample(p, x, y, k) {
276
+ const x0 = Math.floor(x - 0.5);
277
+ const y0 = Math.floor(y - 0.5);
278
+ const fx = x - 0.5 - x0;
279
+ const fy = y - 0.5 - y0;
280
+ let s = 0;
281
+ for (let q = 0; q < 4; q++) {
282
+ const xx = x0 + (q & 1);
283
+ const yy = y0 + (q >> 1);
284
+ if (xx < 0 || yy < 0 || xx >= p.width || yy >= p.height)
285
+ continue;
286
+ s += p.data[(yy * p.width + xx) * 4 + k] * ((q & 1 ? fx : 1 - fx) * (q >> 1 ? fy : 1 - fy));
287
+ }
288
+ return s / 255;
289
+ }
290
+ /** Its box on the card, and whether all of it is inside the card's clear margin. */
291
+ function footprint(L, l) {
292
+ const s = l.sprite;
293
+ const sl = Math.hypot(s.tip[0] - s.base[0], s.tip[1] - s.base[1]) || 1;
294
+ const k = l.len / sl;
295
+ const half = (Math.max(s.w, s.h) * k * Math.max(1, l.width)) / 2 + Math.abs(l.bend) * l.len + 2;
296
+ const cx = l.x + Math.cos(l.ang) * l.len * 0.5;
297
+ const cy = l.y + Math.sin(l.ang) * l.len * 0.5;
298
+ const r = Math.min(half, l.len * 0.5 + (s.w * k * l.width) / 2 + Math.abs(l.bend) * l.len + 2);
299
+ const i0 = Math.floor(cx - r);
300
+ const i1 = Math.ceil(cx + r);
301
+ const j0 = Math.floor(cy - r);
302
+ const j1 = Math.ceil(cy + r);
303
+ const n = L.n;
304
+ return { i0, i1, j0, j1, inside: i0 >= MARGIN && j0 >= MARGIN && i1 <= n - 1 - MARGIN && j1 <= n - 1 - MARGIN };
305
+ }
306
+ /** Whether all of a tuft (`needleTuft`) at a lay's place, its needles as long as the lay, is inside the card's clear margin. */
307
+ function tuftInside(L, l) {
308
+ // (Its needles reach on past the tip, out square and a little back, and from as far back as its stretch.)
309
+ const ca = Math.cos(l.ang);
310
+ const sa = Math.sin(l.ang);
311
+ const r = l.len * 1.3;
312
+ const cx = l.x - ca * l.len * 0.2;
313
+ const cy = l.y - sa * l.len * 0.2;
314
+ // (A long brush's back too: its needles stand out from the shoot that far behind the tip.)
315
+ const b = l.len * Math.max(0, (l.brush ?? TUFT_STRETCH) - TUFT_STRETCH);
316
+ const bx = l.x - ca * (l.len * TUFT_STRETCH + b);
317
+ const by = l.y - sa * (l.len * TUFT_STRETCH + b);
318
+ const rb = l.len;
319
+ const ok = (x, y, rr) => x - rr >= MARGIN && y - rr >= MARGIN && x + rr <= L.n - 1 - MARGIN && y + rr <= L.n - 1 - MARGIN;
320
+ return ok(cx, cy, r) && (b <= 0 || ok(bx, by, rb));
321
+ }
322
+ function leaf(L, sh, l, shadow) {
323
+ const n = L.n;
324
+ const s = l.sprite;
325
+ const ox = s.x;
326
+ const oy = s.y;
327
+ // (The leaf's own frame on its sheet: up its midrib from its stalk's end, and across to its right.)
328
+ const ax = s.tip[0] - s.base[0];
329
+ const ay = s.tip[1] - s.base[1];
330
+ const sl = Math.hypot(ax, ay) || 1;
331
+ const ux = ax / sl;
332
+ const uy = ay / sl;
333
+ const rx = -uy;
334
+ const ry = ux;
335
+ const k = l.len / sl;
336
+ const da = [Math.cos(l.ang), Math.sin(l.ang)];
337
+ const fl = l.flip ? -1 : 1;
338
+ const dc = [-da[1] * fl, da[0] * fl];
339
+ const box = footprint(L, l);
340
+ const i0 = Math.max(MARGIN, box.i0);
341
+ const i1 = Math.min(n - 1 - MARGIN, box.i1);
342
+ const j0 = Math.max(MARGIN, box.j0);
343
+ const j1 = Math.min(n - 1 - MARGIN, box.j1);
344
+ const kept = L.flower ? meansOf(sh) : null;
345
+ const meanL = kept ? kept.leaf : Math.max(0.02, sh.set.meanLinear[0] * 0.2126 + sh.set.meanLinear[1] * 0.7152 + sh.set.meanLinear[2] * 0.0722);
346
+ /** Where on the sheet a texel of the card falls (or null off the leaf's box). */
347
+ const toSheet = (px, py) => {
348
+ const a = px * da[0] + py * da[1];
349
+ const u = a / l.len;
350
+ const c = px * dc[0] + py * dc[1] - l.bend * l.len * u * u;
351
+ const along = a / k;
352
+ if (l.clip && along < 0)
353
+ return null;
354
+ const across = c / (k * l.width);
355
+ const sx = s.base[0] + ux * along + rx * across;
356
+ const sy = s.base[1] + uy * along + ry * across;
357
+ if (sx < 0 || sy < 0 || sx > s.w || sy > s.h)
358
+ return null;
359
+ return [ox + sx, oy + sy];
360
+ };
361
+ // (Its shadow on what is already drawn: down and to the right a little, soft.)
362
+ if (shadow > 0) {
363
+ for (let j = j0; j <= j1 + Math.ceil(shadow); j++) {
364
+ if (j > n - 1 - MARGIN)
365
+ break;
366
+ for (let i = i0; i <= i1 + Math.ceil(shadow * 0.4); i++) {
367
+ if (i > n - 1 - MARGIN)
368
+ break;
369
+ const o = j * n + i;
370
+ if (L.cover[o] <= 0)
371
+ continue;
372
+ const at = toSheet(i + 0.5 - l.x - shadow * 0.4, j + 0.5 - l.y - shadow);
373
+ if (!at)
374
+ continue;
375
+ const c = sample(sh.albedo, at[0], at[1], 3);
376
+ if (c > 0)
377
+ L.tone[o] *= 1 - 0.3 * c;
378
+ }
379
+ }
380
+ }
381
+ for (let j = j0; j <= j1; j++) {
382
+ for (let i = i0; i <= i1; i++) {
383
+ const at = toSheet(i + 0.5 - l.x, j + 0.5 - l.y);
384
+ if (!at)
385
+ continue;
386
+ const c = sample(sh.albedo, at[0], at[1], 3);
387
+ if (c <= 0.004)
388
+ continue;
389
+ // (The texel's lightness, from the nearest sheet texel's colour: its photograph's detail.)
390
+ const xi = Math.min(sh.albedo.width - 1, Math.max(0, Math.floor(at[0])));
391
+ const yi = Math.min(sh.albedo.height - 1, Math.max(0, Math.floor(at[1])));
392
+ const q = (yi * sh.albedo.width + xi) * 4;
393
+ const d = sh.albedo.data;
394
+ const lum = 0.2126 * LIN[d[q]] + 0.7152 * LIN[d[q + 1]] + 0.0722 * LIN[d[q + 2]];
395
+ if (L.warm) {
396
+ // (Its warmth: red over blue for its lightness, beside its sheet's, as a share.)
397
+ const w = (LIN[d[q]] - LIN[d[q + 2]]) / Math.max(0.01, LIN[d[q]] + LIN[d[q + 1]] + LIN[d[q + 2]]);
398
+ const w0 = (sh.set.meanLinear[0] - sh.set.meanLinear[2]) / Math.max(0.01, sh.set.meanLinear[0] + sh.set.meanLinear[1] + sh.set.meanLinear[2]);
399
+ L.warm[j * n + i] = L.warm[j * n + i] * (1 - c) + clamp01(0.5 + (w - w0) * WARMTH) * c;
400
+ }
401
+ // (Its relief, turned with it: lit a little from the card's top, so a vein or a curl shows.)
402
+ const nx = sample(sh.normal, at[0], at[1], 0) * 2 - 1;
403
+ const ny = sample(sh.normal, at[0], at[1], 1) * 2 - 1;
404
+ const thin = sample(sh.normal, at[0], at[1], 2);
405
+ // (The normal's x runs across the leaf, its y up the leaf to its tip; on the card: across dc, along da.
406
+ // Lit from the card's far end (+rows), a little across.)
407
+ const px = (nx * dc[0] + ny * da[0]) * fl;
408
+ const py = nx * dc[1] + ny * da[1];
409
+ const relief = 1 + 0.35 * (px * 0.35 + py * 0.6);
410
+ // (A flower's tone against the flowers' mean, a leaf's against the leaves'.)
411
+ const bloom = kept ? flowerOf(d[q] / 255, d[q + 1] / 255, d[q + 2] / 255) : 0;
412
+ const tone = Math.min(1, LEAF * l.tone * Math.pow(lum / (kept ? lerp(kept.leaf, kept.flower, bloom) : meanL), 0.85) * relief);
413
+ const o = j * n + i;
414
+ if (L.flower)
415
+ L.flower[o] = L.flower[o] * (1 - c) + bloom * c;
416
+ L.tone[o] = L.tone[o] * (1 - c) + tone * c;
417
+ L.own[o] = L.own[o] * (1 - c) + clamp01(l.own * 0.55 + thin * 0.45) * c;
418
+ L.cover[o] = L.cover[o] * (1 - c) + c;
419
+ }
420
+ }
421
+ }
422
+ function grow(x, y, a, len, curl, rnd, n = 8, wobble = 0.08) {
423
+ const pts = [{ x, y, a }];
424
+ for (let i = 1; i <= n; i++) {
425
+ a += curl / n + (rnd() - 0.5) * wobble;
426
+ x += Math.cos(a) * (len / n);
427
+ y += Math.sin(a) * (len / n);
428
+ pts.push({ x, y, a });
429
+ }
430
+ return pts;
431
+ }
432
+ function along(pts, s) {
433
+ const f = clamp01(s) * (pts.length - 1);
434
+ const i = Math.min(pts.length - 2, Math.floor(f));
435
+ const t = f - i;
436
+ return { x: lerp(pts[i].x, pts[i + 1].x, t), y: lerp(pts[i].y, pts[i + 1].y, t), a: pts[i + 1].a };
437
+ }
438
+ const UP = Math.PI / 2;
439
+ /** A near card's spray of `look`'s leaves, `extent` metres across: a short twig or two and their leaves, in metres from its foot. */
440
+ function nearSpray(look, extent, rnd, density = 0.5) {
441
+ const L = look.leafCm / 100;
442
+ const twigs = [];
443
+ const leaves = [];
444
+ const size = () => L * (0.82 + rnd() * 0.3);
445
+ const thick = Math.max(0.0012, L * 0.025);
446
+ const kind = look.spray;
447
+ if (kind === "tufts") {
448
+ // (A pine's twig's end: its last few years' shoots, each year's a whorl, and a tuft of needles at the end
449
+ // of each: two or three big brushes on a long-needled pine, a dozen small ones on a short-needled one.)
450
+ return pineShoots(extent * 0.55, Math.min(needleOf(look), extent * 0.3), density, rnd, look.brush);
451
+ }
452
+ if (kind === "sprays") {
453
+ // (A short bare stem, and off it the sprays fanning out, the middle one last, on top.)
454
+ const stem = extent * 0.12;
455
+ twigs.push({ pts: grow(0, 0, UP, stem, 0, rnd, 2, 0.02), r0: thick * 1.6, r1: thick * 1.2 });
456
+ const n = 4 + Math.floor(rnd() * 2);
457
+ for (let k = n - 1; k >= 0; k--) {
458
+ const side = k % 2 ? 1 : -1;
459
+ const ang = UP + (k === 0 ? (rnd() - 0.5) * 0.2 : side * (0.3 + 0.22 * Math.ceil(k / 2) + rnd() * 0.15));
460
+ const at = k === 0 ? stem : stem * (0.3 + 0.7 * rnd());
461
+ leaves.push({ x: 0, y: at, ang, len: Math.min(extent * 0.78, size() * (k === 0 ? 1.05 : 0.85 - 0.08 * Math.ceil(k / 2))) });
462
+ }
463
+ return { twigs, leaves };
464
+ }
465
+ const reach = extent * 0.72;
466
+ const main = grow(0, 0, UP + (rnd() - 0.5) * 0.25, reach * (kind === "hanging" ? 0.7 : 1), (rnd() - 0.5) * 0.5, rnd, 10, 0.05);
467
+ twigs.push({ pts: main, r0: thick * 1.4, r1: thick * 0.7 });
468
+ const shoots = [{ pts: main, len: reach }];
469
+ if (kind === "alternate" || kind === "opposite") {
470
+ // (Two shorter shoots off the first, one to either side, so the card is a little twig of leaves and not one
471
+ // row of them.)
472
+ let side = rnd() < 0.5 ? 1 : -1;
473
+ for (const at of [0.16 + rnd() * 0.08, 0.36 + rnd() * 0.08, 0.56 + rnd() * 0.08]) {
474
+ const p = along(main, at);
475
+ const len = reach * (0.42 + rnd() * 0.2) * (1.15 - at * 0.6);
476
+ const pts = grow(p.x, p.y, p.a + side * (0.6 + rnd() * 0.3), len, -side * 0.3, rnd, 6, 0.06);
477
+ twigs.push({ pts, r0: thick, r1: thick * 0.6 });
478
+ shoots.push({ pts, len });
479
+ side = -side;
480
+ }
481
+ }
482
+ for (const sh of shoots) {
483
+ const first = sh === shoots[0];
484
+ if (kind === "alternate" || kind === "opposite") {
485
+ // (A leaf at each node, to either side in turn (or a pair), set off from the shoot and forward; nodes
486
+ // closer towards the tip, and a few leaves gathered at it.)
487
+ let s = (first ? 0.22 : 0.12) + rnd() * 0.06;
488
+ let side = rnd() < 0.5 ? 1 : -1;
489
+ const gap = Math.max(0.12, (L * 0.42) / sh.len);
490
+ const per = look.perSpray ? Math.max(1, look.perSpray / 8) : 1;
491
+ while (s < 0.92) {
492
+ const p = along(sh.pts, s);
493
+ for (const sd of kind === "opposite" ? [1, -1] : [(side = -side)]) {
494
+ leaves.push({ x: p.x, y: p.y, ang: p.a + sd * (0.55 + rnd() * 0.45), len: size() * (1 - 0.15 * s) });
495
+ }
496
+ s += (gap / per) * (0.8 + rnd() * 0.4) * (1 - 0.35 * s);
497
+ }
498
+ const tip = sh.pts[sh.pts.length - 1];
499
+ const n = kind === "opposite" ? 3 : 2 + Math.floor(rnd() * 2);
500
+ for (let k = 0; k < n; k++)
501
+ leaves.push({ x: tip.x, y: tip.y, ang: tip.a + (k - (n - 1) / 2) * 0.45 + (rnd() - 0.5) * 0.2, len: size() * 0.95 });
502
+ }
503
+ else if (kind === "hanging") {
504
+ // (A few long leaves alternate along the shoot's outer stretch, each hanging on its stalk beside it,
505
+ // sickle-curved and edge on, a little apart: gathered at its end, they lay over one another into a lobed
506
+ // mass, an oak's leaf.)
507
+ const n = look.perSpray ?? 6;
508
+ let sd = rnd() < 0.5 ? 1 : -1;
509
+ for (let k = 0; k < n; k++) {
510
+ const p = along(sh.pts, 0.2 + 0.8 * (k / Math.max(1, n - 1)) + (rnd() - 0.5) * 0.06);
511
+ sd = -sd;
512
+ leaves.push({ x: p.x, y: p.y, ang: p.a + sd * (0.12 + rnd() * 0.22), len: size() * 1.05 });
513
+ }
514
+ }
515
+ else {
516
+ // (Weeping: narrow leaves all along the hanging shoot, close to it, pointing on down it.)
517
+ const gap = Math.max(0.035, (L * 0.32) / sh.len);
518
+ let side = 1;
519
+ for (let s = 0.06 + rnd() * 0.04; s < 0.97; s += gap * (0.8 + rnd() * 0.4)) {
520
+ const p = along(sh.pts, s);
521
+ side = -side;
522
+ leaves.push({ x: p.x, y: p.y, ang: p.a + side * (0.28 + rnd() * 0.3), len: size() * (0.85 + 0.15 * (1 - s)) });
523
+ }
524
+ }
525
+ }
526
+ return { twigs, leaves };
527
+ }
528
+ /**
529
+ * A pine's shoots, grown as a pine grows them: a shoot `reach` metres long from the card's foot, a whorl of two
530
+ * or three shoots a year up it, each of them in turn putting out its own, all turning up at their ends; and a
531
+ * tuft of needles `N` metres long (`needleTuft`) only at the end of each shoot, the wood behind it bare. So a
532
+ * pine's branch is a candelabrum of separate tufts with sky between, not a mat of needles, which read as a
533
+ * broad leaf's mass; a dense pine's (`density`) more crowded, an open one's sparser.
534
+ */
535
+ export function pineShoots(reach, N, density, rnd, brush) {
536
+ const twigs = [];
537
+ const leaves = [];
538
+ // (A shoot shorter than this ends in a tuft and puts out no more: a year's shoot is some twice its needles
539
+ // long, and its tufts were a mat of needles where its shoots were shorter.)
540
+ const least = N * PINE_SHOOT;
541
+ const keep = 0.55 + 0.45 * Math.min(1, Math.max(0, density));
542
+ const shoot = (x, y, a, len, r, depth) => {
543
+ const pts = grow(x, y, a, len, (UP - a) * 0.35, rnd, 6, 0.06);
544
+ twigs.push({ pts, r0: r, r1: r * 0.6 });
545
+ const tip = pts[pts.length - 1];
546
+ leaves.push({ x: tip.x, y: tip.y, ang: tip.a, len: N * (0.85 + 0.3 * rnd()), needles: true, brush });
547
+ if (depth >= 3 || len < least)
548
+ return;
549
+ // (Its whorls up it, two or three shoots each, shorter towards its end; a few of them lost.)
550
+ for (let f = 0.3 + rnd() * 0.1; f < 0.9; f += 0.28 * (0.85 + rnd() * 0.3)) {
551
+ const p = along(pts, f);
552
+ const m = 2 + (rnd() < 0.35 ? 1 : 0);
553
+ for (let q = 0; q < m; q++) {
554
+ const sub = len * (0.62 - f * 0.4) * (0.8 + rnd() * 0.4);
555
+ if (sub < least * 0.6 || rnd() > keep)
556
+ continue;
557
+ const side = q % 2 ? 1 : -1;
558
+ const off = side * (0.6 + 0.35 * rnd()) + (q === 2 ? (rnd() - 0.5) * 0.3 : 0);
559
+ shoot(p.x, p.y, p.a + off, sub, r * 0.7, depth + 1);
560
+ }
561
+ }
562
+ };
563
+ // (A pine's shoot a centimetre or so through, a long-needled one's stouter; a branch's thicker at its foot.)
564
+ shoot(0, 0, UP + (rnd() - 0.5) * 0.2, reach, Math.min(0.012, Math.max(0.002, N * 0.035 * Math.sqrt(reach / Math.max(N, 0.01)) * 0.6)), 0);
565
+ return { twigs, leaves };
566
+ }
567
+ /**
568
+ * A pine's big card: the end of a branch a clump wide, `extent` metres, seen from the side: the branch running
569
+ * across the card, a little bowed, its shoots rising off it side by side, each of them a pine's shoots
570
+ * (`pineShoots`) at their size, longer towards its middle. A cushion of tufts, flat beneath and domed above,
571
+ * wider than high, as many and as close as the pine is dense, its rim the tufts' spikes: a pine's clump as it
572
+ * is seen from afar. One branch's shoots fanned from a point were a round blot, a broad leaf's mass, and a
573
+ * crown of them a cloud.
574
+ */
575
+ function pineBranch(extent, N, density, rnd, brush) {
576
+ const twigs = [];
577
+ const leaves = [];
578
+ const d = Math.min(1, Math.max(0, density));
579
+ // (A short-needled pine's tufts are small: more of its shoots to fill the cushion.)
580
+ const m = Math.round((BRANCH_SHOOTS + BRANCH_SHOOTS_DENSE * d) * Math.min(2, Math.max(1, BRANCH_NEEDLE / Math.max(N, 0.01))));
581
+ const reach = extent * BRANCH_REACH;
582
+ const main = grow(-reach / 2, 0, (rnd() - 0.5) * 0.1, reach, -0.25 + (rnd() - 0.5) * 0.15, rnd, 10, 0.03);
583
+ const r = Math.min(0.02, Math.max(0.004, N * 0.06));
584
+ twigs.push({ pts: main, r0: r * BRANCH_WOOD, r1: r * 0.5 * BRANCH_WOOD });
585
+ for (let q = 0; q < m; q++) {
586
+ // (Along the branch evenly, give or take; the middle's shoots longest, so the cushion's top is a dome.)
587
+ const f = Math.min(1, Math.max(0, (q + 0.2 + 0.6 * rnd()) / m));
588
+ const p = along(main, f);
589
+ const mid = 1 - Math.abs(2 * f - 1);
590
+ const a = UP + (f - 0.5) * BRANCH_FAN + (rnd() - 0.5) * 0.35;
591
+ const len = extent * (0.1 + 0.22 * mid) * (0.75 + 0.5 * rnd());
592
+ const one = pineShoots(len, N, density, rnd, brush);
593
+ const c = Math.cos(a - UP);
594
+ const sn = Math.sin(a - UP);
595
+ for (const t of one.twigs)
596
+ twigs.push({ pts: t.pts.map((u) => ({ x: p.x + u.x * c - u.y * sn, y: p.y + u.x * sn + u.y * c, a: u.a + a - UP })), r0: t.r0 * BRANCH_WOOD, r1: t.r1 * BRANCH_WOOD });
597
+ for (const l of one.leaves)
598
+ leaves.push({ ...l, x: p.x + l.x * c - l.y * sn, y: p.y + l.x * sn + l.y * c, ang: l.ang + a - UP });
599
+ }
600
+ return { twigs, leaves };
601
+ }
602
+ /**
603
+ * A big card's spray: a branch's end `extent` metres long, side shoots off it, and all along them its near
604
+ * sprays (`near` metres across each), turned their own ways: as many leaves as a clump of the tree carries.
605
+ */
606
+ function bigSpray(look, extent, near, rnd, density = 0) {
607
+ if (look.spray === "tufts")
608
+ return pineBranch(extent, needleOf(look), density, rnd, look.brush);
609
+ const twigs = [];
610
+ const leaves = [];
611
+ const kind = look.spray;
612
+ const thick = Math.max(0.003, (look.leafCm / 100) * 0.04);
613
+ const hangs = kind === "hanging" || kind === "weeping";
614
+ const reach = extent * 0.8;
615
+ const main = grow(0, 0, UP + (rnd() - 0.5) * 0.2, reach, (rnd() - 0.5) * 0.4, rnd, 12, 0.04);
616
+ twigs.push({ pts: main, r0: thick * 2.2, r1: thick * 0.8 });
617
+ const shoots = [main];
618
+ let side = rnd() < 0.5 ? 1 : -1;
619
+ for (let f = 0.05 + rnd() * 0.03; f < 0.88; f += 0.075 * (0.8 + rnd() * 0.4)) {
620
+ const p = along(main, f);
621
+ const off = hangs ? 0.2 + rnd() * 0.3 : 0.65 + (0.6 - f) * 0.5 + rnd() * 0.3;
622
+ const len = reach * (hangs ? 0.7 - f * 0.3 : 0.55 - f * 0.3) * (0.8 + rnd() * 0.4);
623
+ const pts = grow(p.x, p.y, p.a + side * off, len, -side * (0.15 + rnd() * 0.3), rnd, 6, 0.06);
624
+ twigs.push({ pts, r0: thick * 1.3, r1: thick * 0.6 });
625
+ shoots.push(pts);
626
+ side = -side;
627
+ }
628
+ // (The sprays along every shoot, about half a spray apart, to either side in turn, the last at its tip.)
629
+ for (const pts of shoots) {
630
+ const len = pts.reduce((sum, p, i) => (i ? sum + Math.hypot(p.x - pts[i - 1].x, p.y - pts[i - 1].y) : 0), 0);
631
+ // (A conifer's sprays and shoots closer: a bough's needles are a dense comb.)
632
+ const gap = (near * (kind === "sprays" ? 0.17 : 0.3)) / Math.max(len, 1e-3);
633
+ let sd = rnd() < 0.5 ? 1 : -1;
634
+ for (let s = 0.12 + rnd() * 0.08; s <= 1.001; s += gap * (0.75 + rnd() * 0.5)) {
635
+ const p = along(pts, Math.min(1, s));
636
+ sd = -sd;
637
+ const turn = p.a - UP + (s > 0.97 ? (rnd() - 0.5) * 0.3 : sd * (hangs ? 0.15 + rnd() * 0.3 : 0.5 + rnd() * 0.5));
638
+ const k = 0.75 + rnd() * 0.35;
639
+ const c = Math.cos(turn);
640
+ const sn = Math.sin(turn);
641
+ const one = nearSpray(look, near, rnd);
642
+ for (const t of one.twigs)
643
+ twigs.push({ pts: t.pts.map((q) => ({ x: p.x + (q.x * c - q.y * sn) * k, y: p.y + (q.x * sn + q.y * c) * k, a: q.a + turn })), r0: t.r0 * k, r1: t.r1 * k });
644
+ for (const l of one.leaves)
645
+ leaves.push({ x: p.x + (l.x * c - l.y * sn) * k, y: p.y + (l.x * sn + l.y * c) * k, ang: l.ang + turn, len: l.len * k, needles: l.needles });
646
+ }
647
+ }
648
+ return { twigs, leaves };
649
+ }
650
+ /** A palm's leaf, fitted to the card: leaflets along a rachis (`pinnate`), or segments round a fan. */
651
+ function frond(look, rnd) {
652
+ const twigs = [];
653
+ const leaves = [];
654
+ if (look.spray === "pinnate") {
655
+ const len = 1;
656
+ const rachis = grow(0, 0, UP + (rnd() - 0.5) * 0.06, len, (rnd() - 0.5) * 0.15, rnd, 12, 0.01);
657
+ twigs.push({ pts: rachis, r0: 0.012, r1: 0.004 });
658
+ for (const sd of [1, -1]) {
659
+ for (let s = 0.08 + rnd() * 0.01; s < 0.98; s += 0.016 + rnd() * 0.006) {
660
+ if (rnd() < 0.05)
661
+ continue;
662
+ const p = along(rachis, s);
663
+ const env = s < 0.3 ? 0.4 + 0.6 * Math.sin(((s - 0.08) / 0.22) * (Math.PI / 2)) : 1 - 0.78 * Math.pow((s - 0.3) / 0.7, 1.5);
664
+ const spread = (rnd() < 0.5 ? 0.82 : 1.1) + (rnd() - 0.5) * 0.16;
665
+ leaves.push({ x: p.x, y: p.y, ang: p.a + sd * spread * (1 - 0.3 * s * s), len: 0.5 * env * (0.82 + rnd() * 0.3) });
666
+ }
667
+ }
668
+ }
669
+ else {
670
+ const pet = grow(0, 0, UP, 0.38, (rnd() - 0.5) * 0.05, rnd, 6, 0.01);
671
+ twigs.push({ pts: pet, r0: 0.014, r1: 0.011 });
672
+ const h = pet[pet.length - 1];
673
+ const n = 34 + Math.floor(rnd() * 5);
674
+ const from = (-10 * Math.PI) / 180;
675
+ const d = ((200 * Math.PI) / 180) / n;
676
+ for (let i = 0; i < n; i++) {
677
+ const a = from + d * (i + 0.5) + (rnd() - 0.5) * 0.25 * d;
678
+ leaves.push({ x: h.x, y: h.y, ang: a, len: 0.62 * (0.74 + 0.26 * Math.max(0, Math.sin(a))) * (0.93 + rnd() * 0.12) });
679
+ }
680
+ }
681
+ return { twigs, leaves };
682
+ }
683
+ /**
684
+ * Draw one card of a species' leaves into `out` (RGBA8, as leaves.ts's atlas), its cell `n` texels square at
685
+ * (ox, oy): a near card (`big` false, `extent` its width in metres, its twig coming in near its first row),
686
+ * or its big card. `seed` makes each card its own.
687
+ */
688
+ export function drawSprayCard(out, ox, oy, n, sheet, look, extent, big, seed, density = 0, far = 1) {
689
+ const rnd = rng(seed);
690
+ const whole = look.spray === "whole";
691
+ const palm = look.spray === "pinnate" || look.spray === "fan" || whole;
692
+ // (A whole leaf (a Canary palm's frond) is laid at its size: the card, one of the clumps along the frond, shows
693
+ // the stretch of it the card covers, a third to two thirds of the way out.)
694
+ const section = whole ? 0.3 + rnd() * 0.35 : 0;
695
+ const { twigs, leaves } = whole ? { twigs: [], leaves: [{ x: 0, y: -section * (look.leafCm / 100), ang: UP + (rnd() - 0.5) * 0.06, len: look.leafCm / 100 }] } : palm ? frond(look, rnd) : big ? bigSpray(look, extent, 2 * nearCardHalf(look), rnd, density) : nearSpray(look, extent, rnd, density);
696
+ // (A dense species' big card has a second branch's leaves behind its own, in their shade: the leaves deeper
697
+ // in a clump, which close the gaps a single layer of sprays leaves, as a dense crown is closed. As many of
698
+ // them as it is dense past DENSE_FROM.)
699
+ const under = [];
700
+ const thick = big && !palm ? Math.min(1, Math.max(0, (density - DENSE_FROM) / (1 - DENSE_FROM))) : 0;
701
+ if (thick > 0) {
702
+ const more = bigSpray(look, extent, 2 * nearCardHalf(look), rnd, density);
703
+ for (const l of more.leaves)
704
+ if (rnd() < thick)
705
+ under.push(l);
706
+ }
707
+ const L = { n, far, tone: new Float32Array(n * n), own: new Float32Array(n * n), cover: new Float32Array(n * n), ...(look.flowers ? { flower: new Float32Array(n * n) } : {}) };
708
+ // (Metres to texels: a near card at its real size, its foot near its first row; a big card or a palm's
709
+ // leaf fitted inside its cell.)
710
+ let k = n / extent;
711
+ let fx = n / 2;
712
+ let fy = n * FOOT;
713
+ if (whole)
714
+ fy = n / 2;
715
+ else if (big || palm) {
716
+ let x0 = Infinity;
717
+ let x1 = -Infinity;
718
+ let y0 = Infinity;
719
+ let y1 = -Infinity;
720
+ const add = (x, y) => {
721
+ x0 = Math.min(x0, x);
722
+ x1 = Math.max(x1, x);
723
+ y0 = Math.min(y0, y);
724
+ y1 = Math.max(y1, y);
725
+ };
726
+ for (const t of twigs)
727
+ for (const p of t.pts)
728
+ add(p.x, p.y);
729
+ for (const l of leaves) {
730
+ add(l.x, l.y);
731
+ add(l.x + Math.cos(l.ang) * l.len, l.y + Math.sin(l.ang) * l.len);
732
+ }
733
+ // (The leaves behind lie within the branch's own reach: they do not widen the card.)
734
+ const pad = palm ? 0.04 : (look.leafCm / 100) * 0.3;
735
+ const room = n - 2 * MARGIN - 4;
736
+ const fit = Math.min(room / (x1 - x0 + 2 * pad), room / (y1 - y0 + 2 * pad));
737
+ // (A big card keeps its leaves at their size in metres where it can; only a branch too big for it shrinks.)
738
+ k = palm ? fit : Math.min(k, fit);
739
+ fx = n / 2 - ((x0 + x1) / 2) * k;
740
+ fy = n / 2 - ((y0 + y1) / 2) * k;
741
+ }
742
+ const X = (x) => fx + x * k;
743
+ const Y = (y) => fy + y * k;
744
+ for (const t of twigs) {
745
+ for (let i = 1; i < t.pts.length; i++) {
746
+ const f0 = (i - 1) / (t.pts.length - 1);
747
+ const f1 = i / (t.pts.length - 1);
748
+ stroke(L, X(t.pts[i - 1].x), Y(t.pts[i - 1].y), X(t.pts[i].x), Y(t.pts[i].y), Math.max(0.7, lerp(t.r0, t.r1, f0) * k), Math.max(0.6, lerp(t.r0, t.r1, f1) * k), TWIG);
749
+ }
750
+ }
751
+ const sprites = sheet.set.sprites;
752
+ // (Its leaves in a shuffled order, so which lies over which is not by where they are.)
753
+ const order = leaves.map((_, i) => i);
754
+ if (!(look.spray === "sprays" || look.spray === "tufts")) {
755
+ for (let i = order.length - 1; i > 0; i--) {
756
+ const j = Math.floor(rnd() * (i + 1));
757
+ [order[i], order[j]] = [order[j], order[i]];
758
+ }
759
+ }
760
+ const shadow = Math.max(1, Math.min(4, n / 160));
761
+ const all = [...under, ...leaves];
762
+ const deep = under.length;
763
+ for (const i of [...under.map((_, j) => j), ...order.map((j) => j + deep)]) {
764
+ const l = all[i];
765
+ const sprite = sprites[Math.floor(rnd() * sprites.length) % sprites.length];
766
+ const lay = {
767
+ x: X(l.x),
768
+ y: Y(l.y),
769
+ ang: l.ang,
770
+ len: l.len * k * (l.needles ? 1 : 0.75 + 0.25 * sprite.size),
771
+ sprite,
772
+ width: look.leafWidth ?? 1,
773
+ bend: (look.leafBend ?? 0) * (rnd() < 0.5 ? 1 : -1) * (0.6 + rnd() * 0.6) + (rnd() - 0.5) * 0.04,
774
+ flip: rnd() < 0.5,
775
+ tone: (0.9 + rnd() * 0.2) * (i < deep ? UNDER_TONE : 1),
776
+ own: rnd(),
777
+ brush: l.brush,
778
+ };
779
+ // (A pine whose pictures are its whole shoots (`ScanLook.tuftPicture`) lays one at each tuft: its brush reaching as
780
+ // far past the shoot's end as a drawn tuft's needles would, its twig back along the shoot.)
781
+ const shootPicture = !!l.needles && !!look.tuftPicture;
782
+ // (Its picture cut where its brush begins (`tuftFrom`), the wood behind left to the card's own twigs, and laid as
783
+ // a fan of a few (`tuftFan`), turned about the brush's foot: a long-needled pine's brush stands out all round its
784
+ // shoot's end, a starburst, where one flat-laid picture was a narrow tassel, a leaf's blot from the tee.)
785
+ const fan = shootPicture ? Math.max(1, look.tuftFan ?? 1) : 1;
786
+ // (Where the tuft is, and its needles' length, before the picture is laid back from it.)
787
+ const x0 = lay.x;
788
+ const y0 = lay.y;
789
+ const needle = lay.len;
790
+ let tipBack = 0;
791
+ let len0 = lay.len;
792
+ if (shootPicture) {
793
+ const from = Math.min(0.9, Math.max(0, look.tuftFrom ?? 0));
794
+ const shoot = lay.len * (look.tuftShoot ?? TUFT_SHOOT) * (1 - from);
795
+ if (from > 0) {
796
+ const s0 = lay.sprite;
797
+ lay.sprite = { ...s0, base: [s0.base[0] + (s0.tip[0] - s0.base[0]) * from, s0.base[1] + (s0.tip[1] - s0.base[1]) * from] };
798
+ lay.clip = true;
799
+ }
800
+ const back = Math.max(0, shoot - lay.len * TUFT_STRETCH);
801
+ lay.x -= Math.cos(lay.ang) * back;
802
+ lay.y -= Math.sin(lay.ang) * back;
803
+ lay.len = shoot;
804
+ len0 = shoot;
805
+ tipBack = 0;
806
+ }
807
+ const fanned = (c) => (fan > 1 ? { ...lay, ang: lay.ang + (c / (fan - 1) - 0.5) * TUFT_FAN * (fan - 1), flip: c % 2 === 1 ? !lay.flip : lay.flip } : lay);
808
+ // (A leaf that would reach past the card's edge is made smaller, or left off: cut by the edge, it shows a
809
+ // straight line.)
810
+ const inside = () => (l.needles && !shootPicture ? tuftInside(L, lay) : Array.from({ length: fan }, (_, c) => footprint(L, fanned(c)).inside).every(Boolean));
811
+ let fits = whole || inside();
812
+ for (let t = 0; t < 3 && !fits; t++) {
813
+ lay.len *= 0.85;
814
+ fits = inside();
815
+ }
816
+ if (!fits)
817
+ continue;
818
+ if (l.needles && !shootPicture)
819
+ needleTuft(L, lay.x, lay.y, lay.ang, lay.len, rnd, lay.tone, lay.own, l.brush);
820
+ // (A fan's middle picture last, over its sides; each a little longer or shorter than the next, and the pine's own
821
+ // needles drawn under them, longer, so the brush's edge is needle tips at their own lengths and not one smooth arc,
822
+ // which a few dozen metres off was a broad leaf's outline.)
823
+ // (Drawn for a far mip (`FAR_BARE`), its needles alone, as many more as its pictures covered: the pictures
824
+ // fanned about the brush's foot were its few big lobes there, a palmate leaf from the tee.)
825
+ else if (fan > 1 && (L.far ?? 1) >= FAR_BARE)
826
+ needleTuft(L, x0 + Math.cos(lay.ang) * tipBack, y0 + Math.sin(lay.ang) * tipBack, lay.ang, needle * (lay.len / len0), rnd, lay.tone, lay.own, l.brush, FAR_BARE_MORE);
827
+ else if (fan > 1) {
828
+ needleTuft(L, x0 + Math.cos(lay.ang) * tipBack, y0 + Math.sin(lay.ang) * tipBack, lay.ang, needle * (lay.len / len0), rnd, lay.tone, lay.own, l.brush);
829
+ for (const c of Array.from({ length: fan }, (_, c) => c).sort((a, b) => Math.abs(b - (fan - 1) / 2) - Math.abs(a - (fan - 1) / 2)))
830
+ leaf(L, sheet, { ...fanned(c), len: lay.len * (TUFT_FAN_LEN + 0.1 * ((c * 7) % 3)) }, shadow);
831
+ }
832
+ else
833
+ leaf(L, sheet, lay, palm ? shadow * 0.5 : shadow);
834
+ }
835
+ // (Where nothing is drawn, the card's mean tone, so its mips' edges do not darken.)
836
+ let sum = 0;
837
+ let wsum = 0;
838
+ for (let o = 0; o < n * n; o++) {
839
+ sum += L.tone[o];
840
+ wsum += L.cover[o];
841
+ }
842
+ const mean = Math.min(1, wsum > 0 ? sum / wsum : LEAF);
843
+ write(out, ox, oy, n, L, mean, whole);
844
+ }
845
+ /**
846
+ * One big card of a pine's cushions (`ScanLook.cushion`): a whole picture of one of them seen from the side
847
+ * (`cushions`, its `pick`th), its flat base on the card's foot row and as wide as the card, its domed top
848
+ * towards the far end: its own light and shade, lit top and dark beneath, kept as its tone. Fitted inside the
849
+ * cell's clear margin.
850
+ */
851
+ export function drawCushionCard(out, ox, oy, n, cushions, pick, thick = 0) {
852
+ const sprites = cushions.set.sprites;
853
+ // (Stood as its picture stands, its base's middle under its top's: the sheet's stalk-to-tip line, from its
854
+ // lowest and highest covered rows' middles, leans on a wide cushion and would turn it aslant.)
855
+ const s0 = sprites[((pick % sprites.length) + sprites.length) % sprites.length];
856
+ const s = { ...s0, base: [s0.w / 2, s0.base[1]], tip: [s0.w / 2, s0.tip[1]] };
857
+ // (Its warmth kept too: a photograph's sunlit top is yellower than its shaded underside.)
858
+ const L = { n, tone: new Float32Array(n * n), own: new Float32Array(n * n), cover: new Float32Array(n * n), warm: new Float32Array(n * n) };
859
+ // (Its frame on the sheet: up from its base (its flat base's middle) to its top, as the sheet stands it.)
860
+ const sl = Math.hypot(s.tip[0] - s.base[0], s.tip[1] - s.base[1]) || 1;
861
+ const room = n - 2 * MARGIN - 4;
862
+ // (As wide as the card, or as tall; below its base (twigs under it) as much of the picture as hangs there.)
863
+ const k = Math.min(room / s.w, room / s.h);
864
+ // (A pad thinner than its look's pads (`ScanLook.padThick`, its thickness over its width) is laid in layers, each
865
+ // a little narrower, higher and to one side or the other, the higher ones behind: a shelf as thick as a coastal
866
+ // cypress's, lumpy and lit along each layer's top, and not one thin disc. Each a little taller than its picture.)
867
+ const ratio = (s.h * PAD_STRETCH) / s.w;
868
+ const layers = thick > ratio ? Math.min(PAD_LAYERS, 1 + Math.ceil((thick - ratio) / (ratio * PAD_STEP))) : 1;
869
+ const stretch = layers > 1 ? PAD_STRETCH : 1;
870
+ const kk = layers > 1 ? Math.min(k, room / (s.w * (1 + PAD_SHIFT)), room / (s.h * stretch + (thick - ratio) * s.w)) : k;
871
+ // (The top layer, a little shorter than the bottom one, reaching to the pad's thickness.)
872
+ const top = s.h * stretch * (1 - PAD_NARROW * 0.5 * (layers - 1));
873
+ const rise = layers > 1 ? (Math.max(0, thick * s.w - top) * kk) / (layers - 1) : 0;
874
+ const foot = MARGIN + 2 + Math.max(0, s.h - s.base[1]) * kk * stretch;
875
+ for (let q = layers - 1; q >= 0; q--) {
876
+ const narrow = 1 - PAD_NARROW * q;
877
+ const side = q === 0 ? 0 : (q % 2 ? 1 : -1) * PAD_SHIFT * s.w * kk * (0.5 + 0.5 * hash01(q, pick + 11));
878
+ const lay = { x: n / 2 + (s.base[0] - s.w / 2) * kk * narrow + side, y: foot + q * rise, ang: UP, len: sl * kk * stretch * (layers > 1 ? 1 - PAD_NARROW * 0.5 * q : 1), sprite: s, width: narrow / (stretch * (layers > 1 ? 1 - PAD_NARROW * 0.5 * q : 1)), bend: 0, flip: (q + pick) % 2 === 0, tone: 1, own: 0.5 };
879
+ leaf(L, cushions, lay, 0);
880
+ }
881
+ const mean = (() => {
882
+ let sum = 0;
883
+ let w = 0;
884
+ for (let o = 0; o < n * n; o++) {
885
+ sum += L.tone[o];
886
+ w += L.cover[o];
887
+ }
888
+ return Math.min(1, w > 0 ? sum / w : LEAF);
889
+ })();
890
+ write(out, ox, oy, n, L, mean, false);
891
+ }
892
+ /** A pad laid in layers (`drawCushionCard`): each this much taller than its picture, at most this many, each this share of a picture's height above the one below, this much narrower, and to one side by up to this share of its width. */
893
+ const PAD_STRETCH = 1.25;
894
+ const PAD_LAYERS = 4;
895
+ const PAD_STEP = 0.55;
896
+ const PAD_NARROW = 0.1;
897
+ const PAD_SHIFT = 0.06;
898
+ /** A drawn cushion's lit top and shaded underside (tone, as a leaf's), and how dark the creases between its lumps. */
899
+ const CUSHION_TOP = 1.6;
900
+ const CUSHION_UNDER = 0.28;
901
+ const CUSHION_CREASE = 0.6;
902
+ /** How high a drawn cushion's dome is for its width, and how many tufts to a tuft's own area within it (its closeness). */
903
+ const CUSHION_HIGH = 0.5;
904
+ /** How far a drawn cushion's flat base curls up at its ends, a share of its height. */
905
+ const CUSHION_CURL = 0.35;
906
+ /** A pine less dense than this has loose clumps of lumps, not a dome. */
907
+ const CUSHION_LOOSE = 0.5;
908
+ const CUSHION_PACK = 6;
909
+ /** How far a photograph's warmth (red over blue, a share of its lightness) is stretched about its sheet's mean, into 0..1. */
910
+ const WARMTH = 4;
911
+ /**
912
+ * A pine's cushion drawn needle by needle, where it has no picture of one (`ScanLook.cushion` "drawn"): seen
913
+ * from the side, a few lumps side by side on one flat base, the middle's highest, together as wide as the card;
914
+ * filled with its own tufts (`needleTuft`, its needles' length at the card's scale) packed as close as it is
915
+ * dense, each bristling out of its lump; each lit as high as it is in its lump, its top's tips light and its
916
+ * underside near black, darker still in the creases where two lumps meet, a few twigs under its flat base.
917
+ * So a far cushion is a cushion's shape and light, and a near one its needles.
918
+ */
919
+ export function drawnCushionCard(out, ox, oy, n, look, extent, seed, density, far = 1) {
920
+ const rnd = rng(seed);
921
+ const L = { n, far, tone: new Float32Array(n * n), own: new Float32Array(n * n), cover: new Float32Array(n * n) };
922
+ const k = n / extent;
923
+ const N = Math.max(6, Math.min(n * 0.09, needleOf(look) * k));
924
+ const d = clamp01(density);
925
+ // (Its lumps: as wide as the card all told, less a needle at either side for their tufts' tips.)
926
+ const base = MARGIN + 3 + N * 0.35;
927
+ const x0 = MARGIN + N;
928
+ const x1 = n - MARGIN - N;
929
+ // (A dome as wide as the card, flat beneath, and on it its puffs: lumps on its top and shoulders, each
930
+ // a smaller dome of its own, with creases between them.)
931
+ const W = x1 - x0;
932
+ // (An open pine's clumps (an Aleppo pine's) are looser: a few lumps of their own, at heights of their own,
933
+ // with air between, and no one dome under them.)
934
+ const loose = d < CUSHION_LOOSE;
935
+ const lumps = loose ? [] : [{ x: (x0 + x1) / 2, y: base, rx: W / 2, ry: W * CUSHION_HIGH }];
936
+ const m = loose ? 4 + Math.floor(rnd() * 3) : 3 + Math.floor(rnd() * 3);
937
+ for (let q = 0; q < m; q++) {
938
+ if (loose) {
939
+ const rx = W * (0.13 + 0.1 * rnd());
940
+ const ry = rx * (0.65 + 0.35 * rnd());
941
+ const f = (q + 0.5 + (rnd() - 0.5) * 0.8) / m;
942
+ lumps.push({ x: x0 + rx + (W - 2 * rx) * f, y: base + ry * 0.4 + W * CUSHION_HIGH * 0.5 * rnd() * (1 - Math.abs(2 * f - 1) * 0.6), rx, ry });
943
+ continue;
944
+ }
945
+ // (Round the dome from shoulder to shoulder, a little apart.)
946
+ const a = Math.PI * (0.12 + 0.76 * ((q + 0.5 + (rnd() - 0.5) * 0.5) / m));
947
+ const rx = W * (0.15 + 0.08 * rnd());
948
+ const ry = rx * (0.75 + 0.3 * rnd());
949
+ const cx = (x0 + x1) / 2 - Math.cos(a) * (W / 2 - rx * 0.6);
950
+ const cy = base + Math.sin(a) * (W * CUSHION_HIGH - ry * 0.7) * 0.9;
951
+ lumps.push({ x: cx, y: cy, rx, ry });
952
+ }
953
+ /** How far in a point is in a lump (0 its rim, 1 its heart), and which lumps it is in. */
954
+ const inLump = (x, y, l) => {
955
+ // (Its base flat beneath, curling up a little at its ends: a cushion's underside, not a board's edge.)
956
+ const u0 = (x - (x0 + x1) / 2) / (W / 2);
957
+ if (y < base + CUSHION_CURL * W * CUSHION_HIGH * u0 * u0 - N * 0.1)
958
+ return 0;
959
+ const u = (x - l.x) / l.rx;
960
+ const v = (y - l.y) / l.ry;
961
+ return 1 - Math.hypot(u, l.y <= base ? Math.max(0, v) : v);
962
+ };
963
+ // (A few twigs under its flat base, its branch's shoots going up into it.)
964
+ for (let q = 0; q < (loose ? 0 : m + 2); q++) {
965
+ const x = x0 + (x1 - x0) * ((q + 0.5) / (m + 2)) + (rnd() - 0.5) * N;
966
+ stroke(L, x + (rnd() - 0.5) * N * 2, base - N * 0.25, x, base + N * 1.2, 1.2, 0.8, TWIG * 0.7);
967
+ }
968
+ // (Its tufts: as many as cover it some times over, fewer in an open pine's, with gaps between.)
969
+ const area = lumps.reduce((a, l) => a + (Math.PI / 2) * l.rx * l.ry, 0);
970
+ const count = Math.round(((area / (Math.PI * N * N * 0.5)) * CUSHION_PACK * (0.55 + 0.45 * d)));
971
+ const tufts = [];
972
+ for (let q = 0; q < count * 3 && tufts.length < count; q++) {
973
+ const x = x0 + (x1 - x0) * rnd();
974
+ const y = base + (n - MARGIN - base) * rnd() * 0.9;
975
+ let best = -1;
976
+ let bi = 0;
977
+ let second = -1;
978
+ for (let i = 0; i < lumps.length; i++) {
979
+ const f = inLump(x, y, lumps[i]);
980
+ if (f > best) {
981
+ second = best;
982
+ best = f;
983
+ bi = i;
984
+ }
985
+ else if (f > second)
986
+ second = f;
987
+ }
988
+ if (best <= 0)
989
+ continue;
990
+ // (An open pine's cushion has holes: a share of its tufts left out, more of them deep in it.)
991
+ if (rnd() > 0.45 + 0.55 * d + 0.3 * (1 - best))
992
+ continue;
993
+ const l = lumps[bi];
994
+ // (Out of its lump: from its base's middle, up and out, the more so towards its top.)
995
+ const ux = (x - l.x) / l.rx;
996
+ const vy = (y - l.y) / l.ry;
997
+ const ang = Math.atan2(vy + 0.25, ux) + (rnd() - 0.5) * 1.6;
998
+ // (How high it is: in its lump, 0 at the lump's foot and 1 at its top above it, and in the cushion.)
999
+ const top = l.y + l.ry * Math.sqrt(Math.max(0, 1 - ux * ux));
1000
+ const own = clamp01((y - (l.y - (l.y > base ? l.ry * 0.6 : 0))) / Math.max(1, top - l.y + (l.y > base ? l.ry * 0.6 : 0)));
1001
+ const whole = clamp01((y - base) / (W * CUSHION_HIGH));
1002
+ const h = 0.55 * own + 0.45 * whole;
1003
+ // (In a crease where two lumps meet: both about as deep.)
1004
+ const crease = second > 0 ? clamp01(1 - (best - second) / 0.25) : 0;
1005
+ tufts.push({ x, y, ang, h, crease, t: 0.85 + 0.3 * rnd() });
1006
+ }
1007
+ // (The deep and low ones first, those on its top and rim last, over them.)
1008
+ tufts.sort((a, b) => a.h - b.h);
1009
+ for (const tf of tufts) {
1010
+ const lit = lerp(CUSHION_UNDER, CUSHION_TOP, smoothstep01(tf.h ** 1.3)) * (1 - CUSHION_CREASE * tf.crease * (1 - tf.h * 0.5));
1011
+ needleTuft(L, tf.x, tf.y, tf.ang, N * (0.8 + 0.4 * rnd()), rnd, lit * tf.t, rnd(), look.brush);
1012
+ }
1013
+ let sum = 0;
1014
+ let w = 0;
1015
+ for (let o = 0; o < n * n; o++) {
1016
+ sum += L.tone[o];
1017
+ w += L.cover[o];
1018
+ }
1019
+ write(out, ox, oy, n, L, Math.min(1, w > 0 ? sum / w : LEAF), false);
1020
+ }
1021
+ const smoothstep01 = (x) => {
1022
+ const t = clamp01(x);
1023
+ return t * t * (3 - 2 * t);
1024
+ };
1025
+ /** A card's layer written into its cell of the atlas: tone, its own number, coverage; where nothing is drawn, the card's mean tone. Its first row is its foot. */
1026
+ function write(out, ox, oy, n, L, mean, whole) {
1027
+ for (let j = 0; j < n; j++) {
1028
+ for (let i = 0; i < n; i++) {
1029
+ const o = j * n + i;
1030
+ // (Its first row is its foot: the shaders lay a card's first row at its twig's foot (geometry.ts
1031
+ // `cardPieces`, engine.ts `vTreeUv`), every species' alike.)
1032
+ const p = ((oy + j) * out.width + ox + i) * 4;
1033
+ // (A frond's stretch fades out at the card's edges, where its leaflets run on past it.)
1034
+ const edge = Math.min(i, j, n - 1 - i, n - 1 - j) - MARGIN;
1035
+ const fade = whole ? clamp01(edge / (n * 0.08)) : 1;
1036
+ const a = L.cover[o] * fade * (2 - fade);
1037
+ const cov = (a * 255 + 0.5) | 0;
1038
+ const tone = cov > 0 ? Math.min(1, L.tone[o] / L.cover[o]) : mean;
1039
+ const t = (tone * 255 + 0.5) | 0;
1040
+ out.data[p] = t;
1041
+ out.data[p + 1] = cov > 0 ? ((Math.min(1, L.own[o] / L.cover[o]) * 255 + 0.5) | 0) : 128;
1042
+ // (Blue: its tone again, or a photograph's warmth where it keeps it (`Layer.warm`), or how far it is a flower
1043
+ // (`Layer.flower`).)
1044
+ out.data[p + 2] = L.warm ? (cov > 0 ? ((Math.min(1, L.warm[o] / L.cover[o]) * 255 + 0.5) | 0) : 128) : L.flower ? (cov > 0 ? ((Math.min(1, L.flower[o] / L.cover[o]) * 255 + 0.5) | 0) : 0) : t;
1045
+ out.data[p + 3] = cov;
1046
+ }
1047
+ }
1048
+ }
1049
+ /**
1050
+ * A species' row: its three near cards, its big cards and its far cards. `bigExtent`: how wide its big cards
1051
+ * are (m). A pine with pictures of its cushions (`cushions`, `ScanLook.cushion`) has them on its big cards, one
1052
+ * to a card, or its cushions drawn (`"drawn"`); with pictures of its canopy (`far`, `ScanLook.cushionFar`) those
1053
+ * on its far cards. Else its far cards are its big cards again.
1054
+ */
1055
+ export function drawSprayRow(out, row, n, sheet, look, bigExtent, seed, density = 0, cushions, far, mip = 1) {
1056
+ // (A palm's near cards are the drawn ones' size: a clump's radius and a little less (geometry.ts `cardPieces`),
1057
+ // a big card's 0.8 / 1.45 of it.)
1058
+ const near = 2 * nearCardHalf(look) || bigExtent * (0.8 / 1.45);
1059
+ for (let c = 0; c < FAR_CARD; c++) {
1060
+ const big = c >= BIG_CARD;
1061
+ if (big && look.cushion && cushions && cushions.set.sprites.length > 0)
1062
+ drawCushionCard(out, c * n, row * n, n, cushions, c - BIG_CARD, look.padThick);
1063
+ else if (big && look.cushion)
1064
+ drawnCushionCard(out, c * n, row * n, n, look, bigExtent, seed * 31 + c * 7 + 3, density, mip);
1065
+ else
1066
+ drawSprayCard(out, c * n, row * n, n, sheet, look, big && look.spray !== "whole" ? bigExtent : near, big, seed * 31 + c * 7 + 3, density, mip);
1067
+ }
1068
+ for (let c = FAR_CARD; c < FAR_CARD + FAR_CARDS; c++) {
1069
+ if (far && far.set.sprites.length > 0) {
1070
+ drawCushionCard(out, c * n, row * n, n, far, c - FAR_CARD, look.padThick);
1071
+ continue;
1072
+ }
1073
+ // (Its big card again, copied.)
1074
+ const from = BIG_CARD + ((c - FAR_CARD) % BIG_CARDS);
1075
+ for (let j = 0; j < n; j++)
1076
+ out.data.copyWithin(((row * n + j) * out.width + c * n) * 4, ((row * n + j) * out.width + from * n) * 4, ((row * n + j) * out.width + from * n + n) * 4);
1077
+ }
1078
+ }
1079
+ /** How many of a pine's mips below its full-size picture are drawn for themselves (`pineMips`): down to a card of 32 texels from 512. */
1080
+ export const PINE_MIPS = 4;
1081
+ /**
1082
+ * Which of a species' row's cards are a pine's needles drawn tuft by tuft (`needleTuft`), whose mips are drawn
1083
+ * for themselves (`pineMip`): a pine's near cards, and its big and far cards unless they are photographs.
1084
+ */
1085
+ export function needledCards(look, cushions, far) {
1086
+ // (A pine whose tufts are each one picture of its shoot (`tuftPicture`, no fan) has no needles drawn: its mips are its pictures'.)
1087
+ if (look.spray !== "tufts" || (look.tuftPicture && (look.tuftFan ?? 1) <= 1))
1088
+ return Array.from({ length: SPRAY_COLS }, () => false);
1089
+ const bigPhoto = !!look.cushion && !!cushions && cushions.set.sprites.length > 0;
1090
+ const farPhoto = !!far && far.set.sprites.length > 0;
1091
+ return Array.from({ length: SPRAY_COLS }, (_, c) => (c < BIG_CARD ? true : c < FAR_CARD ? !bigPhoto : !farPhoto && !bigPhoto));
1092
+ }
1093
+ /**
1094
+ * A pine's row drawn again for its mip `level` (`Layer.far`): drawn at full size with its needles each as wide
1095
+ * as that mip's texel and as many fewer, then averaged down to the mip's size, `n >> level` texels a card. Its
1096
+ * first row is the row's.
1097
+ */
1098
+ export function pineMip(level, n, sheet, look, bigExtent, seed, density = 0, cushions, far) {
1099
+ const full = { data: new Uint8Array(SPRAY_COLS * n * n * 4), width: SPRAY_COLS * n, height: n };
1100
+ drawSprayRow(full, 0, n, sheet, look, bigExtent, seed, density, cushions, far, 1 << level);
1101
+ return boxDown(full, level);
1102
+ }
1103
+ /** ... for each of its first `levels` mips. */
1104
+ export function pineMips(n, sheet, look, bigExtent, seed, density = 0, cushions, far, levels = PINE_MIPS) {
1105
+ const out = [];
1106
+ for (let l = 1; l <= levels && n >> l >= 1; l++)
1107
+ out.push(pineMip(l, n, sheet, look, bigExtent, seed, density, cushions, far));
1108
+ return out;
1109
+ }
1110
+ /** A picture averaged down `level` times, 2^level texels square to one: colour weighted by coverage where there is any. */
1111
+ function boxDown(px, level) {
1112
+ const f = 1 << level;
1113
+ const w = px.width >> level;
1114
+ const h = px.height >> level;
1115
+ const data = new Uint8Array(w * h * 4);
1116
+ for (let j = 0; j < h; j++) {
1117
+ for (let i = 0; i < w; i++) {
1118
+ let a = 0;
1119
+ const c = [0, 0, 0];
1120
+ for (let y = 0; y < f; y++) {
1121
+ const row = ((j * f + y) * px.width + i * f) * 4;
1122
+ for (let x = 0; x < f; x++) {
1123
+ const p = row + x * 4;
1124
+ a += px.data[p + 3];
1125
+ for (let k = 0; k < 3; k++)
1126
+ c[k] += px.data[p + k];
1127
+ }
1128
+ }
1129
+ const o = (j * w + i) * 4;
1130
+ for (let k = 0; k < 3; k++)
1131
+ data[o + k] = Math.round(c[k] / (f * f));
1132
+ data[o + 3] = Math.round(a / (f * f));
1133
+ }
1134
+ }
1135
+ return { data, width: w, height: h };
1136
+ }
1137
+ //# sourceMappingURL=sprays.js.map