roamwright 0.61.4 → 0.61.6

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.
@@ -15,6 +15,151 @@ import { C } from './primitives.js';
15
15
  // pale (a lighter belly, throat and inner leg), mask (a dark muzzle), socks (dark feet), mottle (blotches of the second
16
16
  // colour), grizzle (a muzzle flecked with grey), mange (bare patches), scars.
17
17
  // ════════════════════════════════════════════════════════════════════════
18
+ /**
19
+ * Cupped ear-shell parameter rows for each canid kind (passed to `earShell` in `geometry/animal-ear.js`).
20
+ * Linear sizes are in head-size units; `at` is `[x/wide, y/size, z/size]` on the skull; `tip` is tip
21
+ * roundness (0 pointed to 1 broad rounded lobe); `fold` is the fraction of height where the blade bends
22
+ * forward (1 = upright) by `bend` radians; `fur` is inner ear furring (0..1).
23
+ */
24
+ export const CANINE_EARS = {
25
+ // Wolf: upright, broad at the base, compact rounded tips, deeply cupped and furred pale inside, seated high and wide.
26
+ wolf: {
27
+ width: 0.12,
28
+ height: 0.132,
29
+ cup: 0.28,
30
+ tip: 0.8,
31
+ fold: 1,
32
+ bend: 0,
33
+ lean: 0.04,
34
+ tilt: 0.05,
35
+ slope: 0.22,
36
+ thick: 0.017,
37
+ fur: 0.9,
38
+ up: [0.24, 0.96, -0.12],
39
+ front: [0.3, 0.12, 0.94],
40
+ at: [0.06, 0.048, 0.04],
41
+ },
42
+ // Jackal: tall, pointed, upright acoustic cups seated high on the narrow skull.
43
+ jackal: {
44
+ width: 0.112,
45
+ height: 0.184,
46
+ cup: 0.3,
47
+ tip: 0.16,
48
+ fold: 1,
49
+ bend: 0,
50
+ lean: 0.03,
51
+ tilt: 0.05,
52
+ slope: 0.22,
53
+ thick: 0.013,
54
+ fur: 0.45,
55
+ up: [0.26, 0.95, -0.08],
56
+ front: [0.28, 0.1, 0.95],
57
+ at: [0.062, 0.048, 0.044],
58
+ },
59
+ // Street / pariah dog: medium semi-erect ears with the upper third tipped forward, seated lower than a wolf's.
60
+ cur: {
61
+ width: 0.108,
62
+ height: 0.144,
63
+ cup: 0.26,
64
+ tip: 0.48,
65
+ fold: 0.62,
66
+ bend: 1.1,
67
+ lean: 0.05,
68
+ tilt: 0.05,
69
+ slope: 0.26,
70
+ thick: 0.013,
71
+ fur: 0.25,
72
+ up: [0.3, 0.93, -0.1],
73
+ front: [0.36, 0.12, 0.92],
74
+ at: [0.074, 0.044, 0.042],
75
+ },
76
+ // Herding / cattle dog: medium-broad ears folded forward, seated low and wide on the broad skull.
77
+ cattle: {
78
+ width: 0.114,
79
+ height: 0.146,
80
+ cup: 0.25,
81
+ tip: 0.58,
82
+ fold: 0.48,
83
+ bend: 1.95,
84
+ lean: 0.05,
85
+ tilt: 0.04,
86
+ slope: 0.24,
87
+ thick: 0.016,
88
+ fur: 0.22,
89
+ up: [0.36, 0.91, -0.08],
90
+ front: [0.4, 0.1, 0.91],
91
+ at: [0.072, 0.04, 0.042],
92
+ },
93
+ // Ratter / small terrier: V-shaped button ears rising cleanly then folded forward at the tip.
94
+ ratter: {
95
+ width: 0.1,
96
+ height: 0.138,
97
+ cup: 0.24,
98
+ tip: 0.48,
99
+ fold: 0.45,
100
+ bend: 2.18,
101
+ lean: 0.04,
102
+ tilt: 0.04,
103
+ slope: 0.24,
104
+ thick: 0.012,
105
+ fur: 0.2,
106
+ up: [0.34, 0.92, -0.06],
107
+ front: [0.38, 0.1, 0.92],
108
+ at: [0.068, 0.046, 0.046],
109
+ },
110
+ // Lapdog: long rounded hanging ears (`point: 0` leaf root) draping smoothly beside the cheeks, cup facing inward.
111
+ lapdog: {
112
+ width: 0.108,
113
+ height: 0.18,
114
+ cup: 0.2,
115
+ point: 0,
116
+ tip: 0.85,
117
+ fold: 1,
118
+ bend: 0,
119
+ lean: -0.1,
120
+ tilt: 0,
121
+ slope: 0,
122
+ thick: 0.015,
123
+ fur: 0.15,
124
+ up: [0.26, -0.95, 0.16],
125
+ front: [-0.88, -0.18, 0.44],
126
+ at: [0.068, 0.042, 0.052],
127
+ },
128
+ // Heavy semi-erect hound ear (used by legacy `['half', size]` breed rows).
129
+ half: {
130
+ width: 0.12,
131
+ height: 0.148,
132
+ cup: 0.28,
133
+ tip: 0.48,
134
+ fold: 0.64,
135
+ bend: 1.05,
136
+ lean: 0.05,
137
+ tilt: 0.05,
138
+ slope: 0.26,
139
+ thick: 0.018,
140
+ fur: 0.35,
141
+ up: [0.28, 0.94, -0.12],
142
+ front: [0.34, 0.12, 0.93],
143
+ at: [0.07, 0.046, 0.04],
144
+ },
145
+ };
146
+
147
+ /**
148
+ * Resolves a breed's `ear` value (an explicit parameter row or a legacy `[kind, size]` tuple) into an
149
+ * `earShell` parameter row, or `null` for non-canid round/cat ear kinds handled elsewhere.
150
+ * @param {object | [string, number]} earSpec
151
+ */
152
+ function resolveEar(earSpec) {
153
+ if (!earSpec) return CANINE_EARS.wolf;
154
+ if (!Array.isArray(earSpec)) return { ...CANINE_EARS.wolf, ...earSpec };
155
+ const [kind, size = 1] = earSpec;
156
+ if (kind === 'round' || kind === 'cat') return null;
157
+ const base =
158
+ CANINE_EARS[kind] ??
159
+ (kind === 'prick' ? CANINE_EARS.wolf : kind === 'fold' ? CANINE_EARS.ratter : kind === 'drop' ? CANINE_EARS.lapdog : CANINE_EARS.cur);
160
+ return size === 1 ? base : { ...base, width: base.width * size, height: base.height * size };
161
+ }
162
+
18
163
  /** The numbers every canine starts from, which a breed and then a look set again (see above). */
