@rig-c/parts 1.83.1 → 1.84.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.
- package/README.md +30 -143
- package/docs/AUTHORING.md +278 -1
- package/docs/STABILITY.md +22 -2
- package/package.json +2 -1
- package/src/artjoints.ts +94 -0
- package/src/assemble.ts +21 -3
- package/src/authoredunit.ts +61 -0
- package/src/build.ts +4 -0
- package/src/check.ts +20 -4
- package/src/parts.ts +18 -5
- package/src/provenance.ts +4 -1
- package/src/requirements.ts +7 -2
- package/src/rig.ts +4 -3
- package/src/turn.ts +4 -0
- package/src/turncoverage.ts +156 -0
- package/src/turnfield.ts +49 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/** Report missing body-turn declarations from shown setup alpha and emitted fields. */
|
|
2
|
+
import { f32 } from '@rig-c/core/compile';
|
|
3
|
+
import { DRAWN_ALPHA } from '@rig-c/core/src/passthrough.ts';
|
|
4
|
+
import { evaluateDeformTransform, type DeformTransform } from '@rig-c/core/src/deformgen.ts';
|
|
5
|
+
import { computeWorldTransforms, paintingToSetupPoint, toWorld } from './coords.ts';
|
|
6
|
+
import { extendDepthSilhouette, sampleDepthFloat } from './depthsheets.ts';
|
|
7
|
+
import { edgeLength } from './length.ts';
|
|
8
|
+
import type { Depth25DTurnOptions } from './turn.ts';
|
|
9
|
+
import { rasterTurnTriangleOwnership, type TurnTriangleQuery } from './turnfield.ts';
|
|
10
|
+
|
|
11
|
+
type Mesh = NonNullable<Depth25DTurnOptions['coverageMeshes']>[number];
|
|
12
|
+
type Rig = Parameters<typeof computeWorldTransforms>[0];
|
|
13
|
+
interface Animation {
|
|
14
|
+
tracks: Array<{ bone?: string; group?: string; property: string; keys: Array<{ t: number; v?: number[] | Record<string, number[]>; derive?: { degrees: number; carried?: number; depth?: number | Record<string, number> } }> }>;
|
|
15
|
+
deform?: Array<{ slot: string; attachment: string; keys: Array<{ t: number; vertices?: number[]; transform?: DeformTransform }> }>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Warning only: overlap cannot prove the author's intended role. Explicit or inherited body
|
|
20
|
+
* roles already declare coverage; this reader never assigns one. A one-millionth
|
|
21
|
+
* pixel is the producer's six-place encoding unit, not a visual acceptance bar.
|
|
22
|
+
*/
|
|
23
|
+
export function warnUncoveredBodyTurn(
|
|
24
|
+
rig: { bones: Rig; stops?: ReadonlyArray<{ links: readonly string[] }> },
|
|
25
|
+
motion: { groups?: Record<string, string[]>; animations: Record<string, unknown> },
|
|
26
|
+
opts: Depth25DTurnOptions,
|
|
27
|
+
): void {
|
|
28
|
+
const body = opts.turn.body, meshes = opts.coverageMeshes;
|
|
29
|
+
const anim = motion.animations.body_turn as Animation | undefined;
|
|
30
|
+
if (body === undefined || meshes === undefined || anim === undefined || opts.warnings === undefined) return;
|
|
31
|
+
const aliases = new Map<string, string>((rig.stops ?? []).flatMap(s => s.links.map(b => [`${b}_d`, b] as const)));
|
|
32
|
+
const resolve = (b: string) => aliases.get(b) ?? b;
|
|
33
|
+
const bones = rig.bones.map(b => b.parent === undefined ? b : { ...b, parent: resolve(b.parent) });
|
|
34
|
+
const world = computeWorldTransforms(bones), parents = new Map(bones.map(b => [b.name, b.parent]));
|
|
35
|
+
const chain = (bone: string): string[] => {
|
|
36
|
+
const result: string[] = [];
|
|
37
|
+
for (let b: string | undefined = resolve(bone); b !== undefined && !result.includes(b); b = parents.get(b)) result.push(b);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
const root = body.bone ?? (parents.has('chest') ? 'chest' : 'hip');
|
|
41
|
+
const torso = (m: Mesh) => m.weights.every(row => row.filter(w => w.weight > 0).every(w => {
|
|
42
|
+
const path = chain(w.bone);
|
|
43
|
+
return path.includes(root) && !path.some(b => ['head', 'head_ctl', 'neck', 'neck_ctl'].includes(b));
|
|
44
|
+
}));
|
|
45
|
+
const positions = new Map(meshes.map(m => [m, m.weights.map(row => {
|
|
46
|
+
let x = 0, y = 0;
|
|
47
|
+
for (const w of row) { const p = toWorld(world.get(resolve(w.bone))!, w.x, w.y); x += p[0] * w.weight; y += p[1] * w.weight; }
|
|
48
|
+
return [x, y];
|
|
49
|
+
})]));
|
|
50
|
+
const times = anim.tracks[0]?.keys;
|
|
51
|
+
if (times === undefined || times.length === 0) return;
|
|
52
|
+
const endpoints = [times[0], times[times.length - 1]].map(({ t }) => {
|
|
53
|
+
const translations = new Map<string, number[]>();
|
|
54
|
+
for (const tr of anim.tracks) {
|
|
55
|
+
const key = tr.keys.find(k => k.t === t);
|
|
56
|
+
if (key === undefined) continue;
|
|
57
|
+
for (const bone of tr.bone === undefined ? motion.groups?.[tr.group!] ?? [] : [tr.bone]) {
|
|
58
|
+
const derive = key.derive, depth = typeof derive?.depth === 'number' ? derive.depth : derive?.depth?.[bone];
|
|
59
|
+
const stated = Array.isArray(key.v) ? key.v[0] : key.v?.[bone]?.[0];
|
|
60
|
+
const value = f32(stated ?? -(depth! - (derive?.carried ?? 0)) * Math.sin(derive!.degrees * Math.PI / 180));
|
|
61
|
+
const parent = parents.get(bone), frame = parent === undefined ? undefined : world.get(resolve(parent));
|
|
62
|
+
const v = tr.property === 'translatex' ? [value, 0] : [0, value];
|
|
63
|
+
translations.set(bone, frame === undefined ? v : [frame.a * v[0] + frame.b * v[1], frame.c * v[0] + frame.d * v[1]]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return new Map(meshes.map(m => {
|
|
67
|
+
const key = anim.deform?.find(d => d.slot === m.slot && d.attachment === m.attachment)?.keys.find(k => k.t === t);
|
|
68
|
+
let generated: number[] | undefined;
|
|
69
|
+
if (key?.transform !== undefined && m.gridSamples !== undefined) {
|
|
70
|
+
const p = opts.parts.find(p => p.name === m.part)!;
|
|
71
|
+
const ext = extendDepthSilhouette(opts.images.get(p.depth!.image)!, opts.images.get(p.name)!);
|
|
72
|
+
const z = m.gridSamples.map(([x, y]) => { const level = sampleDepthFloat(ext, x - p.x, y - p.y); return (p.depth!.near === 'black' ? 255 - level : level) * p.depth!.zScale / 255; });
|
|
73
|
+
generated = evaluateDeformTransform(key.transform, positions.get(m)!.flat(), f32, `body coverage "${m.part}"`, z).offsets;
|
|
74
|
+
}
|
|
75
|
+
let at = 0;
|
|
76
|
+
const vectors = m.weights.map((row, vi) => {
|
|
77
|
+
let x = 0, y = 0;
|
|
78
|
+
for (const w of row) {
|
|
79
|
+
const frame = world.get(resolve(w.bone))!;
|
|
80
|
+
const dx = f32(key?.vertices?.[at++] ?? 0), dy = f32(key?.vertices?.[at++] ?? 0);
|
|
81
|
+
let wx = generated === undefined ? frame.a * dx + frame.b * dy : generated[2 * vi];
|
|
82
|
+
let wy = generated === undefined ? frame.c * dx + frame.d * dy : generated[2 * vi + 1];
|
|
83
|
+
for (const b of chain(w.bone)) { const v = translations.get(b); if (v !== undefined) { wx += v[0]; wy += v[1]; } }
|
|
84
|
+
x += wx * w.weight; y += wy * w.weight;
|
|
85
|
+
}
|
|
86
|
+
return [x, y];
|
|
87
|
+
});
|
|
88
|
+
return [m, vectors] as const;
|
|
89
|
+
}));
|
|
90
|
+
});
|
|
91
|
+
const opaque = (name: string, x: number, y: number) => {
|
|
92
|
+
const p = opts.parts.find(p => p.name === name)!, image = opts.images.get(name)!;
|
|
93
|
+
const px = Math.floor(x - p.x), py = Math.floor(y - p.y);
|
|
94
|
+
return px >= 0 && py >= 0 && px < image.width && py < image.height && image.data[(py * image.width + px) * 4 + 3] >= DRAWN_ALPHA;
|
|
95
|
+
};
|
|
96
|
+
const sample = (m: Mesh, hit: { ids: number[]; weights: number[] }, endpoint: number): number[] => {
|
|
97
|
+
const field = endpoints[endpoint].get(m)!;
|
|
98
|
+
const [a, b, c] = hit.ids.map(i => field[i]), [u, v] = hit.weights;
|
|
99
|
+
// The same translation-invariant barycentric form as sampleTurnTriangleField.
|
|
100
|
+
return [0, 1].map(axis => c[axis] + u * (a[axis] - c[axis]) + v * (b[axis] - c[axis]));
|
|
101
|
+
};
|
|
102
|
+
const keyedBones = new Set(anim.tracks.flatMap(tr => tr.bone === undefined ? motion.groups?.[tr.group!] ?? [] : [tr.bone]));
|
|
103
|
+
const pairs: Array<{ part: Depth25DTurnOptions['parts'][number]; surface: string; source: Depth25DTurnOptions['parts'][number]; targets: Mesh[]; supports: Mesh[] }> = [];
|
|
104
|
+
for (const part of opts.parts) {
|
|
105
|
+
if (body.roles[part.name] !== undefined) continue;
|
|
106
|
+
const targets = meshes.filter(m => m.part === part.name && torso(m));
|
|
107
|
+
if (targets.length === 0 || targets.every(m => m.weights.every(row => row.filter(w => w.weight > 0).every(w => chain(w.bone).some(b => keyedBones.has(b)))))) continue;
|
|
108
|
+
for (const [surface, role] of Object.entries(body.roles)) {
|
|
109
|
+
if (role !== 'surface') continue;
|
|
110
|
+
const source = opts.parts.find(p => p.name === surface)!;
|
|
111
|
+
const supports = meshes.filter(m => m.part === surface);
|
|
112
|
+
pairs.push({ part, surface, source, targets, supports });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const requested = new Map<Mesh, Map<string, TurnTriangleQuery>>();
|
|
116
|
+
for (const { part, source, targets, supports } of pairs) {
|
|
117
|
+
for (let y = Math.max(part.y, source.y); y < Math.min(part.y + part.h, source.y + source.h); y++) {
|
|
118
|
+
for (let x = Math.max(part.x, source.x); x < Math.min(part.x + part.w, source.x + source.w); x++) {
|
|
119
|
+
if (!opaque(part.name, x + 0.5, y + 0.5) || !opaque(source.name, x + 0.5, y + 0.5)) continue;
|
|
120
|
+
for (const target of targets) {
|
|
121
|
+
const point = paintingToSetupPoint([x + 0.5, y + 0.5], target.canvas, target.canvas), key = `${point[0]},${point[1]}`;
|
|
122
|
+
for (const mesh of [target, ...supports]) {
|
|
123
|
+
let cells = requested.get(mesh);
|
|
124
|
+
if (cells === undefined) { cells = new Map(); requested.set(mesh, cells); }
|
|
125
|
+
if (!cells.has(key)) cells.set(key, { x: point[0], y: point[1], point });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const ownership = new Map([...requested].map(([mesh, cells]) => [mesh, rasterTurnTriangleOwnership(positions.get(mesh)!, mesh.triangles ?? [], [...cells.values()])]));
|
|
132
|
+
for (const { part, surface, source, targets, supports } of pairs) {
|
|
133
|
+
const readings = [0, 1].map(() => ({ relative: 0, point: [0, 0], part: [0, 0], surface: [0, 0] }));
|
|
134
|
+
for (let y = Math.max(part.y, source.y); y < Math.min(part.y + part.h, source.y + source.h); y++) {
|
|
135
|
+
for (let x = Math.max(part.x, source.x); x < Math.min(part.x + part.w, source.x + source.w); x++) {
|
|
136
|
+
const px = x + 0.5, py = y + 0.5;
|
|
137
|
+
if (!opaque(part.name, px, py) || !opaque(surface, px, py)) continue;
|
|
138
|
+
for (const target of targets) for (const support of supports) {
|
|
139
|
+
const p = paintingToSetupPoint([px, py], target.canvas, target.canvas);
|
|
140
|
+
const a = ownership.get(target)?.at(p[0], p[1]) ?? null;
|
|
141
|
+
const b = ownership.get(support)?.at(p[0], p[1]) ?? null;
|
|
142
|
+
if (a === null || b === null) continue;
|
|
143
|
+
for (const [endpoint, reading] of readings.entries()) {
|
|
144
|
+
const av = sample(target, a, endpoint), bv = sample(support, b, endpoint);
|
|
145
|
+
const relative = edgeLength(av[0] - bv[0], av[1] - bv[1]);
|
|
146
|
+
if (edgeLength(bv[0], bv[1]) > 0.000001 && relative > reading.relative) Object.assign(reading, { relative, point: [px, py], part: av, surface: bv });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (readings.some(r => r.relative > 0.000001)) opts.warnings.push({
|
|
152
|
+
code: 'RIG_TURN_BODY_UNCOVERED', object: `part "${part.name}"`,
|
|
153
|
+
detail: `overlaps moved body surface "${surface}" at alpha >= ${DRAWN_ALPHA} without a config.turn.body role; body_turn endpoints ${readings.map((r, i) => `t=${i === 0 ? times[0].t : times[times.length - 1].t}: part displacement [${r.part.join(', ')}], surface displacement [${r.surface.join(', ')}], relative ${r.relative} rig px at setup painting [${r.point.join(', ')}]`).join('; ')}; declare the intended body role and inspect both endpoints; no role or depth is inferred`,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
package/src/turnfield.ts
CHANGED
|
@@ -22,6 +22,55 @@ export function turnTriangleCoordinates(points: ReadonlyArray<readonly number[]>
|
|
|
22
22
|
return null;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/** A requested setup-grid cell and its exact caller-computed world point. */
|
|
26
|
+
export interface TurnTriangleQuery { x: number; y: number; point: readonly number[] }
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Rasterise only requested cells, in triangle order. Sparse sorted sample rows
|
|
30
|
+
* bound the storage to the query set, not a world/canvas rectangle. One adjacent
|
|
31
|
+
* row/cell is conservatively visited at each bbox edge; this is a broad phase,
|
|
32
|
+
* never an epsilon in the barycentric test. The original arithmetic below is
|
|
33
|
+
* the sole acceptance rule, including zero-area skips and first-triangle ties.
|
|
34
|
+
*/
|
|
35
|
+
export function rasterTurnTriangleOwnership(points: ReadonlyArray<readonly number[]>, triangles: readonly number[], queries: readonly TurnTriangleQuery[]) {
|
|
36
|
+
const key = (x: number, y: number) => `${x},${y}`;
|
|
37
|
+
const unique = new Map(queries.map(q => [key(q.x, q.y), q]));
|
|
38
|
+
const byY = new Map<number, TurnTriangleQuery[]>();
|
|
39
|
+
for (const q of unique.values()) {
|
|
40
|
+
const row = byY.get(q.point[1]);
|
|
41
|
+
if (row === undefined) byY.set(q.point[1], [q]); else row.push(q);
|
|
42
|
+
}
|
|
43
|
+
const rows = [...byY].sort((a, b) => a[0] - b[0]).map(([y, cells]) => ({ y, cells: cells.sort((a, b) => a.point[0] - b.point[0]) }));
|
|
44
|
+
const bound = <T>(values: readonly T[], value: number, coordinate: (v: T) => number, upper: boolean): number => {
|
|
45
|
+
let lo = 0, hi = values.length;
|
|
46
|
+
while (lo < hi) { const mid = (lo + hi) >>> 1, n = coordinate(values[mid]); if (n < value || (upper && n === value)) lo = mid + 1; else hi = mid; }
|
|
47
|
+
return lo;
|
|
48
|
+
};
|
|
49
|
+
const owners = new Map<string, { ids: number[]; weights: number[] }>();
|
|
50
|
+
for (let i = 0; i < triangles.length; i += 3) {
|
|
51
|
+
const ids = triangles.slice(i, i + 3), [a, b, c] = ids.map(j => points[j]);
|
|
52
|
+
if (a === undefined || b === undefined || c === undefined) continue;
|
|
53
|
+
const area = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
|
54
|
+
if (area === 0) continue;
|
|
55
|
+
const minX = Math.min(a[0], b[0], c[0]), maxX = Math.max(a[0], b[0], c[0]);
|
|
56
|
+
const minY = Math.min(a[1], b[1], c[1]), maxY = Math.max(a[1], b[1], c[1]);
|
|
57
|
+
const fromY = Math.max(0, bound(rows, minY, r => r.y, false) - 1), toY = Math.min(rows.length, bound(rows, maxY, r => r.y, true) + 1);
|
|
58
|
+
for (let yi = fromY; yi < toY; yi++) {
|
|
59
|
+
const row = rows[yi].cells;
|
|
60
|
+
const fromX = Math.max(0, bound(row, minX, q => q.point[0], false) - 1), toX = Math.min(row.length, bound(row, maxX, q => q.point[0], true) + 1);
|
|
61
|
+
for (let xi = fromX; xi < toX; xi++) {
|
|
62
|
+
const q = row[xi], k = key(q.x, q.y);
|
|
63
|
+
if (owners.has(k)) continue;
|
|
64
|
+
const [x, y] = q.point;
|
|
65
|
+
const cross = (p: readonly number[], r: readonly number[]) => (p[0] - x) * (r[1] - y) - (p[1] - y) * (r[0] - x);
|
|
66
|
+
const weights = [cross(b, c) / area, cross(c, a) / area, cross(a, b) / area];
|
|
67
|
+
if (weights.every(w => w >= 0)) owners.set(k, { ids, weights });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { at: (x: number, y: number) => owners.get(key(x, y)) ?? null };
|
|
72
|
+
}
|
|
73
|
+
|
|
25
74
|
/** Named refusal outside support; never fall back to target depth or nearest geometry. */
|
|
26
75
|
export function sampleTurnTriangleField(field: TurnTriangleField, point: readonly number[], object: string): [number, number] {
|
|
27
76
|
const fail = (detail: string): never => { throw new PartsError([{ code: 'RIG_TURN_SURFACE_OVERLAP_SUPPORT', object, detail: `neighbour "${field.label}" ${detail}` }]); };
|