open-bot-avatar 0.2.0 → 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 (69) hide show
  1. package/README.md +25 -6
  2. package/dist/animator.d.ts +9 -4
  3. package/dist/animator.d.ts.map +1 -1
  4. package/dist/animator.js +32 -17
  5. package/dist/animator.js.map +1 -1
  6. package/dist/color.d.ts +4 -0
  7. package/dist/color.d.ts.map +1 -1
  8. package/dist/color.js +45 -0
  9. package/dist/color.js.map +1 -1
  10. package/dist/contour.d.ts +6 -1
  11. package/dist/contour.d.ts.map +1 -1
  12. package/dist/contour.js +83 -7
  13. package/dist/contour.js.map +1 -1
  14. package/dist/dom/mount.d.ts +8 -2
  15. package/dist/dom/mount.d.ts.map +1 -1
  16. package/dist/dom/mount.js +29 -8
  17. package/dist/dom/mount.js.map +1 -1
  18. package/dist/dom/paint.d.ts.map +1 -1
  19. package/dist/dom/paint.js +18 -5
  20. package/dist/dom/paint.js.map +1 -1
  21. package/dist/effects.d.ts +82 -0
  22. package/dist/effects.d.ts.map +1 -0
  23. package/dist/effects.js +352 -0
  24. package/dist/effects.js.map +1 -0
  25. package/dist/eyes.d.ts +29 -0
  26. package/dist/eyes.d.ts.map +1 -0
  27. package/dist/eyes.js +128 -0
  28. package/dist/eyes.js.map +1 -0
  29. package/dist/index.d.ts +3 -3
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/mood.d.ts +10 -5
  34. package/dist/mood.d.ts.map +1 -1
  35. package/dist/mood.js +3 -2
  36. package/dist/mood.js.map +1 -1
  37. package/dist/motion.d.ts +63 -10
  38. package/dist/motion.d.ts.map +1 -1
  39. package/dist/motion.js +239 -41
  40. package/dist/motion.js.map +1 -1
  41. package/dist/path.d.ts +6 -0
  42. package/dist/path.d.ts.map +1 -1
  43. package/dist/path.js +13 -3
  44. package/dist/path.js.map +1 -1
  45. package/dist/render.d.ts +68 -4
  46. package/dist/render.d.ts.map +1 -1
  47. package/dist/render.js +265 -34
  48. package/dist/render.js.map +1 -1
  49. package/dist/types.d.ts +35 -7
  50. package/dist/types.d.ts.map +1 -1
  51. package/dist/volume.d.ts +2 -0
  52. package/dist/volume.d.ts.map +1 -1
  53. package/dist/volume.js +35 -14
  54. package/dist/volume.js.map +1 -1
  55. package/package.json +1 -1
  56. package/src/animator.ts +40 -20
  57. package/src/color.ts +48 -0
  58. package/src/contour.ts +74 -7
  59. package/src/dom/mount.ts +38 -11
  60. package/src/dom/paint.ts +17 -5
  61. package/src/effects.ts +405 -0
  62. package/src/eyes.ts +157 -0
  63. package/src/index.ts +3 -3
  64. package/src/mood.ts +13 -7
  65. package/src/motion.ts +270 -48
  66. package/src/path.ts +14 -3
  67. package/src/render.ts +335 -42
  68. package/src/types.ts +42 -7
  69. package/src/volume.ts +43 -14
package/src/volume.ts CHANGED
@@ -99,8 +99,6 @@ export const EYE_BOOST_PAD = 3;
99
99
  * Advisory (validate.sh --strict); v1 imports below it still render.
100
100
  */
101
101
  export const FACE_ROOM_TARGET = 4.5;
102
- /** Steepness of the outline term in the contour field, so cross-sections hug the outline. */
103
- const OUTLINE_SLOPE = 4;
104
102
  const MAX_X_SLICES = 40;
105
103
  /**
106
104
  * Thin regions get refinement slices (see thinBoxes): where no inscribed circle
@@ -115,13 +113,17 @@ const THIN_MARGIN = 2;
115
113
  /** Depth spacing of refinement slices, units. */
116
114
  const FINE_Z = 0.6;
117
115
 
