roamwright 0.59.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,301 @@
1
+ import { lerp, sstep } from '../math/scalar.js';
2
+ import { C } from './primitives.js';
3
+ import { EYE_TAG, tube } from './animal-skin.js';
4
+ import { almondEye } from './animal-eye.js';
5
+ import { earShell } from './animal-ear.js';
6
+ import { noseLeather } from './animal-nose.js';
7
+ import { BONE } from '../motion/quadruped.js';
8
+
9
+ // ════════════════════════════════════════════════════════════════════════
10
+ // A house cat's head (geometry/animal-cat-body.js builds the body), laid out from a real cat's skull, side and front, in
11
+ // withers heights from the poll with its face along +z. The head is 0.35 long (a fifth of the nose-to-rump length) and 0.33 wide
12
+ // across the cheeks (zygomatic arches), so it is broader than the face is long and a rounded wedge, not a dog's taper; its
13
+ // muzzle is short (a quarter of its length from the eyes), ending in two rounded whisker pads each 0.07 wide, a philtrum and a
14
+ // small triangular nose leather; its eyes are each 0.09 wide, their centres 0.14 apart, set forward at 0.23 of its length and
15
+ // facing 20 degrees out from straight ahead, under a clear brow; and its ears stand 0.22 tall and 0.2 wide at the base on the
16
+ // top corners of the skull, their centres 0.17 apart, tilted 15 degrees out and open to the front.
17
+ // ════════════════════════════════════════════════════════════════════════
18
+ const NOSE = [0.56, 0.31, 0.3], // the nose leather: a muted brick pink
19
+ LIP = [0.1, 0.075, 0.07],
20
+ RIM = [0.07, 0.055, 0.05],
21
+ PUPIL = [0.015, 0.012, 0.012],
22
+ EAR_IN = [0.52, 0.4, 0.38], // the bottom of the ear's cup: a dusky pink-grey
23
+ WHISKER = [0.88, 0.86, 0.82],
24
+ IRIS_DEFAULT = [0.3, 0.27, 0.1, 0.62, 0.53, 0.22, 0.8, 0.7, 0.36], // amber-green: the edge, the middle, the centre
25
+ WET = 1.6,
26
+ SLEEK = 0.52,
27
+ NEAR_DETAIL = 2, // the detail (0 far, 1 middle, 2 near) from which the head has its brow whiskers
28
+ // each part's tube at each detail, far to near: [points round a ring, rings to a span between stations]
29
+ RINGS = {
30
+ skull: [
31
+ [8, 2],
32
+ [14, 2],
33
+ [18, 3],
34
+ ],
35
+ muzzle: [
36
+ [6, 1],
37
+ [8, 2],
38
+ [12, 2],
39
+ ],
40
+ jaw: [
41
+ [6, 1],
42
+ [8, 2],
43
+ [10, 2],
44
+ ],
45
+ };
46
+
47
+ // The skull from the back of the head to the face's front, each station [height, along the head, half-width, up, down]:
48
+ // rounded cranium, broad soft cheeks, gentle brow and smooth nasal bridge flowing directly into the short feline muzzle.
49
+ const SKULL = [
50
+ [-0.105, -0.045, 0, 0, 0, 0],
51
+ [-0.115, 0.0, 0.104, 0.096, 0.088, 0, 2.1],
52
+ [-0.125, 0.056, 0.144, 0.12, 0.102, 0, 2.15], // ear base
53
+ [-0.135, 0.114, 0.156, 0.12, 0.1, 0, 2.2], // widest cheeks
54
+ [-0.146, 0.168, 0.142, 0.104, 0.09, 0, 2.15], // temples & brow
55
+ [-0.158, 0.214, 0.114, 0.076, 0.068, 0, 2.15], // orbital plane
56
+ [-0.17, 0.252, 0.076, 0.048, 0.044, 0, 2.1], // smooth nasal bridge into muzzle
57
+ [-0.178, 0.278, 0, 0, 0, 0],
58
+ ];
59
+ // The muzzle: short, compact feline wedge with soft paired whisker pads, projecting cleanly ahead of the eyes to a neat nose.
60
+ const MUZZLE = [
61
+ [-0.172, 0.188, 0, 0, 0, 0.3],
62
+ [-0.175, 0.228, 0.072, 0.044, 0.036, 0.5, 2.15],
63
+ [-0.178, 0.272, 0.068, 0.036, 0.034, 0.8, 2.2], // soft paired whisker pads
64
+ [-0.182, 0.304, 0.05, 0.025, 0.025, 1, 2.15],
65
+ [-0.186, 0.32, 0.029, 0.014, 0.016, 1, 2.1], // front of lips
66
+ [-0.188, 0.328, 0, 0, 0, 1],
67
+ ];
68
+ // The lower jaw and neat chin, closed snugly under the muzzle with no hanging gap.
69
+ const JAW = [
70
+ [-0.224, 0.095, 0, 0, 0, 0],
71
+ [-0.221, 0.132, 0.046, 0.018, 0.02, 0.3],
72
+ [-0.214, 0.195, 0.046, 0.018, 0.02, 1],
73
+ [-0.207, 0.26, 0.038, 0.016, 0.016, 1],
74
+ [-0.202, 0.296, 0.025, 0.011, 0.011, 1],
75
+ [-0.2, 0.31, 0, 0, 0, 1],
76
+ ];
77
+
78
+ /**
79
+ * The cat head's landmarks in its own frame (withers heights from the poll, nose to +z), in fractions of the head's length.
80
+ * @param {number} [size=1] Head scale relative to a standard house cat.
81
+ */
82
+ function catHeadPlan(size = 1) {
83
+ const k = size;
84
+ return {
85
+ size: k,
86
+ length: 0.35 * k,
87
+ droop: 0.06, // carried level, nose gently poised
88
+ eye: {
89
+ at: [0.058 * k, -0.118 * k, 0.237 * k],
90
+ face: [0.27, 0.22, 0.937],
91
+ wide: 0.045 * k,
92
+ high: 0.027 * k,
93
+ dome: 0.01 * k,
94
+ iris: 0.022 * k,
95
+ },
96
+ ear: { base: [0.072 * k, -0.052 * k, 0.062 * k], height: 0.155 * k, width: 0.138 * k },
97
+ nose: [0, -0.164 * k, 0.32 * k],
98
+ hinge: [0, -0.198, 0.085].map(v => v * k),
99
+ lip: [0, -0.186 * k, 0.265 * k],
100
+ };
101
+ }
102
+
103
+ // A fine whisker: a two-ribbon strand from the root along dir, drooping a little, bright whichever way it is seen.
104
+ function whisker(b, root, dir, drop, len, width, bone) {
105
+ const mx = root[0] + dir[0] * len * 0.55,
106
+ my = root[1] + dir[1] * len * 0.55 - drop * 0.35,
107
+ mz = root[2] + dir[2] * len * 0.55,
108
+ tx = root[0] + dir[0] * len,
109
+ ty = root[1] + dir[1] * len - drop,
110
+ tz = root[2] + dir[2] * len,
111
+ h0 = width * 0.5,
112
+ h1 = width * 0.32,
113
+ h2 = width * 0.1,
114
+ v = (x, y, z) => [x, y, z, 0, 0.92, 0.38, WHISKER[0], WHISKER[1], WHISKER[2]],
115
+ // both windings, with a normal turned up to the light, so a single-sided material never loses a whisker
116
+ quad = (A, B, C, D) => {
117
+ for (const P of [A, B, C, A, C, D, A, C, B, A, D, C]) b.raw(P[0], P[1], P[2], P[3], P[4], P[5], P[6], P[7], P[8], bone, bone, 1);
118
+ };
119
+ quad(v(root[0], root[1] + h0, root[2]), v(mx, my + h1, mz), v(mx, my - h1, mz), v(root[0], root[1] - h0, root[2]));
120
+ quad(v(mx, my + h1, mz), v(tx, ty + h2, tz), v(tx, ty - h2, tz), v(mx, my - h1, mz));
121
+ }
122
+
123
+ /**
124
+ * Builds the house cat's head on the builder `b` (whose matrix `b.M` is already set to the head's frame).
125
+ * @param {object} b GeometryBuilder with bone arrays enabled.
126
+ * @param {object} H Head plan from `catHeadPlan`.
127
+ * @param {object} L Look options (`eye`, `pupil`, `c1`, `c2`, `c3`).
128
+ * @param {(region: number) => Function} coat Coat painter from `catCoatOf` (animal-cat-body.js).
129
+ * @param {number} detail Level of detail: 0 far, 1 middle, 2 near.
130
+ */
131
+ function catHead(b, H, L, coat, detail) {
132
+ const k = H.size,
133
+ skin = coat(1),
134
+ pale = (col, amount) => {
135
+ for (let i = 0; i < 3; i++) col[i] = lerp(col[i], Math.min(0.92, col[i] * 0.5 + 0.42), amount);
136
+ },
137
+ dark = (col, c, amount) => {
138
+ for (let i = 0; i < 3; i++) col[i] = lerp(col[i], c[i], amount);
139
+ },
140
+ // the face: gently paler over the whisker pads and chin, a delicate dark line along the lips and philtrum, and soft tabby cheek streaks
141
+ face = (col, x, y, z, nx, ny, nz, a, v) => {
142
+ skin(col, x, y, z, nx, ny, nz, a, v);
143
+ pale(col, sstep(0.225 * k, 0.285 * k, z) * sstep(-0.13 * k, -0.18 * k, y) * 0.42);
144
+ dark(
145
+ col,
146
+ LIP,
147
+ Math.min(
148
+ 1,
149
+ sstep(0.0022 * k, 0.0006 * k, Math.abs(y + 0.192 * k + (z - 0.215 * k) * 0.22)) *
150
+ sstep(0.01 * k, 0.028 * k, Math.abs(x)) *
151
+ sstep(0.215 * k, 0.235 * k, z) +
152
+ sstep(0.0032 * k, 0.0, Math.abs(x)) * sstep(0.268 * k, 0.284 * k, z) * sstep(-0.165 * k, -0.19 * k, y),
153
+ ),
154
+ );
155
+ col[3] = SLEEK;
156
+ };
157
+ const at = S => S.map(s => s.map(v => v * k));
158
+ tube(b, at(SKULL), {
159
+ chain: [BONE.head, BONE.lip],
160
+ around: RINGS.skull[detail][0],
161
+ sub: RINGS.skull[detail][1],
162
+ coat: face,
163
+ // gentle brow ridge over each eye and a subtle orbital plane so the lower lid sits flush without clipping into the cheek
164
+ warp: (Q, q) => {
165
+ const x = Q[q],
166
+ y = Q[q + 1],
167
+ z = Q[q + 2],
168
+ brow = sstep(0.044 * k, 0.012 * k, Math.hypot(Math.abs(x) - 0.064 * k, (y + 0.068 * k) * 1.3, (z - 0.205 * k) * 0.8)),
169
+ orbit = sstep(0.048 * k, 0.014 * k, Math.hypot(Math.abs(x) - 0.058 * k, (y + 0.12 * k) * 1.15, (z - 0.232 * k) * 0.9));
170
+ Q[q + 1] += 0.004 * k * brow;
171
+ Q[q + 2] += 0.006 * k * brow - 0.009 * k * orbit;
172
+ },
173
+ });
174
+ // the muzzle: two soft whisker pads swell out of its sides, with a delicate philtrum down its front middle
175
+ tube(b, at(MUZZLE), {
176
+ chain: [BONE.head, BONE.lip],
177
+ around: RINGS.muzzle[detail][0],
178
+ sub: RINGS.muzzle[detail][1],
179
+ coat: face,
180
+ warp: (Q, q) => {
181
+ const x = Q[q],
182
+ y = Q[q + 1],
183
+ z = Q[q + 2],
184
+ pad = sstep(0.03 * k, 0.009 * k, Math.hypot(Math.abs(x) - 0.042 * k, (y + 0.188 * k) * 1.25, (z - 0.264 * k) * 1.5)),
185
+ philtrum = sstep(0.0055 * k, 0, Math.abs(x)) * sstep(0.266 * k, 0.286 * k, z) * sstep(-0.162 * k, -0.19 * k, y);
186
+ Q[q] += Math.sign(x) * 0.006 * k * pad;
187
+ Q[q + 2] += 0.006 * k * pad - 0.007 * k * philtrum;
188
+ },
189
+ });
190
+ // the lower jaw, softly pale at the chin
191
+ tube(b, at(JAW), {
192
+ chain: [BONE.head, BONE.jaw],
193
+ around: RINGS.jaw[detail][0],
194
+ sub: RINGS.jaw[detail][1],
195
+ coat: (col, x, y, z, nx, ny, nz, a, v) => {
196
+ skin(col, x, y, z, nx, ny, nz, a, v);
197
+ pale(col, sstep(0.17 * k, 0.25 * k, z) * 0.5);
198
+ col[3] = SLEEK;
199
+ },
200
+ });
201
+ // the nose leather on the muzzle's front, where the nose bridge comes down to the lips
202
+ b.bone = BONE.head;
203
+ noseLeather(b, {
204
+ at: H.nose,
205
+ face: [0, 0.22, 0.975],
206
+ up: [0, 0.975, -0.22],
207
+ wide: 0.022 * k,
208
+ high: 0.016 * k,
209
+ depth: 0.008 * k,
210
+ colour: L.nose ?? NOSE,
211
+ tag: WET,
212
+ bone: BONE.head,
213
+ segments: [6, 8, 12][detail],
214
+ });
215
+ // the eyes: a vertical slit pupil in a warm amber-green iris, outer corners raised symmetrically on both sides
216
+ const { eye, ear } = H,
217
+ // the iris from its edge to its centre: the look's one colour (a hex, or linear) with its shade and its light, or the default's
218
+ given = L.eye === undefined ? null : [L.eye].flat().flatMap(v => (v > 1 ? C(v) : v)),
219
+ irisColours = given
220
+ ? [given.map(v => v * 0.5), given, given.map(v => Math.min(1, v * 1.25))]
221
+ : [IRIS_DEFAULT.slice(0, 3), IRIS_DEFAULT.slice(3, 6), IRIS_DEFAULT.slice(6, 9)];
222
+ [-1, 1].forEach(s => {
223
+ almondEye(b, {
224
+ at: [s * eye.at[0], eye.at[1], eye.at[2]],
225
+ face: [s * eye.face[0], eye.face[1], eye.face[2]],
226
+ up: [-s * 0.06, 0.975, -0.215],
227
+ turn: s * 0.16,
228
+ wide: eye.wide,
229
+ high: eye.high,
230
+ dome: eye.dome,
231
+ iris: eye.iris,
232
+ rim: RIM,
233
+ irisColours,
234
+ pupil: PUPIL,
235
+ pupilSize: L.pupil === false ? [0.002 * k, 0.002 * k] : [0.0055 * k, 0.019 * k],
236
+ glint: 0.14,
237
+ segments: [6, 10, 16][detail],
238
+ tag: EYE_TAG,
239
+ lidTag: SLEEK,
240
+ bone: BONE.head,
241
+ });
242
+ // whiskers from the pad, fanning out and back, and two above the eye
243
+ if (detail >= 1) {
244
+ b.bone = BONE.head;
245
+ b.tv = SLEEK;
246
+ const n = detail === NEAR_DETAIL ? 4 : 3;
247
+ for (let w = 0; w < n; w++) {
248
+ const u = w / (n - 1) - 0.5,
249
+ root = [s * (0.044 + 0.004 * u) * k, (-0.178 + 0.02 * u) * k, (0.29 - 0.005 * Math.abs(u)) * k],
250
+ dir = [s * (0.93 - Math.abs(u) * 0.1), 0.1 - 0.22 * u, -0.22 - 0.08 * Math.abs(u)],
251
+ dl = Math.hypot(dir[0], dir[1], dir[2]);
252
+ whisker(b, root, [dir[0] / dl, dir[1] / dl, dir[2] / dl], 0.02 * k, (0.19 + 0.028 * (w % 2)) * k, 0.0032 * k, BONE.head);
253
+ }
254
+ if (detail === NEAR_DETAIL)
255
+ for (let w = 0; w < 2; w++)
256
+ whisker(
257
+ b,
258
+ [s * (0.062 + 0.018 * w) * k, (-0.068 + 0.007 * w) * k, 0.195 * k],
259
+ [s * (0.55 + 0.25 * w), 0.6, -0.45],
260
+ 0.01 * k,
261
+ 0.09 * k,
262
+ 0.0028 * k,
263
+ BONE.head,
264
+ );
265
+ }
266
+ // the ear: a thin cupped triangular shell seated cleanly into the top corner of the cranium; across = front × up points
267
+ // toward -x on both sides, so slope and tilt are signed by s to keep left and right ears mirror-symmetric.
268
+ const base = [s * ear.base[0], ear.base[1], ear.base[2]],
269
+ up = [s * 0.23, 0.968, 0.09],
270
+ front = [s * 0.24, 0.1, 0.965],
271
+ across = [front[1] * up[2] - front[2] * up[1], front[2] * up[0] - front[0] * up[2], front[0] * up[1] - front[1] * up[0]], // front × up
272
+ unit = v => {
273
+ const l = Math.hypot(v[0], v[1], v[2]) || 1;
274
+ return [v[0] / l, v[1] / l, v[2] / l];
275
+ },
276
+ root = [0, 0, 0, 0];
277
+ skin(root, base[0], base[1] + 0.04 * k, base[2], s * 0.3, 0.9, 0.2);
278
+ const outer = root.slice(0, 3);
279
+ earShell(b, {
280
+ base,
281
+ up: unit(up),
282
+ across: unit(across),
283
+ front: unit(front),
284
+ height: ear.height,
285
+ width: ear.width,
286
+ cup: 0.24,
287
+ lean: 0.05,
288
+ tilt: -s * 0.06,
289
+ slope: s * 0.32,
290
+ thick: 0.014 * k,
291
+ inner: [lerp(outer[0], EAR_IN[0], 0.65), lerp(outer[1], EAR_IN[1], 0.65), lerp(outer[2], EAR_IN[2], 0.65)],
292
+ outer,
293
+ tag: SLEEK,
294
+ bone: s < 0 ? BONE.earL : BONE.earR,
295
+ rows: [3, 4, 6][detail],
296
+ cols: [2, 3, 4][detail],
297
+ });
298
+ });
299
+ }
300
+
301
+ export { catHead, catHeadPlan };