open-bot-avatar 0.1.2 → 0.2.1
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.
- package/README.md +3 -2
- package/dist/contour.d.ts +6 -1
- package/dist/contour.d.ts.map +1 -1
- package/dist/contour.js +83 -7
- package/dist/contour.js.map +1 -1
- package/dist/dom/mount.d.ts +8 -2
- package/dist/dom/mount.d.ts.map +1 -1
- package/dist/dom/mount.js +55 -11
- package/dist/dom/mount.js.map +1 -1
- package/dist/dom/paint.d.ts +49 -0
- package/dist/dom/paint.d.ts.map +1 -0
- package/dist/dom/paint.js +183 -0
- package/dist/dom/paint.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/path.d.ts +6 -0
- package/dist/path.d.ts.map +1 -1
- package/dist/path.js +13 -3
- package/dist/path.js.map +1 -1
- package/dist/render.d.ts +32 -6
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +39 -8
- package/dist/render.js.map +1 -1
- package/dist/validate.js +2 -2
- package/dist/validate.js.map +1 -1
- package/dist/volume.d.ts +2 -0
- package/dist/volume.d.ts.map +1 -1
- package/dist/volume.js +35 -14
- package/dist/volume.js.map +1 -1
- package/package.json +1 -1
- package/src/contour.ts +74 -7
- package/src/dom/mount.ts +62 -13
- package/src/dom/paint.ts +215 -0
- package/src/index.ts +1 -1
- package/src/path.ts +14 -3
- package/src/render.ts +57 -11
- package/src/validate.ts +2 -2
- 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
|
|
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
|
|
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 =
|
|
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 =
|
|
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
|
-
/**
|
|
339
|
-
|
|
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
|
-
|
|
349
|
-
|
|
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)));
|