roamwright 0.60.0 → 0.61.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.
@@ -0,0 +1,137 @@
1
+ // ════════════════════════════════════════════════════════════════════════
2
+ // An ear as a thin shell of skin standing on its base: a rounded triangle open towards a direction (a cat's, a fox's, a
3
+ // wolf's), or a rounded leaf (a bear's). Its inner face is a shallow cup, deepest near the base; its back is the cup's outside;
4
+ // the rim and the tip thin to nothing, so the shell closes on itself and only its base, buried in the head, stays open. The tip
5
+ // leans towards the front or back, and the blade leans outwards, each by a share of its height.
6
+ // ════════════════════════════════════════════════════════════════════════
7
+
8
+ /**
9
+ * An ear as `earShell` takes it, in the lengths of the head it stands on.
10
+ * @typedef {object} EarShell
11
+ * @property {number[]} base The middle of its root, on the head.
12
+ * @property {number[]} up A unit direction along the ear.
13
+ * @property {number[]} across A unit direction across it (front × up, so the three are right-handed).
14
+ * @property {number[]} front A unit direction its opening faces.
15
+ * @property {number} height
16
+ * @property {number} width At the root.
17
+ * @property {number} cup How deep the cup is at its centre, as a share of the width.
18
+ * @property {number} lean How far the tip leans towards the front, as a share of the height (negative: back).
19
+ * @property {number} tilt How far the blade leans towards `across`, as a share of the height.
20
+ * @property {number} thick The shell's thickness at the middle of its root.
21
+ * @property {number} [point=1] 1 a triangle broadest at its root, 0 a leaf rounded at its root.
22
+ * @property {number} [slope=0] How far the root's outer end (across its -1 side) sits lower than its inner end, as a share of the
23
+ * width, as a root does over the corner of a skull.
24
+ * @property {number[]} inner The colour at the bottom of the cup, linear [r, g, b].
25
+ * @property {number[]} outer The colour of the back, the rim and the tip.
26
+ * @property {number} tag The tag of the skin.
27
+ * @property {number} bone The bone every point follows.
28
+ * @property {number} rows The grid's rows, root to tip.
29
+ * @property {number} cols The grid's columns.
30
+ */
31
+
32
+ /**
33
+ * Lays an ear on a head: nothing but vertex colours and one bone, so a worker can build it.
34
+ * @param {import('./builder.js').GB} gb The builder, which keeps tags.
35
+ * @param {EarShell} o
36
+ */
37
+ function earShell(gb, o) {
38
+ const { base, up, across, front, height, width, cup, lean, tilt, thick, inner, outer, tag, bone, rows, cols } = o,
39
+ point = o.point ?? 1,
40
+ slope = o.slope ?? 0,
41
+ half = width / 2,
42
+ // the blade's half-width at a share t of its height: a triangle's sides softly convex toward a rounded apex, or a leaf's round
43
+ wideAt = t => (point ? half * (1 - t) ** 0.78 : half * Math.sin(Math.PI * (0.25 + 0.75 * t)) ** 0.8 * (1 - t ** 3)),
44
+ // a point of the inner face, in the ear's frame: along the ear, across it, and back from its opening (the cup, deepest at
45
+ // the root, and the tip's lean)
46
+ frame = (t, s) => {
47
+ const w = wideAt(t) / half;
48
+ return [
49
+ height * t + slope * half * s * (1 - t), // the root's outer end (s = -1: across points inward) sits lower
50
+ wideAt(t) * s + tilt * height * t,
51
+ -cup * width * (1 - s * s) * w * (1 - 0.5 * t) + lean * height * t * t,
52
+ ];
53
+ },
54
+ world = (f, out) => {
55
+ out[0] = base[0] + up[0] * f[0] + across[0] * f[1] + front[0] * f[2];
56
+ out[1] = base[1] + up[1] * f[0] + across[1] * f[1] + front[1] * f[2];
57
+ out[2] = base[2] + up[2] * f[0] + across[2] * f[1] + front[2] * f[2];
58
+ return out;
59
+ },
60
+ // the inner face's normal at a point, from its slope both ways. The ear's frame (up, across, front) is right-handed when
61
+ // across = front × up, so along × across faces the opening, and the cross product stands in the world as in the frame
62
+ normal = (t, s, out) => {
63
+ const e = 1e-3,
64
+ // the slope across the blade is taken below the tip, where the blade still has width: at the tip itself every column
65
+ // is one point, and the difference of two samples in one point is nothing. At the rim the slope is one-sided.
66
+ wide = Math.min(t, 1 - e),
67
+ a = frame(Math.min(1, t + e), s),
68
+ b = frame(Math.max(0, t - e), s),
69
+ c = frame(wide, Math.min(1, s + e)),
70
+ d = frame(wide, Math.max(-1, s - e)),
71
+ du = [a[0] - b[0], a[1] - b[1], a[2] - b[2]],
72
+ dv = [c[0] - d[0], c[1] - d[1], c[2] - d[2]],
73
+ nx = du[1] * dv[2] - du[2] * dv[1],
74
+ ny = du[2] * dv[0] - du[0] * dv[2],
75
+ nz = du[0] * dv[1] - du[1] * dv[0],
76
+ l = Math.hypot(nx, ny, nz) || 1;
77
+ // the frame's axes are the ear's own; the normal turns with them
78
+ out[0] = (up[0] * nx + across[0] * ny + front[0] * nz) / l;
79
+ out[1] = (up[1] * nx + across[1] * ny + front[1] * nz) / l;
80
+ out[2] = (up[2] * nx + across[2] * ny + front[2] * nz) / l;
81
+ return out;
82
+ },
83
+ // the shell's thickness at a point: nothing at the rim and the tip
84
+ skin = (t, s) => thick * Math.sqrt(Math.max(0, 1 - s * s)) * (1 - t) ** 0.7,
85
+ P = [0, 0, 0],
86
+ N = [0, 0, 0],
87
+ put = (p, n, c) => gb.raw(p[0], p[1], p[2], n[0], n[1], n[2], c[0], c[1], c[2], bone, bone, 1),
88
+ // the colour of the inner face: the cup's colour at the bottom of its middle, the coat's towards the rim and the tip
89
+ innerAt = (t, s) => {
90
+ const k = Math.min(1, 0.25 + 0.75 * Math.abs(s) ** 1.5 + 0.6 * t);
91
+ return [inner[0] + (outer[0] - inner[0]) * k, inner[1] + (outer[1] - inner[1]) * k, inner[2] + (outer[2] - inner[2]) * k];
92
+ };
93
+ gb.tv = tag;
94
+ // the two faces: the inner one wound to face the opening, the back the other way; they share the rim and the tip
95
+ for (const side of [1, -1]) {
96
+ const colour = side > 0 ? innerAt : () => outer,
97
+ // the back lies behind the inner face by the shell's thickness
98
+ back = side > 0 ? 0 : 1,
99
+ // along the ear then across it faces the opening: so the inner face is wound, and the back the other way round
100
+ order = side > 0 ? [0, 1, 2, 0, 2, 3] : [0, 2, 1, 0, 3, 2];
101
+ for (let i = 0; i < rows; i++)
102
+ for (let j = 0; j < cols; j++) {
103
+ const t0 = i / rows,
104
+ t1 = (i + 1) / rows,
105
+ s0 = -1 + (2 * j) / cols,
106
+ s1 = -1 + (2 * (j + 1)) / cols,
107
+ // a point of this face at (t, s), with the inner face's normal there
108
+ corner = [
109
+ [t0, s0],
110
+ [t1, s0],
111
+ [t1, s1],
112
+ [t0, s1],
113
+ ].map(([t, s]) => {
114
+ const f = frame(t, s),
115
+ n = normal(t, s, N),
116
+ k = skin(t, s) * back,
117
+ p = world([f[0] - n[0] * k, f[1] - n[1] * k, f[2] - n[2] * k], P);
118
+ return [p.slice(), side > 0 ? n.slice() : n.map(v => -v)];
119
+ });
120
+ for (const k of order)
121
+ put(
122
+ corner[k][0],
123
+ corner[k][1],
124
+ colour(
125
+ ...[
126
+ [t0, s0],
127
+ [t1, s0],
128
+ [t1, s1],
129
+ [t0, s1],
130
+ ][k],
131
+ ),
132
+ );
133
+ }
134
+ }
135
+ }
136
+
137
+ export { earShell };
@@ -0,0 +1,154 @@
1
+ // ════════════════════════════════════════════════════════════════════════
2
+ // An eye on a face, as a figure's material draws it from vertex colours and tags: the opening of the lids as a domed almond,
3
+ // a dark rim round a round iris, a pupil that is a slit (a cat's, a fox's) or a disc, and a small glint of the sky. It is set at
4
+ // a point of a face and faces a direction of its own, its rim lying in the face's surface and the eyeball swelling out from it,
5
+ // so it needs no socket cut in the head: it is laid over a smooth face. Any feline, a fox, an owl's face or a raptor's takes it.
6
+ // ════════════════════════════════════════════════════════════════════════
7
+
8
+ /**
9
+ * An eye as `almondEye` takes it, in the lengths of the head it is laid on.
10
+ * @typedef {object} AlmondEye
11
+ * @property {number[]} at A point of the face: the eye's centre on its surface.
12
+ * @property {number[]} face The unit direction the eye looks along.
13
+ * @property {number[]} up A unit direction across it: the head's own up.
14
+ * @property {number} [turn=0.12] How far the outer corner (towards right = up × face) is raised, in radians.
15
+ * @property {number} wide The opening's half-width.
16
+ * @property {number} high The opening's half-height (the upper lid is the taller).
17
+ * @property {number} dome How far the eyeball swells out of the face at its centre.
18
+ * @property {number} iris The iris's radius.
19
+ * @property {number[]} rim The colour of the dark rim round the iris and of the lids' edge, linear [r, g, b].
20
+ * @property {number[][]} irisColours The iris from its edge to its centre: two or three colours, linear [r, g, b].
21
+ * @property {number[]} pupil The colour of the pupil.
22
+ * @property {[number, number]} pupilSize The pupil's half-width and half-height (a slit is tall and thin).
23
+ * @property {number} [glint=0.18] The radius of a glint, as a fraction of the iris's; 0 for none.
24
+ * @property {number[]} [glintColour=[0.9, 0.92, 0.95]]
25
+ * @property {number} segments The points of each ring.
26
+ * @property {number} tag The tag of the eyeball (wet and glossy, as a figure's material reads its tags).
27
+ * @property {number} lidTag The tag of the lids' edge (skin).
28
+ * @property {number} bone The bone every point follows.
29
+ */
30
+
31
+ const TAU = Math.PI * 2;
32
+
33
+ /**
34
+ * Lays an eye on a face: nothing but vertex colours and one bone, so a worker can build it.
35
+ * @param {import('./builder.js').GB} gb The builder, which keeps tags.
36
+ * @param {AlmondEye} o
37
+ */
38
+ function almondEye(gb, o) {
39
+ const { at, face, up, wide, high, dome, iris, segments: n, tag, lidTag, bone } = o,
40
+ turn = o.turn ?? 0.12,
41
+ glint = (o.glint ?? 0.18) * iris,
42
+ // across the eye, up × face: the right-hand side of the eye is the outer corner of the eye on the right of a face
43
+ rx = up[1] * face[2] - up[2] * face[1],
44
+ ry = up[2] * face[0] - up[0] * face[2],
45
+ rz = up[0] * face[1] - up[1] * face[0],
46
+ rl = Math.hypot(rx, ry, rz) || 1,
47
+ ux = rx / rl,
48
+ uy = ry / rl,
49
+ uz = rz / rl,
50
+ ct = Math.cos(turn),
51
+ st = Math.sin(turn),
52
+ // the opening's outline at the angle phi, a fraction f of the way out: an almond, pointed at its corners, its upper lid taller
53
+ almond = (phi, f) => {
54
+ const c = Math.cos(phi),
55
+ s = Math.sin(phi),
56
+ corner = 1 - 0.25 * Math.abs(c) ** 1.6,
57
+ u = wide * f * c * corner,
58
+ v = high * f * s * corner * (s > 0 ? 1 : 0.8);
59
+ return [u * ct - v * st, u * st + v * ct];
60
+ },
61
+ // a round iris/inner ring naturally clipped inside the almond opening so the lids cover the top and bottom without overlap
62
+ clipped = (phi, r, maxShare = 0.95) => {
63
+ const [au, av] = almond(phi, 0.91),
64
+ ar = Math.hypot(au, av);
65
+ if (ar < 1e-6) return [0, 0];
66
+ const d = Math.min(r, ar * maxShare);
67
+ return [(au / ar) * d, (av / ar) * d];
68
+ },
69
+ P = [0, 0, 0, 0, 0, 0],
70
+ Q = [0, 0, 0, 0, 0, 0],
71
+ R = [0, 0, 0, 0, 0, 0],
72
+ S = [0, 0, 0, 0, 0, 0];
73
+ // a point of the eye, across the face by (u, v) and out of it by the dome's height there (a shallow paraboloid, so its rim
74
+ // meets the skin at a slope and not in a wall), lifted by `lift` along the way it faces
75
+ const place = (uv, lift, out) => {
76
+ const [u, v] = uv,
77
+ d = dome * (1 - (u / wide) ** 2 - (v / high) ** 2) + lift;
78
+ out[0] = at[0] + ux * u + up[0] * v + face[0] * d;
79
+ out[1] = at[1] + uy * u + up[1] * v + face[1] * d;
80
+ out[2] = at[2] + uz * u + up[2] * v + face[2] * d;
81
+ // the dome's normal, leaning away from its centre
82
+ const nx = face[0] + (ux * (2 * dome * u)) / (wide * wide) + (up[0] * (2 * dome * v)) / (high * high),
83
+ ny = face[1] + (uy * (2 * dome * u)) / (wide * wide) + (up[1] * (2 * dome * v)) / (high * high),
84
+ nz = face[2] + (uz * (2 * dome * u)) / (wide * wide) + (up[2] * (2 * dome * v)) / (high * high),
85
+ l = Math.hypot(nx, ny, nz) || 1;
86
+ out[3] = nx / l;
87
+ out[4] = ny / l;
88
+ out[5] = nz / l;
89
+ return out;
90
+ };
91
+ const put = (p, c) => gb.raw(p[0], p[1], p[2], p[3], p[4], p[5], c[0], c[1], c[2], bone, bone, 1),
92
+ // a strip between two rings (each a function of the angle), wound to face out of the face
93
+ strip = (inner, outer, ci, co) => {
94
+ for (let j = 0; j < n; j++) {
95
+ const a = (j / n) * TAU,
96
+ b = ((j + 1) / n) * TAU;
97
+ place(inner(a), 0, P);
98
+ place(inner(b), 0, Q);
99
+ place(outer(a), 0, R);
100
+ place(outer(b), 0, S);
101
+ put(P, ci);
102
+ put(R, co);
103
+ put(S, co);
104
+ put(P, ci);
105
+ put(S, co);
106
+ put(Q, ci);
107
+ }
108
+ },
109
+ // a disc on the dome: a fan from its centre, wound counter-clockwise in (right, up) so it faces out along `face`
110
+ disc = (edge, centre, lift, c, step = 1) => {
111
+ for (let j = 0; j < n; j += step) {
112
+ place(edge((j / n) * TAU), lift, R);
113
+ place(edge(((j + step) / n) * TAU), lift, Q);
114
+ place(centre, lift, P);
115
+ put(P, c);
116
+ put(R, c);
117
+ put(Q, c);
118
+ }
119
+ };
120
+ // the lids' edge, sunk a little into the face so its outer rim meets the skin
121
+ gb.tv = lidTag;
122
+ strip(
123
+ a => almond(a, 0.91),
124
+ a => almond(a, 1.05),
125
+ o.rim,
126
+ o.rim,
127
+ );
128
+ gb.tv = tag;
129
+ const [e0, e1, e2] = o.irisColours[0],
130
+ [m0, m1, m2] = o.irisColours[1] ?? o.irisColours[0],
131
+ [c0, c1, c2] = o.irisColours[2] ?? o.irisColours[1] ?? o.irisColours[0];
132
+ strip(
133
+ a => clipped(a, iris, 0.95),
134
+ a => almond(a, 0.91),
135
+ [e0, e1, e2],
136
+ o.rim,
137
+ );
138
+ strip(
139
+ a => clipped(a, iris * 0.5, 0.55),
140
+ a => clipped(a, iris, 0.95),
141
+ [m0, m1, m2],
142
+ [e0, e1, e2],
143
+ );
144
+ disc(a => clipped(a, iris * 0.5, 0.55), [0, 0], 0, [c0, c1, c2]);
145
+ // the pupil, a slit or a disc, lying on the iris a little proud of it; and a glint of the sky up and towards the nose
146
+ const [pw, ph] = o.pupilSize;
147
+ disc(a => [pw * Math.cos(a), ph * Math.sin(a)], [0, 0], dome * 0.08, o.pupil);
148
+ if (glint > 0) {
149
+ const [gu, gv] = [-iris * 0.36, iris * 0.34];
150
+ disc(a => [gu + glint * Math.cos(a), gv + glint * Math.sin(a)], [gu, gv], dome * 0.16, o.glintColour ?? [0.9, 0.92, 0.95], 2);
151
+ }
152
+ }
153
+
154
+ export { almondEye };
@@ -0,0 +1,87 @@
1
+ // ════════════════════════════════════════════════════════════════════════
2
+ // The haunch of a hind leg: the mass of a thigh, from the croup down to the hock. Every quadruped of the kit (a horse, an ox, a
3
+ // camel, a dog, a rat, a bear, a cat) builds its hind legs this way.
4
+ //
5
+ // The bones: the femur runs forward and down from the hip to the stifle, which sits up by the belly line; the tibia runs back
6
+ // and down from the stifle to the hock, which is the heel, points backwards and stands well above the ground (about a third of
7
+ // the leg's height in a horse, higher in a dog or a cat); the cannon runs down from the hock near vertical. Round them the gluteal
8
+ // and hamstring muscles fill the rump's rear and the back of the thigh, so that from the side a thigh is a rounded triangle: its
9
+ // front edge falls from the flank fold to the stifle, its back line (the hamstring) runs straight from the buttock to the point
10
+ // of the hock, and it is as wide as the rump at the top and as narrow as the gaskin at the hock. Never a stick under a box.
11
+ //
12
+ // Such a shape is described by its edges at each height rather than by sections across a bone, so a thigh's sections lie level
13
+ // (animal-skin.js, a station's `level`): a kind writes them as slices from the top down, and `haunchStations` binds them to the
14
+ // bones and lays them out as stations. The leg's own stations, across its path, carry on below the hock.
15
+ //
16
+ // A thigh is a skin of its own, hung inside the rump. Where it stood outside the rump's side it would show a shelf along the
17
+ // rump, which no animal has (a thigh creases against the flank, in front, not along its own top), so above the rump's floor its
18
+ // slices are kept just inside the rump's own section, and the thigh steps outside it only where the rump turns under, in a fold.
19
+ // A kind's rump is therefore built with full sections (a high squareness), its sides near vertical down to the floor.
20
+ //
21
+ // The heights the kit builds to, as fractions of the withers height (the joint's centre; a point of hock or of buttock is a
22
+ // little above or behind its joint): a horse's hip 0.80, stifle 0.63, hock 0.37; a cow's 0.83, 0.57, 0.35; a dromedary's 0.87,
23
+ // 0.54, 0.37 (its thigh thin, its legs set close, its stifle free of the body); a dog's 0.84, 0.55, 0.26; a rat's and a bear's
24
+ // hocks are low, for they walk on the whole sole of the foot; a cat's hip 0.80, stifle 0.55 (forward under the belly), hock 0.30.
25
+ // Each kind's own numbers are beside its slices.
26
+ // ════════════════════════════════════════════════════════════════════════
27
+
28
+ /**
29
+ * A slice of a haunch as a kind writes it, in the body's own lengths: the height above the ground; the front edge and the back
30
+ * edge along the body (towards the nose and towards the tail); the half-width to each side of the slice's centre; how far that
31
+ * centre leans from the leg's own line (inboard when negative, as the top of a thigh does inside the rump); optionally the
32
+ * squareness of the section (2 an ellipse, more boxier, as `tube` reads it); and optionally how level the section lies (1 level
33
+ * in the rump, easing to 0 perpendicular to the sloping gaskin). A slice with no width closes the skin at a point.
34
+ * @typedef {[number, number, number, number, number, number?, number?]} HaunchSlice
35
+ */
36
+
37
+ /** How far the top of a thigh stays inside the rump's upper dome near the spine, in the body's lengths. */
38
+ const INSIDE = 0.008;
39
+
40
+ /**
41
+ * The stations of a hind leg's thigh for `tube` (geometry/animal-skin.js), from a kind's slices: each lies level at its height
42
+ * (or eases towards perpendicular along the sloping gaskin when `level < 1`), `rise` towards the nose and `fall` towards the
43
+ * tail, and follows the leg's bones by its height. The rump's own mass, above the hip joint, follows the hips; the thigh follows
44
+ * the femur; below the stifle the skin passes from the femur to the tibia over the gaskin's height. The change from the hips to
45
+ * the femur is spread over the first quarter of the femur's height above its joint, so the buttock moves a little with the leg
46
+ * and never tears from the rump. Below the hock the kind's own stations carry on (half the tibia's and half the cannon's at the
47
+ * hock itself).
48
+ *
49
+ * Given the rump's section, slices above the rump's widest line (`y > rump.y`) stay tucked inside the upper croup's curve so the
50
+ * top of the thigh melts smoothly into the rump with no shelf; at and below `rump.y` the thigh keeps its full rounded muscle
51
+ * width so its outer surface emerges tangentially from the rump's side without flattening into a plank.
52
+ * @param {HaunchSlice[]} slices From the top down, ending above the hock.
53
+ * @param {{ x: number, hip: number, stifle: number, hock: number }} leg The leg's line to the side of the body, and the heights of
54
+ * its hip, stifle and hock joints, in the slices' lengths.
55
+ * @param {{ y: number, h: number, w: number, n: number } | null} [rump] The rump's section at the hip: its centre's height, its
56
+ * half-depth downwards, its half-width and its squareness; or null to keep the slices as written.
57
+ * @param {boolean} [sparse=false] For a far skin, which a herd draws by the hundred: the second slice and the last but one (one
58
+ * hidden in the rump, one in the gaskin) are left out, and the spline carries the shape between those about them.
59
+ * @returns {number[][]} The stations: [y, z, half-width, rise, fall, bone, squareness, lean, level], the bone a place in the leg's
60
+ * chain [hips, femur, tibia].
61
+ */
62
+ function haunchStations(slices, leg, rump = null, sparse = false) {
63
+ const { x, hip, stifle, hock } = leg,
64
+ crown = hip + 0.25 * (hip - stifle); // from here up the skin is the rump's own
65
+ return slices
66
+ .filter((_, i) => !sparse || (i !== 1 && i !== slices.length - 2))
67
+ .map(([y, front, back, wide, lean = 0, square = 2.2, level = 1]) => {
68
+ let bone;
69
+ if (y >= crown) bone = 0;
70
+ else if (y >= hip) bone = 1 - (y - hip) / (crown - hip);
71
+ else if (y >= stifle) bone = 1;
72
+ else bone = 1 + Math.min(1, (stifle - y) / (stifle - hock));
73
+ const open = wide > 0;
74
+ if (rump && open && y > rump.y) {
75
+ // Above the rump's widest line the croup curves inward toward the spine, so keep the thigh's upper edge inside that dome
76
+ // (no shelf on top of the croup). At and below rump.y, let the thigh hold its full rounded elliptical thickness so it
77
+ // emerges tangentially from the rump wall instead of punching through the belly underside as a flat clamped wedge.
78
+ const up = (y - rump.y) / rump.h,
79
+ side = (up < 0.98 ? rump.w * (1 - up ** rump.n) ** (1 / rump.n) : 0) - INSIDE * Math.min(1, up * 2);
80
+ wide = Math.min(wide, Math.max(0.008, side - x - lean));
81
+ }
82
+ const half = open ? (front - back) / 2 : 0;
83
+ return [y, (front + back) / 2, open ? Math.max(0, wide) : 0, half, half, bone, square, lean, level];
84
+ });
85
+ }
86
+
87
+ export { haunchStations };
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Build a domestic hen's sculpted skull, two-part curved horn beak, continuous serrated red comb, pendant wattles, earlobes and eyes.
3
+ * @module
4
+ */
5
+
6
+ import * as THREE from 'three';
7
+ import { C, TRS } from './primitives.js';
8
+ import { EYE_TAG, tube } from './animal-skin.js';
9
+
10
+ const RED = 0x9e1c18, // comb and wattles
11
+ EARLOBE = 0xc97e6e, // pale flesh earlobe behind the eye
12
+ HORN = 0xc49546, // the beak
13
+ EYE = 0x9e5e16, // warm amber iris
14
+ PUPIL = 0x161210,
15
+ CHAIN = [0],
16
+ BEAD = new THREE.IcosahedronGeometry(0.5, 0);
17
+
18
+ /**
19
+ * Blend two linear RGB triples by clamped fraction `t`, scaling by brightness `k` and writing material `tag` into `out[3]`.
20
+ * @param {number[]} out Four-element colour/tag target `[r, g, b, tag]`.
21
+ * @param {number[]} a Start linear RGB triple.
22
+ * @param {number[]} b End linear RGB triple.
23
+ * @param {number} t Blend factor (clamped to `[0, 1]`).
24
+ * @param {number} [k=1] Brightness multiplier.
25
+ * @param {number} [tag=0] Surface material tag.
26
+ */
27
+ function shadeHen(out, a, b, t, k = 1, tag = 0) {
28
+ const f = Math.max(0, Math.min(1, t));
29
+ out[0] = (a[0] + (b[0] - a[0]) * f) * k;
30
+ out[1] = (a[1] + (b[1] - a[1]) * f) * k;
31
+ out[2] = (a[2] + (b[2] - a[2]) * f) * k;
32
+ out[3] = tag;
33
+ }
34
+
35
+ /**
36
+ * Append a hen's head (skull, two-part beak, serrated single comb, wattles, earlobes and glossy eyes) in the builder's
37
+ * active head transform frame.
38
+ * @param {import('./builder.js').GeometryBuilder} b Target geometry builder with `b.M` set to the head transform.
39
+ * @param {number[][]} cols Linear RGB triples `[body, hackles, tail, wingTips]` for the hen's coat.
40
+ */
41
+ function henHead(b, cols) {
42
+ const hackle = cols[1],
43
+ red = C(RED),
44
+ redDark = red.map(v => v * 0.82),
45
+ horn = C(HORN),
46
+ hornDark = horn.map(v => v * 0.78),
47
+ lobe = C(EARLOBE);
48
+ // Smooth lofted avian skull tapering forward to the beak base
49
+ tube(
50
+ b,
51
+ [
52
+ [0.002, -0.026, 0, 0, 0, 0],
53
+ [0.002, -0.018, 0.019, 0.021, 0.021, 0, 2],
54
+ [0.003, 0.001, 0.021, 0.024, 0.022, 0, 2.05],
55
+ [-0.001, 0.018, 0.015, 0.016, 0.016, 0, 2],
56
+ [-0.005, 0.028, 0, 0, 0, 0],
57
+ ],
58
+ {
59
+ chain: CHAIN,
60
+ sub: 2,
61
+ around: 10,
62
+ coat: (col, x, y, z) => {
63
+ // Slight reddish face skin right around the eye and cheek
64
+ const face = z > 0.004 && y < 0.014 && Math.abs(x) > 0.014 ? 0.35 : 0;
65
+ shadeHen(col, hackle, red, face, 0.98, 0);
66
+ },
67
+ },
68
+ );
69
+ // Upper mandible: gently arched and pointed horn beak
70
+ tube(
71
+ b,
72
+ [
73
+ [-0.001, 0.02, 0.009, 0.0085, 0.004, 0, 2.1],
74
+ [-0.002, 0.032, 0.0065, 0.0065, 0.0025, 0, 2.2],
75
+ [-0.007, 0.044, 0.003, 0.0032, 0.0015, 0, 2.1],
76
+ [-0.011, 0.05, 0, 0, 0, 0],
77
+ ],
78
+ {
79
+ chain: CHAIN,
80
+ sub: 1,
81
+ around: 8,
82
+ coat: (col, x, y, z, nx, ny, nz, a, v) => shadeHen(col, hornDark, horn, v * 0.9, 1, 1),
83
+ },
84
+ );
85
+ // Lower mandible tucked cleanly under the upper beak
86
+ tube(
87
+ b,
88
+ [
89
+ [-0.006, 0.02, 0.0075, 0.003, 0.0055, 0, 2],
90
+ [-0.007, 0.033, 0.0045, 0.002, 0.0035, 0, 2],
91
+ [-0.009, 0.042, 0, 0, 0, 0],
92
+ ],
93
+ {
94
+ chain: CHAIN,
95
+ sub: 1,
96
+ around: 6,
97
+ coat: col => shadeHen(col, hornDark, horn, 0.35, 0.95, 1),
98
+ },
99
+ );
100
+ // Continuous serrated single comb running front-to-back along the crown (four distinct rounded points)
101
+ tube(
102
+ b,
103
+ [
104
+ [0.018, -0.024, 0, 0, 0, 0],
105
+ [0.024, -0.018, 0.0034, 0.013, 0.007, 0, 2.3],
106
+ [0.024, -0.011, 0.0032, 0.009, 0.006, 0, 2.2],
107
+ [0.026, -0.004, 0.0036, 0.018, 0.006, 0, 2.3],
108
+ [0.025, 0.003, 0.0032, 0.011, 0.006, 0, 2.2],
109
+ [0.025, 0.01, 0.0036, 0.019, 0.006, 0, 2.3],
110
+ [0.023, 0.017, 0.0032, 0.01, 0.006, 0, 2.2],
111
+ [0.019, 0.023, 0.003, 0.014, 0.005, 0, 2.2],
112
+ [0.013, 0.027, 0, 0, 0, 0],
113
+ ],
114
+ {
115
+ chain: CHAIN,
116
+ sub: 1,
117
+ around: 8,
118
+ coat: (col, x, y) => shadeHen(col, redDark, red, (y - 0.016) * 38, 1, 1),
119
+ },
120
+ );
121
+ // Pendant wattles, earlobes, and glossy amber eyes with dark pupils
122
+ for (const s of [-1, 1]) {
123
+ tube(
124
+ b,
125
+ [
126
+ [-0.011, 0.016, 0, 0, 0, 0],
127
+ [-0.017, 0.018, 0.0036, 0.006, 0.0055, 0, 2.1],
128
+ [-0.027, 0.018, 0.0042, 0.0075, 0.0065, 0, 2.1],
129
+ [-0.035, 0.016, 0, 0, 0, 0],
130
+ ],
131
+ {
132
+ x: s * 0.0075,
133
+ side: s,
134
+ chain: CHAIN,
135
+ sub: 1,
136
+ around: 6,
137
+ coat: (col, x, y) => shadeHen(col, redDark, red, (y + 0.036) * 40, 1, 1),
138
+ },
139
+ );
140
+ b.tv = 1;
141
+ b.add(BEAD, lobe, TRS(s * 0.02, -0.006, -0.004, 0.005, 0.011, 0.009));
142
+ b.tv = EYE_TAG;
143
+ b.add(BEAD, C(EYE), TRS(s * 0.018, 0.007, 0.009, 0.01, 0.01, 0.01));
144
+ b.add(BEAD, C(PUPIL), TRS(s * 0.0215, 0.007, 0.0105, 0.0055, 0.0055, 0.0055));
145
+ b.tv = 0;
146
+ }
147
+ }
148
+
149
+ export { henHead, shadeHen };