webgpu-trees 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1547 -2
- package/assets/SOURCES.md +263 -0
- package/assets/bark/alder.albedo.avif +0 -0
- package/assets/bark/alder.normal.avif +0 -0
- package/assets/bark/aleppoPine.albedo.avif +0 -0
- package/assets/bark/aleppoPine.normal.avif +0 -0
- package/assets/bark/ash.albedo.avif +0 -0
- package/assets/bark/ash.normal.avif +0 -0
- package/assets/bark/aspen.albedo.avif +0 -0
- package/assets/bark/aspen.normal.avif +0 -0
- package/assets/bark/beech.albedo.avif +0 -0
- package/assets/bark/beech.normal.avif +0 -0
- package/assets/bark/birch.albedo.avif +0 -0
- package/assets/bark/birch.normal.avif +0 -0
- package/assets/bark/birchFoot.albedo.avif +0 -0
- package/assets/bark/birchFoot.normal.avif +0 -0
- package/assets/bark/corsicanPine.albedo.avif +0 -0
- package/assets/bark/corsicanPine.normal.avif +0 -0
- package/assets/bark/cypress.albedo.avif +0 -0
- package/assets/bark/cypress.normal.avif +0 -0
- package/assets/bark/douglas.albedo.avif +0 -0
- package/assets/bark/douglas.normal.avif +0 -0
- package/assets/bark/eucalyptus.albedo.avif +0 -0
- package/assets/bark/eucalyptus.normal.avif +0 -0
- package/assets/bark/eucalyptusUpper.albedo.avif +0 -0
- package/assets/bark/eucalyptusUpper.normal.avif +0 -0
- package/assets/bark/fibrous.albedo.avif +0 -0
- package/assets/bark/fibrous.normal.avif +0 -0
- package/assets/bark/hemlock.albedo.avif +0 -0
- package/assets/bark/hemlock.normal.avif +0 -0
- package/assets/bark/hornbeam.albedo.avif +0 -0
- package/assets/bark/hornbeam.normal.avif +0 -0
- package/assets/bark/larch.albedo.avif +0 -0
- package/assets/bark/larch.normal.avif +0 -0
- package/assets/bark/lime.albedo.avif +0 -0
- package/assets/bark/lime.normal.avif +0 -0
- package/assets/bark/liveOak.albedo.avif +0 -0
- package/assets/bark/liveOak.normal.avif +0 -0
- package/assets/bark/magnolia.albedo.avif +0 -0
- package/assets/bark/magnolia.normal.avif +0 -0
- package/assets/bark/maple.albedo.avif +0 -0
- package/assets/bark/maple.normal.avif +0 -0
- package/assets/bark/montereyPine.albedo.avif +0 -0
- package/assets/bark/montereyPine.normal.avif +0 -0
- package/assets/bark/norMaple.albedo.avif +0 -0
- package/assets/bark/norMaple.normal.avif +0 -0
- package/assets/bark/oak.albedo.avif +0 -0
- package/assets/bark/oak.normal.avif +0 -0
- package/assets/bark/palmLattice.albedo.avif +0 -0
- package/assets/bark/palmLattice.normal.avif +0 -0
- package/assets/bark/palmPineapple.albedo.avif +0 -0
- package/assets/bark/palmPineapple.normal.avif +0 -0
- package/assets/bark/palmRings.albedo.avif +0 -0
- package/assets/bark/palmRings.normal.avif +0 -0
- package/assets/bark/pine.albedo.avif +0 -0
- package/assets/bark/pine.normal.avif +0 -0
- package/assets/bark/pineFlakes.albedo.avif +0 -0
- package/assets/bark/pineFlakes.normal.avif +0 -0
- package/assets/bark/plane.albedo.avif +0 -0
- package/assets/bark/plane.normal.avif +0 -0
- package/assets/bark/poplar.albedo.avif +0 -0
- package/assets/bark/poplar.normal.avif +0 -0
- package/assets/bark/redAlder.albedo.avif +0 -0
- package/assets/bark/redAlder.normal.avif +0 -0
- package/assets/bark/redOak.albedo.avif +0 -0
- package/assets/bark/redOak.normal.avif +0 -0
- package/assets/bark/ridged.albedo.avif +0 -0
- package/assets/bark/ridged.normal.avif +0 -0
- package/assets/bark/sitkaSpruce.albedo.avif +0 -0
- package/assets/bark/sitkaSpruce.normal.avif +0 -0
- package/assets/bark/spruce.albedo.avif +0 -0
- package/assets/bark/spruce.normal.avif +0 -0
- package/assets/bark/stonePine.albedo.avif +0 -0
- package/assets/bark/stonePine.normal.avif +0 -0
- package/assets/bark/sweetgum.albedo.avif +0 -0
- package/assets/bark/sweetgum.normal.avif +0 -0
- package/assets/bark/sycMaple.albedo.avif +0 -0
- package/assets/bark/sycMaple.normal.avif +0 -0
- package/assets/bark/torreyPine.albedo.avif +0 -0
- package/assets/bark/torreyPine.normal.avif +0 -0
- package/assets/bark/whitePine.albedo.avif +0 -0
- package/assets/bark/whitePine.normal.avif +0 -0
- package/assets/bark/whiteSpruce.albedo.avif +0 -0
- package/assets/bark/whiteSpruce.normal.avif +0 -0
- package/assets/bark/whiteWillow.albedo.avif +0 -0
- package/assets/bark/whiteWillow.normal.avif +0 -0
- package/assets/bark/willow.albedo.avif +0 -0
- package/assets/bark/willow.normal.avif +0 -0
- package/assets/leaves/alder.albedo.avif +0 -0
- package/assets/leaves/alder.normal.avif +0 -0
- package/assets/leaves/ash.albedo.avif +0 -0
- package/assets/leaves/ash.normal.avif +0 -0
- package/assets/leaves/aspen.albedo.avif +0 -0
- package/assets/leaves/aspen.normal.avif +0 -0
- package/assets/leaves/beech.albedo.avif +0 -0
- package/assets/leaves/beech.normal.avif +0 -0
- package/assets/leaves/birch.albedo.avif +0 -0
- package/assets/leaves/birch.normal.avif +0 -0
- package/assets/leaves/blackthorn.albedo.avif +0 -0
- package/assets/leaves/blackthorn.normal.avif +0 -0
- package/assets/leaves/blade.albedo.avif +0 -0
- package/assets/leaves/blade.normal.avif +0 -0
- package/assets/leaves/broomSprig.albedo.avif +0 -0
- package/assets/leaves/broomSprig.normal.avif +0 -0
- package/assets/leaves/corsicanShoot.albedo.avif +0 -0
- package/assets/leaves/corsicanShoot.normal.avif +0 -0
- package/assets/leaves/cypress.albedo.avif +0 -0
- package/assets/leaves/cypress.normal.avif +0 -0
- package/assets/leaves/cypressPad.albedo.avif +0 -0
- package/assets/leaves/cypressPad.normal.avif +0 -0
- package/assets/leaves/deadFronds.albedo.avif +0 -0
- package/assets/leaves/deadFronds.normal.avif +0 -0
- package/assets/leaves/deltoid.albedo.avif +0 -0
- package/assets/leaves/deltoid.normal.avif +0 -0
- package/assets/leaves/douglasShoot.albedo.avif +0 -0
- package/assets/leaves/douglasShoot.normal.avif +0 -0
- package/assets/leaves/elderSprig.albedo.avif +0 -0
- package/assets/leaves/elderSprig.normal.avif +0 -0
- package/assets/leaves/elliptic.albedo.avif +0 -0
- package/assets/leaves/elliptic.normal.avif +0 -0
- package/assets/leaves/eucalyptus.albedo.avif +0 -0
- package/assets/leaves/eucalyptus.normal.avif +0 -0
- package/assets/leaves/fan.albedo.avif +0 -0
- package/assets/leaves/fan.normal.avif +0 -0
- package/assets/leaves/fanB.albedo.avif +0 -0
- package/assets/leaves/fanB.normal.avif +0 -0
- package/assets/leaves/fir.albedo.avif +0 -0
- package/assets/leaves/fir.normal.avif +0 -0
- package/assets/leaves/frond.albedo.avif +0 -0
- package/assets/leaves/frond.normal.avif +0 -0
- package/assets/leaves/gorse.albedo.avif +0 -0
- package/assets/leaves/gorse.normal.avif +0 -0
- package/assets/leaves/hawthorn.albedo.avif +0 -0
- package/assets/leaves/hawthorn.normal.avif +0 -0
- package/assets/leaves/hemlockShoot.albedo.avif +0 -0
- package/assets/leaves/hemlockShoot.normal.avif +0 -0
- package/assets/leaves/hornbeam.albedo.avif +0 -0
- package/assets/leaves/hornbeam.normal.avif +0 -0
- package/assets/leaves/lance.albedo.avif +0 -0
- package/assets/leaves/lance.normal.avif +0 -0
- package/assets/leaves/larchShoot.albedo.avif +0 -0
- package/assets/leaves/larchShoot.normal.avif +0 -0
- package/assets/leaves/lime.albedo.avif +0 -0
- package/assets/leaves/lime.normal.avif +0 -0
- package/assets/leaves/liveOak.albedo.avif +0 -0
- package/assets/leaves/liveOak.normal.avif +0 -0
- package/assets/leaves/londonPlane.albedo.avif +0 -0
- package/assets/leaves/londonPlane.normal.avif +0 -0
- package/assets/leaves/magnolia.albedo.avif +0 -0
- package/assets/leaves/magnolia.normal.avif +0 -0
- package/assets/leaves/maple.albedo.avif +0 -0
- package/assets/leaves/maple.normal.avif +0 -0
- package/assets/leaves/norMaple.albedo.avif +0 -0
- package/assets/leaves/norMaple.normal.avif +0 -0
- package/assets/leaves/oak.albedo.avif +0 -0
- package/assets/leaves/oak.normal.avif +0 -0
- package/assets/leaves/paintFan.albedo.avif +0 -0
- package/assets/leaves/paintFan.normal.avif +0 -0
- package/assets/leaves/paintFrond.albedo.avif +0 -0
- package/assets/leaves/paintFrond.normal.avif +0 -0
- package/assets/leaves/pine.albedo.avif +0 -0
- package/assets/leaves/pine.normal.avif +0 -0
- package/assets/leaves/pineClumpDark.albedo.avif +0 -0
- package/assets/leaves/pineClumpDark.normal.avif +0 -0
- package/assets/leaves/pineClumpLight.albedo.avif +0 -0
- package/assets/leaves/pineClumpLight.normal.avif +0 -0
- package/assets/leaves/poplar.albedo.avif +0 -0
- package/assets/leaves/poplar.normal.avif +0 -0
- package/assets/leaves/queenFrond.albedo.avif +0 -0
- package/assets/leaves/queenFrond.normal.avif +0 -0
- package/assets/leaves/redAlder.albedo.avif +0 -0
- package/assets/leaves/redAlder.normal.avif +0 -0
- package/assets/leaves/redOak.albedo.avif +0 -0
- package/assets/leaves/redOak.normal.avif +0 -0
- package/assets/leaves/round.albedo.avif +0 -0
- package/assets/leaves/round.normal.avif +0 -0
- package/assets/leaves/scale.albedo.avif +0 -0
- package/assets/leaves/scale.normal.avif +0 -0
- package/assets/leaves/scotsPineClump.albedo.avif +0 -0
- package/assets/leaves/scotsPineClump.normal.avif +0 -0
- package/assets/leaves/seaBuckthornSprig.albedo.avif +0 -0
- package/assets/leaves/seaBuckthornSprig.normal.avif +0 -0
- package/assets/leaves/serrate.albedo.avif +0 -0
- package/assets/leaves/serrate.normal.avif +0 -0
- package/assets/leaves/sitkaShoot.albedo.avif +0 -0
- package/assets/leaves/sitkaShoot.normal.avif +0 -0
- package/assets/leaves/stonePineCanopy.albedo.avif +0 -0
- package/assets/leaves/stonePineCanopy.normal.avif +0 -0
- package/assets/leaves/stonePineCushion.albedo.avif +0 -0
- package/assets/leaves/stonePineCushion.normal.avif +0 -0
- package/assets/leaves/sweetgum.albedo.avif +0 -0
- package/assets/leaves/sweetgum.normal.avif +0 -0
- package/assets/leaves/sycamore.albedo.avif +0 -0
- package/assets/leaves/sycamore.normal.avif +0 -0
- package/assets/leaves/torreyPine.albedo.avif +0 -0
- package/assets/leaves/torreyPine.normal.avif +0 -0
- package/assets/leaves/whitePine.albedo.avif +0 -0
- package/assets/leaves/whitePine.normal.avif +0 -0
- package/assets/leaves/whiteSpruceShoot.albedo.avif +0 -0
- package/assets/leaves/whiteSpruceShoot.normal.avif +0 -0
- package/assets/leaves/whiteWillow.albedo.avif +0 -0
- package/assets/leaves/whiteWillow.normal.avif +0 -0
- package/assets/leaves/willow.albedo.avif +0 -0
- package/assets/leaves/willow.normal.avif +0 -0
- package/assets/marks/marks.json +894 -0
- package/assets/marks/painterly.avif +0 -0
- package/assets/marks/sheets.avif +0 -0
- package/assets/scans.json +6406 -0
- package/lib/age.d.ts +36 -0
- package/lib/age.d.ts.map +1 -0
- package/lib/age.js +170 -0
- package/lib/age.js.map +1 -0
- package/lib/atmosphere.d.ts +64 -0
- package/lib/atmosphere.d.ts.map +1 -0
- package/lib/atmosphere.js +260 -0
- package/lib/atmosphere.js.map +1 -0
- package/lib/dabs.d.ts +4 -0
- package/lib/dabs.d.ts.map +1 -0
- package/lib/dabs.js +77 -0
- package/lib/dabs.js.map +1 -0
- package/lib/engine.d.ts +211 -0
- package/lib/engine.d.ts.map +1 -0
- package/lib/engine.js +3808 -0
- package/lib/engine.js.map +1 -0
- package/lib/genetics.d.ts +129 -0
- package/lib/genetics.d.ts.map +1 -0
- package/lib/genetics.js +721 -0
- package/lib/genetics.js.map +1 -0
- package/lib/geometry.d.ts +237 -0
- package/lib/geometry.d.ts.map +1 -0
- package/lib/geometry.js +1944 -0
- package/lib/geometry.js.map +1 -0
- package/lib/grow.d.ts +83 -0
- package/lib/grow.d.ts.map +1 -0
- package/lib/grow.js +3851 -0
- package/lib/grow.js.map +1 -0
- package/lib/hiz.d.ts +74 -0
- package/lib/hiz.d.ts.map +1 -0
- package/lib/hiz.js +161 -0
- package/lib/hiz.js.map +1 -0
- package/lib/impostor.d.ts +106 -0
- package/lib/impostor.d.ts.map +1 -0
- package/lib/impostor.js +450 -0
- package/lib/impostor.js.map +1 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +16 -0
- package/lib/index.js.map +1 -0
- package/lib/leaves.d.ts +30 -0
- package/lib/leaves.d.ts.map +1 -0
- package/lib/leaves.js +829 -0
- package/lib/leaves.js.map +1 -0
- package/lib/light.d.ts +110 -0
- package/lib/light.d.ts.map +1 -0
- package/lib/light.js +430 -0
- package/lib/light.js.map +1 -0
- package/lib/lobes.d.ts +25 -0
- package/lib/lobes.d.ts.map +1 -0
- package/lib/lobes.js +151 -0
- package/lib/lobes.js.map +1 -0
- package/lib/marks.d.ts +63 -0
- package/lib/marks.d.ts.map +1 -0
- package/lib/marks.js +553 -0
- package/lib/marks.js.map +1 -0
- package/lib/mips.d.ts +42 -0
- package/lib/mips.d.ts.map +1 -0
- package/lib/mips.js +220 -0
- package/lib/mips.js.map +1 -0
- package/lib/paintedMarks.d.ts +21 -0
- package/lib/paintedMarks.d.ts.map +1 -0
- package/lib/paintedMarks.js +53 -0
- package/lib/paintedMarks.js.map +1 -0
- package/lib/palm.d.ts +162 -0
- package/lib/palm.d.ts.map +1 -0
- package/lib/palm.js +779 -0
- package/lib/palm.js.map +1 -0
- package/lib/palmAtlas.d.ts +79 -0
- package/lib/palmAtlas.d.ts.map +1 -0
- package/lib/palmAtlas.js +335 -0
- package/lib/palmAtlas.js.map +1 -0
- package/lib/palmGeometry.d.ts +41 -0
- package/lib/palmGeometry.d.ts.map +1 -0
- package/lib/palmGeometry.js +309 -0
- package/lib/palmGeometry.js.map +1 -0
- package/lib/phenology.d.ts +21 -0
- package/lib/phenology.d.ts.map +1 -0
- package/lib/phenology.js +168 -0
- package/lib/phenology.js.map +1 -0
- package/lib/random.d.ts +34 -0
- package/lib/random.d.ts.map +1 -0
- package/lib/random.js +92 -0
- package/lib/random.js.map +1 -0
- package/lib/scans.d.ts +232 -0
- package/lib/scans.d.ts.map +1 -0
- package/lib/scans.js +397 -0
- package/lib/scans.js.map +1 -0
- package/lib/scatter.d.ts +70 -0
- package/lib/scatter.d.ts.map +1 -0
- package/lib/scatter.js +186 -0
- package/lib/scatter.js.map +1 -0
- package/lib/species.d.ts +369 -0
- package/lib/species.d.ts.map +1 -0
- package/lib/species.js +2978 -0
- package/lib/species.js.map +1 -0
- package/lib/sprays.d.ts +112 -0
- package/lib/sprays.d.ts.map +1 -0
- package/lib/sprays.js +1137 -0
- package/lib/sprays.js.map +1 -0
- package/lib/stand.d.ts +52 -0
- package/lib/stand.d.ts.map +1 -0
- package/lib/stand.js +279 -0
- package/lib/stand.js.map +1 -0
- package/lib/styles.d.ts +135 -0
- package/lib/styles.d.ts.map +1 -0
- package/lib/styles.js +79 -0
- package/lib/styles.js.map +1 -0
- package/lib/tree.d.ts +134 -0
- package/lib/tree.d.ts.map +1 -0
- package/lib/tree.js +57 -0
- package/lib/tree.js.map +1 -0
- package/lib/variants.d.ts +157 -0
- package/lib/variants.d.ts.map +1 -0
- package/lib/variants.js +532 -0
- package/lib/variants.js.map +1 -0
- package/lib/visibility.d.ts +105 -0
- package/lib/visibility.d.ts.map +1 -0
- package/lib/visibility.js +242 -0
- package/lib/visibility.js.map +1 -0
- package/package.json +62 -3
package/lib/leaves.js
ADDED
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
// The picture a realistic crown's leaf cards carry: a spray of foliage at a
|
|
2
|
+
// twig's end, one to three metres across, as it would look laid flat. Five
|
|
3
|
+
// kinds of leaf, two sprays of each, all drawn here by arithmetic (no picture
|
|
4
|
+
// is loaded, so nothing bought or photographed is in the repository). A palm's
|
|
5
|
+
// "spray" is a frond: a feather of leaflets on a rachis, or a pleated fan.
|
|
6
|
+
//
|
|
7
|
+
// A spray is built as a plant is: a twig, side twigs off it, and leaves (or
|
|
8
|
+
// needles) on short stalks along them, each its own size, angle and tone.
|
|
9
|
+
// Then it is drawn shape by shape, each over what is already there, each in
|
|
10
|
+
// its own small box of texels:
|
|
11
|
+
//
|
|
12
|
+
// a twig or a needle is a tapered stroke;
|
|
13
|
+
// a blade is an outline about its midrib (its half width along it: ovate,
|
|
14
|
+
// lanceolate, round; shallow lobes or small teeth on its margin), a little
|
|
15
|
+
// bent, lighter along the midrib and on the side that faces up, darker at
|
|
16
|
+
// its rim, and casting a soft shadow on what it lies over.
|
|
17
|
+
//
|
|
18
|
+
// Channels: red and blue are one grey tone (the shader multiplies the
|
|
19
|
+
// species' colour by it); green is a number of each shape's own, 0..1, so the
|
|
20
|
+
// shader can tell one leaf from the next (how thin it is, against the light);
|
|
21
|
+
// alpha is coverage, antialiased by distance to the shape's edge. No spray comes within a few texels of its cell's border, so
|
|
22
|
+
// the mips of one cell do not bleed into the next.
|
|
23
|
+
import { hash01 } from "./variants.js";
|
|
24
|
+
/** The kinds of leaf the atlas holds, by index (the same as `LEAF_KIND_ROWS` in species.ts). */
|
|
25
|
+
export const LEAF_KINDS = ["broad", "narrow", "needle", "small", "frond", "pine"];
|
|
26
|
+
const CELL = 256;
|
|
27
|
+
const COLS = 4;
|
|
28
|
+
/** Two kinds to a row. */
|
|
29
|
+
const ROWS = Math.ceil(LEAF_KINDS.length / 2);
|
|
30
|
+
const WIDTH = CELL * COLS;
|
|
31
|
+
const HEIGHT = CELL * ROWS;
|
|
32
|
+
/** How far inside its cell's border a spray's shapes are kept (the clear margin, the edge ramp and a little more). */
|
|
33
|
+
const FIT = 4;
|
|
34
|
+
/** The width of a shape's antialiased edge, in texels. */
|
|
35
|
+
const RAMP = 1.5;
|
|
36
|
+
const TWIG = 0.35;
|
|
37
|
+
const LEAF = 0.6;
|
|
38
|
+
/**
|
|
39
|
+
* Where a spray is in the atlas, as a rectangle of UV (v up from the texture's first row): kind `kind`
|
|
40
|
+
* (an index into `LEAF_KINDS`), variant 0 or 1. Kind k is in row floor(k / 2), columns 2 (k % 2) and
|
|
41
|
+
* 2 (k % 2) + 1.
|
|
42
|
+
*/
|
|
43
|
+
export function leafCell(kind, variant) {
|
|
44
|
+
const n = LEAF_KINDS.length;
|
|
45
|
+
const k = (((kind | 0) % n) + n) % n;
|
|
46
|
+
const col = 2 * (k % 2) + (variant & 1);
|
|
47
|
+
const row = Math.floor(k / 2);
|
|
48
|
+
return { u0: col / COLS, v0: row / ROWS, du: 1 / COLS, dv: 1 / ROWS };
|
|
49
|
+
}
|
|
50
|
+
function stroke(ax, ay, bx, by, ra, rb, tone, lit = 0.25, wood = false) {
|
|
51
|
+
return { t: "stroke", ax, ay, bx, by, ra, rb, tone, lit, wood };
|
|
52
|
+
}
|
|
53
|
+
/** A run of numbers from 0 to 1, the same every time for the same seed. */
|
|
54
|
+
function rng(seed) {
|
|
55
|
+
let n = 0;
|
|
56
|
+
return () => hash01(n++, seed);
|
|
57
|
+
}
|
|
58
|
+
const lerp = (a, b, t) => a + (b - a) * t;
|
|
59
|
+
const clamp01 = (x) => (x < 0 ? 0 : x > 1 ? 1 : x);
|
|
60
|
+
/** A twig of `n` strokes from (x, y), setting off at angle a and turning by `curl` (radians) over its length. */
|
|
61
|
+
function twig(out, x, y, a, len, curl, r0, r1, rnd, n = 8, wobble = 0.12) {
|
|
62
|
+
const pts = [{ x, y, a, s: 0 }];
|
|
63
|
+
const step = len / n;
|
|
64
|
+
for (let i = 1; i <= n; i++) {
|
|
65
|
+
a += curl / n + (rnd() - 0.5) * wobble;
|
|
66
|
+
const nx = x + Math.cos(a) * step;
|
|
67
|
+
const ny = y + Math.sin(a) * step;
|
|
68
|
+
out.push(stroke(x, y, nx, ny, lerp(r0, r1, (i - 1) / n), lerp(r0, r1, i / n), TWIG, 0.25, true));
|
|
69
|
+
x = nx;
|
|
70
|
+
y = ny;
|
|
71
|
+
pts.push({ x, y, a, s: i / n });
|
|
72
|
+
}
|
|
73
|
+
return pts;
|
|
74
|
+
}
|
|
75
|
+
/** The point `s` (0..1) of the way along a twig. */
|
|
76
|
+
function along(pts, s) {
|
|
77
|
+
const f = clamp01(s) * (pts.length - 1);
|
|
78
|
+
const i = Math.min(pts.length - 2, Math.floor(f));
|
|
79
|
+
const t = f - i;
|
|
80
|
+
const p = pts[i];
|
|
81
|
+
const q = pts[i + 1];
|
|
82
|
+
return { x: lerp(p.x, q.x, t), y: lerp(p.y, q.y, t), a: q.a, s };
|
|
83
|
+
}
|
|
84
|
+
/** Turn angle a towards angle b by fraction t (the short way round). */
|
|
85
|
+
function towards(a, b, t) {
|
|
86
|
+
let d = b - a;
|
|
87
|
+
while (d > Math.PI)
|
|
88
|
+
d -= Math.PI * 2;
|
|
89
|
+
while (d < -Math.PI)
|
|
90
|
+
d += Math.PI * 2;
|
|
91
|
+
return a + d * t;
|
|
92
|
+
}
|
|
93
|
+
/** A leaf on a stalk of length `stalk` from (x, y), pointing at angle a; its own tone ±10 %. */
|
|
94
|
+
function leaf(out, x, y, a, stalk, o, rnd, stalkR = 1.0) {
|
|
95
|
+
let bx = x;
|
|
96
|
+
let by = y;
|
|
97
|
+
if (stalk > 0) {
|
|
98
|
+
bx = x + Math.cos(a) * stalk;
|
|
99
|
+
by = y + Math.sin(a) * stalk;
|
|
100
|
+
out.push(stroke(x, y, bx, by, stalkR * 1.15, stalkR, TWIG * 1.25, 0, true));
|
|
101
|
+
}
|
|
102
|
+
out.push({
|
|
103
|
+
t: "blade",
|
|
104
|
+
x: bx,
|
|
105
|
+
y: by,
|
|
106
|
+
ang: a + (rnd() - 0.5) * 0.25,
|
|
107
|
+
len: o.len,
|
|
108
|
+
wid: o.wid,
|
|
109
|
+
p: o.p,
|
|
110
|
+
q: o.q,
|
|
111
|
+
bend: o.bend ?? 0,
|
|
112
|
+
lobes: o.lobes ?? 0,
|
|
113
|
+
lobeDepth: o.lobeDepth ?? 0,
|
|
114
|
+
teeth: o.teeth ?? 0,
|
|
115
|
+
toothDepth: o.toothDepth ?? 0,
|
|
116
|
+
veins: o.veins ?? 0,
|
|
117
|
+
tone: LEAF * (0.9 + rnd() * 0.2),
|
|
118
|
+
shadow: o.shadow ?? 3,
|
|
119
|
+
prof: null,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
123
|
+
// The sprays, one kind at a time. Built in texels about the twig's foot; fitted to their cell afterwards.
|
|
124
|
+
/** Oak, maple, plane, beech: a curved twig, side twigs, broad leaves alternate on short stalks, overlapping. */
|
|
125
|
+
function broadSpray(v, rnd) {
|
|
126
|
+
const twigs = [];
|
|
127
|
+
const leaves = [];
|
|
128
|
+
const lobed = v === 1;
|
|
129
|
+
const main = twig(twigs, 0, 0, Math.PI / 2 + (rnd() - 0.5) * 0.3, 120, (rnd() - 0.5) * 0.6, 3.2, 1.5, rnd, 10);
|
|
130
|
+
const branches = [{ pts: main, len: 120, from: 0.3 }];
|
|
131
|
+
let side = rnd() < 0.5 ? 1 : -1;
|
|
132
|
+
for (const f of [0.14, 0.34, 0.54, 0.72]) {
|
|
133
|
+
const p = along(main, f + (rnd() - 0.5) * 0.05);
|
|
134
|
+
const len = (66 + rnd() * 22) * (1 - f * 0.4);
|
|
135
|
+
// (The lower side twigs spread wider.)
|
|
136
|
+
const a = p.a + side * (0.85 + (0.6 - f) * 0.9 + rnd() * 0.25);
|
|
137
|
+
branches.push({ pts: twig(twigs, p.x, p.y, a, len, -side * (0.3 + rnd() * 0.3), 2.3, 1.2, rnd, 6), len, from: 0.22 });
|
|
138
|
+
side = -side;
|
|
139
|
+
}
|
|
140
|
+
for (const b of branches) {
|
|
141
|
+
let ls = rnd() < 0.5 ? 1 : -1;
|
|
142
|
+
const gap = (20 + rnd() * 5) / b.len;
|
|
143
|
+
// (Alternate leaves along it, then one at its tip.)
|
|
144
|
+
for (let s = b.from + rnd() * 0.08; s < 0.93; s += gap * (0.85 + rnd() * 0.3)) {
|
|
145
|
+
const p = along(b.pts, s);
|
|
146
|
+
broadLeaf(leaves, p.x, p.y, p.a + ls * (0.65 + rnd() * 0.5), (80 + rnd() * 26) * (1 - 0.15 * s), lobed, rnd);
|
|
147
|
+
ls = -ls;
|
|
148
|
+
}
|
|
149
|
+
const tip = b.pts[b.pts.length - 1];
|
|
150
|
+
broadLeaf(leaves, tip.x, tip.y, tip.a + (rnd() - 0.5) * 0.3, 88 + rnd() * 18, lobed, rnd);
|
|
151
|
+
}
|
|
152
|
+
return [...twigs, ...under(shuffle(leaves, rnd))];
|
|
153
|
+
}
|
|
154
|
+
function broadLeaf(out, x, y, a, len, lobed, rnd) {
|
|
155
|
+
// (Seen at an angle, a leaf looks narrower than it is.)
|
|
156
|
+
const wid = len * (0.29 + rnd() * 0.08) * (0.8 + rnd() * 0.2);
|
|
157
|
+
const shallow = !lobed && rnd() < 0.25;
|
|
158
|
+
const o = { len, wid, p: 0.55 + rnd() * 0.2, q: 0.95 + rnd() * 0.3, bend: (rnd() - 0.5) * 0.16, veins: 1, shadow: 3.5 };
|
|
159
|
+
if (lobed) {
|
|
160
|
+
o.lobes = 4 + Math.floor(rnd() * 2);
|
|
161
|
+
o.lobeDepth = 0.35 + rnd() * 0.15;
|
|
162
|
+
o.wid = len * (0.32 + rnd() * 0.08) * (0.8 + rnd() * 0.2);
|
|
163
|
+
o.p = 0.75 + rnd() * 0.2;
|
|
164
|
+
o.q = 0.75 + rnd() * 0.2;
|
|
165
|
+
}
|
|
166
|
+
else if (shallow) {
|
|
167
|
+
o.lobes = 3;
|
|
168
|
+
o.lobeDepth = 0.15 + rnd() * 0.08;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
o.teeth = 16 + Math.floor(rnd() * 6);
|
|
172
|
+
o.toothDepth = 0.05;
|
|
173
|
+
}
|
|
174
|
+
leaf(out, x, y, a, 5 + rnd() * 5, o, rnd, 1.1);
|
|
175
|
+
}
|
|
176
|
+
/** Eucalyptus (v 0): long sickle leaves hanging from a few twigs. Willow (v 1): an arching twig, narrow leaves on hanging shoots. */
|
|
177
|
+
function narrowSpray(v, rnd) {
|
|
178
|
+
const twigs = [];
|
|
179
|
+
const leaves = [];
|
|
180
|
+
const down = -Math.PI / 2;
|
|
181
|
+
if (v === 0) {
|
|
182
|
+
const main = twig(twigs, 0, 0, Math.PI / 2 + (rnd() - 0.5) * 0.3, 190, (rnd() < 0.5 ? 1 : -1) * (0.3 + rnd() * 0.3), 2.8, 1.3, rnd, 10);
|
|
183
|
+
const branches = [main];
|
|
184
|
+
let side = rnd() < 0.5 ? 1 : -1;
|
|
185
|
+
for (const f of [0.14, 0.28, 0.42, 0.56, 0.7, 0.84]) {
|
|
186
|
+
const p = along(main, f);
|
|
187
|
+
branches.push(twig(twigs, p.x, p.y, p.a + side * (1.0 + rnd() * 0.35), (55 + rnd() * 25) * (1 - f * 0.5), -side * 0.4, 1.9, 1.0, rnd, 6));
|
|
188
|
+
side = -side;
|
|
189
|
+
}
|
|
190
|
+
for (const b of branches) {
|
|
191
|
+
let ls = rnd() < 0.5 ? 1 : -1;
|
|
192
|
+
for (let s = (b === main ? 0.3 : 0.15) + rnd() * 0.1; s < 1.0; s += (b === main ? 0.07 : 0.11) + rnd() * 0.06) {
|
|
193
|
+
const p = along(b, s);
|
|
194
|
+
// (Each leaf set off from its twig, then hanging, the higher ones less.)
|
|
195
|
+
const a = towards(p.a + ls * (0.6 + rnd() * 0.4), down, (0.45 + rnd() * 0.35) * (b === main ? 1 - 0.5 * s : 1));
|
|
196
|
+
const len = 80 + rnd() * 32;
|
|
197
|
+
leaf(leaves, p.x, p.y, a, 7 + rnd() * 5, { len, wid: len * (0.11 + rnd() * 0.04), p: 0.75, q: 1.45, bend: (ls > 0 ? 1 : -1) * (0.1 + rnd() * 0.1), veins: 0, shadow: 2.5 }, rnd, 0.95);
|
|
198
|
+
ls = -ls;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
// (The twig leans out and bends over, not round: shoots hang from it to either side, leaves along them, its own
|
|
204
|
+
// leaves set off to either side, so it runs inside the spray. Bent on round, as it was, it lay bare along
|
|
205
|
+
// the spray's top, an arc across every card: a dark ring round the leaves.)
|
|
206
|
+
const dir = rnd() < 0.5 ? 1 : -1;
|
|
207
|
+
const main = twig(twigs, 0, 0, Math.PI / 2 - dir * 0.2, 200, -dir * 0.7, 2.2, 1.1, rnd, 12, 0.08);
|
|
208
|
+
const shoots = [main];
|
|
209
|
+
let side = rnd() < 0.5 ? 1 : -1;
|
|
210
|
+
for (let s = 0.12; s < 0.99; s += 0.05 + rnd() * 0.03) {
|
|
211
|
+
const p = along(main, s);
|
|
212
|
+
shoots.push(twig(twigs, p.x, p.y, towards(p.a + side * (0.8 + rnd() * 0.4), down, 0.35 + rnd() * 0.2), 60 + rnd() * 70 * (1 - Math.abs(s - 0.55)), dir * (rnd() - 0.5) * 0.5, 1.4, 0.8, rnd, 6));
|
|
213
|
+
side = -side;
|
|
214
|
+
}
|
|
215
|
+
for (const b of shoots) {
|
|
216
|
+
let ls = rnd() < 0.5 ? 1 : -1;
|
|
217
|
+
const start = b === main ? 0.2 : 0.06;
|
|
218
|
+
for (let s = start + rnd() * 0.05; s < 1.0; s += (b === main ? 0.05 : 0.085) + rnd() * 0.045) {
|
|
219
|
+
const p = along(b, s);
|
|
220
|
+
const a = towards(p.a + ls * (b === main ? 0.7 + rnd() * 0.5 : 0.25 + rnd() * 0.4), down, 0.15);
|
|
221
|
+
const len = 52 + rnd() * 26;
|
|
222
|
+
leaf(leaves, p.x, p.y, a, 2 + rnd() * 2, { len, wid: len * (0.08 + rnd() * 0.03), p: 0.8, q: 1.3, bend: (rnd() - 0.5) * 0.2, veins: 0, shadow: 2 }, rnd, 0.8);
|
|
223
|
+
ls = -ls;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return [...twigs, ...under(shuffle(leaves, rnd))];
|
|
228
|
+
}
|
|
229
|
+
/** A fir's or a spruce's spray (two of them, v 0 and 1): a flat frond, side shoots combed with short needles. */
|
|
230
|
+
function needleSpray(_v, rnd) {
|
|
231
|
+
return conifer(0, rnd);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* A pine's spray (v 0 and 1): a branch's end of shoots, each ending in a brush of needles, the shoots behind
|
|
235
|
+
* them bare, as a pine keeps its needles only on its last few years' growth.
|
|
236
|
+
*/
|
|
237
|
+
function pineSpray(v, rnd) {
|
|
238
|
+
const twigs = [];
|
|
239
|
+
const needles = [];
|
|
240
|
+
// (A branch's end: a shoot forking into shoots, each of the last few years' growth, each ending in its brush;
|
|
241
|
+
// v 1 forks once more, finer.)
|
|
242
|
+
const tips = [];
|
|
243
|
+
const main = twig(twigs, 0, 0, Math.PI / 2 + (rnd() - 0.5) * 0.25, 120, (rnd() - 0.5) * 0.3, 3.2, 2.2, rnd, 8, 0.06);
|
|
244
|
+
tips.push({ ...main[main.length - 1], len: 30 + rnd() * 6 });
|
|
245
|
+
let side = rnd() < 0.5 ? 1 : -1;
|
|
246
|
+
const forks = v === 0 ? [0.3, 0.48, 0.64, 0.8] : [0.22, 0.36, 0.5, 0.62, 0.74, 0.86];
|
|
247
|
+
for (const f of forks) {
|
|
248
|
+
const p = along(main, f + (rnd() - 0.5) * 0.04);
|
|
249
|
+
const len = (75 + rnd() * 25) * (1.15 - f * 0.6);
|
|
250
|
+
const pts = twig(twigs, p.x, p.y, p.a + side * (0.6 + rnd() * 0.35), len, -side * 0.25, 2.3, 1.7, rnd, 6, 0.07);
|
|
251
|
+
tips.push({ ...pts[pts.length - 1], len: 26 + rnd() * 6 });
|
|
252
|
+
if (len > 55) {
|
|
253
|
+
const q = along(pts, 0.55 + rnd() * 0.1);
|
|
254
|
+
const sub = twig(twigs, q.x, q.y, q.a - side * (0.5 + rnd() * 0.3), len * 0.45, 0, 1.6, 1.3, rnd, 4, 0.07);
|
|
255
|
+
tips.push({ ...sub[sub.length - 1], len: 22 + rnd() * 5 });
|
|
256
|
+
}
|
|
257
|
+
side = -side;
|
|
258
|
+
}
|
|
259
|
+
for (const tip of tips) {
|
|
260
|
+
const shade = 0.84 + rnd() * 0.26;
|
|
261
|
+
// (Its brush: needles off the last stretch of the shoot and all round its tip, splayed forward, the shoot
|
|
262
|
+
// behind it bare.)
|
|
263
|
+
for (let k = 0; k < 90; k++) {
|
|
264
|
+
const back = rnd() * tip.len * 0.55;
|
|
265
|
+
const x = tip.x - Math.cos(tip.a) * back;
|
|
266
|
+
const y = tip.y - Math.sin(tip.a) * back;
|
|
267
|
+
const a = tip.a + (rnd() - 0.5) * 2.6 * (0.6 + 0.4 * (back / (tip.len * 0.55)));
|
|
268
|
+
const forward = 0.6 + 0.4 * Math.cos(a - tip.a);
|
|
269
|
+
needle(needles, x, y, a, tip.len * forward * (0.75 + rnd() * 0.4), 1.35, rnd, shade);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return [...twigs, ...needles];
|
|
273
|
+
}
|
|
274
|
+
/** Fir or spruce (0): a flat frond, side shoots combed with short needles. Pine (1): long needles in brushes. */
|
|
275
|
+
function conifer(v, rnd) {
|
|
276
|
+
const twigs = [];
|
|
277
|
+
const needles = [];
|
|
278
|
+
const shoots = [];
|
|
279
|
+
if (v === 0) {
|
|
280
|
+
const main = twig(twigs, 0, 0, Math.PI / 2 + (rnd() - 0.5) * 0.2, 200, (rnd() - 0.5) * 0.4, 2.4, 1.3, rnd, 12, 0.05);
|
|
281
|
+
shoots.push({ pts: main, len: 200 });
|
|
282
|
+
let side = rnd() < 0.5 ? 1 : -1;
|
|
283
|
+
for (let f = 0.04 + rnd() * 0.03; f < 0.9; f += 0.06 + rnd() * 0.025) {
|
|
284
|
+
const p = along(main, f);
|
|
285
|
+
// (Its outline: widest a third of the way up, ragged.)
|
|
286
|
+
const env = Math.pow(Math.sin(Math.PI * Math.min(1, f * 1.05 + 0.08)), 0.6);
|
|
287
|
+
const len = Math.max(20, (100 + rnd() * 30) * env * (rnd() < 0.2 ? 0.7 : 1));
|
|
288
|
+
const a = p.a + side * (0.9 + rnd() * 0.25);
|
|
289
|
+
const pts = twig(twigs, p.x, p.y, a, len, -side * (0.15 + rnd() * 0.2), 1.6, 1.0, rnd, 6, 0.06);
|
|
290
|
+
shoots.push({ pts, len });
|
|
291
|
+
if (len > 50) {
|
|
292
|
+
let s2 = rnd() < 0.5 ? 1 : -1;
|
|
293
|
+
for (const g of [0.3, 0.55, 0.75]) {
|
|
294
|
+
const q = along(pts, g + (rnd() - 0.5) * 0.08);
|
|
295
|
+
const l2 = len * (0.3 + rnd() * 0.12) * (1 - g * 0.5);
|
|
296
|
+
shoots.push({ pts: twig(twigs, q.x, q.y, q.a + s2 * (0.75 + rnd() * 0.2), l2, 0, 1.1, 0.9, rnd, 4, 0.06), len: l2 });
|
|
297
|
+
s2 = -s2;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
side = -side;
|
|
301
|
+
}
|
|
302
|
+
for (const sh of shoots) {
|
|
303
|
+
// (Each shoot a little lighter or darker than the next, as it lies nearer the light or further in.)
|
|
304
|
+
const shade = 0.84 + rnd() * 0.26;
|
|
305
|
+
const step = 2.1 / sh.len;
|
|
306
|
+
for (let s = 0.03; s < 1.0; s += step * (0.85 + rnd() * 0.3)) {
|
|
307
|
+
const p = along(sh.pts, s);
|
|
308
|
+
// (Short needles, nearly square to the shoot and a little forward, shorter towards its tip.)
|
|
309
|
+
const nl = (15 + rnd() * 6) * (1 - 0.45 * s * s);
|
|
310
|
+
for (const sd of [1, -1]) {
|
|
311
|
+
if (rnd() < 0.08)
|
|
312
|
+
continue;
|
|
313
|
+
needle(needles, p.x, p.y, p.a + sd * (1.0 + rnd() * 0.4), nl, 1.3, rnd, shade);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// (A tuft at its tip.)
|
|
317
|
+
const tip = sh.pts[sh.pts.length - 1];
|
|
318
|
+
for (let k = 0; k < 5; k++)
|
|
319
|
+
needle(needles, tip.x, tip.y, tip.a + (k - 2) * 0.38 + (rnd() - 0.5) * 0.2, 8 + rnd() * 3, 1.25, rnd, shade);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
const main = twig(twigs, 0, 0, Math.PI / 2 + (rnd() - 0.5) * 0.3, 160, (rnd() - 0.5) * 0.5, 3.4, 2.0, rnd, 10, 0.08);
|
|
324
|
+
shoots.push({ pts: main, len: 160 });
|
|
325
|
+
let side = rnd() < 0.5 ? 1 : -1;
|
|
326
|
+
for (const f of [0.12, 0.26, 0.4, 0.54, 0.68, 0.8]) {
|
|
327
|
+
const p = along(main, f + (rnd() - 0.5) * 0.04);
|
|
328
|
+
const len = (90 + rnd() * 25) * (1 - f * 0.4);
|
|
329
|
+
shoots.push({ pts: twig(twigs, p.x, p.y, p.a + side * (0.8 + (0.5 - f) * 0.6 + rnd() * 0.3), len, -side * 0.35, 2.4, 1.6, rnd, 6, 0.08), len });
|
|
330
|
+
side = -side;
|
|
331
|
+
}
|
|
332
|
+
for (const sh of shoots) {
|
|
333
|
+
const shade = 0.84 + rnd() * 0.26;
|
|
334
|
+
// (Long needles all along the shoot's outer part, splayed forward, and a brush about its tip.)
|
|
335
|
+
const step = 0.9 / sh.len;
|
|
336
|
+
for (let s = 0.15 + rnd() * 0.1; s < 1.0; s += step * (0.8 + rnd() * 0.4)) {
|
|
337
|
+
const p = along(sh.pts, s);
|
|
338
|
+
const sd = rnd() < 0.5 ? 1 : -1;
|
|
339
|
+
const a = p.a + sd * (0.35 + rnd() * 0.8) * (1.1 - 0.4 * s);
|
|
340
|
+
needle(needles, p.x, p.y, a, 32 + rnd() * 14, 1.25, rnd, shade);
|
|
341
|
+
}
|
|
342
|
+
const tip = sh.pts[sh.pts.length - 1];
|
|
343
|
+
for (let k = 0; k < 28; k++)
|
|
344
|
+
needle(needles, tip.x, tip.y, tip.a + (rnd() - 0.5) * 2.4, 26 + rnd() * 14, 1.2, rnd, shade);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return [...twigs, ...needles];
|
|
348
|
+
}
|
|
349
|
+
function needle(out, x, y, a, len, r, rnd, shade = 1) {
|
|
350
|
+
// (A needle grows a little in from where it leaves the shoot, so its root is hidden on the shoot.)
|
|
351
|
+
const x0 = x + Math.cos(a) * 1.5;
|
|
352
|
+
const y0 = y + Math.sin(a) * 1.5;
|
|
353
|
+
const a2 = a + (rnd() - 0.5) * 0.12;
|
|
354
|
+
out.push(stroke(x0, y0, x0 + Math.cos(a2) * len, y0 + Math.sin(a2) * len, r, r * 0.7, LEAF * shade * (0.9 + rnd() * 0.2), 0.35));
|
|
355
|
+
}
|
|
356
|
+
/** Box (v 0): small oval leaves in opposite pairs, dense on branching twigs. v 1: rounder leaves, alternate. */
|
|
357
|
+
function smallSpray(v, rnd) {
|
|
358
|
+
const twigs = [];
|
|
359
|
+
const leaves = [];
|
|
360
|
+
const all = [];
|
|
361
|
+
const main = twig(twigs, 0, 0, Math.PI / 2 + (rnd() - 0.5) * 0.3, 170, (rnd() - 0.5) * 0.6, 2.4, 1.2, rnd, 10);
|
|
362
|
+
all.push(main);
|
|
363
|
+
let side = rnd() < 0.5 ? 1 : -1;
|
|
364
|
+
for (let f = 0.1 + rnd() * 0.04; f < 0.85; f += 0.1 + rnd() * 0.04) {
|
|
365
|
+
const p = along(main, f);
|
|
366
|
+
const len = (88 + rnd() * 30) * (1 - f * 0.45);
|
|
367
|
+
const b = twig(twigs, p.x, p.y, p.a + side * (0.7 + rnd() * 0.35), len, -side * (0.2 + rnd() * 0.4), 1.7, 1.0, rnd, 6, 0.2);
|
|
368
|
+
all.push(b);
|
|
369
|
+
if (len > 40) {
|
|
370
|
+
for (const g of [0.35, 0.65]) {
|
|
371
|
+
const q = along(b, g + rnd() * 0.1);
|
|
372
|
+
all.push(twig(twigs, q.x, q.y, q.a + (g < 0.5 ? -side : side) * (0.6 + rnd() * 0.3), len * (0.35 + rnd() * 0.15), 0, 1.2, 0.9, rnd, 4, 0.2));
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
side = -side;
|
|
376
|
+
}
|
|
377
|
+
for (const b of all) {
|
|
378
|
+
const total = (b.length - 1) * Math.hypot(b[1].x - b[0].x, b[1].y - b[0].y);
|
|
379
|
+
const gap = (v === 0 ? 8 : 5.5) / total;
|
|
380
|
+
let ls = 1;
|
|
381
|
+
for (let s = (b === main ? 0.2 : 0.06) + rnd() * 0.05; s < 1.0; s += gap * (0.85 + rnd() * 0.3)) {
|
|
382
|
+
const p = along(b, s);
|
|
383
|
+
const sides = v === 0 ? [1, -1] : [(ls = -ls)];
|
|
384
|
+
for (const sd of sides) {
|
|
385
|
+
const len = v === 0 ? 21 + rnd() * 8 : 22 + rnd() * 9;
|
|
386
|
+
const wid = len * (v === 0 ? 0.27 + rnd() * 0.06 : 0.37 + rnd() * 0.07) * (0.8 + rnd() * 0.2);
|
|
387
|
+
leaf(leaves, p.x, p.y, p.a + sd * (0.6 + rnd() * 0.45), 1.5, { len, wid, p: 0.85, q: v === 0 ? 0.95 : 0.8, bend: (rnd() - 0.5) * 0.15, shadow: 2 }, rnd, 0.7);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// (A small tuft of new leaves at the tip.)
|
|
391
|
+
const tip = b[b.length - 1];
|
|
392
|
+
for (const d of [-0.35, 0, 0.35]) {
|
|
393
|
+
const len = 13 + rnd() * 4;
|
|
394
|
+
leaf(leaves, tip.x, tip.y, tip.a + d + (rnd() - 0.5) * 0.2, 1, { len, wid: len * 0.3, p: 0.85, q: 0.95, shadow: 1.5 }, rnd, 0.6);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return [...twigs, ...under(leaves)];
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Palms. Canary date palm (v 0): a pinnate frond, a stiff rachis with long narrow leaflets in two ranks on
|
|
401
|
+
* each side, angled out and forward, short spines at its foot. Washingtonia (v 1): a petiole to about a
|
|
402
|
+
* third of the way up, then a costapalmate fan of narrow pleated segments radiating over ~200 degrees, joined
|
|
403
|
+
* for half their length, then split and drooping at their tips, a few threads hanging from them.
|
|
404
|
+
*/
|
|
405
|
+
function frondSpray(v, rnd) {
|
|
406
|
+
return v === 0 ? pinnateFrond(rnd) : fanFrond(rnd);
|
|
407
|
+
}
|
|
408
|
+
function pinnateFrond(rnd) {
|
|
409
|
+
const rachis = [];
|
|
410
|
+
const spines = [];
|
|
411
|
+
const leaflets = [];
|
|
412
|
+
const len = 236;
|
|
413
|
+
const main = twig(rachis, 0, 0, Math.PI / 2 + (rnd() - 0.5) * 0.08, len, (rnd() - 0.5) * 0.2, 2.6, 1.0, rnd, 12, 0.02);
|
|
414
|
+
// (A palm's rachis is pale, not bark.)
|
|
415
|
+
for (const r of rachis) {
|
|
416
|
+
r.tone = LEAF * 0.98;
|
|
417
|
+
r.lit = 0.5;
|
|
418
|
+
}
|
|
419
|
+
for (const sd of [1, -1]) {
|
|
420
|
+
for (let s = 0.025 + rnd() * 0.01; s < 0.985; s += 0.014 + rnd() * 0.006) {
|
|
421
|
+
const p = along(main, s);
|
|
422
|
+
if (s < 0.1) {
|
|
423
|
+
// (The lowest leaflets are spines: short, stiff, steep.)
|
|
424
|
+
const a = p.a + sd * (0.45 + rnd() * 0.25);
|
|
425
|
+
const l = 7 + s * 140 * (0.8 + rnd() * 0.4);
|
|
426
|
+
spines.push(stroke(p.x, p.y, p.x + Math.cos(a) * l, p.y + Math.sin(a) * l, 1.1, 0.4, LEAF * 0.9, 0.3));
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
// (Longest from a third of the way up, shorter to the tip; each its own length, now and then one missing.)
|
|
430
|
+
if (rnd() < 0.05)
|
|
431
|
+
continue;
|
|
432
|
+
const env = s < 0.3 ? 0.4 + 0.6 * Math.sin(((s - 0.1) / 0.2) * (Math.PI / 2)) : 1 - 0.78 * Math.pow((s - 0.3) / 0.7, 1.5);
|
|
433
|
+
const l = 122 * env * (0.82 + rnd() * 0.3);
|
|
434
|
+
// (Two ranks, as the leaflets stand in a V about the rachis: one lies wider than the other.)
|
|
435
|
+
const spread = (rnd() < 0.5 ? 0.82 : 1.1) + (rnd() - 0.5) * 0.16;
|
|
436
|
+
const a = p.a + sd * spread * (1 - 0.3 * s * s);
|
|
437
|
+
leaf(leaflets, p.x, p.y, a, 0, { len: l, wid: 1.25 + rnd() * 0.35, p: 0.45, q: 1.05, bend: sd * (0.03 + rnd() * 0.06), shadow: 1.6 }, rnd);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return [...spines, ...leaflets, ...rachis];
|
|
441
|
+
}
|
|
442
|
+
function fanFrond(rnd) {
|
|
443
|
+
const stalk = [];
|
|
444
|
+
const pet = twig(stalk, 0, 0, Math.PI / 2 + (rnd() - 0.5) * 0.05, 92, (rnd() - 0.5) * 0.06, 3.2, 2.5, rnd, 6, 0.02);
|
|
445
|
+
for (const r of stalk) {
|
|
446
|
+
r.tone = LEAF * 0.85;
|
|
447
|
+
r.lit = 0.45;
|
|
448
|
+
}
|
|
449
|
+
// (Hooked teeth along the petiole's edges.)
|
|
450
|
+
for (let s = 0.08; s < 0.92; s += 0.075 + rnd() * 0.04) {
|
|
451
|
+
const p = along(pet, s);
|
|
452
|
+
for (const sd of [1, -1]) {
|
|
453
|
+
if (rnd() < 0.3)
|
|
454
|
+
continue;
|
|
455
|
+
const nx = -Math.sin(p.a) * sd * 2.6;
|
|
456
|
+
const ny = Math.cos(p.a) * sd * 2.6;
|
|
457
|
+
const a = p.a + sd * (0.9 + rnd() * 0.3);
|
|
458
|
+
spinesOn(stalk, p.x + nx, p.y + ny, a, 3.5 + rnd() * 2);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
const h = pet[pet.length - 1];
|
|
462
|
+
const n = 34 + Math.floor(rnd() * 5);
|
|
463
|
+
const from = (-10 * Math.PI) / 180;
|
|
464
|
+
const sweep = (200 * Math.PI) / 180;
|
|
465
|
+
const d = sweep / n;
|
|
466
|
+
const R = 170;
|
|
467
|
+
const valleys = [];
|
|
468
|
+
const ridges = [];
|
|
469
|
+
const threads = [];
|
|
470
|
+
for (let i = 0; i < n; i++) {
|
|
471
|
+
const a = from + d * (i + 0.5) + (rnd() - 0.5) * 0.25 * d;
|
|
472
|
+
// (The fan nearly round, its side segments shorter than its top ones, and a little ragged.)
|
|
473
|
+
const len = R * (0.74 + 0.26 * Math.max(0, Math.sin(a))) * (0.93 + rnd() * 0.12);
|
|
474
|
+
// (Wide enough to overlap its neighbours to past half its length, then narrowing to a tip apart from them.)
|
|
475
|
+
const wid = 0.64 * len * Math.sin(d / 2) * (0.95 + rnd() * 0.1);
|
|
476
|
+
// (Its free end droops: the more so the nearer level it points.)
|
|
477
|
+
const bend = -Math.cos(a) * (0.17 + rnd() * 0.08) + (rnd() - 0.5) * 0.03;
|
|
478
|
+
const ridge = i % 2 === 1;
|
|
479
|
+
const tone = LEAF * (ridge ? 1.1 : 0.86) * (0.95 + rnd() * 0.1);
|
|
480
|
+
(ridge ? ridges : valleys).push(frondBlade(h.x, h.y, a, len, wid, 1, 0.8, bend, tone, ridge ? 2.2 : 1.2));
|
|
481
|
+
// (Now and then a thread hanging from a side segment's tip, drawn under the fan so only its free end shows.)
|
|
482
|
+
if (Math.abs(Math.cos(a)) > 0.4 && rnd() < 0.5) {
|
|
483
|
+
const c = bend * len;
|
|
484
|
+
const tx = h.x + Math.cos(a) * len * 0.97 - Math.sin(a) * c;
|
|
485
|
+
const ty = h.y + Math.sin(a) * len * 0.97 + Math.cos(a) * c;
|
|
486
|
+
const ta = towards(a, -Math.PI / 2, 0.75 + rnd() * 0.2);
|
|
487
|
+
const tl = 9 + rnd() * 12;
|
|
488
|
+
threads.push(stroke(tx, ty, tx + Math.cos(ta) * tl, ty + Math.sin(ta) * tl, 0.6, 0.4, LEAF * 0.8, 0));
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
// (The hastula: a small pale crest where the petiole meets the fan.)
|
|
492
|
+
const hastula = frondBlade(h.x, h.y - 2, Math.PI / 2, 18, 4.5, 0.8, 1.2, 0, LEAF * 1.05, 1.5);
|
|
493
|
+
// (The petiole over the fan's foot, where the segments' shadows would gather into a dark spot.)
|
|
494
|
+
return [...threads, ...valleys, ...ridges, ...stalk, hastula];
|
|
495
|
+
}
|
|
496
|
+
function spinesOn(out, x, y, a, len) {
|
|
497
|
+
out.push(stroke(x, y, x + Math.cos(a) * len, y + Math.sin(a) * len, 0.9, 0.35, LEAF * 0.75, 0));
|
|
498
|
+
}
|
|
499
|
+
function frondBlade(x, y, ang, len, wid, p, q, bend, tone, shadow) {
|
|
500
|
+
return { t: "blade", x, y, ang, len, wid, p, q, bend, lobes: 0, lobeDepth: 0, teeth: 0, toothDepth: 0, veins: 0, tone, shadow, prof: null };
|
|
501
|
+
}
|
|
502
|
+
/** The leaves' stalks first, under every blade (a stalk drawn across another leaf reads as a scratch). */
|
|
503
|
+
function under(leaves) {
|
|
504
|
+
return [...leaves.filter((s) => s.t === "stroke"), ...leaves.filter((s) => s.t === "blade")];
|
|
505
|
+
}
|
|
506
|
+
function shuffle(list, rnd) {
|
|
507
|
+
for (let i = list.length - 1; i > 0; i--) {
|
|
508
|
+
const j = Math.floor(rnd() * (i + 1));
|
|
509
|
+
[list[i], list[j]] = [list[j], list[i]];
|
|
510
|
+
}
|
|
511
|
+
return list;
|
|
512
|
+
}
|
|
513
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
514
|
+
// Fitting a spray to its cell, and drawing it.
|
|
515
|
+
/** Scale and move a spray (about its own middle) so its shapes sit inside the cell, `FIT` texels clear of the border. */
|
|
516
|
+
function fit(shapes) {
|
|
517
|
+
let x0 = Infinity;
|
|
518
|
+
let y0 = Infinity;
|
|
519
|
+
let x1 = -Infinity;
|
|
520
|
+
let y1 = -Infinity;
|
|
521
|
+
const add = (x, y, r) => {
|
|
522
|
+
x0 = Math.min(x0, x - r);
|
|
523
|
+
y0 = Math.min(y0, y - r);
|
|
524
|
+
x1 = Math.max(x1, x + r);
|
|
525
|
+
y1 = Math.max(y1, y + r);
|
|
526
|
+
};
|
|
527
|
+
for (const s of shapes) {
|
|
528
|
+
if (s.t === "stroke") {
|
|
529
|
+
add(s.ax, s.ay, s.ra);
|
|
530
|
+
add(s.bx, s.by, s.rb);
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
// (Its outline, sampled along the midrib.)
|
|
534
|
+
const prof = bladeProfile(s);
|
|
535
|
+
const cx = Math.cos(s.ang);
|
|
536
|
+
const cy = Math.sin(s.ang);
|
|
537
|
+
for (let n = 0; n <= 16; n++) {
|
|
538
|
+
const u = n / 16;
|
|
539
|
+
const a = u * s.len;
|
|
540
|
+
const c = s.bend * s.len * u * u;
|
|
541
|
+
const w = s.wid * prof[Math.round(u * PROFILE)];
|
|
542
|
+
add(s.x + cx * a - cy * c, s.y + cy * a + cx * c, w);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
const room = CELL - 2 * FIT;
|
|
547
|
+
const k = Math.min(room / (x1 - x0), room / (y1 - y0));
|
|
548
|
+
const cx = (x0 + x1) / 2;
|
|
549
|
+
const cy = (y0 + y1) / 2;
|
|
550
|
+
const mx = (x) => CELL / 2 + (x - cx) * k;
|
|
551
|
+
const my = (y) => CELL / 2 + (y - cy) * k;
|
|
552
|
+
for (const s of shapes) {
|
|
553
|
+
if (s.t === "stroke") {
|
|
554
|
+
s.ax = mx(s.ax);
|
|
555
|
+
s.ay = my(s.ay);
|
|
556
|
+
s.bx = mx(s.bx);
|
|
557
|
+
s.by = my(s.by);
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
560
|
+
s.x = mx(s.x);
|
|
561
|
+
s.y = my(s.y);
|
|
562
|
+
s.len *= k;
|
|
563
|
+
s.wid *= k;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
function box(cx0, cy0, cx1, cy1) {
|
|
568
|
+
return [Math.max(0, Math.floor(cx0)), Math.max(0, Math.floor(cy0)), Math.min(CELL - 1, Math.ceil(cx1)), Math.min(CELL - 1, Math.ceil(cy1))];
|
|
569
|
+
}
|
|
570
|
+
function drawStroke(L, s, own) {
|
|
571
|
+
const r = Math.max(s.ra, s.rb) + RAMP;
|
|
572
|
+
const [i0, j0, i1, j1] = box(Math.min(s.ax, s.bx) - r, Math.min(s.ay, s.by) - r, Math.max(s.ax, s.bx) + r, Math.max(s.ay, s.by) + r);
|
|
573
|
+
const dx = s.bx - s.ax;
|
|
574
|
+
const dy = s.by - s.ay;
|
|
575
|
+
const len2 = dx * dx + dy * dy || 1e-9;
|
|
576
|
+
const len = Math.sqrt(len2);
|
|
577
|
+
// (Its upper side: the side of it whose normal points up.)
|
|
578
|
+
const ny = dx / len;
|
|
579
|
+
for (let j = j0; j <= j1; j++) {
|
|
580
|
+
const py = j + 0.5 - s.ay;
|
|
581
|
+
for (let i = i0; i <= i1; i++) {
|
|
582
|
+
const px = i + 0.5 - s.ax;
|
|
583
|
+
const t = clamp01((px * dx + py * dy) / len2);
|
|
584
|
+
const ex = px - dx * t;
|
|
585
|
+
const ey = py - dy * t;
|
|
586
|
+
const rad = s.ra + (s.rb - s.ra) * t;
|
|
587
|
+
const dist = Math.sqrt(ex * ex + ey * ey);
|
|
588
|
+
const c = clamp01((rad - dist) / RAMP + 0.5);
|
|
589
|
+
if (c <= 0)
|
|
590
|
+
continue;
|
|
591
|
+
const off = (dx * py - dy * px) / len / rad;
|
|
592
|
+
const light = Math.max(-1, Math.min(1, off * Math.sign(ny || 1)));
|
|
593
|
+
const tone = s.tone * (1 + s.lit * 0.3 * light) * (1 + 0.1 * t * s.lit);
|
|
594
|
+
const o = j * CELL + i;
|
|
595
|
+
L.tone[o] = L.tone[o] * (1 - c) + tone * c;
|
|
596
|
+
L.own[o] = L.own[o] * (1 - c) + own * c;
|
|
597
|
+
L.cover[o] = L.cover[o] * (1 - c) + c;
|
|
598
|
+
L.wood[o] = L.wood[o] * (1 - c) + (s.wood ? c : 0);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
const PROFILE = 128;
|
|
603
|
+
/** A blade's half width along it without lobes or teeth, for a greatest half width of 1: `PROFILE` + 1 samples from base to tip. */
|
|
604
|
+
function bladeProfile(b) {
|
|
605
|
+
if (b.prof)
|
|
606
|
+
return b.prof;
|
|
607
|
+
const um = b.p / (b.p + b.q);
|
|
608
|
+
const k = 1 / (Math.pow(um, b.p) * Math.pow(1 - um, b.q));
|
|
609
|
+
const out = new Float32Array(PROFILE + 1);
|
|
610
|
+
for (let n = 1; n < PROFILE; n++)
|
|
611
|
+
out[n] = k * Math.pow(n / PROFILE, b.p) * Math.pow(1 - n / PROFILE, b.q);
|
|
612
|
+
b.prof = out;
|
|
613
|
+
return out;
|
|
614
|
+
}
|
|
615
|
+
function bladeWidth(b, prof, u, side) {
|
|
616
|
+
if (u <= 0 || u >= 1)
|
|
617
|
+
return 0;
|
|
618
|
+
const f = u * PROFILE;
|
|
619
|
+
const n = Math.floor(f);
|
|
620
|
+
let w = b.wid * (prof[n] + (prof[n + 1] - prof[n]) * (f - n));
|
|
621
|
+
if (b.lobes > 0) {
|
|
622
|
+
// (Narrow, rounded sinuses between broad lobes, the two sides a little out of step.)
|
|
623
|
+
const c = 0.5 + 0.5 * Math.cos(Math.PI * 2 * b.lobes * u + (side > 0 ? 0 : 1.9));
|
|
624
|
+
w *= 1 - b.lobeDepth * c * c * c * Math.min(1, u * 4);
|
|
625
|
+
}
|
|
626
|
+
if (b.teeth > 0) {
|
|
627
|
+
const f = b.teeth * u + (side > 0 ? 0 : 0.5);
|
|
628
|
+
w *= 1 - b.toothDepth * (f - Math.floor(f));
|
|
629
|
+
}
|
|
630
|
+
return w;
|
|
631
|
+
}
|
|
632
|
+
// (Where the last point `bladeEdge` measured is: along the blade 0..1, across it from the midrib, and the half width there.)
|
|
633
|
+
const edge = { u: 0, v: 0, w: 0 };
|
|
634
|
+
/** Signed distance (texels, + inside) from a point in a blade's own frame (a along its midrib's chord, c across it) to its edge. */
|
|
635
|
+
function bladeEdge(b, prof, a, c) {
|
|
636
|
+
const u = a / b.len;
|
|
637
|
+
const v = c - b.bend * b.len * u * u;
|
|
638
|
+
edge.u = u;
|
|
639
|
+
edge.v = v;
|
|
640
|
+
edge.w = 0;
|
|
641
|
+
if (u <= 0)
|
|
642
|
+
return -Math.sqrt(a * a + v * v);
|
|
643
|
+
if (u >= 1)
|
|
644
|
+
return -Math.sqrt((a - b.len) * (a - b.len) + v * v);
|
|
645
|
+
// (Well outside its widest: no closer than that.)
|
|
646
|
+
const av = Math.abs(v);
|
|
647
|
+
if (av > b.wid + 4)
|
|
648
|
+
return b.wid - av;
|
|
649
|
+
const w = bladeWidth(b, prof, u, v >= 0 ? 1 : -1);
|
|
650
|
+
edge.w = w;
|
|
651
|
+
// (The outline's slope, from the base profile alone: enough to keep the edge ramp even.)
|
|
652
|
+
const dw = (w * (b.p / u - b.q / (1 - u))) / b.len;
|
|
653
|
+
return (w - av) / Math.sqrt(1 + dw * dw);
|
|
654
|
+
}
|
|
655
|
+
/** The texels of a row that can lie in a blade's band: [first, last], from the row's own [i0, i1]. */
|
|
656
|
+
const span = [0, 0];
|
|
657
|
+
/** Narrow `span` to the texels whose point (i + 0.5 - ox, py) has k (i + 0.5 - ox) + k0 within [lo, hi]. */
|
|
658
|
+
function clip(k, k0, lo, hi, ox) {
|
|
659
|
+
if (Math.abs(k) < 1e-6) {
|
|
660
|
+
if (k0 < lo || k0 > hi)
|
|
661
|
+
span[1] = span[0] - 1;
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
let p0 = (lo - k0) / k;
|
|
665
|
+
let p1 = (hi - k0) / k;
|
|
666
|
+
if (p0 > p1)
|
|
667
|
+
[p0, p1] = [p1, p0];
|
|
668
|
+
span[0] = Math.max(span[0], Math.floor(p0 + ox - 0.5));
|
|
669
|
+
span[1] = Math.min(span[1], Math.ceil(p1 + ox - 0.5));
|
|
670
|
+
}
|
|
671
|
+
/** The texels of row j near the blade (its base at ox, oy): within `reach` of its midrib's chord, and of its length. */
|
|
672
|
+
function bladeRow(j, ox, oy, cx, cy, len, reach, i0, i1) {
|
|
673
|
+
const py = j + 0.5 - oy;
|
|
674
|
+
span[0] = i0;
|
|
675
|
+
span[1] = i1;
|
|
676
|
+
clip(cx, py * cy, -reach, len + reach, ox);
|
|
677
|
+
clip(-cy, py * cx, -reach, reach, ox);
|
|
678
|
+
}
|
|
679
|
+
function drawBlade(L, b, own) {
|
|
680
|
+
const cx = Math.cos(b.ang);
|
|
681
|
+
const cy = Math.sin(b.ang);
|
|
682
|
+
// (Its left side, +v: does it face up?)
|
|
683
|
+
const nx = -cy;
|
|
684
|
+
const ny = cx;
|
|
685
|
+
const upSide = ny;
|
|
686
|
+
const prof = bladeProfile(b);
|
|
687
|
+
const reach = b.wid + Math.abs(b.bend) * b.len + RAMP + 1;
|
|
688
|
+
const tipX = b.x + cx * b.len;
|
|
689
|
+
const tipY = b.y + cy * b.len;
|
|
690
|
+
const sx = 1.0 * (b.shadow / 3);
|
|
691
|
+
const sy = -b.shadow;
|
|
692
|
+
const [i0, j0, i1, j1] = box(Math.min(b.x, tipX) - reach, Math.min(b.y, tipY) - reach + Math.min(0, sy), Math.max(b.x, tipX) + reach + Math.max(0, sx), Math.max(b.y, tipY) + reach);
|
|
693
|
+
// (Its shadow first, on what is already drawn: offset down and soft.)
|
|
694
|
+
if (b.shadow > 0) {
|
|
695
|
+
for (let j = j0; j <= j1; j++) {
|
|
696
|
+
bladeRow(j, b.x + sx, b.y + sy, cx, cy, b.len, reach + b.shadow * 0.6, i0, i1);
|
|
697
|
+
for (let i = span[0]; i <= span[1]; i++) {
|
|
698
|
+
const o = j * CELL + i;
|
|
699
|
+
if (L.cover[o] <= 0)
|
|
700
|
+
continue;
|
|
701
|
+
const px = i + 0.5 - b.x - sx;
|
|
702
|
+
const py = j + 0.5 - b.y - sy;
|
|
703
|
+
const d = bladeEdge(b, prof, px * cx + py * cy, px * nx + py * ny);
|
|
704
|
+
const c = clamp01(d / (b.shadow * 1.2) + 0.5);
|
|
705
|
+
if (c > 0)
|
|
706
|
+
L.tone[o] *= 1 - 0.32 * c;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
const rib = Math.max(0.8, b.wid * 0.05);
|
|
711
|
+
const veinGap = b.len / 8;
|
|
712
|
+
for (let j = j0; j <= j1; j++) {
|
|
713
|
+
bladeRow(j, b.x, b.y, cx, cy, b.len, reach, i0, i1);
|
|
714
|
+
for (let i = span[0]; i <= span[1]; i++) {
|
|
715
|
+
const px = i + 0.5 - b.x;
|
|
716
|
+
const py = j + 0.5 - b.y;
|
|
717
|
+
const a = px * cx + py * cy;
|
|
718
|
+
const cc = px * nx + py * ny;
|
|
719
|
+
const d = bladeEdge(b, prof, a, cc);
|
|
720
|
+
const c = clamp01(d / RAMP + 0.5);
|
|
721
|
+
if (c <= 0)
|
|
722
|
+
continue;
|
|
723
|
+
const u = clamp01(edge.u);
|
|
724
|
+
const v = edge.v;
|
|
725
|
+
const w = Math.max(1e-3, edge.w);
|
|
726
|
+
const sv = Math.max(-1, Math.min(1, v / w));
|
|
727
|
+
// (Lit from above: the half that faces up lighter, the other darker; the base a little darker.)
|
|
728
|
+
let tone = b.tone * (1 + 0.13 * sv * upSide) * (0.9 + 0.1 * Math.min(1, u * 3));
|
|
729
|
+
// (Its midrib, lighter, fading to the tip.)
|
|
730
|
+
const ribW = rib * (1 - 0.6 * u);
|
|
731
|
+
const m = clamp01((ribW - Math.abs(v)) / 0.9 + 0.5) * (1 - 0.7 * u);
|
|
732
|
+
tone *= 1 + 0.2 * m;
|
|
733
|
+
// (Side veins running out and forward from the midrib, faint.)
|
|
734
|
+
if (b.veins > 0 && Math.abs(v) > ribW && Math.abs(sv) < 0.85) {
|
|
735
|
+
const q = (a - Math.abs(v) * 1.1) / veinGap;
|
|
736
|
+
const dd = Math.abs(q - Math.round(q)) * veinGap;
|
|
737
|
+
tone *= 1 + 0.08 * b.veins * clamp01(1 - dd / 0.9);
|
|
738
|
+
}
|
|
739
|
+
// (A darker rim.)
|
|
740
|
+
if (d < 2.2)
|
|
741
|
+
tone *= 0.88 + 0.12 * clamp01(d / 2.2);
|
|
742
|
+
const o = j * CELL + i;
|
|
743
|
+
L.tone[o] = L.tone[o] * (1 - c) + tone * c;
|
|
744
|
+
L.own[o] = L.own[o] * (1 - c) + own * c;
|
|
745
|
+
L.cover[o] = L.cover[o] * (1 - c) + c;
|
|
746
|
+
L.wood[o] *= 1 - c;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
/** The atlas's texels, RGBA8 (row 0 is v = 0): the realistic look's texture (engine.ts makes it, with its mips: mips.ts). Pure, so it runs anywhere. */
|
|
751
|
+
export function leafAtlasPixels() {
|
|
752
|
+
return drawAtlas(null);
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* How much of each of the atlas's texels is wood (a twig or a stalk), 0..1, laid out as `leafAtlasPixels` is,
|
|
756
|
+
* one number a texel: for the tests, which look at where a spray's wood shows.
|
|
757
|
+
*/
|
|
758
|
+
export function leafAtlasWood() {
|
|
759
|
+
const wood = new Float32Array(WIDTH * HEIGHT);
|
|
760
|
+
drawAtlas(wood);
|
|
761
|
+
return { wood, width: WIDTH, height: HEIGHT };
|
|
762
|
+
}
|
|
763
|
+
function drawAtlas(woodOut) {
|
|
764
|
+
const data = new Uint8Array(WIDTH * HEIGHT * 4);
|
|
765
|
+
const L = { tone: new Float32Array(CELL * CELL), own: new Float32Array(CELL * CELL), cover: new Float32Array(CELL * CELL), wood: new Float32Array(CELL * CELL) };
|
|
766
|
+
for (let kind = 0; kind < LEAF_KINDS.length; kind++) {
|
|
767
|
+
for (let v = 0; v < 2; v++) {
|
|
768
|
+
const rnd = rng(7919 * (kind + 1) + 131 * v + 17);
|
|
769
|
+
const shapes = [broadSpray, narrowSpray, needleSpray, smallSpray, frondSpray, pineSpray][kind](v, rnd);
|
|
770
|
+
fit(shapes);
|
|
771
|
+
L.tone.fill(0);
|
|
772
|
+
L.own.fill(0);
|
|
773
|
+
L.cover.fill(0);
|
|
774
|
+
L.wood.fill(0);
|
|
775
|
+
// (Each shape's own number from where it is in the list, not from the spray's run of numbers, which
|
|
776
|
+
// laid the spray as it is.)
|
|
777
|
+
shapes.forEach((s, n) => {
|
|
778
|
+
const own = hash01(n, 7919 * (kind + 1) + 131 * v + 29);
|
|
779
|
+
if (s.t === "stroke")
|
|
780
|
+
drawStroke(L, s, own);
|
|
781
|
+
else
|
|
782
|
+
drawBlade(L, s, own);
|
|
783
|
+
});
|
|
784
|
+
// (Where nothing is drawn, the spray's mean tone, so the mips' edges do not darken.)
|
|
785
|
+
const T = L.tone;
|
|
786
|
+
const C = L.cover;
|
|
787
|
+
let sum = 0;
|
|
788
|
+
let wsum = 0;
|
|
789
|
+
for (let n = 0; n < CELL * CELL; n++) {
|
|
790
|
+
sum += T[n];
|
|
791
|
+
wsum += C[n];
|
|
792
|
+
}
|
|
793
|
+
const mean = Math.min(1, wsum > 0 ? sum / wsum : LEAF);
|
|
794
|
+
const { u0, v0 } = leafCell(kind, v);
|
|
795
|
+
const ox = Math.round(u0 * WIDTH);
|
|
796
|
+
const oy = Math.round(v0 * HEIGHT);
|
|
797
|
+
for (let j = 0; j < CELL; j++) {
|
|
798
|
+
let o = ((oy + j) * WIDTH + ox) * 4;
|
|
799
|
+
for (let n = j * CELL, end = n + CELL; n < end; n++, o += 4) {
|
|
800
|
+
const a = C[n];
|
|
801
|
+
const k = (a * 255 + 0.5) | 0;
|
|
802
|
+
const tone = k > 0 ? Math.min(1, T[n] / a) : mean;
|
|
803
|
+
const t = (tone * 255 + 0.5) | 0;
|
|
804
|
+
data[o] = t;
|
|
805
|
+
data[o + 1] = k > 0 ? ((Math.min(1, L.own[n] / a) * 255 + 0.5) | 0) : 128;
|
|
806
|
+
data[o + 2] = t;
|
|
807
|
+
data[o + 3] = k;
|
|
808
|
+
if (woodOut)
|
|
809
|
+
woodOut[(oy + j) * WIDTH + ox + (n - j * CELL)] = k > 0 ? Math.min(1, L.wood[n] / a) : 0;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
// (The empty cells of the last row: clear, at a leaf's tone, so a mip of the cells beside them does not darken.)
|
|
815
|
+
const t = (LEAF * 255 + 0.5) | 0;
|
|
816
|
+
for (let cell = LEAF_KINDS.length * 2; cell < COLS * ROWS; cell++) {
|
|
817
|
+
const k = Math.floor(cell / 2);
|
|
818
|
+
const ox = (2 * (k % 2) + (cell % 2)) * CELL;
|
|
819
|
+
const oy = Math.floor(k / 2) * CELL;
|
|
820
|
+
for (let j = 0; j < CELL; j++) {
|
|
821
|
+
for (let o = ((oy + j) * WIDTH + ox) * 4, end = o + CELL * 4; o < end; o += 4) {
|
|
822
|
+
data[o] = data[o + 2] = t;
|
|
823
|
+
data[o + 1] = 128;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
return { data, width: WIDTH, height: HEIGHT };
|
|
828
|
+
}
|
|
829
|
+
//# sourceMappingURL=leaves.js.map
|