@ikijs/mcp 0.6.0 → 0.8.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 +31 -3
- package/dist/{chunk-JTK6HWKT.mjs → chunk-JPWDKP43.mjs} +562 -250
- package/dist/chunk-JPWDKP43.mjs.map +1 -0
- package/dist/cli.js +545 -236
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +173 -7
- package/dist/index.d.ts +173 -7
- package/dist/index.js +549 -236
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -1
- package/package.json +2 -2
- package/dist/chunk-JTK6HWKT.mjs.map +0 -1
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/measure-turn.ts
|
|
2
2
|
import path2 from "path";
|
|
3
|
-
import {
|
|
4
|
-
parseIkiModel,
|
|
5
|
-
loadIkiModel,
|
|
6
|
-
IkiFormatError,
|
|
7
|
-
StandardParameter
|
|
8
|
-
} from "@ikijs/format";
|
|
9
|
-
import {
|
|
10
|
-
EditorDocument,
|
|
11
|
-
packAtlas,
|
|
12
|
-
uvRectFor,
|
|
13
|
-
generateIkiFromLayerSet,
|
|
14
|
-
parseLayerRoles
|
|
15
|
-
} from "@ikijs/editor";
|
|
16
3
|
|
|
17
4
|
// src/node-images.ts
|
|
18
5
|
import sharp from "sharp";
|
|
@@ -181,6 +168,9 @@ function detectAlphaBbox(rgba, width, height) {
|
|
|
181
168
|
function cropToBuffer(rgba, width, height, bbox) {
|
|
182
169
|
return sharp(rgba, { raw: { width, height, channels: 4 } }).extract({ left: bbox.x, top: bbox.y, width: bbox.w, height: bbox.h }).png().toBuffer();
|
|
183
170
|
}
|
|
171
|
+
function encodeOverlayPng(filePath, svg) {
|
|
172
|
+
return sharp(filePath, { limitInputPixels: MAX_INPUT_PIXELS }).composite([{ input: Buffer.from(svg) }]).png().toBuffer();
|
|
173
|
+
}
|
|
184
174
|
async function renderAtlasToDataUri(crops, layout, quantizeColors) {
|
|
185
175
|
const byId = /* @__PURE__ */ new Map();
|
|
186
176
|
for (const crop of crops) byId.set(crop.id, crop);
|
|
@@ -210,7 +200,388 @@ async function renderAtlasToDataUri(crops, layout, quantizeColors) {
|
|
|
210
200
|
return `data:image/png;base64,${page.toString("base64")}`;
|
|
211
201
|
}
|
|
212
202
|
|
|
203
|
+
// src/measure-turn.ts
|
|
204
|
+
var DEFAULT_IRIS = { hueMin: 230, hueMax: 300, satMin: 0.22 };
|
|
205
|
+
var IRIS_V_MIN = 0.18;
|
|
206
|
+
var IRIS_V_MAX = 0.95;
|
|
207
|
+
var IRIS_MEAN_V_MIN = 0.32;
|
|
208
|
+
var CLOSE_RADIUS = 3;
|
|
209
|
+
var CLOSE_RADIUS_REFERENCE_PX = 1254;
|
|
210
|
+
var BLOB_MAX_FRAC = 0.12;
|
|
211
|
+
var KEYED_SPAN_MAX_FRAC = 0.95;
|
|
212
|
+
var MIN_AREA_REFERENCE_PIXELS = 1254 * 1254;
|
|
213
|
+
var MIN_AREA = 300;
|
|
214
|
+
var PAIR_DX_MIN_FRAC = 0.08;
|
|
215
|
+
var PAIR_DX_MAX_FRAC = 0.35;
|
|
216
|
+
var PAIR_DY_MAX_FRAC = 0.05;
|
|
217
|
+
var MAX_CANDIDATES = 12;
|
|
218
|
+
var HEAD_BAND = 10;
|
|
219
|
+
var ALPHA_OPAQUE = 128;
|
|
220
|
+
var BG_HUE_MIN = 220;
|
|
221
|
+
var BG_HUE_MAX = 260;
|
|
222
|
+
var BG_SAT_MAX = 0.2;
|
|
223
|
+
var BG_V_MIN = 0.55;
|
|
224
|
+
var BG_V_BLACK = 0.03;
|
|
225
|
+
function rgbToHsv(r, g, b) {
|
|
226
|
+
r /= 255;
|
|
227
|
+
g /= 255;
|
|
228
|
+
b /= 255;
|
|
229
|
+
const max = Math.max(r, g, b);
|
|
230
|
+
const min = Math.min(r, g, b);
|
|
231
|
+
const d = max - min;
|
|
232
|
+
let h = 0;
|
|
233
|
+
if (d > 0) {
|
|
234
|
+
if (max === r) h = (g - b) / d % 6;
|
|
235
|
+
else if (max === g) h = (b - r) / d + 2;
|
|
236
|
+
else h = (r - g) / d + 4;
|
|
237
|
+
h *= 60;
|
|
238
|
+
if (h < 0) h += 360;
|
|
239
|
+
}
|
|
240
|
+
return [h, max === 0 ? 0 : d / max, max];
|
|
241
|
+
}
|
|
242
|
+
function dilate(mask, width, height, r) {
|
|
243
|
+
const out = new Uint8Array(width * height);
|
|
244
|
+
for (let y = 0; y < height; y++) {
|
|
245
|
+
for (let x = 0; x < width; x++) {
|
|
246
|
+
if (!mask[y * width + x]) continue;
|
|
247
|
+
for (let dy = -r; dy <= r; dy++) {
|
|
248
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
249
|
+
const yy = y + dy;
|
|
250
|
+
const xx = x + dx;
|
|
251
|
+
if (yy >= 0 && yy < height && xx >= 0 && xx < width)
|
|
252
|
+
out[yy * width + xx] = 1;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return out;
|
|
258
|
+
}
|
|
259
|
+
function erode(mask, width, height, r) {
|
|
260
|
+
const out = new Uint8Array(width * height);
|
|
261
|
+
for (let y = 0; y < height; y++) {
|
|
262
|
+
for (let x = 0; x < width; x++) {
|
|
263
|
+
if (!mask[y * width + x]) continue;
|
|
264
|
+
let keep = true;
|
|
265
|
+
for (let dy = -r; dy <= r && keep; dy++) {
|
|
266
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
267
|
+
const yy = y + dy;
|
|
268
|
+
const xx = x + dx;
|
|
269
|
+
if (yy < 0 || yy >= height || xx < 0 || xx >= width || !mask[yy * width + xx]) {
|
|
270
|
+
keep = false;
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (keep) out[y * width + x] = 1;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
function closeMask(mask, width, height, r) {
|
|
281
|
+
return erode(dilate(mask, width, height, r), width, height, r);
|
|
282
|
+
}
|
|
283
|
+
function components(mask, width, height, minArea) {
|
|
284
|
+
const seen = new Uint8Array(width * height);
|
|
285
|
+
const out = [];
|
|
286
|
+
for (let i = 0; i < width * height; i++) {
|
|
287
|
+
if (!mask[i] || seen[i]) continue;
|
|
288
|
+
const stack = [i];
|
|
289
|
+
seen[i] = 1;
|
|
290
|
+
let x0 = width;
|
|
291
|
+
let x1 = 0;
|
|
292
|
+
let y0 = height;
|
|
293
|
+
let y1 = 0;
|
|
294
|
+
let n = 0;
|
|
295
|
+
let sx = 0;
|
|
296
|
+
let sy = 0;
|
|
297
|
+
while (stack.length) {
|
|
298
|
+
const p = stack.pop();
|
|
299
|
+
const x = p % width;
|
|
300
|
+
const y = p / width | 0;
|
|
301
|
+
n++;
|
|
302
|
+
sx += x;
|
|
303
|
+
sy += y;
|
|
304
|
+
if (x < x0) x0 = x;
|
|
305
|
+
if (x > x1) x1 = x;
|
|
306
|
+
if (y < y0) y0 = y;
|
|
307
|
+
if (y > y1) y1 = y;
|
|
308
|
+
const neighbours = [];
|
|
309
|
+
if (x > 0) neighbours.push(p - 1);
|
|
310
|
+
if (x < width - 1) neighbours.push(p + 1);
|
|
311
|
+
if (y > 0) neighbours.push(p - width);
|
|
312
|
+
if (y < height - 1) neighbours.push(p + width);
|
|
313
|
+
for (const q of neighbours) {
|
|
314
|
+
if (seen[q] || !mask[q]) continue;
|
|
315
|
+
seen[q] = 1;
|
|
316
|
+
stack.push(q);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (n >= minArea) {
|
|
320
|
+
out.push({
|
|
321
|
+
x0,
|
|
322
|
+
x1,
|
|
323
|
+
y0,
|
|
324
|
+
y1,
|
|
325
|
+
w: x1 - x0 + 1,
|
|
326
|
+
h: y1 - y0 + 1,
|
|
327
|
+
area: n,
|
|
328
|
+
cx: sx / n,
|
|
329
|
+
cy: sy / n
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return out.sort((a, b) => b.area - a.area);
|
|
334
|
+
}
|
|
335
|
+
function foregroundSpan(mask, width, height, rowLo, rowHi) {
|
|
336
|
+
let left = width;
|
|
337
|
+
let right = -1;
|
|
338
|
+
for (let y = Math.max(0, rowLo); y <= Math.min(height - 1, rowHi); y++) {
|
|
339
|
+
for (let x = 0; x < width; x++) {
|
|
340
|
+
if (!mask[y * width + x]) continue;
|
|
341
|
+
if (x < left) left = x;
|
|
342
|
+
if (x > right) right = x;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return { left, right };
|
|
346
|
+
}
|
|
347
|
+
function headHalfOf(span) {
|
|
348
|
+
return (span.right - span.left + 1) / 2;
|
|
349
|
+
}
|
|
350
|
+
function resolveIris(override) {
|
|
351
|
+
const iris = { ...DEFAULT_IRIS, ...override };
|
|
352
|
+
if (iris.hueMin < 0 || iris.hueMax > 360 || iris.hueMin >= iris.hueMax) {
|
|
353
|
+
throw new AutoRigInputError(
|
|
354
|
+
`iris hue window must be 0 <= hueMin < hueMax <= 360, got ${iris.hueMin}..${iris.hueMax}`
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
if (iris.satMin < 0 || iris.satMin > 1) {
|
|
358
|
+
throw new AutoRigInputError(
|
|
359
|
+
`iris satMin must be within 0..1, got ${iris.satMin}`
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
return iris;
|
|
363
|
+
}
|
|
364
|
+
async function measureTurnImage(filePath, iris) {
|
|
365
|
+
const { width, height, rgba, hasAlpha } = await decodePng(filePath);
|
|
366
|
+
const n = width * height;
|
|
367
|
+
let transparent = false;
|
|
368
|
+
if (hasAlpha) {
|
|
369
|
+
for (let i = 0; i < n; i++) {
|
|
370
|
+
if (rgba[i * 4 + 3] < ALPHA_OPAQUE) {
|
|
371
|
+
transparent = true;
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
const maskMode = hasAlpha && transparent ? "alpha" : "keyed";
|
|
377
|
+
const irisMask = new Uint8Array(n);
|
|
378
|
+
const fg = new Uint8Array(n);
|
|
379
|
+
const value = new Float32Array(n);
|
|
380
|
+
for (let i = 0; i < n; i++) {
|
|
381
|
+
if (rgba[i * 4 + 3] < ALPHA_OPAQUE) continue;
|
|
382
|
+
const [hue, s, v] = rgbToHsv(rgba[i * 4], rgba[i * 4 + 1], rgba[i * 4 + 2]);
|
|
383
|
+
if (maskMode === "alpha") {
|
|
384
|
+
fg[i] = 1;
|
|
385
|
+
} else {
|
|
386
|
+
const isBg = hue >= BG_HUE_MIN && hue <= BG_HUE_MAX && s < BG_SAT_MAX && v > BG_V_MIN || v < BG_V_BLACK;
|
|
387
|
+
if (!isBg) fg[i] = 1;
|
|
388
|
+
}
|
|
389
|
+
if (hue >= iris.hueMin && hue <= iris.hueMax && s > iris.satMin && v > IRIS_V_MIN && v < IRIS_V_MAX) {
|
|
390
|
+
irisMask[i] = 1;
|
|
391
|
+
value[i] = v;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const minArea = Math.max(
|
|
395
|
+
1,
|
|
396
|
+
Math.round(MIN_AREA * n / MIN_AREA_REFERENCE_PIXELS)
|
|
397
|
+
);
|
|
398
|
+
const closeRadius = Math.max(
|
|
399
|
+
1,
|
|
400
|
+
Math.round(
|
|
401
|
+
CLOSE_RADIUS * Math.max(width, height) / CLOSE_RADIUS_REFERENCE_PX
|
|
402
|
+
)
|
|
403
|
+
);
|
|
404
|
+
const meanValue = (b) => {
|
|
405
|
+
let count = 0;
|
|
406
|
+
let total = 0;
|
|
407
|
+
for (let y = b.y0; y <= b.y1; y++) {
|
|
408
|
+
for (let x = b.x0; x <= b.x1; x++) {
|
|
409
|
+
const i = y * width + x;
|
|
410
|
+
if (!irisMask[i]) continue;
|
|
411
|
+
count++;
|
|
412
|
+
total += value[i];
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return count ? total / count : 0;
|
|
416
|
+
};
|
|
417
|
+
const candidates = components(
|
|
418
|
+
closeMask(irisMask, width, height, closeRadius),
|
|
419
|
+
width,
|
|
420
|
+
height,
|
|
421
|
+
minArea
|
|
422
|
+
).filter((b) => b.w < width * BLOB_MAX_FRAC && b.h < height * BLOB_MAX_FRAC).filter((b) => meanValue(b) > IRIS_MEAN_V_MIN).slice(0, MAX_CANDIDATES);
|
|
423
|
+
let best = null;
|
|
424
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
425
|
+
for (let j = i + 1; j < candidates.length; j++) {
|
|
426
|
+
const a = candidates[i];
|
|
427
|
+
const b = candidates[j];
|
|
428
|
+
const dx = Math.abs(a.cx - b.cx);
|
|
429
|
+
const dy = Math.abs(a.cy - b.cy);
|
|
430
|
+
if (dx < width * PAIR_DX_MIN_FRAC || dx > width * PAIR_DX_MAX_FRAC)
|
|
431
|
+
continue;
|
|
432
|
+
if (dy > height * PAIR_DY_MAX_FRAC) continue;
|
|
433
|
+
const score = a.area + b.area;
|
|
434
|
+
if (best === null || score > best.score) {
|
|
435
|
+
const pair = a.cx <= b.cx ? [a, b] : [b, a];
|
|
436
|
+
best = { score, pair };
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (best === null) {
|
|
441
|
+
throw new AutoRigInputError(
|
|
442
|
+
`no iris pair found in ${filePath} among ${candidates.length} candidate blobs (iris hue ${iris.hueMin}..${iris.hueMax}, sat > ${iris.satMin})`
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
const [irisL, irisR] = best.pair;
|
|
446
|
+
const eyeRow = Math.round((irisL.cy + irisR.cy) / 2);
|
|
447
|
+
const { left, right } = foregroundSpan(
|
|
448
|
+
fg,
|
|
449
|
+
width,
|
|
450
|
+
height,
|
|
451
|
+
eyeRow - HEAD_BAND,
|
|
452
|
+
eyeRow + HEAD_BAND
|
|
453
|
+
);
|
|
454
|
+
if (right <= left) {
|
|
455
|
+
throw new AutoRigInputError(
|
|
456
|
+
`no head span at the eye row (y=${eyeRow}) in ${filePath}: the ${maskMode} foreground mask is empty there`
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
if (maskMode === "keyed" && right - left + 1 > width * KEYED_SPAN_MAX_FRAC) {
|
|
460
|
+
throw new AutoRigInputError(
|
|
461
|
+
`head span at the eye row (y=${eyeRow}) in ${filePath} covers ${right - left + 1}/${width} px: background not keyed / not an engine render`
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
file: filePath,
|
|
466
|
+
width,
|
|
467
|
+
height,
|
|
468
|
+
maskMode,
|
|
469
|
+
irisL,
|
|
470
|
+
irisR,
|
|
471
|
+
eyeRow,
|
|
472
|
+
head: {
|
|
473
|
+
left,
|
|
474
|
+
right,
|
|
475
|
+
cx: (left + right) / 2,
|
|
476
|
+
half: headHalfOf({ left, right })
|
|
477
|
+
},
|
|
478
|
+
pairCx: (irisL.cx + irisR.cx) / 2,
|
|
479
|
+
eyeGap: irisR.cx - irisL.cx
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function farNearWidths(m, turnSign) {
|
|
483
|
+
return turnSign < 0 ? { far: m.irisL.w, near: m.irisR.w } : { far: m.irisR.w, near: m.irisL.w };
|
|
484
|
+
}
|
|
485
|
+
function overlaySvg(m) {
|
|
486
|
+
const box = (b, stroke) => `<rect x="${b.x0}" y="${b.y0}" width="${b.w}" height="${b.h}" fill="none" stroke="${stroke}" stroke-width="3"/>`;
|
|
487
|
+
const tick = (x, half, stroke) => `<line x1="${x}" y1="${m.eyeRow - half}" x2="${x}" y2="${m.eyeRow + half}" stroke="${stroke}" stroke-width="3"/>`;
|
|
488
|
+
return `<svg width="${m.width}" height="${m.height}">` + box(m.irisL, "#ff0000") + box(m.irisR, "#00c000") + tick(m.head.left, 40, "#0000ff") + tick(m.head.right, 40, "#0000ff") + tick(m.head.cx, 60, "#ff8800") + tick(m.pairCx, 60, "#ff00ff") + "</svg>";
|
|
489
|
+
}
|
|
490
|
+
async function writeOverlay(debugDir, label, m) {
|
|
491
|
+
const outPath = path2.join(
|
|
492
|
+
debugDir,
|
|
493
|
+
`${label}-${path2.basename(m.file, ".png")}.debug.png`
|
|
494
|
+
);
|
|
495
|
+
writeFileAtomic(outPath, await encodeOverlayPng(m.file, overlaySvg(m)));
|
|
496
|
+
return outPath;
|
|
497
|
+
}
|
|
498
|
+
var IRIS_HEIGHT_SCALE_TOLERANCE = 0.1;
|
|
499
|
+
async function measureTurnReference(input) {
|
|
500
|
+
try {
|
|
501
|
+
const iris = resolveIris(input.iris);
|
|
502
|
+
const frontPath = resolveInputPath(input.front);
|
|
503
|
+
const turnedPath = resolveInputPath(input.turned);
|
|
504
|
+
const debugDir = input.debugDir === void 0 ? null : resolveOutputDir(input.debugDir);
|
|
505
|
+
const front = await measureTurnImage(frontPath, iris);
|
|
506
|
+
const turned = await measureTurnImage(turnedPath, iris);
|
|
507
|
+
const frontIrisH = (front.irisL.h + front.irisR.h) / 2;
|
|
508
|
+
const turnedIrisH = (turned.irisL.h + turned.irisR.h) / 2;
|
|
509
|
+
if (Math.abs(turnedIrisH - frontIrisH) / frontIrisH > IRIS_HEIGHT_SCALE_TOLERANCE) {
|
|
510
|
+
throw new AutoRigInputError(
|
|
511
|
+
`front and turned images are not at the same scale: mean iris height ${frontIrisH.toFixed(1)}px front vs ${turnedIrisH.toFixed(1)}px turned, more than ${(IRIS_HEIGHT_SCALE_TOLERANCE * 100).toFixed(0)}% apart \u2014 a yaw does not change iris height, so the pair must share the same framing and head size`
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
const frontShift = front.pairCx - front.head.cx;
|
|
515
|
+
const turnedShift = turned.pairCx - turned.head.cx;
|
|
516
|
+
const shift = turnedShift - frontShift;
|
|
517
|
+
const turnSign = shift < 0 ? -1 : 1;
|
|
518
|
+
const f = farNearWidths(front, turnSign);
|
|
519
|
+
const t = farNearWidths(turned, turnSign);
|
|
520
|
+
const debug = debugDir === null ? void 0 : [
|
|
521
|
+
await writeOverlay(debugDir, "front", front),
|
|
522
|
+
await writeOverlay(debugDir, "turned", turned)
|
|
523
|
+
];
|
|
524
|
+
return {
|
|
525
|
+
ok: true,
|
|
526
|
+
front,
|
|
527
|
+
turned,
|
|
528
|
+
farEyeRatio: t.far / t.near / (f.far / f.near),
|
|
529
|
+
eyeShift: shift / front.head.half,
|
|
530
|
+
silhouetteRatio: turned.head.half / front.head.half,
|
|
531
|
+
turnSign,
|
|
532
|
+
...debug === void 0 ? {} : { debug }
|
|
533
|
+
};
|
|
534
|
+
} catch (err) {
|
|
535
|
+
if (err instanceof AutoRigInputError)
|
|
536
|
+
return { ok: false, error: err.message };
|
|
537
|
+
throw err;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
var n1 = (v) => v.toFixed(1);
|
|
541
|
+
var n3 = (v) => v.toFixed(3);
|
|
542
|
+
function imageBlock(label, m) {
|
|
543
|
+
return [
|
|
544
|
+
`# ${label} ${m.file} ${m.width}x${m.height} mask: ${m.maskMode}`,
|
|
545
|
+
`iris widths ${m.irisL.w} / ${m.irisR.w} px centres ${n1(m.irisL.cx)} / ${n1(m.irisR.cx)} eye row ${m.eyeRow} gap ${n1(m.eyeGap)}`,
|
|
546
|
+
`head ${m.head.left}..${m.head.right} centre ${n1(m.head.cx)} half ${n1(m.head.half)} pair centre ${n1(m.pairCx)}`
|
|
547
|
+
];
|
|
548
|
+
}
|
|
549
|
+
function formatTurnReport(result) {
|
|
550
|
+
const { front, turned } = result;
|
|
551
|
+
const f = farNearWidths(front, result.turnSign);
|
|
552
|
+
const t = farNearWidths(turned, result.turnSign);
|
|
553
|
+
const lines = [
|
|
554
|
+
`farEyeRatio ${n3(result.farEyeRatio)} far/near iris width ${t.far}/${t.near} turned over ${f.far}/${f.near} front`,
|
|
555
|
+
`eyeShift ${n3(result.eyeShift)} pair-vs-head centre ${n1(turned.pairCx - turned.head.cx)} px turned, ${n1(front.pairCx - front.head.cx)} px front, over front head half ${n1(front.head.half)} px (hh)`,
|
|
556
|
+
`silhouetteRatio ${n3(result.silhouetteRatio)} head half ${n1(turned.head.half)} px turned over ${n1(front.head.half)} px front`,
|
|
557
|
+
`turnSign ${result.turnSign} (the head turned toward the image's ${result.turnSign < 0 ? "left" : "right"})`,
|
|
558
|
+
"",
|
|
559
|
+
...imageBlock("front", front),
|
|
560
|
+
"",
|
|
561
|
+
...imageBlock("turned", turned)
|
|
562
|
+
];
|
|
563
|
+
if (result.debug !== void 0) {
|
|
564
|
+
lines.push("", ...result.debug.map((p) => `debug overlay ${p}`));
|
|
565
|
+
}
|
|
566
|
+
return lines.join("\n");
|
|
567
|
+
}
|
|
568
|
+
|
|
213
569
|
// src/tools.ts
|
|
570
|
+
import path3 from "path";
|
|
571
|
+
import {
|
|
572
|
+
parseIkiModel,
|
|
573
|
+
loadIkiModel,
|
|
574
|
+
IkiFormatError,
|
|
575
|
+
StandardParameter
|
|
576
|
+
} from "@ikijs/format";
|
|
577
|
+
import {
|
|
578
|
+
EditorDocument,
|
|
579
|
+
packAtlas,
|
|
580
|
+
uvRectFor,
|
|
581
|
+
generateIkiFromLayerSet,
|
|
582
|
+
parseLayerRoles,
|
|
583
|
+
TurnTargetError
|
|
584
|
+
} from "@ikijs/editor";
|
|
214
585
|
function coerceModel(model) {
|
|
215
586
|
try {
|
|
216
587
|
const parsed = typeof model === "string" ? loadIkiModel(model) : parseIkiModel(model);
|
|
@@ -354,6 +725,38 @@ function expectInput(label, fn) {
|
|
|
354
725
|
);
|
|
355
726
|
}
|
|
356
727
|
}
|
|
728
|
+
function eyeRowOf(layers) {
|
|
729
|
+
const centreY = (role) => {
|
|
730
|
+
const layer = layers.find((l) => l.role === role);
|
|
731
|
+
return layer && layer.bbox.y + layer.bbox.h / 2;
|
|
732
|
+
};
|
|
733
|
+
const pairRow = (left, right) => {
|
|
734
|
+
const a = centreY(left);
|
|
735
|
+
const b = centreY(right);
|
|
736
|
+
return a === void 0 || b === void 0 ? void 0 : (a + b) / 2;
|
|
737
|
+
};
|
|
738
|
+
const row = pairRow("iris_L", "iris_R") ?? pairRow("eye_L", "eye_R");
|
|
739
|
+
if (row === void 0) {
|
|
740
|
+
throw new Error("auto-rig: no eye pair to measure the head width at");
|
|
741
|
+
}
|
|
742
|
+
return Math.round(row);
|
|
743
|
+
}
|
|
744
|
+
function foregroundSpanOfRows(rowLeft, rowRight, rowLo, rowHi) {
|
|
745
|
+
let left = Infinity;
|
|
746
|
+
let right = -Infinity;
|
|
747
|
+
for (let y = Math.max(0, rowLo); y <= Math.min(rowLeft.length - 1, rowHi); y++) {
|
|
748
|
+
if (rowLeft[y] < left) left = rowLeft[y];
|
|
749
|
+
if (rowRight[y] > right) right = rowRight[y];
|
|
750
|
+
}
|
|
751
|
+
return { left, right };
|
|
752
|
+
}
|
|
753
|
+
function facePlateHalfOf(layers) {
|
|
754
|
+
const face = layers.find((l) => l.role === "face");
|
|
755
|
+
if (face === void 0) {
|
|
756
|
+
throw new Error("auto-rig: no face layer to measure the plate against");
|
|
757
|
+
}
|
|
758
|
+
return face.cropW / 2;
|
|
759
|
+
}
|
|
357
760
|
async function autoRigFromLayers(input) {
|
|
358
761
|
try {
|
|
359
762
|
const layers = input.layers;
|
|
@@ -376,7 +779,7 @@ async function autoRigFromLayers(input) {
|
|
|
376
779
|
}
|
|
377
780
|
const resolvedLayers = layers.map((layer) => {
|
|
378
781
|
const resolved = resolveInputPath(layer.path);
|
|
379
|
-
return { resolved, fileName: layer.fileName ??
|
|
782
|
+
return { resolved, fileName: layer.fileName ?? path3.basename(resolved) };
|
|
380
783
|
});
|
|
381
784
|
const rolePairs = expectInput(
|
|
382
785
|
"role parsing",
|
|
@@ -386,8 +789,10 @@ async function autoRigFromLayers(input) {
|
|
|
386
789
|
let canvasW = 0;
|
|
387
790
|
let canvasH = 0;
|
|
388
791
|
let totalPixels = 0;
|
|
792
|
+
let opaque = new Uint8Array(0);
|
|
389
793
|
const layerInputs = [];
|
|
390
794
|
const crops = [];
|
|
795
|
+
const rowSpansByRole = [];
|
|
391
796
|
for (let i = 0; i < resolvedLayers.length; i++) {
|
|
392
797
|
const { resolved, fileName } = resolvedLayers[i];
|
|
393
798
|
const png = await decodePng(resolved);
|
|
@@ -410,6 +815,7 @@ async function autoRigFromLayers(input) {
|
|
|
410
815
|
`canvas ${canvasW}x${canvasH} exceeds ${MAX_CANVAS_DIM}`
|
|
411
816
|
);
|
|
412
817
|
}
|
|
818
|
+
opaque = new Uint8Array(canvasW * canvasH);
|
|
413
819
|
} else if (png.width !== canvasW || png.height !== canvasH) {
|
|
414
820
|
throw new AutoRigInputError(
|
|
415
821
|
`layer "${fileName}" size ${png.width}x${png.height} differs from canvas ${canvasW}x${canvasH}`
|
|
@@ -429,6 +835,18 @@ async function autoRigFromLayers(input) {
|
|
|
429
835
|
);
|
|
430
836
|
}
|
|
431
837
|
const buffer = await cropToBuffer(png.rgba, png.width, png.height, bbox);
|
|
838
|
+
const rowLeft = new Int32Array(canvasH).fill(canvasW);
|
|
839
|
+
const rowRight = new Int32Array(canvasH).fill(-1);
|
|
840
|
+
for (let y = 0; y < canvasH; y++) {
|
|
841
|
+
for (let x = 0; x < canvasW; x++) {
|
|
842
|
+
const p = y * canvasW + x;
|
|
843
|
+
if (png.rgba[p * 4 + 3] < ALPHA_OPAQUE) continue;
|
|
844
|
+
opaque[p] = 1;
|
|
845
|
+
if (x < rowLeft[y]) rowLeft[y] = x;
|
|
846
|
+
if (x > rowRight[y]) rowRight[y] = x;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
rowSpansByRole.push({ role, rowLeft, rowRight });
|
|
432
850
|
layerInputs.push({
|
|
433
851
|
role,
|
|
434
852
|
fileName,
|
|
@@ -440,10 +858,56 @@ async function autoRigFromLayers(input) {
|
|
|
440
858
|
});
|
|
441
859
|
crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
|
|
442
860
|
}
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
861
|
+
const eyeRow = eyeRowOf(layerInputs);
|
|
862
|
+
const span = foregroundSpan(
|
|
863
|
+
opaque,
|
|
864
|
+
canvasW,
|
|
865
|
+
canvasH,
|
|
866
|
+
eyeRow - HEAD_BAND,
|
|
867
|
+
eyeRow + HEAD_BAND
|
|
868
|
+
);
|
|
869
|
+
const headHalfWidth = span.right > span.left ? headHalfOf(span) : void 0;
|
|
870
|
+
const headHalfWidthApplied = headHalfWidth !== void 0 && headHalfWidth > facePlateHalfOf(layerInputs);
|
|
871
|
+
let headEdges;
|
|
872
|
+
if (headHalfWidthApplied) {
|
|
873
|
+
const bandLo = eyeRow - HEAD_BAND;
|
|
874
|
+
const bandHi = eyeRow + HEAD_BAND;
|
|
875
|
+
const left = [];
|
|
876
|
+
const right = [];
|
|
877
|
+
for (const { role, rowLeft, rowRight } of rowSpansByRole) {
|
|
878
|
+
const own = foregroundSpanOfRows(rowLeft, rowRight, bandLo, bandHi);
|
|
879
|
+
if (own.right <= own.left) continue;
|
|
880
|
+
left.push({ role, x: own.left - canvasW / 2 });
|
|
881
|
+
right.push({ role, x: own.right - canvasW / 2 });
|
|
882
|
+
}
|
|
883
|
+
headEdges = { left, right };
|
|
884
|
+
}
|
|
885
|
+
let turn;
|
|
886
|
+
let model;
|
|
887
|
+
try {
|
|
888
|
+
const { eyeShift, farEyeRatio, silhouetteRatio, noseShift, mouthShift } = input.turnTargets ?? {};
|
|
889
|
+
model = generateIkiFromLayerSet(
|
|
890
|
+
layerInputs,
|
|
891
|
+
{ width: canvasW, height: canvasH },
|
|
892
|
+
{
|
|
893
|
+
turnTargets: {
|
|
894
|
+
eyeShift,
|
|
895
|
+
farEyeRatio,
|
|
896
|
+
silhouetteRatio,
|
|
897
|
+
noseShift,
|
|
898
|
+
mouthShift,
|
|
899
|
+
...headHalfWidthApplied ? { headHalfWidth } : {}
|
|
900
|
+
},
|
|
901
|
+
onTurnSolved: (report) => {
|
|
902
|
+
turn = report;
|
|
903
|
+
},
|
|
904
|
+
...headEdges === void 0 ? {} : { headEdges }
|
|
905
|
+
}
|
|
906
|
+
);
|
|
907
|
+
} catch (e) {
|
|
908
|
+
if (!(e instanceof TurnTargetError)) throw e;
|
|
909
|
+
throw new AutoRigInputError(e.message);
|
|
910
|
+
}
|
|
447
911
|
const doc = new EditorDocument(model);
|
|
448
912
|
const layout = packAtlas(
|
|
449
913
|
crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
|
|
@@ -480,7 +944,11 @@ async function autoRigFromLayers(input) {
|
|
|
480
944
|
path: outPath,
|
|
481
945
|
canvas: { width: canvasW, height: canvasH },
|
|
482
946
|
partCount: finalModel.parts.length,
|
|
483
|
-
atlasBytes: dataUri.length
|
|
947
|
+
atlasBytes: dataUri.length,
|
|
948
|
+
...headHalfWidth === void 0 ? {} : { headHalfWidth },
|
|
949
|
+
headHalfWidthApplied,
|
|
950
|
+
...headEdges === void 0 ? {} : { headEdges },
|
|
951
|
+
...turn === void 0 ? {} : { turn }
|
|
484
952
|
};
|
|
485
953
|
} catch (err) {
|
|
486
954
|
if (err instanceof AutoRigInputError)
|
|
@@ -491,7 +959,7 @@ async function autoRigFromLayers(input) {
|
|
|
491
959
|
|
|
492
960
|
// src/measure.ts
|
|
493
961
|
import fs2 from "fs";
|
|
494
|
-
import
|
|
962
|
+
import path4 from "path";
|
|
495
963
|
var IRIS_RATIO_MIN = 0.45;
|
|
496
964
|
var IRIS_RATIO_MAX = 0.72;
|
|
497
965
|
var EYE_ASPECT_MIN = 0.5;
|
|
@@ -592,8 +1060,8 @@ async function measureDir(absDir) {
|
|
|
592
1060
|
const layers = {};
|
|
593
1061
|
const empty = [];
|
|
594
1062
|
for (const f of files) {
|
|
595
|
-
const role =
|
|
596
|
-
const m = await layerStats(
|
|
1063
|
+
const role = path4.basename(f, ".png");
|
|
1064
|
+
const m = await layerStats(path4.join(absDir, f));
|
|
597
1065
|
if (!m) {
|
|
598
1066
|
empty.push(role);
|
|
599
1067
|
continue;
|
|
@@ -671,7 +1139,11 @@ async function measureDir(absDir) {
|
|
|
671
1139
|
}
|
|
672
1140
|
for (const [role, why] of [
|
|
673
1141
|
["body", "without it the character reads as a floating head on head-turn"],
|
|
674
|
-
["hair_back", "without it the silhouette is flat behind the face"]
|
|
1142
|
+
["hair_back", "without it the silhouette is flat behind the face"],
|
|
1143
|
+
[
|
|
1144
|
+
"nose",
|
|
1145
|
+
"without it nothing on the face slides on the head turn \u2014 the features stay on the plate"
|
|
1146
|
+
]
|
|
675
1147
|
]) {
|
|
676
1148
|
if (layers[role] === void 0) warnings.push(`${role}: missing \u2014 ${why}.`);
|
|
677
1149
|
}
|
|
@@ -720,7 +1192,7 @@ function formatMeasureReport(result) {
|
|
|
720
1192
|
|
|
721
1193
|
// src/compose.ts
|
|
722
1194
|
import fs3 from "fs";
|
|
723
|
-
import
|
|
1195
|
+
import path5 from "path";
|
|
724
1196
|
import sharp2 from "sharp";
|
|
725
1197
|
var CANVAS = 1100;
|
|
726
1198
|
var EYE_W = 128;
|
|
@@ -728,13 +1200,6 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
|
|
|
728
1200
|
var EYEWHITE_SRC = "eyewhite.png";
|
|
729
1201
|
var EYE_LASH_LUMA = 120;
|
|
730
1202
|
var LASH_KEEP_FRACTION = 0.5;
|
|
731
|
-
var NOSE_INK_TOL = 20;
|
|
732
|
-
var NOSE_INK_LOW = 8;
|
|
733
|
-
var NOSE_GROW = 2;
|
|
734
|
-
var NOSE_MIN_PIXELS = 12;
|
|
735
|
-
var NOSE_MAX_CUT_FRACTION = 0.25;
|
|
736
|
-
var NOSE_ROW_MIN_PIXELS = 8;
|
|
737
|
-
var NOSE_FILL_REACH = 8;
|
|
738
1203
|
var DEFAULT_LAYOUT = {
|
|
739
1204
|
// Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
|
|
740
1205
|
// without them still composes (head-only character).
|
|
@@ -744,6 +1209,11 @@ var DEFAULT_LAYOUT = {
|
|
|
744
1209
|
// not a floating head.
|
|
745
1210
|
body: { src: "body.png", cx: 550, cy: 1017, w: 840, optional: true },
|
|
746
1211
|
face: { src: "face.png", cx: 550, cy: 475, w: 400 },
|
|
1212
|
+
// The nose, drawn on its own (the face is drawn without one): the auto-rig
|
|
1213
|
+
// leads the head turn with it and keys the other features' slide on its
|
|
1214
|
+
// presence, so without it the features stay on the face plate. Sits between
|
|
1215
|
+
// the eyes and above the mouth. OPTIONAL: a parts dir without it composes.
|
|
1216
|
+
nose: { src: "nose.png", cx: 550, cy: 586, w: 40, optional: true },
|
|
747
1217
|
mouth: { src: "mouth.png", cx: 550, cy: 619, w: 68 },
|
|
748
1218
|
// Cross-fades with `mouth` on ParamMouthOpenY (opacity, not scaleY) once the
|
|
749
1219
|
// auto-rig sees both roles. Same cx/w as `mouth` so the lip width matches;
|
|
@@ -785,6 +1255,7 @@ var ORDER = [
|
|
|
785
1255
|
"hair_back",
|
|
786
1256
|
"body",
|
|
787
1257
|
"face",
|
|
1258
|
+
"nose",
|
|
788
1259
|
"mouth",
|
|
789
1260
|
"mouth_open",
|
|
790
1261
|
"eye_L",
|
|
@@ -846,7 +1317,7 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
|
|
|
846
1317
|
if (inMemory !== void 0) {
|
|
847
1318
|
img = sharp2(inMemory);
|
|
848
1319
|
} else {
|
|
849
|
-
const srcPath =
|
|
1320
|
+
const srcPath = path5.join(partsDir, cfg.src);
|
|
850
1321
|
if (!fs3.existsSync(srcPath)) {
|
|
851
1322
|
if (cfg.optional) return null;
|
|
852
1323
|
throw new AutoRigInputError(
|
|
@@ -901,7 +1372,7 @@ function blankCanvas() {
|
|
|
901
1372
|
});
|
|
902
1373
|
}
|
|
903
1374
|
async function prepEyeSplit(partsDir) {
|
|
904
|
-
const srcPath =
|
|
1375
|
+
const srcPath = path5.join(partsDir, EYEWHITE_SRC);
|
|
905
1376
|
if (!fs3.existsSync(srcPath)) {
|
|
906
1377
|
throw new AutoRigInputError(`missing eyewhite source: ${srcPath}`);
|
|
907
1378
|
}
|
|
@@ -946,164 +1417,6 @@ async function prepEyeSplit(partsDir) {
|
|
|
946
1417
|
lash: await cropToBuffer(lash, W, H, region)
|
|
947
1418
|
};
|
|
948
1419
|
}
|
|
949
|
-
function cutNose(rgba, box) {
|
|
950
|
-
const W = CANVAS;
|
|
951
|
-
const x0 = Math.max(0, box.x0);
|
|
952
|
-
const y0 = Math.max(0, box.y0);
|
|
953
|
-
const x1 = Math.min(W, box.x1);
|
|
954
|
-
const y1 = Math.min(W, box.y1);
|
|
955
|
-
if (x1 - x0 < 4 || y1 - y0 < 4) return null;
|
|
956
|
-
const bw = x1 - x0;
|
|
957
|
-
const bh = y1 - y0;
|
|
958
|
-
const at = (x, y) => (y * W + x) * 4;
|
|
959
|
-
const median = (v) => v.sort((a, b) => a - b)[v.length >> 1];
|
|
960
|
-
const all = [[], [], []];
|
|
961
|
-
const rows = [];
|
|
962
|
-
for (let y = y0; y < y1; y++) {
|
|
963
|
-
const row = [[], [], []];
|
|
964
|
-
for (let x = x0; x < x1; x++) {
|
|
965
|
-
const i = at(x, y);
|
|
966
|
-
if (rgba[i + 3] === 0) continue;
|
|
967
|
-
for (let c = 0; c < 3; c++) {
|
|
968
|
-
row[c].push(rgba[i + c]);
|
|
969
|
-
all[c].push(rgba[i + c]);
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
rows.push(row);
|
|
973
|
-
}
|
|
974
|
-
const opaqueCount = all[0].length;
|
|
975
|
-
if (opaqueCount === 0) return null;
|
|
976
|
-
const skin = all.map(median);
|
|
977
|
-
const skinRow = rows.map(
|
|
978
|
-
(row) => row[0].length < NOSE_ROW_MIN_PIXELS ? skin : row.map(median)
|
|
979
|
-
);
|
|
980
|
-
const distAt = (i, y) => {
|
|
981
|
-
const ref = skinRow[y - y0];
|
|
982
|
-
return Math.max(
|
|
983
|
-
Math.abs(rgba[i] - ref[0]),
|
|
984
|
-
Math.abs(rgba[i + 1] - ref[1]),
|
|
985
|
-
Math.abs(rgba[i + 2] - ref[2])
|
|
986
|
-
);
|
|
987
|
-
};
|
|
988
|
-
const dist = new Uint8Array(bw * bh);
|
|
989
|
-
const ink = new Uint8Array(bw * bh);
|
|
990
|
-
const queue = [];
|
|
991
|
-
let inkCount = 0;
|
|
992
|
-
for (let y = y0; y < y1; y++) {
|
|
993
|
-
for (let x = x0; x < x1; x++) {
|
|
994
|
-
const i = at(x, y);
|
|
995
|
-
if (rgba[i + 3] === 0) continue;
|
|
996
|
-
const d = distAt(i, y);
|
|
997
|
-
const k = (y - y0) * bw + (x - x0);
|
|
998
|
-
dist[k] = d;
|
|
999
|
-
if (d > NOSE_INK_TOL) {
|
|
1000
|
-
ink[k] = 1;
|
|
1001
|
-
queue.push(k);
|
|
1002
|
-
inkCount++;
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
}
|
|
1006
|
-
if (inkCount < NOSE_MIN_PIXELS) return null;
|
|
1007
|
-
while (queue.length > 0) {
|
|
1008
|
-
const k = queue.pop();
|
|
1009
|
-
const kx = k % bw;
|
|
1010
|
-
const ky = (k - kx) / bw;
|
|
1011
|
-
for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
|
|
1012
|
-
for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
|
|
1013
|
-
const nx = kx + dx;
|
|
1014
|
-
const ny = ky + dy;
|
|
1015
|
-
if (nx < 0 || nx >= bw || ny < 0 || ny >= bh) continue;
|
|
1016
|
-
const n = ny * bw + nx;
|
|
1017
|
-
if (ink[n] === 0 && dist[n] > NOSE_INK_LOW) {
|
|
1018
|
-
ink[n] = 1;
|
|
1019
|
-
queue.push(n);
|
|
1020
|
-
}
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
const cut = new Uint8Array(bw * bh);
|
|
1025
|
-
for (let y = 0; y < bh; y++) {
|
|
1026
|
-
for (let x = 0; x < bw; x++) {
|
|
1027
|
-
if (ink[y * bw + x] === 0) continue;
|
|
1028
|
-
for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
|
|
1029
|
-
for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
|
|
1030
|
-
const yy = y + dy;
|
|
1031
|
-
const xx = x + dx;
|
|
1032
|
-
if (yy >= 0 && yy < bh && xx >= 0 && xx < bw) cut[yy * bw + xx] = 1;
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
let cutCount = 0;
|
|
1038
|
-
for (let y = 0; y < bh; y++) {
|
|
1039
|
-
for (let x = 0; x < bw; x++) {
|
|
1040
|
-
if (cut[y * bw + x] === 0) continue;
|
|
1041
|
-
const a = rgba[at(x0 + x, y0 + y) + 3];
|
|
1042
|
-
if (a === 0) continue;
|
|
1043
|
-
if (a < 255) return null;
|
|
1044
|
-
cutCount++;
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
if (cutCount > NOSE_MAX_CUT_FRACTION * opaqueCount) return null;
|
|
1048
|
-
const face = Buffer.from(rgba);
|
|
1049
|
-
const nose = Buffer.from(rgba);
|
|
1050
|
-
for (let i = 3; i < nose.length; i += 4) nose[i] = 0;
|
|
1051
|
-
let minX = W;
|
|
1052
|
-
let minY = W;
|
|
1053
|
-
let maxX = -1;
|
|
1054
|
-
let maxY = -1;
|
|
1055
|
-
for (let y = 0; y < bh; y++) {
|
|
1056
|
-
const cy = y0 + y;
|
|
1057
|
-
let x = 0;
|
|
1058
|
-
while (x < bw) {
|
|
1059
|
-
if (cut[y * bw + x] === 0) {
|
|
1060
|
-
x++;
|
|
1061
|
-
continue;
|
|
1062
|
-
}
|
|
1063
|
-
const start = x;
|
|
1064
|
-
while (x < bw && cut[y * bw + x] === 1) x++;
|
|
1065
|
-
const end = x;
|
|
1066
|
-
const skinAt = (bx) => {
|
|
1067
|
-
for (let step = 0; step < NOSE_FILL_REACH && bx >= 0 && bx < bw; step++) {
|
|
1068
|
-
const i = at(x0 + bx, cy);
|
|
1069
|
-
if (rgba[i + 3] > 0 && cut[y * bw + bx] === 0) {
|
|
1070
|
-
if (distAt(i, cy) <= NOSE_INK_TOL / 2) return i;
|
|
1071
|
-
}
|
|
1072
|
-
bx += bx < start ? -1 : 1;
|
|
1073
|
-
}
|
|
1074
|
-
return -1;
|
|
1075
|
-
};
|
|
1076
|
-
const l = skinAt(start - 1);
|
|
1077
|
-
const r = skinAt(end);
|
|
1078
|
-
for (let k = start; k < end; k++) {
|
|
1079
|
-
const cx = x0 + k;
|
|
1080
|
-
const i = at(cx, cy);
|
|
1081
|
-
if (rgba[i + 3] === 0) continue;
|
|
1082
|
-
const t = (k - start + 1) / (end - start + 1);
|
|
1083
|
-
for (let c = 0; c < 3; c++) {
|
|
1084
|
-
const rowSkin = skinRow[y];
|
|
1085
|
-
const lc = l >= 0 ? rgba[l + c] : r >= 0 ? rgba[r + c] : rowSkin[c];
|
|
1086
|
-
const rc = r >= 0 ? rgba[r + c] : l >= 0 ? rgba[l + c] : rowSkin[c];
|
|
1087
|
-
face[i + c] = Math.round(lc * (1 - t) + rc * t);
|
|
1088
|
-
}
|
|
1089
|
-
nose[i] = rgba[i];
|
|
1090
|
-
nose[i + 1] = rgba[i + 1];
|
|
1091
|
-
nose[i + 2] = rgba[i + 2];
|
|
1092
|
-
nose[i + 3] = rgba[i + 3];
|
|
1093
|
-
if (cx < minX) minX = cx;
|
|
1094
|
-
if (cx > maxX) maxX = cx;
|
|
1095
|
-
if (cy < minY) minY = cy;
|
|
1096
|
-
if (cy > maxY) maxY = cy;
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
if (maxX < 0) return null;
|
|
1101
|
-
return {
|
|
1102
|
-
face,
|
|
1103
|
-
nose,
|
|
1104
|
-
bbox: { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 }
|
|
1105
|
-
};
|
|
1106
|
-
}
|
|
1107
1420
|
async function composeLayersFromParts(input) {
|
|
1108
1421
|
try {
|
|
1109
1422
|
const outDir = resolveOutputDir(input.outDir);
|
|
@@ -1122,7 +1435,6 @@ async function composeLayersFromParts(input) {
|
|
|
1122
1435
|
const layers = [];
|
|
1123
1436
|
const skipped = [];
|
|
1124
1437
|
const preview = [];
|
|
1125
|
-
let faceRaw;
|
|
1126
1438
|
for (const role of ORDER) {
|
|
1127
1439
|
const cfg = layout[role];
|
|
1128
1440
|
const part = await partBuffer(
|
|
@@ -1133,19 +1445,13 @@ async function composeLayersFromParts(input) {
|
|
|
1133
1445
|
);
|
|
1134
1446
|
if (part === null) {
|
|
1135
1447
|
skipped.push(role);
|
|
1136
|
-
fs3.rmSync(
|
|
1448
|
+
fs3.rmSync(path5.join(outDir, `${role}.png`), { force: true });
|
|
1137
1449
|
continue;
|
|
1138
1450
|
}
|
|
1139
1451
|
const { left, top } = placement(role, cfg, part.w, part.h);
|
|
1140
|
-
const
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
const outPath = path4.join(outDir, `${role}.png`);
|
|
1144
|
-
if (role === "face") {
|
|
1145
|
-
faceRaw = await composed.raw().toBuffer();
|
|
1146
|
-
} else {
|
|
1147
|
-
writeFileAtomic(outPath, await composed.png().toBuffer());
|
|
1148
|
-
}
|
|
1452
|
+
const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
|
|
1453
|
+
const outPath = path5.join(outDir, `${role}.png`);
|
|
1454
|
+
writeFileAtomic(outPath, layer);
|
|
1149
1455
|
layers.push({
|
|
1150
1456
|
role,
|
|
1151
1457
|
path: outPath,
|
|
@@ -1156,48 +1462,6 @@ async function composeLayersFromParts(input) {
|
|
|
1156
1462
|
});
|
|
1157
1463
|
preview.push({ input: part.buf, left, top });
|
|
1158
1464
|
}
|
|
1159
|
-
const placed = (role) => layers.find((l) => l.role === role);
|
|
1160
|
-
const eyeL = placed("eye_L");
|
|
1161
|
-
const eyeR = placed("eye_R");
|
|
1162
|
-
const mouth = placed("mouth");
|
|
1163
|
-
const noseCut = cutNose(faceRaw, {
|
|
1164
|
-
x0: eyeR.left + eyeR.width,
|
|
1165
|
-
x1: eyeL.left,
|
|
1166
|
-
y0: Math.round(
|
|
1167
|
-
(eyeL.top + eyeL.height / 2 + eyeR.top + eyeR.height / 2) / 2
|
|
1168
|
-
),
|
|
1169
|
-
y1: mouth.top - 2
|
|
1170
|
-
});
|
|
1171
|
-
const rawCanvas = {
|
|
1172
|
-
raw: { width: CANVAS, height: CANVAS, channels: 4 }
|
|
1173
|
-
};
|
|
1174
|
-
const facePath = path4.join(outDir, "face.png");
|
|
1175
|
-
const nosePath = path4.join(outDir, "nose.png");
|
|
1176
|
-
if (noseCut === null) {
|
|
1177
|
-
writeFileAtomic(
|
|
1178
|
-
facePath,
|
|
1179
|
-
await sharp2(faceRaw, rawCanvas).png().toBuffer()
|
|
1180
|
-
);
|
|
1181
|
-
fs3.rmSync(nosePath, { force: true });
|
|
1182
|
-
skipped.push("nose");
|
|
1183
|
-
} else {
|
|
1184
|
-
writeFileAtomic(
|
|
1185
|
-
facePath,
|
|
1186
|
-
await sharp2(noseCut.face, rawCanvas).png().toBuffer()
|
|
1187
|
-
);
|
|
1188
|
-
writeFileAtomic(
|
|
1189
|
-
nosePath,
|
|
1190
|
-
await sharp2(noseCut.nose, rawCanvas).png().toBuffer()
|
|
1191
|
-
);
|
|
1192
|
-
layers.splice(layers.findIndex((l) => l.role === "face") + 1, 0, {
|
|
1193
|
-
role: "nose",
|
|
1194
|
-
path: nosePath,
|
|
1195
|
-
width: noseCut.bbox.w,
|
|
1196
|
-
height: noseCut.bbox.h,
|
|
1197
|
-
left: noseCut.bbox.x,
|
|
1198
|
-
top: noseCut.bbox.y
|
|
1199
|
-
});
|
|
1200
|
-
}
|
|
1201
1465
|
const previewPng = await sharp2({
|
|
1202
1466
|
create: {
|
|
1203
1467
|
width: CANVAS,
|
|
@@ -1206,7 +1470,7 @@ async function composeLayersFromParts(input) {
|
|
|
1206
1470
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
1207
1471
|
}
|
|
1208
1472
|
}).composite(preview).png().toBuffer();
|
|
1209
|
-
const previewPath =
|
|
1473
|
+
const previewPath = path5.join(outDir, "preview.png");
|
|
1210
1474
|
writeFileAtomic(previewPath, previewPng);
|
|
1211
1475
|
return {
|
|
1212
1476
|
ok: true,
|
|
@@ -1227,7 +1491,7 @@ async function composeLayersFromParts(input) {
|
|
|
1227
1491
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
1228
1492
|
import { z } from "zod";
|
|
1229
1493
|
function createIkiMcpServer() {
|
|
1230
|
-
const server = new McpServer({ name: "iki", version: "0.
|
|
1494
|
+
const server = new McpServer({ name: "iki", version: "0.8.0" });
|
|
1231
1495
|
server.registerTool(
|
|
1232
1496
|
"validate_iki",
|
|
1233
1497
|
{
|
|
@@ -1307,6 +1571,15 @@ function createIkiMcpServer() {
|
|
|
1307
1571
|
),
|
|
1308
1572
|
quantizeColors: z.number().int().min(2).max(256).optional().describe(
|
|
1309
1573
|
"Palette-quantize the atlas PNG to this many colours (2..256). Flat-shaded art keeps its look at 256 and the model shrinks to about a quarter; omit for lossless."
|
|
1574
|
+
),
|
|
1575
|
+
turnTargets: z.object({
|
|
1576
|
+
eyeShift: z.number().optional(),
|
|
1577
|
+
farEyeRatio: z.number().optional(),
|
|
1578
|
+
silhouetteRatio: z.number().optional(),
|
|
1579
|
+
noseShift: z.number().optional(),
|
|
1580
|
+
mouthShift: z.number().optional()
|
|
1581
|
+
}).optional().describe(
|
|
1582
|
+
"What the head turn is fitted to, as `measure_turn_reference` reports it off a front/turned reference pair. `eyeShift` (and the optional `noseShift`/`mouthShift`, derived from it when absent) is how far that feature slides across the head at full turn, as a fraction of the head's half-width \u2014 a MAGNITUDE: the rig turns both ways, so the sign is ignored. `farEyeRatio` is the far/near eye width at full turn over that same ratio at rest, `silhouetteRatio` the head's half-width turned over at rest. The half-width the shifts are fractions of is measured off the layers themselves, so it is not an input. An omitted field falls back to a default, which is clamped to what this layer set can do (the result's `turn.clamped` lists the ones that were, `turn.achieved` what the rig reaches); a target you DO pass that this layer set cannot reach comes back as `INVALID: \u2026` naming the field and the attainable range, since it is a measurement rather than a preference."
|
|
1310
1583
|
)
|
|
1311
1584
|
}
|
|
1312
1585
|
},
|
|
@@ -1328,7 +1601,7 @@ function createIkiMcpServer() {
|
|
|
1328
1601
|
server.registerTool(
|
|
1329
1602
|
"compose_layers_from_parts",
|
|
1330
1603
|
{
|
|
1331
|
-
description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split,
|
|
1604
|
+
description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split, alpha-trim/white-key, and placement pipeline the character-generation skill needs), with the same geometry report measure_layers returns standalone included inline. face, mouth, eyewhite, iris, brow, hair_front are required; hair_back, body, mouth_open, nose are optional \u2014 without a nose layer the rig does not slide the features on the head turn.",
|
|
1332
1605
|
inputSchema: {
|
|
1333
1606
|
partsDir: z.string().describe(
|
|
1334
1607
|
"Directory of the source part PNGs (resolved against cwd)."
|
|
@@ -1345,7 +1618,7 @@ function createIkiMcpServer() {
|
|
|
1345
1618
|
h: z.number()
|
|
1346
1619
|
}).partial()
|
|
1347
1620
|
).optional().describe(
|
|
1348
|
-
"Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, mouth, mouth_open, eye_L, eye_R, iris_L, iris_R, lash_L, lash_R, brow_L, brow_R, hair_front \u2014 e.g. `{ eye_L: { cx: 660 } }`. `w` and `h` are integers in 1..1100; omit `h` to keep the part's own aspect, set it to stretch (set it on a sclera and its lash together, or the blink fold tears)."
|
|
1621
|
+
"Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, nose, mouth, mouth_open, eye_L, eye_R, iris_L, iris_R, lash_L, lash_R, brow_L, brow_R, hair_front \u2014 e.g. `{ eye_L: { cx: 660 } }`. `w` and `h` are integers in 1..1100; omit `h` to keep the part's own aspect, set it to stretch (set it on a sclera and its lash together, or the blink fold tears)."
|
|
1349
1622
|
)
|
|
1350
1623
|
}
|
|
1351
1624
|
},
|
|
@@ -1395,10 +1668,49 @@ function createIkiMcpServer() {
|
|
|
1395
1668
|
}
|
|
1396
1669
|
}
|
|
1397
1670
|
);
|
|
1671
|
+
server.registerTool(
|
|
1672
|
+
"measure_turn_reference",
|
|
1673
|
+
{
|
|
1674
|
+
description: "Measures how far a head turns between two images of the same character \u2014 a front view and a turned one, at the SAME scale \u2014 as three ratios, so a rigged turn can be compared to a reference turn (or to an earlier rig) without registering the images: farEyeRatio (turned far/near iris width over the same ratio at rest), eyeShift (how far the eye pair slides across the head, in units of the FRONT head half-width; NEGATIVE = toward the image's left), silhouetteRatio (head half-width, turned over front). The ratios divide out crop and position, not scale, so the pair must already share framing and head size; since a yaw never changes iris height, a pair whose mean iris height differs by more than 10% is refused instead of measured as a turn. Reads engine renders (transparent backdrop) and opaque reference art alike.",
|
|
1675
|
+
inputSchema: {
|
|
1676
|
+
front: z.string().describe("Front-facing PNG file path (resolved against cwd)."),
|
|
1677
|
+
turned: z.string().describe("The same character turned, as a PNG file path."),
|
|
1678
|
+
iris: z.object({
|
|
1679
|
+
hueMin: z.number().min(0).max(360),
|
|
1680
|
+
hueMax: z.number().min(0).max(360),
|
|
1681
|
+
satMin: z.number().min(0).max(1)
|
|
1682
|
+
}).partial().optional().describe(
|
|
1683
|
+
"Iris colour window override merged over the violet default (hue 230..300, sat > 0.22) \u2014 set it for a character whose eyes are another colour."
|
|
1684
|
+
),
|
|
1685
|
+
debugDir: z.string().optional().describe(
|
|
1686
|
+
"Existing directory (resolved against cwd; confined to the working directory) to write `front-<name>.debug.png` / `turned-<name>.debug.png` overlays into, with the iris boxes and head edges drawn."
|
|
1687
|
+
)
|
|
1688
|
+
}
|
|
1689
|
+
},
|
|
1690
|
+
async (args) => {
|
|
1691
|
+
try {
|
|
1692
|
+
const r = await measureTurnReference(args);
|
|
1693
|
+
const text = r.ok ? formatTurnReport(r) : `INVALID: ${r.error}`;
|
|
1694
|
+
return {
|
|
1695
|
+
content: [{ type: "text", text }],
|
|
1696
|
+
structuredContent: { ...r }
|
|
1697
|
+
};
|
|
1698
|
+
} catch (err) {
|
|
1699
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
1700
|
+
return {
|
|
1701
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
1702
|
+
structuredContent: { ok: false, error },
|
|
1703
|
+
isError: true
|
|
1704
|
+
};
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
);
|
|
1398
1708
|
return server;
|
|
1399
1709
|
}
|
|
1400
1710
|
|
|
1401
1711
|
export {
|
|
1712
|
+
measureTurnReference,
|
|
1713
|
+
formatTurnReport,
|
|
1402
1714
|
validateIki,
|
|
1403
1715
|
describeIki,
|
|
1404
1716
|
listStandardParameters,
|
|
@@ -1408,4 +1720,4 @@ export {
|
|
|
1408
1720
|
composeLayersFromParts,
|
|
1409
1721
|
createIkiMcpServer
|
|
1410
1722
|
};
|
|
1411
|
-
//# sourceMappingURL=chunk-
|
|
1723
|
+
//# sourceMappingURL=chunk-JPWDKP43.mjs.map
|