@rig-c/parts 1.83.2 → 1.85.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/cli.ts +5 -2
- package/docs/AUTHORING.md +240 -0
- package/docs/STABILITY.md +32 -1
- package/package.json +1 -1
- package/src/artjoints.ts +94 -0
- package/src/assemble.ts +42 -3
- package/src/build.ts +19 -1
- package/src/check.ts +72 -6
- package/src/cutmaterial.ts +285 -0
- package/src/cutmaterialio.ts +63 -0
- package/src/requirements.ts +15 -2
- package/src/rig.ts +4 -3
- package/src/setuppainting.ts +126 -0
- package/src/turn.ts +4 -0
- package/src/turncoverage.ts +156 -0
- package/src/turnfield.ts +49 -0
package/src/requirements.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { setupPaintingProblems, type SetupPainting } from './setuppainting.ts';
|
|
2
|
+
import { jointMaterialProblems, type JointMaterial } from './cutmaterial.ts';
|
|
3
|
+
import { fixedSkeletonProblems, type FixedSkeleton } from './artjoints.ts';
|
|
1
4
|
/**
|
|
2
5
|
* What a scene requires of a rig's motion, declared in a file `check` is given
|
|
3
6
|
* (`--requirements`, issue #93, increment 2 of #87) and measured from the
|
|
@@ -190,6 +193,12 @@ export interface RequirementsFile {
|
|
|
190
193
|
* not declared: no fold in a named animation is meant.
|
|
191
194
|
*/
|
|
192
195
|
may_fold: MayFold[];
|
|
196
|
+
/** Issue #880: an explicit fixed-skeleton art comparison, absent on other rigs. */
|
|
197
|
+
fixed_skeleton?: FixedSkeleton;
|
|
198
|
+
/** Issue #887: authored source-painting limits over native renderer setup. */
|
|
199
|
+
setup_painting?: SetupPainting;
|
|
200
|
+
/** Explicit source-support declarations (#888), absent otherwise. */
|
|
201
|
+
cut_material?: JointMaterial[];
|
|
193
202
|
}
|
|
194
203
|
|
|
195
204
|
/** One declared fold (issue #320): a slot, a named animation, and why the fold is the art's. */
|
|
@@ -280,7 +289,10 @@ export function readRequirements(path: string): RequirementsFile {
|
|
|
280
289
|
problems.push({ code: 'REQUIREMENTS_SELF_TARGET', object: `${label} field "target"`, detail });
|
|
281
290
|
};
|
|
282
291
|
if (raw.spec !== REQUIREMENTS_SPEC) field(`${at} field "spec"`, `is ${JSON.stringify(raw.spec)}; ${JSON.stringify(REQUIREMENTS_SPEC)} is required`);
|
|
283
|
-
for (const k of Object.keys(raw)) if (!['spec', 'fps', 'targets', 'requirements', ...EXCEPTION_LISTS, 'may_fold'].includes(k) && !freeKey(k, raw[k])) field(`${at} field "${k}"`, 'is not a field this format reads (spec, fps, targets, requirements, not_planted, not_swinging, rises, rests, may_fold; note or *_note as a string; x-<name> as a record)');
|
|
292
|
+
for (const k of Object.keys(raw)) if (!['spec', 'fps', 'targets', 'requirements', ...EXCEPTION_LISTS, 'may_fold', 'fixed_skeleton', 'cut_material', 'setup_painting'].includes(k) && !freeKey(k, raw[k])) field(`${at} field "${k}"`, 'is not a field this format reads (spec, fps, targets, requirements, not_planted, not_swinging, rises, rests, may_fold, fixed_skeleton, cut_material, setup_painting; note or *_note as a string; x-<name> as a record)');
|
|
293
|
+
if ('setup_painting' in raw) problems.push(...setupPaintingProblems(raw.setup_painting, `${at} setup_painting`));
|
|
294
|
+
if ('cut_material' in raw) problems.push(...jointMaterialProblems(raw.cut_material, `${at} cut_material`));
|
|
295
|
+
if ('fixed_skeleton' in raw) problems.push(...fixedSkeletonProblems(raw.fixed_skeleton, `${at} fixed_skeleton`));
|
|
284
296
|
let fps = NaN;
|
|
285
297
|
if (!(finite(raw.fps) && raw.fps > 0)) field(`${at} field "fps"`, `is ${JSON.stringify(raw.fps)}; a positive number of frames per second is required — every named animation is sampled at it, and there is no default`);
|
|
286
298
|
else fps = raw.fps;
|
|
@@ -464,7 +476,7 @@ export function readRequirements(path: string): RequirementsFile {
|
|
|
464
476
|
}
|
|
465
477
|
}
|
|
466
478
|
refuseIfAny(problems);
|
|
467
|
-
return { path: at, fps, targets, requirements, not_planted: lists.not_planted, not_swinging: lists.not_swinging, rises: lists.rises, rests: lists.rests, may_fold: mayFold };
|
|
479
|
+
return { path: at, fps, targets, requirements, not_planted: lists.not_planted, not_swinging: lists.not_swinging, rises: lists.rises, rests: lists.rests, may_fold: mayFold, ...(raw.fixed_skeleton === undefined ? {} : { fixed_skeleton: raw.fixed_skeleton as FixedSkeleton }), ...(raw.cut_material === undefined ? {} : { cut_material: raw.cut_material as JointMaterial[] }), ...(raw.setup_painting === undefined ? {} : { setup_painting: raw.setup_painting as SetupPainting }) };
|
|
468
480
|
}
|
|
469
481
|
|
|
470
482
|
// ---------------------------------------------------------------------------
|
|
@@ -542,6 +554,7 @@ export function resolveRequirements(file: RequirementsFile, rig: Record<string,
|
|
|
542
554
|
const target = (object: string, t: TargetRef): void => {
|
|
543
555
|
if ('bone' in t) bone(`${object} target`, t.bone);
|
|
544
556
|
};
|
|
557
|
+
for (const j of file.fixed_skeleton?.joints ?? []) bone(`fixed_skeleton joint "${j.name}"`, j.bone);
|
|
545
558
|
for (const r of file.requirements) {
|
|
546
559
|
const object = `requirement "${r.name}" (${r.kind})`;
|
|
547
560
|
animation(object, r.animation);
|
package/src/rig.ts
CHANGED
|
@@ -3668,6 +3668,7 @@ export function buildWaistPairSource(
|
|
|
3668
3668
|
hasAlternateFaceSet,
|
|
3669
3669
|
surfaceMeshes: turnSurfaceMeshes(cfg, rig, parts.rig_size),
|
|
3670
3670
|
neighbourMeshes: turnNeighbourMeshes(cfg, rig, parts, new Map(outImages)),
|
|
3671
|
+
coverageMeshes: cfg.turn.body === undefined ? undefined : turnNeighbourMeshes(cfg, rig, parts, new Map(outImages), true),
|
|
3671
3672
|
});
|
|
3672
3673
|
}
|
|
3673
3674
|
bindFaceSurfaceDeform(motion, (merged) => {
|
|
@@ -4206,13 +4207,13 @@ function turnSurfaceMeshes(cfg: CharacterConfig, rig: RigSpec, canvas: readonly
|
|
|
4206
4207
|
}
|
|
4207
4208
|
|
|
4208
4209
|
/** Actual shown support; Core's own grid topology is inspection metadata only. */
|
|
4209
|
-
export function turnNeighbourMeshes(cfg: CharacterConfig, rig: RigSpec, parts: PartsFile, emittedImages: ReadonlyMap<string, Raster
|
|
4210
|
+
export function turnNeighbourMeshes(cfg: CharacterConfig, rig: RigSpec, parts: PartsFile, emittedImages: ReadonlyMap<string, Raster>, coverage = false) {
|
|
4210
4211
|
const meshes: NonNullable<Parameters<typeof buildDepth25DTurn>[2]['neighbourMeshes']>[number][] = [];
|
|
4211
4212
|
const canvas = parts.rig_size;
|
|
4212
4213
|
const aliases = new Map<string, string>((rig.stops ?? []).flatMap(s => s.links.map(link => [`${link}_d`, link])));
|
|
4213
4214
|
const world = computeWorldTransforms(rig.bones.map(b => b.parent === undefined ? b : { ...b, parent: aliases.get(b.parent) ?? b.parent }));
|
|
4214
|
-
for (const part of new Set([...Object.keys(cfg.turn?.roles ?? {}), ...Object.keys(cfg.turn?.body?.roles ?? {})])) {
|
|
4215
|
-
if (cfg.meshes[part] !== undefined && [cfg.turn?.roles?.[part], cfg.turn?.body?.roles?.[part]].includes('surface')) continue;
|
|
4215
|
+
for (const part of new Set([...Object.keys(cfg.turn?.roles ?? {}), ...Object.keys(cfg.turn?.body?.roles ?? {}), ...(coverage ? parts.parts.map(p => p.name) : [])])) {
|
|
4216
|
+
if (!coverage && cfg.meshes[part] !== undefined && [cfg.turn?.roles?.[part], cfg.turn?.body?.roles?.[part]].includes('surface')) continue;
|
|
4216
4217
|
const record = parts.parts.find(p => p.name === part);
|
|
4217
4218
|
if (record === undefined) continue;
|
|
4218
4219
|
const split = cfg.regions[part] !== undefined && cfg.motion.blink?.still?.[part] !== undefined
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** Authored source-painting limits over actual renderer setup pixels (#887). */
|
|
2
|
+
import { refuseIfAny, type Problem } from './errors.ts';
|
|
3
|
+
import type { Raster } from './raster/types.ts';
|
|
4
|
+
|
|
5
|
+
export const SETUP_PAINTING_LINE = 'SETUP_MATCHES_PAINTING';
|
|
6
|
+
export interface SetupPaintingRegion {
|
|
7
|
+
name: string;
|
|
8
|
+
/** Stage pixels, x right/y down: [x, y, width, height], exclusive far edge. */
|
|
9
|
+
box: [number, number, number, number];
|
|
10
|
+
/** Maximum count over the entire named region. Absent means SKIP. */
|
|
11
|
+
max_pixels?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface SetupPainting {
|
|
14
|
+
/** A pixel counts when its maximum RGB difference is strictly greater. */
|
|
15
|
+
rgb_tolerance: number;
|
|
16
|
+
regions: SetupPaintingRegion[];
|
|
17
|
+
}
|
|
18
|
+
const object = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
19
|
+
const integer = (v: unknown): v is number => typeof v === 'number' && Number.isSafeInteger(v);
|
|
20
|
+
const free = (k: string, v: unknown): boolean => (k.startsWith('x-') && k.length > 2) || ((k === 'note' || k.endsWith('_note')) && typeof v === 'string');
|
|
21
|
+
|
|
22
|
+
/** Shape only: no default limit, inferred box, art alignment or slot name. */
|
|
23
|
+
export function setupPaintingProblems(value: unknown, at: string): Problem[] {
|
|
24
|
+
const problems: Problem[] = [];
|
|
25
|
+
const fail = (where: string, detail: string): void => { problems.push({ code: 'REQUIREMENTS_FIELD', object: where, detail }); };
|
|
26
|
+
const keys = (v: Record<string, unknown>, allowed: string[], where: string): void => {
|
|
27
|
+
for (const k of Object.keys(v)) if (!allowed.includes(k) && !free(k, v[k])) fail(`${where}.${k}`, `is unknown; fields are ${allowed.join(', ')}`);
|
|
28
|
+
};
|
|
29
|
+
if (!object(value)) return [{ code: 'REQUIREMENTS_FIELD', object: at, detail: 'requires {rgb_tolerance, regions: [{name, box, max_pixels?}]}' }];
|
|
30
|
+
keys(value, ['rgb_tolerance', 'regions'], at);
|
|
31
|
+
if (!integer(value.rgb_tolerance) || value.rgb_tolerance < 0 || value.rgb_tolerance > 255) fail(`${at}.rgb_tolerance`, 'requires an authored integer from 0 through 255; difference strictly above it counts');
|
|
32
|
+
if (!Array.isArray(value.regions) || value.regions.length === 0) fail(`${at}.regions`, 'requires a nonempty list of named source-grid regions');
|
|
33
|
+
else {
|
|
34
|
+
const names = new Set<string>();
|
|
35
|
+
value.regions.forEach((r, i) => {
|
|
36
|
+
const where = `${at}.regions[${i}]`;
|
|
37
|
+
if (!object(r)) { fail(where, 'requires {name, box, max_pixels?}'); return; }
|
|
38
|
+
keys(r, ['name', 'box', 'max_pixels'], where);
|
|
39
|
+
if (typeof r.name !== 'string' || r.name.trim() === '') fail(`${where}.name`, 'requires a nonempty region name');
|
|
40
|
+
else { if (names.has(r.name)) fail(`${where}.name`, `declares region "${r.name}" twice`); names.add(r.name); }
|
|
41
|
+
if (!Array.isArray(r.box) || r.box.length !== 4 || !r.box.every(integer) || r.box[0] < 0 || r.box[1] < 0 || r.box[2] <= 0 || r.box[3] <= 0 || !Number.isSafeInteger(r.box[0] + r.box[2]) || !Number.isSafeInteger(r.box[1] + r.box[3])) fail(`${where}.box`, 'requires nonnegative integer x/y and positive integer width/height in stage pixels, with finite integer far edges');
|
|
42
|
+
if ('max_pixels' in r && (!integer(r.max_pixels) || r.max_pixels < 0)) fail(`${where}.max_pixels`, 'requires a nonnegative authored integer pixel count, or omission for SKIP');
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return problems;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function setupPaintingBounds(value: SetupPainting, width: number, height: number): Problem[] {
|
|
49
|
+
return value.regions.flatMap(r => r.box[0] + r.box[2] > width || r.box[1] + r.box[3] > height
|
|
50
|
+
? [{ code: 'REQUIREMENTS_FIELD', object: `setup_painting region "${r.name}" box`, detail: `[${r.box.join(', ')}] exceeds the declared ${width}x${height} stage; regions use its native pixel grid, not a fitted render` }] : []);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface SetupPaintingSlot {
|
|
54
|
+
slot: string;
|
|
55
|
+
/** Bounding box of failing pixel centres, in stage-grid pixels. */
|
|
56
|
+
box: [number, number, number, number];
|
|
57
|
+
pixel_count: number;
|
|
58
|
+
}
|
|
59
|
+
export interface SetupPaintingReading extends SetupPaintingRegion {
|
|
60
|
+
status: 'PASS' | 'FAIL' | 'SKIP';
|
|
61
|
+
pixel_count: number;
|
|
62
|
+
limit: number | null;
|
|
63
|
+
slots: SetupPaintingSlot[];
|
|
64
|
+
}
|
|
65
|
+
export interface SetupPaintingLine {
|
|
66
|
+
status: 'PASS' | 'FAIL' | 'SKIP';
|
|
67
|
+
instrument: 'rigc renderer setup textured pieces';
|
|
68
|
+
registration: 'declared stage grid, scale 1, no fitting';
|
|
69
|
+
compositing: 'source-over white, RGBA8';
|
|
70
|
+
figure: 'source figureSilhouette union rendered alpha > 0';
|
|
71
|
+
ownership: 'last actual drawn piece with sampled alpha > 0; uncovered explicit';
|
|
72
|
+
rgb_tolerance: number;
|
|
73
|
+
regions: SetupPaintingReading[];
|
|
74
|
+
reason?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Pixel metric only. Caller supplies the actual renderer's textured setup and
|
|
78
|
+
* its per-pixel drawn-piece owners on precisely the source grid. RGB under
|
|
79
|
+
* zero alpha is ignored: both observations are source-over white first.
|
|
80
|
+
* Region totals count each pixel once, even with translucent contributors.
|
|
81
|
+
*/
|
|
82
|
+
export function setupPaintingLine(
|
|
83
|
+
declared: SetupPainting, source: Raster, setup: Raster, figure: Uint8Array,
|
|
84
|
+
owners: Int32Array, slots: readonly string[], problems: Problem[],
|
|
85
|
+
): SetupPaintingLine {
|
|
86
|
+
refuseIfAny(setupPaintingProblems(declared, 'setup_painting'));
|
|
87
|
+
refuseIfAny(setupPaintingBounds(declared, source.width, source.height));
|
|
88
|
+
const W = source.width, H = source.height, n = W * H;
|
|
89
|
+
if (setup.width !== W || setup.height !== H || figure.length !== n || owners.length !== n || source.data.length !== n * 4 || setup.data.length !== n * 4 || owners.some(i => i < -1 || i >= slots.length)) {
|
|
90
|
+
refuseIfAny([{ code: 'CHECK_SETUP_PAINTING_MEASURED', object: SETUP_PAINTING_LINE, detail: 'requires actual setup pixels, figure domain and renderer owners on the declared native source grid; no fitted registration or guessed owner is substituted' }]);
|
|
91
|
+
}
|
|
92
|
+
const regions = declared.regions.map<SetupPaintingReading>(r => {
|
|
93
|
+
const rows = new Map<string, { pixels: number; x0: number; y0: number; x1: number; y1: number }>();
|
|
94
|
+
let count = 0;
|
|
95
|
+
for (let y = r.box[1]; y < r.box[1] + r.box[3]; y++) for (let x = r.box[0]; x < r.box[0] + r.box[2]; x++) {
|
|
96
|
+
const p = y * W + x, q = p * 4;
|
|
97
|
+
if (figure[p] === 0 && setup.data[q + 3] === 0) continue;
|
|
98
|
+
let delta = 0;
|
|
99
|
+
for (let c = 0; c < 3; c++) {
|
|
100
|
+
const white = (image: Raster): number => Math.round(image.data[q + c] * image.data[q + 3] / 255 + 255 * (1 - image.data[q + 3] / 255));
|
|
101
|
+
delta = Math.max(delta, Math.abs(white(source) - white(setup)));
|
|
102
|
+
}
|
|
103
|
+
if (delta <= declared.rgb_tolerance) continue;
|
|
104
|
+
count++;
|
|
105
|
+
const slot = owners[p] < 0 ? '(uncovered)' : slots[owners[p]], row = rows.get(slot);
|
|
106
|
+
if (row === undefined) rows.set(slot, { pixels: 1, x0: x, y0: y, x1: x + 1, y1: y + 1 });
|
|
107
|
+
else { row.pixels++; row.x0 = Math.min(row.x0, x); row.y0 = Math.min(row.y0, y); row.x1 = Math.max(row.x1, x + 1); row.y1 = Math.max(row.y1, y + 1); }
|
|
108
|
+
}
|
|
109
|
+
const status = r.max_pixels === undefined ? 'SKIP' : count > r.max_pixels ? 'FAIL' : 'PASS';
|
|
110
|
+
const result: SetupPaintingReading = { ...r, status, pixel_count: count, limit: r.max_pixels ?? null,
|
|
111
|
+
slots: [...rows].map(([slot, row]) => ({ slot, box: [row.x0, row.y0, row.x1 - row.x0, row.y1 - row.y0], pixel_count: row.pixels })) };
|
|
112
|
+
if (status === 'FAIL') {
|
|
113
|
+
for (const row of result.slots) problems.push({ code: 'CHECK_SETUP_MATCHES_PAINTING', object: `region "${r.name}" slot "${row.slot}"`, detail: `box [${row.box.join(', ')}], pixelcount ${row.pixel_count}; region pixelcount ${count} > declared limit ${r.max_pixels}, RGB tolerance ${declared.rgb_tolerance}; compare the actual setup to the source painting on the declared stage grid` });
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
});
|
|
117
|
+
const noLimit = regions.some(r => r.status === 'SKIP');
|
|
118
|
+
return { status: regions.some(r => r.status === 'FAIL') ? 'FAIL' : noLimit ? 'SKIP' : 'PASS', instrument: 'rigc renderer setup textured pieces', registration: 'declared stage grid, scale 1, no fitting', compositing: 'source-over white, RGBA8', figure: 'source figureSilhouette union rendered alpha > 0', ownership: 'last actual drawn piece with sampled alpha > 0; uncovered explicit', rgb_tolerance: declared.rgb_tolerance, regions,
|
|
119
|
+
...(noLimit ? { reason: 'a region has no declared max_pixels; it is measured as SKIP, never PASS' } : {}) };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function setupPaintingLines(line: SetupPaintingLine): string[] {
|
|
123
|
+
return [`${SETUP_PAINTING_LINE}: ${line.status}${line.reason === undefined ? '' : ` — ${line.reason}`}; ${line.registration}; ${line.compositing}`,
|
|
124
|
+
...line.regions.flatMap(r => [`${SETUP_PAINTING_LINE}: region "${r.name}" ${r.status} — box [${r.box.join(', ')}], pixelcount ${r.pixel_count}, limit ${r.limit ?? 'undeclared (SKIP)'}, RGB tolerance ${line.rgb_tolerance}`,
|
|
125
|
+
...r.slots.map(s => `${SETUP_PAINTING_LINE}: slot "${s.slot}" region "${r.name}" — box [${s.box.join(', ')}], pixelcount ${s.pixel_count}; region total ${r.pixel_count}, limit ${r.limit ?? 'undeclared (SKIP)'}`)])];
|
|
126
|
+
}
|
package/src/turn.ts
CHANGED
|
@@ -37,6 +37,7 @@ import { turnOverlapOwners } from './turnoverlap.ts';
|
|
|
37
37
|
import { sampleTurnTriangleField, turnTriangleCoordinates } from './turnfield.ts';
|
|
38
38
|
import { PartsError } from './errors.ts';
|
|
39
39
|
import { f32 } from '@rig-c/core/compile';
|
|
40
|
+
import { warnUncoveredBodyTurn } from './turncoverage.ts';
|
|
40
41
|
import { evaluateDeformTransform } from '@rig-c/core/src/deformgen.ts';
|
|
41
42
|
|
|
42
43
|
/**
|
|
@@ -1505,6 +1506,8 @@ export interface Depth25DTurnOptions {
|
|
|
1505
1506
|
}>;
|
|
1506
1507
|
/** Final weighted unit/rear drawings are support only, never surface targets. */
|
|
1507
1508
|
neighbourMeshes?: Depth25DTurnOptions['surfaceMeshes'];
|
|
1509
|
+
/** Shown setup geometry for coverage diagnostics only; never a role opt-in. */
|
|
1510
|
+
coverageMeshes?: Depth25DTurnOptions['surfaceMeshes'];
|
|
1508
1511
|
}
|
|
1509
1512
|
|
|
1510
1513
|
interface Depth25DGroupSpec {
|
|
@@ -2216,4 +2219,5 @@ export function buildDepth25DTurn(
|
|
|
2216
2219
|
}
|
|
2217
2220
|
|
|
2218
2221
|
refuseIfAny(problems);
|
|
2222
|
+
if (opts.warnings !== undefined) warnUncoveredBodyTurn(rig, motion, opts);
|
|
2219
2223
|
}
|
|
@@ -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}` }]); };
|