@anime-studio/core 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/audio.d.ts +44 -0
  2. package/dist/audio.d.ts.map +1 -0
  3. package/dist/audio.js +57 -0
  4. package/dist/audio.js.map +1 -0
  5. package/dist/bounds.d.ts +25 -0
  6. package/dist/bounds.d.ts.map +1 -0
  7. package/dist/bounds.js +211 -0
  8. package/dist/bounds.js.map +1 -0
  9. package/dist/fastmath.d.ts +12 -0
  10. package/dist/fastmath.d.ts.map +1 -0
  11. package/dist/fastmath.js +35 -0
  12. package/dist/fastmath.js.map +1 -0
  13. package/dist/field.d.ts.map +1 -1
  14. package/dist/field.js +74 -38
  15. package/dist/field.js.map +1 -1
  16. package/dist/format/schema.d.ts +22 -0
  17. package/dist/format/schema.d.ts.map +1 -1
  18. package/dist/format/schema.js +18 -3
  19. package/dist/format/schema.js.map +1 -1
  20. package/dist/format/validate.d.ts.map +1 -1
  21. package/dist/format/validate.js +7 -2
  22. package/dist/format/validate.js.map +1 -1
  23. package/dist/fx.d.ts +26 -0
  24. package/dist/fx.d.ts.map +1 -1
  25. package/dist/fx.js +122 -22
  26. package/dist/fx.js.map +1 -1
  27. package/dist/index.d.ts +3 -0
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +3 -0
  30. package/dist/index.js.map +1 -1
  31. package/dist/preview.d.ts +16 -1
  32. package/dist/preview.d.ts.map +1 -1
  33. package/dist/preview.js +43 -4
  34. package/dist/preview.js.map +1 -1
  35. package/dist/rig.d.ts.map +1 -1
  36. package/dist/rig.js +3 -1
  37. package/dist/rig.js.map +1 -1
  38. package/dist/sanitize.d.ts +24 -0
  39. package/dist/sanitize.d.ts.map +1 -0
  40. package/dist/sanitize.js +79 -0
  41. package/dist/sanitize.js.map +1 -0
  42. package/dist/scene.d.ts +31 -3
  43. package/dist/scene.d.ts.map +1 -1
  44. package/dist/scene.js +136 -23
  45. package/dist/scene.js.map +1 -1
  46. package/dist/sequence.d.ts +5 -3
  47. package/dist/sequence.d.ts.map +1 -1
  48. package/dist/sequence.js +15 -6
  49. package/dist/sequence.js.map +1 -1
  50. package/dist/solid.d.ts.map +1 -1
  51. package/dist/solid.js +437 -47
  52. package/dist/solid.js.map +1 -1
  53. package/package.json +1 -1
package/dist/solid.js CHANGED
@@ -11,6 +11,7 @@
11
11
  * solid shows there (the torso drawn by the rig, with the legs' solid over it only where they are
12
12
  * nearer than the torso).
13
13
  */
14
+ import { hypot3 } from "./fastmath.js";
14
15
  import { fieldPath, fieldStroke, refineEdges } from "./field.js";
15
16
  /** Light from the upper left, in front (view space, y down, z towards the camera). */
