@ikijs/mcp 0.9.0 → 0.11.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 +6 -5
- package/dist/{chunk-R6LYJVYC.mjs → chunk-LOOIWB2J.mjs} +219 -63
- package/dist/chunk-LOOIWB2J.mjs.map +1 -0
- package/dist/cli.js +218 -62
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +31 -4
- package/dist/index.d.ts +31 -4
- package/dist/index.js +218 -62
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
- package/dist/chunk-R6LYJVYC.mjs.map +0 -1
package/dist/cli.mjs
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -66,7 +66,9 @@ interface AutoRigInput {
|
|
|
66
66
|
/** Output `.iki` path (relative paths resolve against the process cwd). */
|
|
67
67
|
outputPath?: string;
|
|
68
68
|
/** Palette-quantize the atlas PNG to this many colours (integer, 2..256).
|
|
69
|
-
* Omitted = lossless. See renderAtlasToDataUri for the size/quality trade.
|
|
69
|
+
* Omitted = lossless. See renderAtlasToDataUri for the size/quality trade.
|
|
70
|
+
* A `nose` layer is left out of the quantized page, on a second, lossless
|
|
71
|
+
* page of its own, because the palette rims a soft-shaded nose's feather. */
|
|
70
72
|
quantizeColors?: number;
|
|
71
73
|
/** Head-turn cues to fit the rig to, as `measure_turn_reference` reports
|
|
72
74
|
* them. */
|
|
@@ -84,6 +86,8 @@ type AutoRigResult = {
|
|
|
84
86
|
height: number;
|
|
85
87
|
};
|
|
86
88
|
partCount: number;
|
|
89
|
+
/** The atlas's embedded size: its data URI's length, summed over both
|
|
90
|
+
* pages when a quantized rig puts the nose on a page of its own. */
|
|
87
91
|
atlasBytes: number;
|
|
88
92
|
/** The head's half-width at the eye row, measured off the layers.
|
|
89
93
|
* Absent when the opaque (alpha >= 128) union has no span there — a
|
|
@@ -129,6 +133,17 @@ type AutoRigResult = {
|
|
|
129
133
|
left?: IrisStrand;
|
|
130
134
|
right?: IrisStrand;
|
|
131
135
|
};
|
|
136
|
+
/** The nose's dense core, canvas px — its pixels at alpha >= 128, not
|
|
137
|
+
* grown — the box handed to the generator as the nose layer's
|
|
138
|
+
* `denseCore` and, there, its turn landmark. Absent without a `nose`
|
|
139
|
+
* layer, or when the nose has no pixel at that threshold (painted
|
|
140
|
+
* wholly translucent). */
|
|
141
|
+
noseCore?: {
|
|
142
|
+
x: number;
|
|
143
|
+
y: number;
|
|
144
|
+
w: number;
|
|
145
|
+
h: number;
|
|
146
|
+
};
|
|
132
147
|
/** What the turn solve settled on — the cues the rig reaches, the
|
|
133
148
|
* targets it had to cut down (`clamped`) and, per side whose far iris
|
|
134
149
|
* the bangs' run still covers at some far stop, how much of it
|
|
@@ -154,7 +169,12 @@ type AutoRigResult = {
|
|
|
154
169
|
* run it would slide under are measured on the iris's own centre row too
|
|
155
170
|
* (`strandEdges`), so the turn keeps the far iris from sliding under the bangs
|
|
156
171
|
* any further than it is painted; a run narrower than half the iris there is
|
|
157
|
-
* hair detail the iris may cross, and is skipped.
|
|
172
|
+
* hair detail the iris may cross, and is skipped. The nose layer alone also
|
|
173
|
+
* gets its dense core measured (`LayerInput.denseCore`, returned as
|
|
174
|
+
* `noseCore`), so a shaded nose is fitted and tilted by the drawing inside its
|
|
175
|
+
* feather rather than by the feather itself. Under `quantizeColors` that nose
|
|
176
|
+
* is atlased alone on a second, lossless page, which the palette would
|
|
177
|
+
* otherwise rim; `atlasBytes` sums both pages.
|
|
158
178
|
*
|
|
159
179
|
* Re-host of examples/editor/src/store.ts `importLayerSet` with the three DOM
|
|
160
180
|
* pixel functions swapped for the sharp-backed ./node-images helpers; the pure
|
|
@@ -242,7 +262,8 @@ declare function formatMeasureReport(result: MeasureReport & {
|
|
|
242
262
|
* Compose AI-generated part PNGs into canvas-aligned, role-named layers for the
|
|
243
263
|
* Iki auto-rig (`auto_rig_from_layers` / `@ikijs/editor` generateIkiFromLayerSet).
|
|
244
264
|
* Each part is alpha-trimmed, resized to a target width, optionally mirrored,
|
|
245
|
-
* and pasted at a chosen center on a shared transparent CANVAS x CANVAS canvas
|
|
265
|
+
* and pasted at a chosen center on a shared transparent CANVAS x CANVAS canvas
|
|
266
|
+
* — the nose by its dense core, the drawing inside a soft nose's feather.
|
|
246
267
|
*
|
|
247
268
|
* Ports the composer that shipped as a script in the Claude Code plugin, so a
|
|
248
269
|
* plugin user gets it from the MCP server instead of an ad-hoc `sharp` install.
|
|
@@ -277,7 +298,6 @@ declare const DEFAULT_LAYOUT: {
|
|
|
277
298
|
nose: {
|
|
278
299
|
src: string;
|
|
279
300
|
cx: number;
|
|
280
|
-
cy: number;
|
|
281
301
|
w: number;
|
|
282
302
|
optional: true;
|
|
283
303
|
};
|
|
@@ -380,6 +400,13 @@ interface ComposeInput {
|
|
|
380
400
|
*/
|
|
381
401
|
outDir: string;
|
|
382
402
|
layout?: LayoutOverride;
|
|
403
|
+
/**
|
|
404
|
+
* Part files to flip left-right as they are read, e.g. `["eyewhite.png"]`
|
|
405
|
+
* for an eye drawn facing the other way. It flips the SOURCE, so every role
|
|
406
|
+
* cut from it flips together — the eye pair's sclera and lash stay in one
|
|
407
|
+
* frame, which a per-role flag would leave to the caller to keep in sync.
|
|
408
|
+
*/
|
|
409
|
+
mirrorParts?: string[];
|
|
383
410
|
}
|
|
384
411
|
interface ComposedLayer {
|
|
385
412
|
role: Role;
|
package/dist/index.d.ts
CHANGED
|
@@ -66,7 +66,9 @@ interface AutoRigInput {
|
|
|
66
66
|
/** Output `.iki` path (relative paths resolve against the process cwd). */
|
|
67
67
|
outputPath?: string;
|
|
68
68
|
/** Palette-quantize the atlas PNG to this many colours (integer, 2..256).
|
|
69
|
-
* Omitted = lossless. See renderAtlasToDataUri for the size/quality trade.
|
|
69
|
+
* Omitted = lossless. See renderAtlasToDataUri for the size/quality trade.
|
|
70
|
+
* A `nose` layer is left out of the quantized page, on a second, lossless
|
|
71
|
+
* page of its own, because the palette rims a soft-shaded nose's feather. */
|
|
70
72
|
quantizeColors?: number;
|
|
71
73
|
/** Head-turn cues to fit the rig to, as `measure_turn_reference` reports
|
|
72
74
|
* them. */
|
|
@@ -84,6 +86,8 @@ type AutoRigResult = {
|
|
|
84
86
|
height: number;
|
|
85
87
|
};
|
|
86
88
|
partCount: number;
|
|
89
|
+
/** The atlas's embedded size: its data URI's length, summed over both
|
|
90
|
+
* pages when a quantized rig puts the nose on a page of its own. */
|
|
87
91
|
atlasBytes: number;
|
|
88
92
|
/** The head's half-width at the eye row, measured off the layers.
|
|
89
93
|
* Absent when the opaque (alpha >= 128) union has no span there — a
|
|
@@ -129,6 +133,17 @@ type AutoRigResult = {
|
|
|
129
133
|
left?: IrisStrand;
|
|
130
134
|
right?: IrisStrand;
|
|
131
135
|
};
|
|
136
|
+
/** The nose's dense core, canvas px — its pixels at alpha >= 128, not
|
|
137
|
+
* grown — the box handed to the generator as the nose layer's
|
|
138
|
+
* `denseCore` and, there, its turn landmark. Absent without a `nose`
|
|
139
|
+
* layer, or when the nose has no pixel at that threshold (painted
|
|
140
|
+
* wholly translucent). */
|
|
141
|
+
noseCore?: {
|
|
142
|
+
x: number;
|
|
143
|
+
y: number;
|
|
144
|
+
w: number;
|
|
145
|
+
h: number;
|
|
146
|
+
};
|
|
132
147
|
/** What the turn solve settled on — the cues the rig reaches, the
|
|
133
148
|
* targets it had to cut down (`clamped`) and, per side whose far iris
|
|
134
149
|
* the bangs' run still covers at some far stop, how much of it
|
|
@@ -154,7 +169,12 @@ type AutoRigResult = {
|
|
|
154
169
|
* run it would slide under are measured on the iris's own centre row too
|
|
155
170
|
* (`strandEdges`), so the turn keeps the far iris from sliding under the bangs
|
|
156
171
|
* any further than it is painted; a run narrower than half the iris there is
|
|
157
|
-
* hair detail the iris may cross, and is skipped.
|
|
172
|
+
* hair detail the iris may cross, and is skipped. The nose layer alone also
|
|
173
|
+
* gets its dense core measured (`LayerInput.denseCore`, returned as
|
|
174
|
+
* `noseCore`), so a shaded nose is fitted and tilted by the drawing inside its
|
|
175
|
+
* feather rather than by the feather itself. Under `quantizeColors` that nose
|
|
176
|
+
* is atlased alone on a second, lossless page, which the palette would
|
|
177
|
+
* otherwise rim; `atlasBytes` sums both pages.
|
|
158
178
|
*
|
|
159
179
|
* Re-host of examples/editor/src/store.ts `importLayerSet` with the three DOM
|
|
160
180
|
* pixel functions swapped for the sharp-backed ./node-images helpers; the pure
|
|
@@ -242,7 +262,8 @@ declare function formatMeasureReport(result: MeasureReport & {
|
|
|
242
262
|
* Compose AI-generated part PNGs into canvas-aligned, role-named layers for the
|
|
243
263
|
* Iki auto-rig (`auto_rig_from_layers` / `@ikijs/editor` generateIkiFromLayerSet).
|
|
244
264
|
* Each part is alpha-trimmed, resized to a target width, optionally mirrored,
|
|
245
|
-
* and pasted at a chosen center on a shared transparent CANVAS x CANVAS canvas
|
|
265
|
+
* and pasted at a chosen center on a shared transparent CANVAS x CANVAS canvas
|
|
266
|
+
* — the nose by its dense core, the drawing inside a soft nose's feather.
|
|
246
267
|
*
|
|
247
268
|
* Ports the composer that shipped as a script in the Claude Code plugin, so a
|
|
248
269
|
* plugin user gets it from the MCP server instead of an ad-hoc `sharp` install.
|
|
@@ -277,7 +298,6 @@ declare const DEFAULT_LAYOUT: {
|
|
|
277
298
|
nose: {
|
|
278
299
|
src: string;
|
|
279
300
|
cx: number;
|
|
280
|
-
cy: number;
|
|
281
301
|
w: number;
|
|
282
302
|
optional: true;
|
|
283
303
|
};
|
|
@@ -380,6 +400,13 @@ interface ComposeInput {
|
|
|
380
400
|
*/
|
|
381
401
|
outDir: string;
|
|
382
402
|
layout?: LayoutOverride;
|
|
403
|
+
/**
|
|
404
|
+
* Part files to flip left-right as they are read, e.g. `["eyewhite.png"]`
|
|
405
|
+
* for an eye drawn facing the other way. It flips the SOURCE, so every role
|
|
406
|
+
* cut from it flips together — the eye pair's sclera and lash stay in one
|
|
407
|
+
* frame, which a per-role flag would leave to the caller to keep in sync.
|
|
408
|
+
*/
|
|
409
|
+
mirrorParts?: string[];
|
|
383
410
|
}
|
|
384
411
|
interface ComposedLayer {
|
|
385
412
|
role: Role;
|
package/dist/index.js
CHANGED
|
@@ -270,6 +270,24 @@ var BG_HUE_MAX = 260;
|
|
|
270
270
|
var BG_SAT_MAX = 0.2;
|
|
271
271
|
var BG_V_MIN = 0.55;
|
|
272
272
|
var BG_V_BLACK = 0.03;
|
|
273
|
+
function denseCoreOf(rgba, width, height) {
|
|
274
|
+
let minX = width;
|
|
275
|
+
let minY = height;
|
|
276
|
+
let maxX = -1;
|
|
277
|
+
let maxY = -1;
|
|
278
|
+
for (let y = 0; y < height; y++) {
|
|
279
|
+
for (let x = 0; x < width; x++) {
|
|
280
|
+
if (rgba[(y * width + x) * 4 + 3] >= ALPHA_OPAQUE) {
|
|
281
|
+
if (x < minX) minX = x;
|
|
282
|
+
if (x > maxX) maxX = x;
|
|
283
|
+
if (y < minY) minY = y;
|
|
284
|
+
if (y > maxY) maxY = y;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (maxX < 0) return null;
|
|
289
|
+
return { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 };
|
|
290
|
+
}
|
|
273
291
|
function rgbToHsv(r, g, b) {
|
|
274
292
|
r /= 255;
|
|
275
293
|
g /= 255;
|
|
@@ -791,6 +809,19 @@ function facePlateHalfOf(layers) {
|
|
|
791
809
|
return face.cropW / 2;
|
|
792
810
|
}
|
|
793
811
|
var STRAND_MIN_RUN_FRACTION = 0.5;
|
|
812
|
+
var LOSSLESS_PAGE_ROLE = "nose";
|
|
813
|
+
async function renderAtlasPage(crops, quantizeColors) {
|
|
814
|
+
const layout = (0, import_editor2.packAtlas)(
|
|
815
|
+
crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
|
|
816
|
+
);
|
|
817
|
+
if (layout.pageWidth * layout.pageHeight > MAX_ATLAS_AREA) {
|
|
818
|
+
throw new AutoRigInputError(
|
|
819
|
+
`atlas page ${layout.pageWidth}x${layout.pageHeight} exceeds max area ${MAX_ATLAS_AREA}`
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
const dataUri = await renderAtlasToDataUri(crops, layout, quantizeColors);
|
|
823
|
+
return { crops, layout, dataUri };
|
|
824
|
+
}
|
|
794
825
|
async function autoRigFromLayers(input) {
|
|
795
826
|
try {
|
|
796
827
|
const layers = input.layers;
|
|
@@ -902,10 +933,14 @@ async function autoRigFromLayers(input) {
|
|
|
902
933
|
);
|
|
903
934
|
}
|
|
904
935
|
layer.rowHalfWidths = rowHalfWidths;
|
|
936
|
+
} else if (role === "nose") {
|
|
937
|
+
const core = denseCoreOf(png.rgba, canvasW, canvasH);
|
|
938
|
+
if (core !== null) layer.denseCore = core;
|
|
905
939
|
}
|
|
906
940
|
layerInputs.push(layer);
|
|
907
941
|
crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
|
|
908
942
|
}
|
|
943
|
+
const noseCore = layerInputs.find((l) => l.role === "nose")?.denseCore;
|
|
909
944
|
const eyeRow = eyeRowOf(layerInputs);
|
|
910
945
|
const span = foregroundSpan(
|
|
911
946
|
opaque,
|
|
@@ -1015,46 +1050,58 @@ async function autoRigFromLayers(input) {
|
|
|
1015
1050
|
throw new AutoRigInputError(e.message);
|
|
1016
1051
|
}
|
|
1017
1052
|
const doc = new import_editor2.EditorDocument(model);
|
|
1018
|
-
const
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
)
|
|
1025
|
-
|
|
1026
|
-
const
|
|
1027
|
-
if (
|
|
1053
|
+
const losslessCrop = quantizeColors === void 0 ? void 0 : crops.find((c) => c.id === LOSSLESS_PAGE_ROLE);
|
|
1054
|
+
const pages = losslessCrop === void 0 ? [await renderAtlasPage(crops, quantizeColors)] : [
|
|
1055
|
+
await renderAtlasPage(
|
|
1056
|
+
crops.filter((c) => c !== losslessCrop),
|
|
1057
|
+
quantizeColors
|
|
1058
|
+
),
|
|
1059
|
+
await renderAtlasPage([losslessCrop], void 0)
|
|
1060
|
+
];
|
|
1061
|
+
const atlasBytes = pages.reduce((sum, p) => sum + p.dataUri.length, 0);
|
|
1062
|
+
if (atlasBytes > MAX_OUTPUT_BYTES) {
|
|
1028
1063
|
throw new AutoRigInputError(
|
|
1029
|
-
`atlas data URI ${
|
|
1064
|
+
`atlas data URI ${atlasBytes} bytes exceeds ${MAX_OUTPUT_BYTES}`
|
|
1030
1065
|
);
|
|
1031
1066
|
}
|
|
1032
|
-
const partTextureAssignments =
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1067
|
+
const partTextureAssignments = pages.flatMap(
|
|
1068
|
+
({ crops: pageCrops, layout }) => pageCrops.map((crop) => {
|
|
1069
|
+
const placement2 = layout.placements.find((p) => p.id === crop.id);
|
|
1070
|
+
if (placement2 === void 0) {
|
|
1071
|
+
throw new Error(`auto-rig: no atlas placement for "${crop.id}"`);
|
|
1072
|
+
}
|
|
1073
|
+
return {
|
|
1074
|
+
partId: crop.id,
|
|
1075
|
+
uv: (0, import_editor2.uvRectFor)(placement2, {
|
|
1076
|
+
width: layout.pageWidth,
|
|
1077
|
+
height: layout.pageHeight
|
|
1078
|
+
})
|
|
1079
|
+
};
|
|
1080
|
+
})
|
|
1081
|
+
);
|
|
1082
|
+
doc.applyAtlas({
|
|
1083
|
+
textures: [{ source: pages[0].dataUri }],
|
|
1084
|
+
partTextureAssignments
|
|
1044
1085
|
});
|
|
1045
|
-
doc.
|
|
1046
|
-
|
|
1086
|
+
let patched = doc.getModel();
|
|
1087
|
+
if (losslessCrop !== void 0) {
|
|
1088
|
+
patched = structuredClone(patched);
|
|
1089
|
+
patched.textures = pages.map((p) => ({ source: p.dataUri }));
|
|
1090
|
+
patched.parts.find((p) => p.id === losslessCrop.id).texture.index = 1;
|
|
1091
|
+
}
|
|
1092
|
+
const finalModel = (0, import_format.parseIkiModel)(patched);
|
|
1047
1093
|
writeFileAtomic(outPath, JSON.stringify(finalModel));
|
|
1048
1094
|
return {
|
|
1049
1095
|
ok: true,
|
|
1050
1096
|
path: outPath,
|
|
1051
1097
|
canvas: { width: canvasW, height: canvasH },
|
|
1052
1098
|
partCount: finalModel.parts.length,
|
|
1053
|
-
atlasBytes
|
|
1099
|
+
atlasBytes,
|
|
1054
1100
|
...headHalfWidth === void 0 ? {} : { headHalfWidth },
|
|
1055
1101
|
headHalfWidthApplied,
|
|
1056
1102
|
...headEdges === void 0 ? {} : { headEdges },
|
|
1057
1103
|
...strandEdges === void 0 ? {} : { strandEdges },
|
|
1104
|
+
...noseCore === void 0 ? {} : { noseCore },
|
|
1058
1105
|
...turn === void 0 ? {} : { turn }
|
|
1059
1106
|
};
|
|
1060
1107
|
} catch (err) {
|
|
@@ -1078,7 +1125,8 @@ var EYE_ASPECT_MIN = 0.5;
|
|
|
1078
1125
|
var IRIS_OFFSET_MAX_X = 3;
|
|
1079
1126
|
var IRIS_OFFSET_MAX_Y = 10;
|
|
1080
1127
|
var EDGE_SOLID_MAX = 0.5;
|
|
1081
|
-
var
|
|
1128
|
+
var LASH_STRAY_TOL_FRAC = 0.02;
|
|
1129
|
+
var EYE_FACING_TOL_FRAC = 0.02;
|
|
1082
1130
|
var EDGE_RUN_MIN_PX = 40;
|
|
1083
1131
|
var FLAT_CUT_MAX_FRAC = 0.1;
|
|
1084
1132
|
var FLAT_CUT_MIN_PX = 40;
|
|
@@ -1159,6 +1207,21 @@ async function layerStats(filePath) {
|
|
|
1159
1207
|
};
|
|
1160
1208
|
}
|
|
1161
1209
|
var pct = (v) => `${(v * 100).toFixed(0)}%`;
|
|
1210
|
+
async function lashOffSclera(eyePath, lashPath) {
|
|
1211
|
+
const eye = await decodePng(eyePath);
|
|
1212
|
+
const lash = await decodePng(lashPath);
|
|
1213
|
+
let opaque = 0;
|
|
1214
|
+
let off = 0;
|
|
1215
|
+
for (let y = 0; y < lash.height; y++) {
|
|
1216
|
+
for (let x = 0; x < lash.width; x++) {
|
|
1217
|
+
if (lash.rgba[(y * lash.width + x) * 4 + 3] < 128) continue;
|
|
1218
|
+
opaque++;
|
|
1219
|
+
const onEye = x < eye.width && y < eye.height;
|
|
1220
|
+
if (!onEye || eye.rgba[(y * eye.width + x) * 4 + 3] <= 8) off++;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
return { opaque, off };
|
|
1224
|
+
}
|
|
1162
1225
|
async function measureDir(absDir) {
|
|
1163
1226
|
const files = import_node_fs2.default.readdirSync(absDir).filter((f) => f.endsWith(".png") && f !== "preview.png").sort();
|
|
1164
1227
|
if (files.length === 0) {
|
|
@@ -1240,12 +1303,28 @@ async function measureDir(absDir) {
|
|
|
1240
1303
|
}
|
|
1241
1304
|
}
|
|
1242
1305
|
if (lash) {
|
|
1243
|
-
const
|
|
1306
|
+
const { opaque, off } = await lashOffSclera(
|
|
1307
|
+
import_node_path4.default.join(absDir, `eye_${side}.png`),
|
|
1308
|
+
import_node_path4.default.join(absDir, `lash_${side}.png`)
|
|
1309
|
+
);
|
|
1244
1310
|
const dTop = lash.marginTop - eye.marginTop;
|
|
1245
|
-
|
|
1311
|
+
const faults = [
|
|
1312
|
+
...off > opaque * LASH_STRAY_TOL_FRAC ? [`${off} of its ${opaque} opaque px lie off eye_${side}'s sclera`] : [],
|
|
1313
|
+
...Math.abs(dTop) > 0.5 ? [`its top edge is ${dTop.toFixed(1)} px off the sclera's`] : []
|
|
1314
|
+
];
|
|
1315
|
+
if (faults.length > 0) {
|
|
1246
1316
|
warnings.push(
|
|
1247
|
-
`lash_${side}:
|
|
1317
|
+
`lash_${side}: ${faults.join(" and ")} \u2014 they are split from one source and MUST share cx/cy/w/h in layout, or the blink fold tears. Retune to match.`
|
|
1248
1318
|
);
|
|
1319
|
+
} else {
|
|
1320
|
+
const bareLeft = lash.marginLeft - eye.marginLeft;
|
|
1321
|
+
const bareRight = lash.marginRight - eye.marginRight;
|
|
1322
|
+
const [bareOuter, bareInner] = side === "R" ? [bareLeft, bareRight] : [bareRight, bareLeft];
|
|
1323
|
+
if ((bareOuter - bareInner) / eye.w > EYE_FACING_TOL_FRAC) {
|
|
1324
|
+
warnings.push(
|
|
1325
|
+
`eye_${side}: its lash stops ${pct(bareOuter / eye.w)} of the eye width short of the outer corner and ${pct(bareInner / eye.w)} short of the nose side \u2014 the eyewhite is drawn facing the other way, lash wing at the inner corner. The composer reads eyewhite.png as the eye on the screen LEFT: lash wing at the image's left end, lash-free tear duct at its right. Free fix: compose again with mirrorParts: ["eyewhite.png"] \u2014 no regeneration.`
|
|
1326
|
+
);
|
|
1327
|
+
}
|
|
1249
1328
|
}
|
|
1250
1329
|
}
|
|
1251
1330
|
}
|
|
@@ -1309,6 +1388,7 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
|
|
|
1309
1388
|
var EYEWHITE_SRC = "eyewhite.png";
|
|
1310
1389
|
var EYE_LASH_LUMA = 120;
|
|
1311
1390
|
var LASH_KEEP_FRACTION = 0.5;
|
|
1391
|
+
var NOSE_TIP_AT = 0.86;
|
|
1312
1392
|
var DEFAULT_LAYOUT = {
|
|
1313
1393
|
// Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
|
|
1314
1394
|
// without them still composes (head-only character).
|
|
@@ -1320,9 +1400,11 @@ var DEFAULT_LAYOUT = {
|
|
|
1320
1400
|
face: { src: "face.png", cx: 550, cy: 475, w: 400 },
|
|
1321
1401
|
// The nose, drawn on its own (the face is drawn without one): the auto-rig
|
|
1322
1402
|
// leads the head turn with it and keys the other features' slide on its
|
|
1323
|
-
// presence, so without it the features stay on the face plate.
|
|
1324
|
-
//
|
|
1325
|
-
|
|
1403
|
+
// presence, so without it the features stay on the face plate. `w` is the
|
|
1404
|
+
// width of its dense core, and it has no `cy`: its row comes from the tip
|
|
1405
|
+
// rule (NOSE_TIP_AT), between the eyes and the mouth, so it follows them
|
|
1406
|
+
// when they are retuned. OPTIONAL: a parts dir without it composes.
|
|
1407
|
+
nose: { src: "nose.png", cx: 550, w: 40, optional: true },
|
|
1326
1408
|
mouth: { src: "mouth.png", cx: 550, cy: 619, w: 68 },
|
|
1327
1409
|
// Cross-fades with `mouth` on ParamMouthOpenY (opacity, not scaleY) once the
|
|
1328
1410
|
// auto-rig sees both roles. Same cx/w as `mouth` so the lip width matches;
|
|
@@ -1377,6 +1459,12 @@ var ORDER = [
|
|
|
1377
1459
|
"brow_R",
|
|
1378
1460
|
"hair_front"
|
|
1379
1461
|
];
|
|
1462
|
+
var PART_FILES = [
|
|
1463
|
+
...new Set(
|
|
1464
|
+
Object.values(DEFAULT_LAYOUT).map((cfg) => cfg.src).filter((src) => !src.startsWith("eyewhite_"))
|
|
1465
|
+
),
|
|
1466
|
+
EYEWHITE_SRC
|
|
1467
|
+
];
|
|
1380
1468
|
function resolveLayout(overrides) {
|
|
1381
1469
|
const resolved = { ...DEFAULT_LAYOUT };
|
|
1382
1470
|
if (overrides === void 0) return resolved;
|
|
@@ -1387,7 +1475,7 @@ function resolveLayout(overrides) {
|
|
|
1387
1475
|
);
|
|
1388
1476
|
}
|
|
1389
1477
|
if (override === void 0) continue;
|
|
1390
|
-
const
|
|
1478
|
+
const set = {};
|
|
1391
1479
|
for (const field of ["cx", "cy"]) {
|
|
1392
1480
|
const value = override[field];
|
|
1393
1481
|
if (value === void 0) continue;
|
|
@@ -1396,7 +1484,7 @@ function resolveLayout(overrides) {
|
|
|
1396
1484
|
`layout.${role}.${field} must be a finite number, got ${String(value)}`
|
|
1397
1485
|
);
|
|
1398
1486
|
}
|
|
1399
|
-
|
|
1487
|
+
set[field] = value;
|
|
1400
1488
|
}
|
|
1401
1489
|
for (const field of ["w", "h"]) {
|
|
1402
1490
|
const value = override[field];
|
|
@@ -1406,12 +1494,26 @@ function resolveLayout(overrides) {
|
|
|
1406
1494
|
`layout.${role}.${field} must be an integer in 1..${CANVAS}, got ${String(value)}`
|
|
1407
1495
|
);
|
|
1408
1496
|
}
|
|
1409
|
-
|
|
1497
|
+
set[field] = value;
|
|
1410
1498
|
}
|
|
1411
|
-
|
|
1499
|
+
const key = role;
|
|
1500
|
+
if (key === "nose") resolved.nose = { ...resolved.nose, ...set };
|
|
1501
|
+
else resolved[key] = { ...resolved[key], ...set };
|
|
1412
1502
|
}
|
|
1413
1503
|
return resolved;
|
|
1414
1504
|
}
|
|
1505
|
+
function resolveMirrorParts(names) {
|
|
1506
|
+
const mirrored = /* @__PURE__ */ new Set();
|
|
1507
|
+
for (const [i, name] of (names ?? []).entries()) {
|
|
1508
|
+
if (!PART_FILES.includes(name)) {
|
|
1509
|
+
throw new AutoRigInputError(
|
|
1510
|
+
`mirrorParts[${i}]: unknown part ${JSON.stringify(name)} \u2014 expected one of ${PART_FILES.join(", ")}`
|
|
1511
|
+
);
|
|
1512
|
+
}
|
|
1513
|
+
mirrored.add(name);
|
|
1514
|
+
}
|
|
1515
|
+
return mirrored;
|
|
1516
|
+
}
|
|
1415
1517
|
function keyWhiteToAlpha(rgba) {
|
|
1416
1518
|
const keyed = Buffer.from(rgba);
|
|
1417
1519
|
for (let i = 0; i < keyed.length; i += 4) {
|
|
@@ -1421,7 +1523,16 @@ function keyWhiteToAlpha(rgba) {
|
|
|
1421
1523
|
}
|
|
1422
1524
|
return keyed;
|
|
1423
1525
|
}
|
|
1424
|
-
async function
|
|
1526
|
+
async function coreOf(png) {
|
|
1527
|
+
const { data, info } = await (0, import_sharp2.default)(png).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
1528
|
+
return denseCoreOf(data, info.width, info.height) ?? {
|
|
1529
|
+
x: 0,
|
|
1530
|
+
y: 0,
|
|
1531
|
+
w: info.width,
|
|
1532
|
+
h: info.height
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
async function partBuffer(role, cfg, partsDir, inMemory, flipSource) {
|
|
1425
1536
|
let img;
|
|
1426
1537
|
if (inMemory !== void 0) {
|
|
1427
1538
|
img = (0, import_sharp2.default)(inMemory);
|
|
@@ -1439,35 +1550,58 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
|
|
|
1439
1550
|
});
|
|
1440
1551
|
}
|
|
1441
1552
|
if (!cfg.noTrim) img = img.trim({ threshold: 12 });
|
|
1442
|
-
if (cfg.mirror) img = img.flop();
|
|
1553
|
+
if ((cfg.mirror ?? false) !== flipSource) img = img.flop();
|
|
1443
1554
|
const trimmed = await img.png().toBuffer({ resolveWithObject: true });
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1555
|
+
const { width: trimmedW, height: trimmedH } = trimmed.info;
|
|
1556
|
+
const sized = role === "nose" ? await coreOf(trimmed.data) : { x: 0, y: 0, w: trimmedW, h: trimmedH };
|
|
1557
|
+
const width = Math.round(trimmedW * cfg.w / sized.w);
|
|
1558
|
+
const height = cfg.h === void 0 ? Math.round(trimmedH * width / trimmedW) : Math.round(trimmedH * cfg.h / sized.h);
|
|
1559
|
+
if (width > CANVAS || height > CANVAS) {
|
|
1560
|
+
const field = width > CANVAS || cfg.h === void 0 ? "w" : "h";
|
|
1561
|
+
throw new AutoRigInputError(
|
|
1562
|
+
`layout.${role}.${field}: resized part ${width}x${height} exceeds the ${CANVAS} canvas`
|
|
1447
1563
|
);
|
|
1448
|
-
if (scaledH > CANVAS) {
|
|
1449
|
-
throw new AutoRigInputError(
|
|
1450
|
-
`layout.${role}.w: resized part ${cfg.w}x${scaledH} exceeds the ${CANVAS} canvas`
|
|
1451
|
-
);
|
|
1452
|
-
}
|
|
1453
1564
|
}
|
|
1454
|
-
const resized = await (0, import_sharp2.default)(trimmed.data).resize(
|
|
1455
|
-
cfg.h === void 0 ? { width: cfg.w } : { width: cfg.w, height: cfg.h, fit: "fill" }
|
|
1456
|
-
).png().toBuffer({ resolveWithObject: true });
|
|
1565
|
+
const resized = await (0, import_sharp2.default)(trimmed.data).resize(cfg.h === void 0 ? { width } : { width, height, fit: "fill" }).png().toBuffer({ resolveWithObject: true });
|
|
1457
1566
|
return {
|
|
1458
1567
|
buf: resized.data,
|
|
1459
1568
|
w: resized.info.width,
|
|
1460
1569
|
h: resized.info.height
|
|
1461
1570
|
};
|
|
1462
1571
|
}
|
|
1463
|
-
function
|
|
1464
|
-
const left = Math.round(cfg.cx - w / 2);
|
|
1465
|
-
const top = Math.round(cfg.cy - h / 2);
|
|
1572
|
+
function assertOnCanvas(source, w, h, left, top) {
|
|
1466
1573
|
if (left + w <= 0 || top + h <= 0 || left >= CANVAS || top >= CANVAS) {
|
|
1467
1574
|
throw new AutoRigInputError(
|
|
1468
|
-
|
|
1575
|
+
`${source}: the placed part (${w}x${h} at ${left},${top}) falls entirely outside the ${CANVAS} canvas`
|
|
1469
1576
|
);
|
|
1470
1577
|
}
|
|
1578
|
+
}
|
|
1579
|
+
function placement(role, cfg, w, h) {
|
|
1580
|
+
const left = Math.round(cfg.cx - w / 2);
|
|
1581
|
+
const top = Math.round(cfg.cy - h / 2);
|
|
1582
|
+
assertOnCanvas(`layout.${role}.cx/cy`, w, h, left, top);
|
|
1583
|
+
return { left, top };
|
|
1584
|
+
}
|
|
1585
|
+
function nosePlacement(layout, core, w, h) {
|
|
1586
|
+
const { cx, cy } = layout.nose;
|
|
1587
|
+
const left = Math.round(cx - (core.x + core.w / 2));
|
|
1588
|
+
let top;
|
|
1589
|
+
if (cy !== void 0) {
|
|
1590
|
+
top = Math.round(cy - (core.y + core.h / 2));
|
|
1591
|
+
} else {
|
|
1592
|
+
const eyeRow = (layout.eye_L.cy + layout.eye_R.cy) / 2;
|
|
1593
|
+
const tipRow = Math.round(
|
|
1594
|
+
eyeRow + NOSE_TIP_AT * (layout.mouth.cy - eyeRow)
|
|
1595
|
+
);
|
|
1596
|
+
top = tipRow - (core.y + core.h - 1);
|
|
1597
|
+
}
|
|
1598
|
+
assertOnCanvas(
|
|
1599
|
+
cy !== void 0 ? "layout.nose.cx/cy" : "layout.nose.cx/cy (cy unset: its tip row comes from layout.eye_L/eye_R/mouth.cy)",
|
|
1600
|
+
w,
|
|
1601
|
+
h,
|
|
1602
|
+
left,
|
|
1603
|
+
top
|
|
1604
|
+
);
|
|
1471
1605
|
return { left, top };
|
|
1472
1606
|
}
|
|
1473
1607
|
function blankCanvas() {
|
|
@@ -1536,28 +1670,47 @@ async function composeLayersFromParts(input) {
|
|
|
1536
1670
|
);
|
|
1537
1671
|
}
|
|
1538
1672
|
const layout = resolveLayout(input.layout);
|
|
1673
|
+
const mirrored = resolveMirrorParts(input.mirrorParts);
|
|
1539
1674
|
const split = await prepEyeSplit(partsDir);
|
|
1540
1675
|
const splitSources = /* @__PURE__ */ new Map([
|
|
1541
1676
|
["eyewhite_sclera.png", split.sclera],
|
|
1542
1677
|
["eyewhite_lash.png", split.lash]
|
|
1543
1678
|
]);
|
|
1544
|
-
const
|
|
1679
|
+
const placed = [];
|
|
1545
1680
|
const skipped = [];
|
|
1546
|
-
const preview = [];
|
|
1547
1681
|
for (const role of ORDER) {
|
|
1548
1682
|
const cfg = layout[role];
|
|
1683
|
+
const inMemory = splitSources.get(cfg.src);
|
|
1549
1684
|
const part = await partBuffer(
|
|
1550
1685
|
role,
|
|
1551
1686
|
cfg,
|
|
1552
1687
|
partsDir,
|
|
1553
|
-
|
|
1688
|
+
inMemory,
|
|
1689
|
+
// The split halves are cut from eyewhite.png, the file a caller names.
|
|
1690
|
+
mirrored.has(inMemory === void 0 ? cfg.src : EYEWHITE_SRC)
|
|
1554
1691
|
);
|
|
1555
1692
|
if (part === null) {
|
|
1556
1693
|
skipped.push(role);
|
|
1557
|
-
import_node_fs3.default.rmSync(import_node_path5.default.join(outDir, `${role}.png`), { force: true });
|
|
1558
1694
|
continue;
|
|
1559
1695
|
}
|
|
1560
|
-
const { left, top } = placement(role,
|
|
1696
|
+
const { left, top } = role === "nose" ? nosePlacement(layout, await coreOf(part.buf), part.w, part.h) : placement(role, layout[role], part.w, part.h);
|
|
1697
|
+
if (role === "lash_L" || role === "lash_R") {
|
|
1698
|
+
const eyeRole = role === "lash_L" ? "eye_L" : "eye_R";
|
|
1699
|
+
const eye = placed.find((p) => p.role === eyeRole);
|
|
1700
|
+
if (eye !== void 0 && (eye.left !== left || eye.top !== top || eye.part.w !== part.w || eye.part.h !== part.h)) {
|
|
1701
|
+
throw new AutoRigInputError(
|
|
1702
|
+
`layout.${role} places the lash at ${part.w}x${part.h} (${left},${top}), but layout.${eyeRole} places its sclera at ${eye.part.w}x${eye.part.h} (${eye.left},${eye.top}) \u2014 they are cut from one eyewhite into one frame, and the blink fold tears where they part. Set cx/cy/w/h the same on both.`
|
|
1703
|
+
);
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
placed.push({ role, part, left, top });
|
|
1707
|
+
}
|
|
1708
|
+
for (const role of skipped) {
|
|
1709
|
+
import_node_fs3.default.rmSync(import_node_path5.default.join(outDir, `${role}.png`), { force: true });
|
|
1710
|
+
}
|
|
1711
|
+
const layers = [];
|
|
1712
|
+
const preview = [];
|
|
1713
|
+
for (const { role, part, left, top } of placed) {
|
|
1561
1714
|
const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
|
|
1562
1715
|
const outPath = import_node_path5.default.join(outDir, `${role}.png`);
|
|
1563
1716
|
writeFileAtomic(outPath, layer);
|
|
@@ -1600,7 +1753,7 @@ async function composeLayersFromParts(input) {
|
|
|
1600
1753
|
var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
1601
1754
|
var import_zod = require("zod");
|
|
1602
1755
|
function createIkiMcpServer() {
|
|
1603
|
-
const server = new import_mcp.McpServer({ name: "iki", version: "0.
|
|
1756
|
+
const server = new import_mcp.McpServer({ name: "iki", version: "0.11.0" });
|
|
1604
1757
|
server.registerTool(
|
|
1605
1758
|
"validate_iki",
|
|
1606
1759
|
{
|
|
@@ -1679,7 +1832,7 @@ function createIkiMcpServer() {
|
|
|
1679
1832
|
"Output .iki path (resolved against cwd; parent dir must exist)."
|
|
1680
1833
|
),
|
|
1681
1834
|
quantizeColors: import_zod.z.number().int().min(2).max(256).optional().describe(
|
|
1682
|
-
"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."
|
|
1835
|
+
"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. A `nose` layer stays lossless on a second atlas page of its own, since the palette rims a soft-shaded nose's feather; the nose's own lossless page adds a content-dependent amount, since splitting it off also changes how page 0 re-quantizes without it; the result's `atlasBytes` sums both pages."
|
|
1683
1836
|
),
|
|
1684
1837
|
turnTargets: import_zod.z.object({
|
|
1685
1838
|
eyeShift: import_zod.z.number().optional(),
|
|
@@ -1727,7 +1880,10 @@ function createIkiMcpServer() {
|
|
|
1727
1880
|
h: import_zod.z.number()
|
|
1728
1881
|
}).partial()
|
|
1729
1882
|
).optional().describe(
|
|
1730
|
-
"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 (
|
|
1883
|
+
"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. The nose is sized and placed by its dense core (its pixels at alpha >= 128, the drawing inside a soft nose's feather), not its whole part: `w`/`h` size the core, `cx` centres it, and a set `cy` centres its rows. Its `cy` defaults to unset, which lands the core's bottom row (the tip) 0.86 of the way from the eye row down to the mouth's, so the nose follows a retuned eye or mouth row. eye_L/lash_L and eye_R/lash_R are cut from one eyewhite into one frame, so each pair must land on the same frame \u2014 a pair set to land apart is rejected."
|
|
1884
|
+
),
|
|
1885
|
+
mirrorParts: import_zod.z.array(import_zod.z.string()).optional().describe(
|
|
1886
|
+
"Part files to flip left-right as they are read, e.g. `[\"eyewhite.png\"]`. The composer reads eyewhite.png as the eye on the screen LEFT (lash wing at the image's left end, lash-free tear duct at its right) and brow.png as the brow on the screen RIGHT (thick head at the image's left end, tail at its right); a part drawn the other way round is fixed here for free. It flips the source, so every role cut from it flips together and an eye's sclera and lash stay in one frame."
|
|
1731
1887
|
)
|
|
1732
1888
|
}
|
|
1733
1889
|
},
|