118
- function segDist(px: number, py: number, ax: number, ay: number, bx: number, by: number): number {
116
+ function segDist2(px: number, py: number, ax: number, ay: number, bx: number, by: number): number {
119
117
  const dx = bx - ax, dy = by - ay;
120
118
  const len = dx * dx + dy * dy;
121
119
  let t = len > 0 ? ((px - ax) * dx + (py - ay) * dy) / len : 0;
122
120
  t = t < 0 ? 0 : t > 1 ? 1 : t;
123
121
  const ex = ax + t * dx - px, ey = ay + t * dy - py;
124
- return Math.sqrt(ex * ex + ey * ey);
122
+ return ex * ex + ey * ey;
123
+ }
124
+
125
+ function segDist(px: number, py: number, ax: number, ay: number, bx: number, by: number): number {
126
+ return Math.sqrt(segDist2(px, py, ax, ay, bx, by));
125
127
  }
126
128
 
127
129
  /** Distance from a point to the nearest edge of the polygons. */
@@ -150,6 +152,15 @@ export function erosionAt(part: Pick<VolumePart, 'depth' | 'rxy' | 'rz'>, z: num
150
152
  return part.rxy * (1 - Math.sqrt(Math.max(0, 1 - Math.min(1, u) ** 2)));
151
153
  }
152
154
 
155
+ /** How far inside the outline an inflated part's surface reaches `level` (inverse of surfaceZ). */
156
+ export function insetAt(part: Pick<VolumePart, 'depth' | 'rxy' | 'rz'>, level: number): number {
157
+ const half = part.depth / 2;
158
+ if (part.rxy <= 0 || part.rz <= 0 || level <= half - part.rz) return 0;
159
+ if (level >= half) return part.rxy;
160
+ const s = (level - (half - part.rz)) / part.rz;
161
+ return part.rxy * (1 - Math.sqrt(Math.max(0, 1 - s * s)));
162
+ }
163
+
153
164
  /** Front surface depth of an inflated part at a point `dist` units inside the outline. */
154
165
  export function surfaceZ(part: Pick<VolumePart, 'depth' | 'rxy' | 'rz'>, dist: number): number {
155
166
  const half = part.depth / 2;
@@ -248,7 +259,7 @@ interface Grid { x0: number; y0: number; step: number; nx: number; ny: number; s
248
259
 
249
260
  /** Edges bucketed on a coarse grid, for nearest-edge queries that only visit nearby buckets. */
250
261
  class EdgeIndex {
251
- private readonly cell = 3;
262
+ private readonly cell = 6;
252
263
  private readonly buckets = new Map<number, number[]>();
253
264
  private readonly e: number[] = [];
254
265
  private readonly ox: number;
@@ -291,15 +302,15 @@ class EdgeIndex {
291
302
  const list = this.buckets.get(cy * this.span + cx);
292
303
  if (!list) continue;
293
304
  for (const k of list) {
294
- const d = segDist(x, y, e[k], e[k + 1], e[k + 2], e[k + 3]);
305
+ const d = segDist2(x, y, e[k], e[k + 1], e[k + 2], e[k + 3]);
295
306
  if (d < best) best = d;
296
307
  }
297
308
  }
298
309
  }
299
- // Every bucket outside ring r is at least r cells away.
300
- if (best <= r * this.cell) break;
310
+ // Every bucket outside ring r is at least r cells away (best is squared).
311
+ if (best <= (r * this.cell) ** 2) break;
301
312
  }
302
- return best;
313
+ return Math.sqrt(best);
303
314
  }
304
315
  }
305
316
 
@@ -335,8 +346,16 @@ interface CellBox { i0: number; i1: number; j0: number; j1: number }
335
346
  /** A thin region's box, and the highest surface over the thin samples themselves. */
336
347
  interface ThinBox extends CellBox { top: number }
337
348
 