19
164
  export const CANINE_BASE = {
20
165
  leg: 0.53,
@@ -29,7 +174,7 @@ export const CANINE_BASE = {
29
174
  stop: 0.8,
30
175
  skull: 1,
31
176
  jowl: 0.15,
32
- ear: ['prick', 1],
177
+ ear: CANINE_EARS.wolf,
33
178
  tail: [1, 0.08, 1.15],
34
179
  fur: 1,
35
180
  ruff: 1,
@@ -67,7 +212,7 @@ export const CANINE_BREEDS = {
67
212
  stop: 0.65,
68
213
  skull: 0.9,
69
214
  jowl: 0,
70
- ear: ['prick', 1.5],
215
+ ear: CANINE_EARS.jackal,
71
216
  tail: [0.8, 0.05, 1.25],
72
217
  fur: 0.7,
73
218
  ruff: 0.5,
@@ -87,7 +232,7 @@ export const CANINE_BREEDS = {
87
232
  stop: 1.5,
88
233
  skull: 1.3,
89
234
  jowl: 1,
90
- ear: ['fold', 0.85],
235
+ ear: CANINE_EARS.cattle,
91
236
  tail: [0.9, 0.3, 0.55],
92
237
  fur: 0.55,
93
238
  ruff: 0.5,
@@ -113,7 +258,7 @@ export const CANINE_BREEDS = {
113
258
  stop: 1.9,
114
259
  skull: 1.2,
115
260
  jowl: 0.45,
116
- ear: ['drop', 1.2],
261
+ ear: CANINE_EARS.lapdog,
117
262
  tail: [0.75, 0.85, 0.55],
118
263
  fur: 1.35,
119
264
  ruff: 0.75,
@@ -128,7 +273,7 @@ export const CANINE_BREEDS = {
128
273
  leg: 0.56,
129
274
  girth: 0.9,
130
275
  bone: 0.9,
131
- ear: ['fold', 0.9],
276
+ ear: CANINE_EARS.cur,
132
277
  tail: [0.95, 0.5, 0.35],
133
278
  fur: 0.45,
134
279
  ruff: 0.3,
@@ -173,7 +318,7 @@ export const CANINE_BREEDS = {
173
318
  head: 1.15,
174
319
  muzzle: 0.9,
175
320
  stop: 1.2,
176
- ear: ['fold', 0.8],
321
+ ear: CANINE_EARS.ratter,
177
322
  tail: [0.4, 0.9, 0.3],
178
323
  fur: 0.9,
179
324
  ruff: 0.6,
@@ -316,4 +461,4 @@ function coatOf(L, p, head, fbm) {
316
461
  };
317
462
  }
318
463
 
319
- export { HEAD, LIMB, TAIL, TRUNK, breedOf, coatOf, hair };
464
+ export { HEAD, LIMB, TAIL, TRUNK, breedOf, coatOf, hair, resolveEar };
@@ -2,7 +2,8 @@ import { lerp, sstep } from '../math/scalar.js';
2
2
  import { C, TRS } from './primitives.js';
3
3
  import { SHAPES } from './animal-shapes.js';
4
4
  import { EYE_TAG, leaf, tube } from './animal-skin.js';
5
- import { HEAD } from './animal-canine-breeds.js';
5
+ import { earShell } from './animal-ear.js';
6
+ import { HEAD, resolveEar } from './animal-canine-breeds.js';
6
7
  import { BONE } from '../motion/quadruped.js';
7
8
 
8
9
  // ════════════════════════════════════════════════════════════════════════
@@ -80,7 +81,10 @@ function muzzledFace(H, p) {
80
81
  H.pads = null;
81
82
  H.iris = p.darkEye ? 0x161210 : 0x8a5e1e;
82
83
  H.mouth = [-0.028 * k, -0.036 * k]; // the height of the upper teeth and the lower
83
- H.ear = [0.08 * wide, 0.052 * k, 0.035 * k]; // the right ear's root
84
+ const earRow = resolveEar(p.ear);
85
+ // Canid ears sit where each kind's ears really grow (high and wide on wolves/jackals, lower on dogs);
86
+ // non-canid round ears (rat, bear) keep their original root placement.
87
+ H.ear = earRow ? [earRow.at[0] * wide, earRow.at[1] * k, earRow.at[2] * k] : [0.08 * wide, 0.052 * k, 0.035 * k];
84
88
  H.hinge = [0, -0.032 * k, 0.1 * k]; // the jaw's
85
89
  H.lip = [0, 0, stop + 0.4 * muzzle]; // the lips' bone
86
90
  return H;
@@ -137,8 +141,8 @@ function catFace(H) {
137
141
  }
138
142
 
139
143
  // The head: skull, brow and face as one skin, with sockets for the eyes; the lower jaw on its hinge; nose, eyes, whisker pads,
140
- // ears, teeth and tongue. Built along its own length and set on the poll. twoFaced: the ears drawn on both faces (a herd's).
141
- function head(b, H, p, L, coat, detail, twoFaced) {
144
+ // ears, teeth and tongue. Built along its own length and set on the poll.
145
+ function head(b, H, p, L, coat, detail) {
142
146
  const { size: k, stop, nose, muzzle, snout, eye, socket } = H,
143
147
  skin = coat(HEAD),
144
148
  lips = (col, x, y, z, nx, ny, nz, a, v) => {
@@ -219,7 +223,7 @@ function head(b, H, p, L, coat, detail, twoFaced) {
219
223
  if (L.pupil !== false)
220
224
  b.add(SHAPES.sph, BLACK, TRS(x + s * ball.pupilAt[0], eye[1], eye[2] + ball.pupilAt[1], px, py, pz, s * ball.turn));
221
225
  if (H.pads) pad(b, H, skin, s);
222
- ear(b, H, p, coat, s, twoFaced);
226
+ ear(b, H, p, L, coat, s, detail);
223
227
  });
224
228
  if (detail === NEAR) teeth(b, H, p, L);
225
229
  }
@@ -238,11 +242,64 @@ function pad(b, H, skin, s) {
238
242
  );
239
243
  }
240
244
 
241
- // An ear by its kind: pricked, tipped over, folded, hanging, a bear's round one, or a cat's upright triangle facing forwards.
242
- function ear(b, H, p, coat, s) {
243
- const [kind, size] = p.ear,
244
- k = H.size,
245
+ const unit3 = v => {
246
+ const l = Math.hypot(v[0], v[1], v[2]) || 1;
247
+ return [v[0] / l, v[1] / l, v[2] / l];
248
+ };
249
+ const cross3 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
250
+
251
+ // An ear by its kind: canids wear the cupped shell of `geometry/animal-ear.js` shaped by their row in `CANINE_EARS`
252
+ // (upright, semi-erect, folded forward, or hanging); non-canid round ears (rat, bear) keep the round leaf unchanged.
253
+ function ear(b, H, p, L, coat, s, detail) {
254
+ const k = H.size,
245
255
  base = [s * H.ear[0], H.ear[1], H.ear[2]],
256
+ row = resolveEar(p.ear);
257
+ if (row) {
258
+ // Build a strictly orthonormal right-handed frame (up, across = front × up, front = up × across) so inner and outer
259
+ // ear faces stay mirror-symmetric across x = 0 and never invert winding on either side.
260
+ const u = unit3([s * row.up[0], row.up[1], row.up[2]]),
261
+ rawFront = [s * row.front[0], row.front[1], row.front[2]],
262
+ ac = unit3(cross3(rawFront, u)),
263
+ fr = cross3(u, ac),
264
+ col = [0, 0, 0, 0];
265
+ coat(HEAD)(col, base[0], base[1] + 0.04 * k, base[2], s * 0.3, 0.9, 0.2);
266
+ const outer = col.slice(0, 3),
267
+ baseCoat = C(L.c1 ?? 0x4a4038),
268
+ dusky = [outer[0] * 0.52 + 0.05, outer[1] * 0.45 + 0.035, outer[2] * 0.43 + 0.035],
269
+ // Derive pale inner guard fur from the primary coat colour (not the dark crown saddle) so furred ears read pale inside.
270
+ pale = L.c3 !== undefined ? C(L.c3) : [baseCoat[0] * 0.45 + 0.42, baseCoat[1] * 0.45 + 0.39, baseCoat[2] * 0.45 + 0.34],
271
+ furred = [pale[0] * 0.92, pale[1] * 0.9, pale[2] * 0.86],
272
+ inner = [lerp(dusky[0], furred[0], row.fur), lerp(dusky[1], furred[1], row.fur), lerp(dusky[2], furred[2], row.fur)],
273
+ // Match the cat's ear LOD budget; folded ears take one extra row so the forward crease curves smoothly.
274
+ rows = (row.fold < 1 ? [4, 5, 7] : [3, 4, 6])[detail],
275
+ cols = [2, 3, 4][detail];
276
+ earShell(b, {
277
+ base,
278
+ up: u,
279
+ across: ac,
280
+ front: fr,
281
+ height: row.height * k,
282
+ width: row.width * k,
283
+ cup: row.cup,
284
+ point: row.point ?? 1,
285
+ tip: row.tip,
286
+ fold: row.fold,
287
+ bend: row.bend,
288
+ lean: row.lean,
289
+ tilt: -s * row.tilt,
290
+ slope: s * row.slope,
291
+ thick: row.thick * k,
292
+ fur: row.fur,
293
+ inner,
294
+ outer,
295
+ tag: col[3],
296
+ bone: s < 0 ? BONE.earL : BONE.earR,
297
+ rows,
298
+ cols,
299
+ });
300
+ return;
301
+ }
302
+ const [kind, size] = p.ear,
246
303
  hang = kind === 'drop',
247
304
  cat = kind === 'cat',
248
305
  round = kind === 'round',
@@ -1,8 +1,9 @@
1
1
  // ════════════════════════════════════════════════════════════════════════
2
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.
3
+ // wolf's, a jackal's), a folded ear whose upper blade bends forward over a smooth crease (a terrier's, a pariah dog's),
4
+ // or a rounded leaf (a bear's, a hanging drop ear). Its inner face is a shallow cup, deepest near the base; its back is
5
+ // the cup's outside; the rim and the tip thin to nothing, so the shell closes on itself and only its base, buried in the
6
+ // head, stays open. The tip leans towards the front or back, and the blade leans outwards, each by a share of its height.
6
7
  // ════════════════════════════════════════════════════════════════════════
7
8
 
8
9
  /**
@@ -19,6 +20,10 @@
19
20
  * @property {number} tilt How far the blade leans towards `across`, as a share of the height.
20
21
  * @property {number} thick The shell's thickness at the middle of its root.
21
22
  * @property {number} [point=1] 1 a triangle broadest at its root, 0 a leaf rounded at its root.
23
+ * @property {number} [tip] Tip roundness in [0, 1] when `point` is set (0 sharp pointed triangle, ~0.38 cat default, 0.72 wolf rounded tip, 0.85 broad rounded lobe).
24
+ * @property {number} [fold=1] Share of height at which the upper blade begins to fold forward (1: no fold).
25
+ * @property {number} [bend=0] Forward fold angle in radians past `fold` (0: straight upright; ~1.1 semi-erect; ~2.1 folded forward).
26
+ * @property {number} [fur=0] Inner ear furring in [0, 1] (0: bare/cat inner blend; higher fills the cup with soft inner fur).
22
27
  * @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
28
  * width, as a root does over the corner of a skull.
24
29
  * @property {number[]} inner The colour at the bottom of the cup, linear [r, g, b].
@@ -38,17 +43,54 @@ function earShell(gb, o) {
38
43
  const { base, up, across, front, height, width, cup, lean, tilt, thick, inner, outer, tag, bone, rows, cols } = o,
39
44
  point = o.point ?? 1,
40
45
  slope = o.slope ?? 0,
46
+ fold = o.fold ?? 1,
47
+ bend = o.bend ?? 0,
48
+ fur = o.fur ?? 0,
49
+ // Keep the original exponent and thickness path verbatim when no canid shaping option is passed so existing cat bytes never shift.
50
+ shaped = o.tip !== undefined || o.fold !== undefined || o.bend !== undefined || o.fur !== undefined,
51
+ tipExp = o.tip === undefined ? 0.78 : 1.02 - 0.64 * o.tip,
52
+ folded = fold < 1 && bend > 0,
53
+ crease = folded ? Math.min(0.24, Math.max(0.08, (1 - fold) * 0.48)) : 0,
41
54
  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)
55
+ // the blade's half-width at a share t of its height: a triangle's sides convex toward a pointed or rounded apex, or a leaf's round
56
+ wideAt = t => (point ? half * (1 - t) ** tipExp : half * Math.sin(Math.PI * (0.25 + 0.75 * t)) ** 0.8 * (1 - t ** 3)),
57
+ // a point of the inner face, in the ear's frame: along the ear, across it, and forward along its opening
46
58
  frame = (t, s) => {
47
59
  const w = wideAt(t) / half;
60
+ if (!folded) {
61
+ return [
62
+ height * t + slope * half * s * (1 - t), // the root's outer end (s = -1: across points inward) sits lower
63
+ wideAt(t) * s + tilt * height * t,
64
+ -cup * width * (1 - s * s) * w * (1 - 0.5 * t) + lean * height * t * t,
65
+ ];
66
+ }
67
+ // Smooth arc-length-preserving circular crease from t = fold over `crease`, then a straight upper flap at angle `bend`.
68
+ let along, fwd, angle;
69
+ if (t <= fold) {
70
+ angle = 0;
71
+ along = height * t;
72
+ fwd = lean * height * t * t;
73
+ } else {
74
+ const dt = t - fold,
75
+ baseFwd = lean * height * fold * fold,
76
+ radius = (crease * height) / bend;
77
+ if (dt <= crease) {
78
+ angle = (bend * dt) / crease;
79
+ along = height * fold + radius * Math.sin(angle);
80
+ fwd = baseFwd + radius * (1 - Math.cos(angle));
81
+ } else {
82
+ angle = bend;
83
+ const rem = (dt - crease) * height;
84
+ along = height * fold + radius * Math.sin(bend) + rem * Math.cos(bend);
85
+ fwd = baseFwd + radius * (1 - Math.cos(bend)) + rem * Math.sin(bend);
86
+ }
87
+ }
88
+ // Push the cup perpendicular to the bent spine so the outer face stays convex over the crease with no self-intersection.
89
+ const depth = cup * width * (1 - s * s) * w * (1 - 0.5 * t);
48
90
  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,
91
+ along + slope * half * s * (1 - t) + depth * Math.sin(angle),
92
+ wideAt(t) * s + tilt * height * Math.min(t, fold),
93
+ fwd - depth * Math.cos(angle),
52
94
  ];
53
95
  },
54
96
  world = (f, out) => {
@@ -85,8 +127,19 @@ function earShell(gb, o) {
85
127
  P = [0, 0, 0],
86
128
  N = [0, 0, 0],
87
129
  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
130
+ // 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;
131
+ // furred ears keep the paler inner fur broad across the cup with a soft dark hollow right at the root canal
89
132
  innerAt = (t, s) => {
133
+ if (fur > 0) {
134
+ // Keep furred inner pinna bright across the cup, blending to the outer coat only along the rim and tip.
135
+ const edge = Math.min(1, 0.06 + 0.94 * Math.abs(s) ** 2.4 + 0.55 * t ** 2.4);
136
+ const hollow = 1 - fur * 0.16 * (1 - Math.min(1, t * 2.8)) * (1 - s * s);
137
+ return [
138
+ (inner[0] + (outer[0] - inner[0]) * edge) * hollow,
139
+ (inner[1] + (outer[1] - inner[1]) * edge) * hollow,
140
+ (inner[2] + (outer[2] - inner[2]) * edge) * hollow,
141
+ ];
142
+ }
90
143
  const k = Math.min(1, 0.25 + 0.75 * Math.abs(s) ** 1.5 + 0.6 * t);
91
144
  return [inner[0] + (outer[0] - inner[0]) * k, inner[1] + (outer[1] - inner[1]) * k, inner[2] + (outer[2] - inner[2]) * k];
92
145
  };
@@ -113,8 +166,17 @@ function earShell(gb, o) {
113
166
  ].map(([t, s]) => {
114
167
  const f = frame(t, s),
115
168
  n = normal(t, s, N),
116
- k = skin(t, s) * back,
169
+ k = skin(t, s) * back;
170
+ let p;
171
+ if (shaped) {
172
+ // Offset along the world-space surface normal so folded and tilted ears keep uniform skin thickness.
173
+ p = world(f, P);
174
+ p[0] -= n[0] * k;
175
+ p[1] -= n[1] * k;
176
+ p[2] -= n[2] * k;
177
+ } else {
117
178
  p = world([f[0] - n[0] * k, f[1] - n[1] * k, f[2] - n[2] * k], P);
179
+ }
118
180
  return [p.slice(), side > 0 ? n.slice() : n.map(v => -v)];
119
181
  });
120
182
  for (const k of order)
@@ -53,10 +53,11 @@ const SOOT = [0.05, 0.045, 0.04];
53
53
  * @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
54
54
  * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
55
55
  * @param {typeof GB} [options.Builder] - Geometry builder class.
56
- * @param {Function} [options.archOpening] - House kit `archOpening` function.
57
- * @param {Function} [options.archRing] - House kit `archRing` function.
58
- * @param {Function} [options.facePut] - House kit `facePut` function.
59
- * @param {Function} [options.kitNew] - House kit `kitNew` factory for city sub-builders.
56
+ * @param {object} [options.houseKit] - Optional house kit (`createHouseKit`) supplying `archOpening`, `archRing`, `facePut`, and `kitNew` defaults.
57
+ * @param {Function} [options.archOpening] - House kit `archOpening` function (overrides `options.houseKit.archOpening`).
58
+ * @param {Function} [options.archRing] - House kit `archRing` function (overrides `options.houseKit.archRing`).
59
+ * @param {Function} [options.facePut] - House kit `facePut` function (overrides `options.houseKit.facePut`).
60
+ * @param {Function} [options.kitNew] - House kit `kitNew` factory for city sub-builders (overrides `options.houseKit.kitNew`).
60
61
  * @param {Function} [options.greatWalls] - Walkable great-walls builder (defaults to `createGreatWalls`).
61
62
  * @param {Function} [options.greatGateMouth] - Walkable great-wall gate mouth dimensions helper.
62
63
  * @returns {{
@@ -79,12 +80,13 @@ export function createCityFortification(options = {}) {
79
80
  const color = options.color ?? defaultColor;
80
81
  const transform = options.transform ?? defaultTransform;
81
82
  const Builder = options.Builder ?? GB;
83
+ const houseKit = options.houseKit ?? null;
82
84
  let houseKitFallback = null;
83
85
  const fallbackKit = () => (houseKitFallback ??= createHouseKit({ shapes }));
84
- const archOpening = options.archOpening ?? fallbackKit().archOpening;
85
- const archRing = options.archRing ?? fallbackKit().archRing;
86
- const facePut = options.facePut ?? fallbackKit().facePut;
87
- const kitNew = options.kitNew ?? fallbackKit().kitNew;
86
+ const archOpening = options.archOpening ?? houseKit?.archOpening ?? fallbackKit().archOpening;
87
+ const archRing = options.archRing ?? houseKit?.archRing ?? fallbackKit().archRing;
88
+ const facePut = options.facePut ?? houseKit?.facePut ?? fallbackKit().facePut;
89
+ const kitNew = options.kitNew ?? houseKit?.kitNew ?? fallbackKit().kitNew;
88
90
  const defaultGreat = options.greatWalls && options.greatGateMouth ? null : createGreatWalls({ markWall, color });
89
91
  const greatWalls = options.greatWalls ?? defaultGreat.greatWalls;
90
92
  const greatGateMouth = options.greatGateMouth ?? defaultGreat.greatGateMouth;
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Worker-safe instance placement planner: samples `(x, z)` points over a ground sampler and fills compact column-major
3
+ * 4×4 transform buffers per variant bucket with zero Three.js dependency or per-instance object allocation.
4
+ * @module
5
+ */
6
+
7
+ import { mulberry32 } from '../math/random.js';
8
+ import { TAU } from '../math/scalar.js';
9
+
10
+ const DEFAULT_SPREAD_METRES = 40;
11
+ const MATRIX_STRIDE = 16;
12
+
13
+ /**
14
+ * Samples candidate `(x, z)` placements across `attempts` iterations, distributes accepted points round-robin into
15
+ * `buckets` variant pools (`i % buckets`), grounds each at `groundAt(x, z) - sink`, applies a random Y-axis yaw and
16
+ * uniform scale `scaleRange[0] + random() * scaleRange[1]`, and writes column-major 4×4 transform matrices into compact
17
+ * `Float32Array` buffers ready for `THREE.InstancedMesh` or structured transfer from a worker.
18
+ *
19
+ * @param {object} [options] - Placement parameters.
20
+ * @param {number} [options.attempts=90] - Number of candidate samples to draw.
21
+ * @param {number} [options.maxPerBucket=32] - Maximum accepted instances per bucket.
22
+ * @param {number} [options.buckets=1] - Number of variant buckets (round-robin by attempt index `i % buckets`).
23
+ * @param {(attempt: number) => [number, number] | null | undefined} [options.sample] - Returns `[x, z]` in metres or a falsy value to reject the candidate. Defaults to uniform random points in a 40 m square.
24
+ * @param {(x: number, z: number) => number} [options.groundAt] - Terrain elevation sampler `(x, z) => y` in metres (defaults to flat `0`).
25
+ * @param {() => number} [options.random] - Deterministic PRNG in `[0, 1)` (defaults to `mulberry32(1)`).
26
+ * @param {[number, number]} [options.scaleRange=[1, 0]] - Uniform scale `[minScale, scaleSpan]`: each instance receives `scaleRange[0] + random() * scaleRange[1]`.
27
+ * @param {number} [options.sink=0] - Vertical sink below `groundAt(x, z)` in metres so trunks and roots seat cleanly on slopes.
28
+ * @param {boolean} [options.stopWhenFull=false] - When `true`, stops sampling once every bucket reaches `maxPerBucket`. Leave `false` when callers rely on a fixed `attempts` draw count against a shared PRNG.
29
+ * @returns {{ buckets: Array<{ count: number, matrices: Float32Array }>, total: number }} Per-bucket counts and tightly sized 4×4 transform buffers, plus total placed count.
30
+ */
31
+ export function planScatterInstances({
32
+ attempts = 90,
33
+ maxPerBucket = 32,
34
+ buckets = 1,
35
+ sample,
36
+ groundAt = () => 0,
37
+ random = mulberry32(1),
38
+ scaleRange = [1, 0],
39
+ sink = 0,
40
+ stopWhenFull = false,
41
+ } = {}) {
42
+ if (!Number.isInteger(buckets) || buckets < 1) throw new RangeError('buckets must be a positive integer');
43
+ if (!Number.isInteger(maxPerBucket) || maxPerBucket < 0) throw new RangeError('maxPerBucket must be a non-negative integer');
44
+ if (!Number.isInteger(attempts) || attempts < 0) throw new RangeError('attempts must be a non-negative integer');
45
+
46
+ const samplePoint = sample ?? (() => [(random() - 0.5) * DEFAULT_SPREAD_METRES, (random() - 0.5) * DEFAULT_SPREAD_METRES]);
47
+ const buffers = Array.from({ length: buckets }, () => new Float32Array(maxPerBucket * MATRIX_STRIDE));
48
+ const counts = new Int32Array(buckets);
49
+ const maxTotal = buckets * maxPerBucket;
50
+ const minScale = scaleRange[0];
51
+ const scaleSpan = scaleRange[1];
52
+ let total = 0;
53
+
54
+ for (let i = 0; i < attempts; i++) {
55
+ if (stopWhenFull && total >= maxTotal) break;
56
+ const point = samplePoint(i);
57
+ if (!point) continue;
58
+ const bucketIndex = i % buckets;
59
+ const count = counts[bucketIndex];
60
+ if (count >= maxPerBucket) continue;
61
+
62
+ const tx = point[0];
63
+ const tz = point[1];
64
+ const ty = groundAt(tx, tz) - sink;
65
+ const yaw = random() * TAU;
66
+ const scale = minScale + random() * scaleSpan;
67
+ const cos = Math.cos(yaw);
68
+ const sin = Math.sin(yaw);
69
+
70
+ const matrices = buffers[bucketIndex];
71
+ const offset = count * MATRIX_STRIDE;
72
+ matrices[offset] = cos * scale;
73
+ matrices[offset + 1] = 0;
74
+ matrices[offset + 2] = -sin * scale;
75
+ matrices[offset + 3] = 0;
76
+ matrices[offset + 4] = 0;
77
+ matrices[offset + 5] = scale;
78
+ matrices[offset + 6] = 0;
79
+ matrices[offset + 7] = 0;
80
+ matrices[offset + 8] = sin * scale;
81
+ matrices[offset + 9] = 0;
82
+ matrices[offset + 10] = cos * scale;
83
+ matrices[offset + 11] = 0;
84
+ matrices[offset + 12] = tx;
85
+ matrices[offset + 13] = ty;
86
+ matrices[offset + 14] = tz;
87
+ matrices[offset + 15] = 1;
88
+
89
+ counts[bucketIndex] = count + 1;
90
+ total++;
91
+ }
92
+
93
+ return {
94
+ buckets: buffers.map((buffer, index) => {
95
+ const count = counts[index];
96
+ return {
97
+ count,
98
+ matrices: count === maxPerBucket ? buffer : buffer.slice(0, count * MATRIX_STRIDE),
99
+ };
100
+ }),
101
+ total,
102
+ };
103
+ }