16
17
  const LIGHT = (() => {
@@ -27,11 +28,11 @@ function shapeSdf(s) {
27
28
  const bx = b[0] - a[0], by = b[1] - a[1], bz = b[2] - a[2];
28
29
  const ll = bx * bx + by * by + bz * bz;
29
30
  if (ll < 1e-9)
30
- return (x, y, z) => Math.hypot(x - a[0], y - a[1], z - a[2]) - ra;
31
+ return (x, y, z) => hypot3(x - a[0], y - a[1], z - a[2]) - ra;
31
32
  return (x, y, z) => {
32
33
  const px = x - a[0], py = y - a[1], pz = z - a[2];
33
34
  const h = Math.max(0, Math.min(1, (px * bx + py * by + pz * bz) / ll));
34
- return Math.hypot(px - bx * h, py - by * h, pz - bz * h) - (ra + (rb - ra) * h);
35
+ return hypot3(px - bx * h, py - by * h, pz - bz * h) - (ra + (rb - ra) * h);
35
36
  };
36
37
  }
37
38
  if (s.kind === "ellipsoid") {
@@ -49,7 +50,7 @@ function shapeSdf(s) {
49
50
  const qx = Math.abs(m[0] * px + m[1] * py + m[2] * pz) - h[0] + round;
50
51
  const qy = Math.abs(m[3] * px + m[4] * py + m[5] * pz) - h[1] + round;
51
52
  const qz = Math.abs(m[6] * px + m[7] * py + m[8] * pz) - h[2] + round;
52
- return Math.hypot(Math.max(qx, 0), Math.max(qy, 0), Math.max(qz, 0)) + Math.min(Math.max(qx, qy, qz), 0) - round;
53
+ return hypot3(Math.max(qx, 0), Math.max(qy, 0), Math.max(qz, 0)) + Math.min(Math.max(qx, qy, qz), 0) - round;
53
54
  };
54
55
  }
55
56
  function bodySdf(shapes, k) {
@@ -71,6 +72,238 @@ function bodySdf(shapes, k) {
71
72
  return d;
72
73
  };
73
74
  }
75
+ /**
76
+ * A body's SDF along one screen column (the ray of a grid cell), for the ray caster: the same
77
+ * numbers as `bodySdf` bit for bit, only cheaper. The terms of each shape that depend on the
78
+ * column's x and y alone are worked out once per column, with a lower bound of the shape's distance
79
+ * anywhere on the ray (from the column's distance to the shape's picture). A shape is skipped at a
80
+ * sample where it provably cannot change the result: where its bound already exceeds the running
81
+ * union by the blend, the smooth minimum gives back the running value unchanged (exactly: its
82
+ * correction term is 0). The union starts from +Infinity, which the first shape kept replaces
83
+ * exactly by its own value.
84
+ */
85
+ class ColumnSdf {
86
+ k;
87
+ n;
88
+ kind;
89
+ /** Per shape: 16 parameters (see `constructor`). */
90
+ p;
91
+ /** Per box: the capsule bounding it (radius, unused, half its axis on screen). */
92
+ box;
93
+ /** Per shape: whether its lower bound holds (an ellipsoid's or a box's needs an unscaled rotation). */
94
+ bounded;
95
+ /** Per shape, per column: lower bound of the distance, and the x/y partial terms. */
96
+ lb;
97
+ cx;
98
+ constructor(shapes, k) {
99
+ this.k = k;
100
+ const n = (this.n = shapes.length);
101
+ this.kind = new Int8Array(n);
102
+ this.p = new Float64Array(n * 16);
103
+ this.box = new Float64Array(n * 4);
104
+ this.bounded = new Uint8Array(n);
105
+ this.lb = new Float64Array(n);
106
+ this.cx = new Float64Array(n * 5);
107
+ shapes.forEach((s, i) => {
108
+ const o = i * 16, p = this.p;
109
+ if (s.kind === "box") {
110
+ // (Its longest axis in view space — half of it, on screen — and the capsule's radius.)
111
+ const h = s.h, L = h[0] >= h[1] && h[0] >= h[2] ? 0 : h[1] >= h[2] ? 1 : 2;
112
+ const r = Math.hypot(...h.filter((_, a) => a !== L));
113
+ this.box.set([r, 0, s.m[L * 3] * h[L], s.m[L * 3 + 1] * h[L]], i * 4);
114
+ }
115
+ if (s.kind === "cone") {
116
+ const { a, b: e, ra, rb } = s;
117
+ const bx = e[0] - a[0], by = e[1] - a[1], bz = e[2] - a[2];
118
+ const ll = bx * bx + by * by + bz * bz;
119
+ this.kind[i] = ll < 1e-9 ? 3 : 0;
120
+ p.set([a[0], a[1], a[2], bx, by, bz, ll, ra, rb], o);
121
+ this.bounded[i] = 1;
122
+ }
123
+ else {
124
+ this.kind[i] = s.kind === "ellipsoid" ? 1 : 2;
125
+ p.set([s.c[0], s.c[1], s.c[2], ...s.m, ...s.h, s.kind === "box" ? s.round : 0], o);
126
+ // (A bound needs the rotation to be one: a scaled bone would make the SDF no distance.)
127
+ const m = s.m;
128
+ const ortho = [0, 1, 2].every((r) => [0, 1, 2].every((c) => Math.abs(m[r * 3] * m[c * 3] + m[r * 3 + 1] * m[c * 3 + 1] + m[r * 3 + 2] * m[c * 3 + 2] - (r === c ? 1 : 0)) < 1e-9));
129
+ this.bounded[i] = ortho ? 1 : 0;
130
+ }
131
+ });
132
+ }
133
+ /**
134
+ * How far the column passes outside the body for sure (the least lower bound of its shapes, less
135
+ * what the smooth union can add): where positive, no sample of the ray is inside it. −Infinity
136
+ * when nothing is known (the column crosses an ellipsoid's bounding sphere, or a shape on a scaled
137
+ * bone); NaN propagates (no skipping).
138
+ */
139
+ miss = -Infinity;
140
+ /** Sets the column (screen x, y). */
141
+ column(X, Y) {
142
+ const p = this.p, cx = this.cx;
143
+ let least = Infinity;
144
+ for (let i = 0; i < this.n; i++) {
145
+ const o = i * 16, q = i * 5;
146
+ // A lower bound of the shape's distance anywhere along the ray, shrunk by a margin far above any
147
+ // rounding error, so it holds in floating point too.
148
+ if (this.kind[i] === 0 || this.kind[i] === 3) {
149
+ // A cone: its distance is at least the column's distance to the segment's picture, less its
150
+ // larger radius (the nearest point of a cone's axis is on the segment, and a radius at most that).
151
+ const px = X - p[o], py = Y - p[o + 1], bx = this.kind[i] === 0 ? p[o + 3] : 0, by = this.kind[i] === 0 ? p[o + 4] : 0;
152
+ const l2 = bx * bx + by * by;
153
+ const t = l2 > 0 ? Math.max(0, Math.min(1, (px * bx + py * by) / l2)) : 0;
154
+ const dx = px - bx * t, dy = py - by * t;
155
+ this.lb[i] = Math.sqrt(dx * dx + dy * dy) * (1 - 1e-7) - 1e-4 - Math.max(p[o + 7], p[o + 8]);
156
+ }
157
+ else if (this.bounded[i] && this.kind[i] === 1) {
158
+ // An ellipsoid (half axes h): outside its bounding sphere, at a distance D from its centre, its
159
+ // SDF k0 (k0 − 1) / k1 is at least (D / hmax − 1) · hmin (k1 ≤ k0 / hmin, k0 ≥ D / hmax), and
160
+ // D is at least the column's distance to the centre's picture.
161
+ const h0 = p[o + 12], h1 = p[o + 13], h2 = p[o + 14];
162
+ const hmax = Math.max(h0, h1, h2), hmin = Math.min(h0, h1, h2);
163
+ const dx = X - p[o], dy = Y - p[o + 1], D = Math.sqrt(dx * dx + dy * dy) * (1 - 1e-7) - 1e-4;
164
+ this.lb[i] = D > hmax && hmin > 0 ? (D / hmax - 1) * hmin * (1 - 1e-7) - 1e-4 : -Infinity;
165
+ }
166
+ else if (this.bounded[i]) {
167
+ // A box: it lies in the capsule round its longest axis (radius: the half diagonal across it),
168
+ // and its SDF, a true distance, is at least the capsule's — at least the column's distance to
169
+ // the axis's picture, less that radius.
170
+ const b = i * 4, bx = this.box;
171
+ const sx = bx[b + 2], sy = bx[b + 3], r = bx[b];
172
+ const qx = X - (p[o] - sx), qy = Y - (p[o + 1] - sy), ex = 2 * sx, ey = 2 * sy;
173
+ const l2 = ex * ex + ey * ey;
174
+ const t = l2 > 0 ? Math.max(0, Math.min(1, (qx * ex + qy * ey) / l2)) : 0;
175
+ const dx = qx - ex * t, dy = qy - ey * t;
176
+ this.lb[i] = Math.sqrt(dx * dx + dy * dy) * (1 - 1e-7) - 1e-4 - r * (1 + 1e-7);
177
+ }
178
+ else
179
+ this.lb[i] = -Infinity;
180
+ // (A NaN bound — a NaN pose — makes the least NaN: nothing is skipped.)
181
+ if (!(this.lb[i] >= least))
182
+ least = least === least ? this.lb[i] : least;
183
+ const px = X - p[o], py = Y - p[o + 1];
184
+ cx[q] = px;
185
+ cx[q + 1] = py;
186
+ if (this.kind[i] === 0)
187
+ cx[q + 2] = px * p[o + 3] + py * p[o + 4];
188
+ else if (this.kind[i] === 1 || this.kind[i] === 2) {
189
+ cx[q + 2] = p[o + 3] * px + p[o + 4] * py;
190
+ cx[q + 3] = p[o + 6] * px + p[o + 7] * py;
191
+ cx[q + 4] = p[o + 9] * px + p[o + 10] * py;
192
+ }
193
+ }
194
+ // (Each smooth union step goes at most k/4 below the smaller of its two distances.)
195
+ this.miss = least - ((this.n - 1) * Math.max(this.k, 0)) / 4 - 1e-4;
196
+ }
197
+ /** Depth of the first hit of the last `march` (−Infinity where it missed). */
198
+ hitZ = -Infinity;
199
+ /**
200
+ * Ray marches the current column from the front (`zmax`) to the back (`zmin`): the silhouette
201
+ * value — negative inside (how deep the ray goes), else its closest miss — and `hitZ`. With
202
+ * `firstHit`, it stops at the first hit (−0.05: only the sign and the depth are right).
203
+ */
204
+ march(zmin, zmax, minStep, firstHit = false) {
205
+ let Z = zmax + 1, closest = Infinity, deepest = 0, entered = false;
206
+ this.hitZ = -Infinity;
207
+ while (Z > zmin - 1) {
208
+ const d = this.at(Z);
209
+ if (d < closest)
210
+ closest = d;
211
+ if (d <= 0) {
212
+ if (!entered) {
213
+ entered = true;
214
+ this.hitZ = Z;
215
+ // (Inside, and only that and the depth are asked for: where the ray would go on from.)
216
+ if (firstHit) {
217
+ this.resumeZ = Z - Math.max(minStep, -d * 0.8);
218
+ this.resumeDeep = -d > 0 ? -d : 0;
219
+ return -0.05;
220
+ }
221
+ }
222
+ if (-d > deepest)
223
+ deepest = -d;
224
+ Z -= Math.max(minStep, -d * 0.8);
225
+ }
226
+ else {
227
+ if (entered)
228
+ break;
229
+ Z -= Math.max(minStep, d * 0.9);
230
+ }
231
+ }
232
+ return entered ? -Math.max(deepest, 0.05) : closest;
233
+ }
234
+ /** Where a `march` with `firstHit` stopped: the next depth and the deepest point so far. */
235
+ resumeZ = 0;
236
+ resumeDeep = 0;
237
+ /**
238
+ * Goes on with a march stopped at its first hit (from `resumeZ`, `resumeDeep`): the same steps the
239
+ * whole march takes after it, so the same value.
240
+ */
241
+ resume(zmin, minStep, Z, deepest) {
242
+ while (Z > zmin - 1) {
243
+ const d = this.at(Z);
244
+ if (d > 0)
245
+ break;
246
+ if (-d > deepest)
247
+ deepest = -d;
248
+ Z -= Math.max(minStep, -d * 0.8);
249
+ }
250
+ return -Math.max(deepest, 0.05);
251
+ }
252
+ /** The body's SDF at depth Z on the current column. */
253
+ at(Z) {
254
+ const p = this.p, cx = this.cx, k = this.k, n = this.n, lb = this.lb, kind = this.kind;
255
+ let d = Infinity;
256
+ for (let i = 0; i < n; i++) {
257
+ if (k <= 0 ? lb[i] > d : lb[i] >= d + k)
258
+ continue;
259
+ let e;
260
+ if (kind[i] === 0) {
261
+ const o = i * 16, q = i * 5, px = cx[q], py = cx[q + 1], pz = Z - p[o + 2];
262
+ const bx = p[o + 3], by = p[o + 4], bz = p[o + 5], ra = p[o + 7], rb = p[o + 8];
263
+ const h = Math.max(0, Math.min(1, (cx[q + 2] + pz * bz) / p[o + 6]));
264
+ e = hypot3(px - bx * h, py - by * h, pz - bz * h) - (ra + (rb - ra) * h);
265
+ }
266
+ else
267
+ e = this.shape(i, Z, p, cx);
268
+ if (n === 1)
269
+ return e;
270
+ const x = k - Math.abs(d - e);
271
+ // (Past the blend the smooth minimum is the minimum — its correction term is 0 exactly.)
272
+ if (k <= 0 || x <= 0)
273
+ d = Math.min(d, e);
274
+ else {
275
+ const hh = x / k;
276
+ d = Math.min(d, e) - hh * hh * k * 0.25;
277
+ }
278
+ }
279
+ return d;
280
+ }
281
+ shape(i, Z, p, cx) {
282
+ const o = i * 16, q = i * 5, px = cx[q], py = cx[q + 1], pz = Z - p[o + 2];
283
+ switch (this.kind[i]) {
284
+ case 0: {
285
+ const bx = p[o + 3], by = p[o + 4], bz = p[o + 5], ra = p[o + 7], rb = p[o + 8];
286
+ const h = Math.max(0, Math.min(1, (cx[q + 2] + pz * bz) / p[o + 6]));
287
+ return hypot3(px - bx * h, py - by * h, pz - bz * h) - (ra + (rb - ra) * h);
288
+ }
289
+ case 3:
290
+ return hypot3(px, py, pz) - p[o + 7];
291
+ case 1: {
292
+ const h0 = p[o + 12], h1 = p[o + 13], h2 = p[o + 14];
293
+ const lx = (cx[q + 2] + p[o + 5] * pz) / h0, ly = (cx[q + 3] + p[o + 8] * pz) / h1, lz = (cx[q + 4] + p[o + 11] * pz) / h2;
294
+ const k0 = Math.sqrt(lx * lx + ly * ly + lz * lz), k1 = Math.sqrt((lx / h0) ** 2 + (ly / h1) ** 2 + (lz / h2) ** 2);
295
+ return k1 === 0 ? -Math.min(h0, h1, h2) : (k0 * (k0 - 1)) / k1;
296
+ }
297
+ default: {
298
+ const round = p[o + 15];
299
+ const qx = Math.abs(cx[q + 2] + p[o + 5] * pz) - p[o + 12] + round;
300
+ const qy = Math.abs(cx[q + 3] + p[o + 8] * pz) - p[o + 13] + round;
301
+ const qz = Math.abs(cx[q + 4] + p[o + 11] * pz) - p[o + 14] + round;
302
+ return hypot3(Math.max(qx, 0), Math.max(qy, 0), Math.max(qz, 0)) + Math.min(Math.max(qx, qy, qz), 0) - round;
303
+ }
304
+ }
305
+ }
306
+ }
74
307
  /** Screen box (x0, y0, x1, y1) and depth range of a view shape. */
75
308
  function shapeBounds(s) {
76
309
  if (s.kind === "cone") {
@@ -117,34 +350,102 @@ const CACHE_SIZE = 256;
117
350
  * keys, so a frame is the same drawing whatever was drawn before it (`pose = f(doc, t)`; parallel
118
351
  * renders match byte for byte).
119
352
  */
120
- function cacheKey(bodies, posed, step) {
353
+ function cacheKey(bodies, shapes, step) {
121
354
  const body = (b) => [b.fill ?? "", b.shade ?? "", b.stroke ?? "", b.strokeWidth, b.blend, b.seamless ? 1 : 0, b.over ?? 0, b.margin ?? 0, (b.join ?? []).join(",")].join("\u0001");
122
355
  const under = (b) => b.shapes.map((x) => (x.under ? 1 : 0)).join("");
123
- const shape = (s) => {
124
- const nums = s.kind === "cone" ? [...s.a, ...s.b, s.ra, s.rb] : [...s.c, ...s.m, ...s.h, s.kind === "box" ? s.round : -1];
125
- return s.kind + ":" + nums.join(",");
126
- };
127
- return step + "|" + bodies.map((b, i) => body(b) + under(b) + "\u0002" + posed[i].map(shape).join(";")).join("/");
356
+ return step + "|" + bodies.map((b, i) => body(b) + under(b) + "\u0002" + shapes[i]).join("/");
128
357
  }
358
+ /** A posed shape at full precision, as a key. */
359
+ const shapeKey = (s) => {
360
+ const nums = s.kind === "cone" ? [...s.a, ...s.b, s.ra, s.rb] : [...s.c, ...s.m, ...s.h, s.kind === "box" ? s.round : -1];
361
+ return s.kind + ":" + nums.join(",");
362
+ };
129
363
  /**
130
364
  * The paths drawing a solid for this frame (character space). Deterministic, and cached: a pose
131
365
  * held still (sitting through a dialogue) is drawn once.
132
366
  */
133
367
  export function drawSolid(bodies, step, r3, frame) {
134
368
  const posed = bodies.map((b) => poseShapes(b, r3, frame));
135
- const key = cacheKey(bodies, posed, step);
369
+ const shapes = posed.map((ss) => ss.map(shapeKey).join(";"));
370
+ const key = cacheKey(bodies, shapes, step);
136
371
  const hit = cache.get(key);
137
372
  if (hit) {
138
373
  cache.delete(key);
139
374
  cache.set(key, hit);
140
375
  return hit;
141
376
  }
142
- const out = castSolid(bodies, posed, step);
377
+ const out = castSolid(bodies, posed, step, shapes);
143
378
  cache.set(key, out);
144
379
  if (cache.size > CACHE_SIZE)
145
380
  cache.delete(cache.keys().next().value);
146
381
  return out;
147
382
  }
383
+ /** A memo cell: worked out in full, or only its sign and depth (an occluder's). */
384
+ const FULL = 1, SIGN = 2;
385
+ const columnMemos = new Map();
386
+ let columnCells = 0;
387
+ const COLUMN_CELLS = 2_000_000;
388
+ /** Bodies seen once (keys only), the last `SEEN_BODIES`: a memo is made on the second sight. */
389
+ const seenBodies = new Map();
390
+ const SEEN_BODIES = 1024;
391
+ /** The memo of a body (its posed shapes' key, its blend), on the lattice of a step; `b`: its bounds. */
392
+ function columnMemo(shapes, blend, step, b) {
393
+ if (!Number.isInteger(step) || step <= 0)
394
+ return undefined;
395
+ const key = step + "|" + blend + "|" + shapes;
396
+ const hit = columnMemos.get(key);
397
+ if (hit) {
398
+ columnMemos.delete(key);
399
+ columnMemos.set(key, hit);
400
+ return hit;
401
+ }
402
+ // (A body seen for the first time gets no memo: most move on every frame, never to be seen again.)
403
+ if (!seenBodies.delete(key)) {
404
+ seenBodies.set(key, true);
405
+ if (seenBodies.size > SEEN_BODIES)
406
+ seenBodies.delete(seenBodies.keys().next().value);
407
+ return undefined;
408
+ }
409
+ // The lattice columns a cast can visit: within two cells of the body's screen box.
410
+ const ox = Math.floor((b[0] - step * 2) / step) * step, oy = Math.floor((b[1] - step * 2) / step) * step;
411
+ const w = Math.floor((b[2] + step * 2 - ox) / step) + 1, h = Math.floor((b[3] + step * 2 - oy) / step) + 1;
412
+ if (!(w > 0 && h > 0) || !Number.isSafeInteger(ox) || !Number.isSafeInteger(oy) || w * h > COLUMN_CELLS / 4)
413
+ return undefined;
414
+ const memo = { ox, oy, w, h, val: new Float32Array(w * h), depth: new Float32Array(w * h), done: new Uint8Array(w * h), rz: new Float64Array(w * h), rd: new Float64Array(w * h) };
415
+ columnMemos.set(key, memo);
416
+ columnCells += w * h;
417
+ for (const [k, m] of columnMemos) {
418
+ if (columnCells <= COLUMN_CELLS)
419
+ break;
420
+ columnMemos.delete(k);
421
+ columnCells -= m.w * m.h;
422
+ }
423
+ return memo;
424
+ }
425
+ /** A 1D dilation (radius r cells) of a mask along rows or columns: set within r cells of a set cell. */
426
+ function dilate(src, w, h, r, horizontal) {
427
+ const out = new Uint8Array(src.length);
428
+ const lines = horizontal ? h : w, len = horizontal ? w : h, stride = horizontal ? 1 : w, next = horizontal ? w : 1;
429
+ for (let l = 0; l < lines; l++) {
430
+ const base = l * next;
431
+ // The last set cell seen; each cell is set when one is within r behind it or r ahead of it.
432
+ let last = -Infinity;
433
+ for (let i = 0; i < len; i++) {
434
+ if (src[base + i * stride])
435
+ last = i;
436
+ if (i - last <= r)
437
+ out[base + i * stride] = 1;
438
+ }
439
+ last = Infinity;
440
+ for (let i = len - 1; i >= 0; i--) {
441
+ if (src[base + i * stride])
442
+ last = i;
443
+ if (last - i <= r)
444
+ out[base + i * stride] = 1;
445
+ }
446
+ }
447
+ return out;
448
+ }
148
449
  /** A 1D max filter (radius r cells) along rows or columns of a grid of values. */
149
450
  function maxFilter(src, w, h, r, horizontal) {
150
451
  const out = new Float32Array(src.length);
@@ -160,7 +461,7 @@ function maxFilter(src, w, h, r, horizontal) {
160
461
  }
161
462
  return out;
162
463
  }
163
- function castSolid(bodies, posed, step) {
464
+ function castSolid(bodies, posed, step, shapes) {
164
465
  const bounds = posed.map((ss) => {
165
466
  const b = ss.map(shapeBounds);
166
467
  return [Math.min(...b.map((q) => q[0])), Math.min(...b.map((q) => q[1])), Math.max(...b.map((q) => q[2])), Math.max(...b.map((q) => q[3])), Math.min(...b.map((q) => q[4])), Math.max(...b.map((q) => q[5]))];
@@ -176,9 +477,38 @@ function castSolid(bodies, posed, step) {
176
477
  const sdfs = bodies.map((b, i) => bodySdf(posed[i], b.blend));
177
478
  const minStep = step * 0.25;
178
479
  const vals = [], depths = [];
480
+ // Inside cells of the drawn bodies whose value is not worked out yet (only their sign and depth):
481
+ // how deep a ray goes counts only near the outline (see `finishBody`).
482
+ const rough = [], resumeZ = [], resumeDeep = [];
483
+ const memos = [];
484
+ /** The column at a grid cell in a body's memo (−1: none). */
485
+ const memoCell = (memo, X, Y) => {
486
+ if (!memo)
487
+ return -1;
488
+ const mi = (X - memo.ox) / step, mj = (Y - memo.oy) / step;
489
+ return mi >= 0 && mj >= 0 && mi < memo.w && mj < memo.h && Number.isInteger(mi) && Number.isInteger(mj) ? mj * memo.w + mi : -1;
490
+ };
491
+ /**
492
+ * How far (cells) from where a drawn body's field changes sign its values are read: the corners of
493
+ * a traced outline's edges (one cell), and for a `seamless` body the neighbours its seam test reads
494
+ * the outline's direction from (two more).
495
+ */
496
+ const reach = (k) => (bodies[k].seamless ? 3 : 1);
497
+ /**
498
+ * Casts a body on the grid (an occluder only on the cells of `only`). Exact where it counts: a
499
+ * column the body surely misses, farther from it than its values are read (`reach`), counts only
500
+ * by its sign and is left out, as the columns outside the body's box are; an occluder's columns
501
+ * count only by their sign and depth, so a miss is left out anywhere and a hit ends its ray; a
502
+ * drawn body's hit ends its ray too, its depth inside worked out later where it counts
503
+ * (`finishBody`).
504
+ */
179
505
  const castBody = (k, only) => {
180
- const f = sdfs[k], [bx0, by0, bx1, by1, zmin, zmax] = bounds[k];
506
+ const [bx0, by0, bx1, by1, zmin, zmax] = bounds[k];
507
+ const memo = (memos[k] = columnMemo(shapes[k], bodies[k].blend, step, bounds[k]));
508
+ let f;
181
509
  const val = new Float32Array(n).fill(step * 4), depth = new Float32Array(n).fill(-Infinity);
510
+ const rk = only ? undefined : (rough[k] = new Uint8Array(n));
511
+ const rz = only ? undefined : (resumeZ[k] = new Float64Array(n)), rd = only ? undefined : (resumeDeep[k] = new Float64Array(n));
182
512
  for (let j = 0; j < g.h; j++) {
183
513
  const Y = g.y0 + j * step;
184
514
  if (Y < by0 - step * 2 || Y > by1 + step * 2)
@@ -190,55 +520,107 @@ function castSolid(bodies, posed, step) {
190
520
  const c = j * g.w + i;
191
521
  if (only && !only[c])
192
522
  continue;
193
- let Z = zmax + 1, closest = Infinity, deepest = 0, entered = false;
194
- while (Z > zmin - 1) {
195
- const d = f(X, Y, Z);
196
- if (d < closest)
197
- closest = d;
198
- if (d <= 0) {
199
- if (!entered) {
200
- entered = true;
201
- depth[c] = Z;
202
- }
203
- if (-d > deepest)
204
- deepest = -d;
205
- Z -= Math.max(minStep, -d * 0.8);
523
+ const m = memoCell(memo, X, Y);
524
+ // (A drawn body takes a column known only by its sign when it is inside: its value there is
525
+ // worked out later if it counts. A miss known only by its sign may lie near its outline.)
526
+ if (m >= 0 && (memo.done[m] === FULL || (memo.done[m] === SIGN && (only || memo.val[m] < 0)))) {
527
+ val[c] = memo.val[m];
528
+ depth[c] = memo.depth[m];
529
+ if (rk && memo.done[m] === SIGN && val[c] < 0) {
530
+ rk[c] = 1;
531
+ rz[c] = memo.rz[m];
532
+ rd[c] = memo.rd[m];
206
533
  }
207
- else {
208
- if (entered)
209
- break;
210
- Z -= Math.max(minStep, d * 0.9);
534
+ continue;
535
+ }
536
+ f ??= new ColumnSdf(posed[k], bodies[k].blend);
537
+ f.column(X, Y);
538
+ let done = FULL;
539
+ if (f.miss > (only ? 0 : reach(k) * step * 1.001))
540
+ done = only ? SIGN : FULL;
541
+ else {
542
+ val[c] = f.march(zmin, zmax, minStep, true);
543
+ // (Left at −Infinity where the ray missed.)
544
+ if (f.hitZ !== -Infinity)
545
+ depth[c] = f.hitZ;
546
+ if (val[c] < 0) {
547
+ done = SIGN;
548
+ if (rk) {
549
+ rk[c] = 1;
550
+ rz[c] = f.resumeZ;
551
+ rd[c] = f.resumeDeep;
552
+ }
211
553
  }
212
554
  }
213
- val[c] = entered ? -Math.max(deepest, 0.05) : closest;
555
+ if (m >= 0) {
556
+ memo.val[m] = val[c];
557
+ memo.depth[m] = depth[c];
558
+ memo.done[m] = done;
559
+ memo.rz[m] = f.resumeZ;
560
+ memo.rd[m] = f.resumeDeep;
561
+ }
214
562
  }
215
563
  }
216
- // (The crossing put on the silhouette: a clean outline.)
217
- if (!only)
564
+ if (rk) {
565
+ // (The crossing put on the silhouette: a clean outline. An inside cell next to an outside one
566
+ // gets its value from that one, whatever its own.)
218
567
  refineEdges(val, g.w, g.h, step);
568
+ for (let j = 0; j < g.h; j++)
569
+ for (let i = 0; i < g.w; i++) {
570
+ const c = j * g.w + i;
571
+ if (rk[c] && (i === 0 || j === 0 || i === g.w - 1 || j === g.h - 1 || val[c - 1] >= 0 || val[c + 1] >= 0 || val[c - g.w] >= 0 || val[c + g.w] >= 0))
572
+ rk[c] = 0;
573
+ }
574
+ }
219
575
  vals[k] = val;
220
576
  depths[k] = depth;
221
577
  };
578
+ /**
579
+ * The inside values of a drawn body that count: within `reach` of where it is not the nearest body
580
+ * (its outline is traced from `field`, outside there), each worked out in full as the ray caster
581
+ * gives it (−how deep the ray goes).
582
+ */
583
+ const finishBody = (k, near) => {
584
+ const rk = rough[k], val = vals[k], memo = memos[k];
585
+ const zmin = bounds[k][4];
586
+ let f;
587
+ for (let c = 0; c < n; c++) {
588
+ if (!rk[c] || !near[c])
589
+ continue;
590
+ const X = g.x0 + (c % g.w) * step, Y = g.y0 + Math.floor(c / g.w) * step;
591
+ const m = memoCell(memo, X, Y);
592
+ if (m >= 0 && memo.done[m] === FULL) {
593
+ val[c] = memo.val[m];
594
+ continue;
595
+ }
596
+ f ??= new ColumnSdf(posed[k], bodies[k].blend);
597
+ f.column(X, Y);
598
+ val[c] = f.resume(zmin, minStep, resumeZ[k][c], resumeDeep[k][c]);
599
+ if (m >= 0) {
600
+ memo.val[m] = val[c];
601
+ memo.depth[m] = depths[k][c];
602
+ memo.done[m] = FULL;
603
+ }
604
+ }
605
+ };
222
606
  for (const k of drawn)
223
607
  castBody(k);
224
608
  // Occluders only where a drawn body is (and a few cells around: where a seam is looked for).
225
- const covered = new Uint8Array(n);
609
+ // (Every cell within R cells — a square — of an inside cell: a dilation, done along rows, then columns.)
226
610
  const R = 7;
611
+ const seed = new Uint8Array(n);
227
612
  for (const k of drawn)
228
- for (let c = 0; c < n; c++) {
229
- if (vals[k][c] >= 0)
230
- continue;
231
- const ci = c % g.w, cj = Math.floor(c / g.w);
232
- for (let dj = -R; dj <= R; dj++)
233
- for (let di = -R; di <= R; di++) {
234
- const i = ci + di, j = cj + dj;
235
- if (i >= 0 && j >= 0 && i < g.w && j < g.h)
236
- covered[j * g.w + i] = 1;
237
- }
238
- }
613
+ for (let c = 0; c < n; c++)
614
+ if (vals[k][c] < 0)
615
+ seed[c] = 1;
616
+ const covered = dilate(dilate(seed, g.w, g.h, R, true), g.w, g.h, R, false);
617
+ // (An occluder counts only where a drawn body is — which one is nearest there — unless its edge is
618
+ // grown by a `margin` or a `seamless` body's line looks for it around its outline: then over all
619
+ // of `covered`, as everything reads it.)
620
+ const nearOutline = drawn.some((k) => bodies[k].seamless);
239
621
  bodies.forEach((b, k) => {
240
622
  if (!b.fill && posed[k].length)
241
- castBody(k, covered);
623
+ castBody(k, b.margin || nearOutline ? covered : seed);
242
624
  });
243
625
  // An occluder's `margin`: its edge grown in the picture, each new cell as deep as its edge there
244
626
  // (the depth of its silhouette cells around it: the middle of the volume, not its near surface)
@@ -343,6 +725,14 @@ function castSolid(bodies, posed, step) {
343
725
  };
344
726
  };
345
727
  const fills = [], lines = [];
728
+ for (const k of drawn) {
729
+ // (Where body k is not the nearest, its field is outside: its inside values near there count.)
730
+ const notK = new Uint8Array(n);
731
+ for (let c = 0; c < n; c++)
732
+ if (nearest[c] !== k)
733
+ notK[c] = 1;
734
+ finishBody(k, dilate(dilate(notK, g.w, g.h, reach(k), true), g.w, g.h, reach(k), false));
735
+ }
346
736
  for (const k of drawn) {
347
737
  const b = bodies[k], val = vals[k], f = sdfs[k];
348
738
  const field = new Float32Array(n);
@@ -361,7 +751,7 @@ function castSolid(bodies, posed, step) {
361
751
  continue;
362
752
  const X = g.x0 + (c % g.w) * step, Y = g.y0 + Math.floor(c / g.w) * step, Z = depths[k][c];
363
753
  const nx = f(X + e, Y, Z) - f(X - e, Y, Z), ny = f(X, Y + e, Z) - f(X, Y - e, Z), nz = f(X, Y, Z + e) - f(X, Y, Z - e);
364
- const l = Math.hypot(nx, ny, nz) || 1;
754
+ const l = hypot3(nx, ny, nz) || 1;
365
755
  light[c] = ((nx * LIGHT[0] + ny * LIGHT[1] + nz * LIGHT[2]) / l - SHADE_AT) * 20;
366
756
  }
367
757
  const d = fieldPath(light, g, 6);