338
- /** Cross-section {h ≥ level} clipped to the outline (and to `box`, if given), as loops. */
339
- function levelLoops(g: Grid, level: number, box?: CellBox): Point[][] {
349
+ /**
350
+ * A part's cross-section at `level` as a field that is positive inside it, zero on its edge and
351
+ * roughly a distance in viewBox units: smooth across the rim, so contours interpolated between grid
352
+ * samples follow the real edge instead of the grid. (Height itself is steep at the rim and zero
353
+ * outside, and contouring it left the outline faceted and stair-stepped.)
354
+ */
355
+ type LevelField = (x: number, y: number, sd: number, level: number) => number;
356
+
357
+ /** Cross-section at `level` clipped to the outline (and to `box`, if given), as loops. */
358
+ function levelLoops(g: Grid, level: number, field: LevelField, box?: CellBox): Point[][] {
340
359
  // A box is contoured on its own samples plus one ring outside it, where the clip is negative.
341
360
  const i0 = box ? Math.max(0, box.i0 - 1) : 0, i1 = box ? Math.min(g.nx - 1, box.i1 + 1) : g.nx - 1;
342
361
  const j0 = box ? Math.max(0, box.j0 - 1) : 0, j1 = box ? Math.min(g.ny - 1, box.j1 + 1) : g.ny - 1;
@@ -345,8 +364,10 @@ function levelLoops(g: Grid, level: number, box?: CellBox): Point[][] {
345
364
  for (let j = 0; j < ny; j++) {
346
365
  for (let i = 0; i < nx; i++) {
347
366
  const k = (j + j0) * g.nx + i + i0;
348
- let v = Math.min(g.h[k] - level, OUTLINE_SLOPE * g.sd[k]);
349
- if (box) v = Math.min(v, OUTLINE_SLOPE * g.step * Math.min(i + i0 - box.i0, box.i1 - i - i0, j + j0 - box.j0, box.j1 - j - j0));
367
+ const sd = g.sd[k];
368
+ // Both terms are continuous across the outline, so the min is too.
369
+ let v = Math.min(field(g.x0 + (i + i0) * g.step, g.y0 + (j + j0) * g.step, sd, level), sd);
370
+ if (box) v = Math.min(v, g.step * Math.min(i + i0 - box.i0, box.i1 - i - i0, j + j0 - box.j0, box.j1 - j - j0));
350
371
  values[j * nx + i] = v;
351
372
  }
352
373
  }
@@ -501,6 +522,7 @@ export function buildVolume(def: AvatarDefinition, volume = 1, body: BodyAnalysi
501
522
 
502
523
  let thickness: number, depth: number, rxy = 0, rz = 0;
503
524
  let heightOf: (x: number, y: number, sd: number) => number;
525
+ let levelField: LevelField;
504
526
  if (kind === 'inflate' || !profile) {
505
527
  const coarse = sampleGrid(polys, b, Math.max(step, 0.6), () => 0);
506
528
  thickness = 2 * coarse.sd.reduce((m, d) => Math.max(m, d), 0);
@@ -509,6 +531,8 @@ export function buildVolume(def: AvatarDefinition, volume = 1, body: BodyAnalysi
509
531
  rz = (depth / 2) * 0.85;
510
532
  const shape = { depth, rxy, rz };
511
533
  heightOf = (_x, _y, sd) => surfaceZ(shape, sd);
534
+ // Height grows with distance from the outline, so a level is reached at a fixed inset.
535
+ levelField = (_x, _y, sd, level) => sd - insetAt(shape, level);
512
536
  } else {
513
537
  const p = profile;
514
538
  const axis = kind === 'revolve-x' ? (xAxis as number) : center[0];
@@ -518,6 +542,11 @@ export function buildVolume(def: AvatarDefinition, volume = 1, body: BodyAnalysi
518
542
  heightOf = kind === 'revolve-x'
519
543
  ? (x, y) => v * Math.sqrt(Math.max(0, radiusAt(p, x) ** 2 - (y - axis) ** 2))
520
544
  : (x, y) => v * Math.sqrt(Math.max(0, radiusAt(p, y) ** 2 - (x - axis) ** 2));
545
+ // Height ≥ level where t² ≤ R² - (level / v)²: that difference over 2R is about a distance.
546
+ levelField = (x, y, _sd, level) => {
547
+ const r = radiusAt(p, kind === 'revolve-x' ? x : y), t = kind === 'revolve-x' ? y - axis : x - axis;
548
+ return (r * r - t * t - (level / Math.max(v, 1e-6)) ** 2) / (2 * Math.max(r, 0.5));
549
+ };
521
550
  }
522
551
  const height = (x: number, y: number) =>
523
552
  isInside(polys, x, y) ? heightOf(x, y, kind === 'inflate' ? edgeDistance(polys, x, y) : 0) : 0;
@@ -537,7 +566,7 @@ export function buildVolume(def: AvatarDefinition, volume = 1, body: BodyAnalysi
537
566
  // Inflated parts keep a straight wall up to half - rz; one slice at its end carries it.
538
567
  const ls = kind === 'inflate' ? [half - rz, ...levels(half - rz, rz, steps)] : levels(0, half, steps);
539
568
  const addLevel = (level: number, box?: CellBox) => {
540
- const loops = levelLoops(g, level, box);
569
+ const loops = levelLoops(g, level, levelField, box);
541
570
  if (!loops.length) return false;
542
571
  const sample = loops.flat().filter((_, k) => k % 3 === 0);
543
572
  const inset = Math.min(...sample.map(([x, y]) => edgeDistance(polys, x, y)));