@openbone/mcp 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +143 -2
- package/dist/atlas-schema.d.ts +149 -0
- package/dist/atlas-schema.js +42 -0
- package/dist/audio-meta.d.ts +6 -0
- package/dist/audio-meta.js +139 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +80 -0
- package/dist/bridge.d.ts +16 -0
- package/dist/bridge.js +151 -0
- package/dist/export.d.ts +97 -0
- package/dist/export.js +310 -0
- package/dist/guide.d.ts +1 -0
- package/dist/guide.js +83 -0
- package/dist/image-load.d.ts +9 -0
- package/dist/image-load.js +40 -0
- package/dist/image-ops.d.ts +77 -0
- package/dist/image-ops.js +235 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/local-only.d.ts +7 -0
- package/dist/local-only.js +38 -0
- package/dist/media.d.ts +27 -0
- package/dist/media.js +124 -0
- package/dist/render.d.ts +81 -0
- package/dist/render.js +224 -0
- package/dist/server.d.ts +28 -0
- package/dist/server.js +354 -0
- package/dist/session.d.ts +16 -0
- package/dist/session.js +43 -0
- package/dist/tools-extra.d.ts +4 -0
- package/dist/tools-extra.js +355 -0
- package/dist/tools-image.d.ts +3 -0
- package/dist/tools-image.js +176 -0
- package/package.json +67 -3
package/dist/guide.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export const GUIDE = `# OpenBone authoring guide (for agents)
|
|
2
|
+
|
|
3
|
+
Coordinate system: skeleton space is y-UP, rotations in degrees counter-clockwise. The skeleton origin (0,0)
|
|
4
|
+
sits at (originX, originY) pixels in the artboard (artboard pixels are y-down). Default artboard 800x600,
|
|
5
|
+
origin (400,450) — so y=0 is near the bottom, positive y goes up.
|
|
6
|
+
|
|
7
|
+
Everything can be referenced by **name or id**. Every edit tool validates its input and returns ids.
|
|
8
|
+
|
|
9
|
+
## Typical workflow
|
|
10
|
+
1. new_project {name, path} (autosaves to path) — or open_project.
|
|
11
|
+
2. Rig: add_bone (parent defaults to root; x/y/rotation are LOCAL to the parent). A bone points along its local +x.
|
|
12
|
+
E.g. an upright torso: add_bone {name:"torso", y:100, rotation:90, length:120}.
|
|
13
|
+
Children of a rotated parent use the parent's frame: a head on top of that torso is add_bone {name:"head", parent:"torso", x:120}.
|
|
14
|
+
3. Visuals: add_slot {bone} then add_shape (no art needed) or import_image + add_region / add_mesh.
|
|
15
|
+
Art prep: process_image {input, out, ops:[...], importAs | replace} keys out flat backgrounds (keyColor), cuts an object
|
|
16
|
+
out by comparing with a painted-out copy (differenceMatte), trims, crops, resizes, flips and feathers edges; its "box"
|
|
17
|
+
says where the result sat in the input picture, so a layer cut from a full scene can be placed exactly where it was.
|
|
18
|
+
Attachments are positioned in the slot BONE's local frame (x along the bone).
|
|
19
|
+
Draw order = slot order (first is back). Use update_slot {index} to reorder.
|
|
20
|
+
4. Deformation: add_mesh {cols, rows} (or trace_mesh on a region) then auto_weights {bones:[...]} so the mesh bends with bones.
|
|
21
|
+
trace_mesh on a mesh re-traces it in place and rebinds the same bones (tolerance 1–2 for detailed edges such as feet).
|
|
22
|
+
set_weights_in_area {slot, attachment, area:{yMax:...} | polygon, influences, blend} pins every vertex in an area
|
|
23
|
+
to bones (e.g. everything below the knees to the legs bone) without listing vertex indices.
|
|
24
|
+
5. Constraints (solved every frame in one list; default order IK, transform, path, physics — set_constraint_order
|
|
25
|
+
{constraints:[every constraint, first applied first]} changes it, [] restores the default):
|
|
26
|
+
add_ik_constraint {bones:["thigh","shin"], target:"foot-target"} (target must not be a child of the chain);
|
|
27
|
+
optional softness (2-bone), stretch, compress (1-bone).
|
|
28
|
+
add_transform_constraint {bones, target, mixRotate, mixTranslate, mixScale, mixShear, local?, relative?}.
|
|
29
|
+
Path: add_path {slot, anchors:[x0,y0,x1,y1,...]} (smooth bezier in the slot bone's frame; bind it to control bones
|
|
30
|
+
with auto_weights or set_weights so moving them bends it), then add_path_constraint {bones:[parent..child], target:slot,
|
|
31
|
+
positionMode:"fixed"|"percent", spacingMode:"length"|"fixed"|"percent"|"proportional", rotateMode:"tangent"|"chain"|"chainScale"}.
|
|
32
|
+
Physics: add_physics_constraint {bone, inertia, strength, damping, mass, gravity, wind, mix} per bone (parent first)
|
|
33
|
+
for hair/cloth/tails. It simulates over time, so check it with render_gif rather than a single frame.
|
|
34
|
+
Physics gravity/wind and IK softness scale with the skeleton's size relative to set_document {referenceScale} (default 100 %).
|
|
35
|
+
6. Animate: create_animation {name}, then set_keys with absolute values:
|
|
36
|
+
set_keys {animation:"walk", target:"bone", id:"thigh", property:"rotation", keys:[{time:0,value:-20},{time:0.5,value:20,curve:"easeInOut"},{time:1,value:-20}]}
|
|
37
|
+
Properties: bone rotation|x|y|scaleX|scaleY|shearX|shearY|inherit (stepped string: normal|onlyTranslation|noRotation|noScale), slot color ("#rrggbbaa") | dark (two-colour tint "#rrggbb") | attachment (name or null),
|
|
38
|
+
ik mix|softness, transform mix|mixRotate|mixTranslate|mixScale|mixShear, path position|spacing|mixRotate|mixX|mixY,
|
|
39
|
+
physics mix|inertia|strength|damping|mass|gravity|wind, deform offsets (mesh or path), sequence frame (with attachment), drawOrder. Curves: linear, stepped, easeIn, easeOut, easeInOut, backOut or [cx1,cy1,cx2,cy2].
|
|
40
|
+
set_keys merges into the timeline; pass replace:true to set the whole timeline (e.g. when redoing a motion path).
|
|
41
|
+
Loop cleanly by making the last key equal the first. add_event_key fires events (footsteps, sounds).
|
|
42
|
+
7. Interactivity (Rive-style): create_state_machine → add_input (bool/number/trigger) → add_state {animation}
|
|
43
|
+
(first state = entry) → add_transition {from, to, conditions:[{input, op, value}], duration, exitTime}.
|
|
44
|
+
Transition with no conditions waits for the source animation to finish (exitTime 1).
|
|
45
|
+
from:"@any" fires from any state. blend1d states blend animations by a number input.
|
|
46
|
+
Pointer: add_bounding_box on a slot, then add_listener {slot, event:"click"|"down"|"up"|"enter"|"exit"|"move", actions:[...]}.
|
|
47
|
+
Staged flows: add_state {..., loop:false, onComplete:"next"} plays once then moves on (its own transitions can still interrupt).
|
|
48
|
+
Live data: add_input {type:"string"} or number inputs; add_text {slot, text:"{multiplier:0.00}x", size, weight, color, outline}
|
|
49
|
+
shows inputs (number patterns: 0.00, #,##0, +0, 0%); add_binding {input, target:"bone"|"slot"|"ik"|..., object, property,
|
|
50
|
+
inMin, inMax, outMin, outMax} drives a property from a number input every frame (slot color takes hex outMin/outMax).
|
|
51
|
+
simulate_state_machine / render_frame steps accept setInputs with strings too.
|
|
52
|
+
Scripts: add_script {code} + attach_script. Script API (global ob): ob.input(n), ob.setInput(n,v), ob.fire(n),
|
|
53
|
+
ob.emit(event,{int,float,string}), ob.bone(n), ob.setBone(n,{rotation,...}) (per-frame override), ob.slot(n,{color,attachment}),
|
|
54
|
+
ob.pointer(), ob.time(), ob.log(...). Hooks: onStart(), onUpdate(dt), onEvent(e), onPointer(p), or named fns for runScript actions.
|
|
55
|
+
8. Particles: add_particles {slot, preset:"fire"|"smoke"|"sparks"|"confetti"|"exhaust"|"coins", ...overrides} emits from the
|
|
56
|
+
slot bone (rotation = emission direction in the bone's frame, e.g. -90 for exhaust under an upright rocket bone; space
|
|
57
|
+
"world" leaves trails). update_particles changes settings. Key it with set_keys {target:"particles", id:slot,
|
|
58
|
+
property:"emitting" (true/false) | "rate"}. State machines: set_state_emitter {state, slot, emitting} and listener actions
|
|
59
|
+
{type:"setEmitter", slot, emitting} / {type:"emitParticles", slot, count}. Seeded and deterministic; check with render_gif.
|
|
60
|
+
9. Skins (outfits): the first skin is the default skin, shared by all. add_skin {name}, then either pass skin:"<name>" to
|
|
61
|
+
add_shape/add_region/... or copy_attachment {slot, attachment, toSkin} and update_attachment {..., skin} to override a
|
|
62
|
+
default attachment under the same name. rename_skin, duplicate_skin, delete_skin manage them; render_frame {skin} previews one.
|
|
63
|
+
Skin bones/constraints: add_to_skin {skin, bones?, constraints?} makes them skin-only (active only while a skin listing
|
|
64
|
+
them is set; inactive bones and their children aren't posed and their slots aren't drawn); remove_from_skin undoes it.
|
|
65
|
+
10. Audio: import_audio {path} (or add_audio {name, src:data:/path, duration}) creates an asset and a same-named sound on the
|
|
66
|
+
sfx bus. Play it with add_audio_key {animation, sound, time, volume?, rate?, loop?}, set_event_sound {event, sound} (plays
|
|
67
|
+
whenever the event fires), set_state_sound {stateMachine, state, sound, on:"enter"|"exit", action:"play"|"stop"} or a
|
|
68
|
+
listener action {type:"playSound", sound} / {type:"stopSound", sound, fade?}. Mute buttons: give a bool input the bus it
|
|
69
|
+
mutes (add_input / update_input {mutes:"music"|"sfx"|... or "master" for everything}), then a listener {type:"setMute",
|
|
70
|
+
bus:"music", muted:"toggle"} (or toggleInput on that input; not both) and a slot color binding to show the state. The
|
|
71
|
+
app embedding the animation mutes it the same way (setInput("musicOff", true), or player.setBusMuted("music", true)).
|
|
72
|
+
Looping sounds stop when their state or
|
|
73
|
+
animation ends. Mix with update_sound {volume, rate, loop, fadeIn, fadeOut, bus}, add_bus/update_bus {volume, muted},
|
|
74
|
+
set_audio_mix {master, muted}. simulate_state_machine lists the audio commands.
|
|
75
|
+
11. Sprite sequences (frame-by-frame, e.g. fire or explosions): add_image for each frame (or one sheet image), then
|
|
76
|
+
add_sequence {slot, frames:["fire_0001","fire_0002",...], fps, mode:"loop"|"once"|"pingPong"|"holdLast", offset}
|
|
77
|
+
or add_sequence {slot, grid:{image:"sheet", cols, rows}}. Bind input (a number input) to pick the frame from a state
|
|
78
|
+
machine, or set_keys target:"sequence" {id:slot, attachment, property:"frame"}. set_sequence_frames replaces/reorders frames.
|
|
79
|
+
12. Check your work: render_frame (returns a PNG; bones are drawn as blue wedges) and simulate_state_machine (text log of
|
|
80
|
+
state changes/events). render_gif for motion. get_summary for an outline, validate for problems.
|
|
81
|
+
|
|
82
|
+
Tips: keep bones' length meaningful (used by IK and auto weights). Use update_* tools to fix mistakes, undo to revert.
|
|
83
|
+
`;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Image } from "@napi-rs/canvas";
|
|
2
|
+
/**
|
|
3
|
+
* A PNG without its metadata chunks. Generated art often carries large ones (C2PA "caBX" content credentials, XMP)
|
|
4
|
+
* before the pixel data, and @napi-rs/canvas then fails to recognise the buffer ("Invalid SVG image"). Returns the
|
|
5
|
+
* input unchanged when it is not a PNG or has nothing to drop.
|
|
6
|
+
*/
|
|
7
|
+
export declare function stripPngMetadata(buf: Buffer): Buffer;
|
|
8
|
+
/** loadImage for image bytes, a data: URI, a URL or a path, robust to PNG metadata chunks. */
|
|
9
|
+
export declare function decodeImage(src: Buffer | string): Promise<Image>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { loadImage } from "@napi-rs/canvas";
|
|
2
|
+
const SIG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
3
|
+
/** PNG chunks that affect how pixels decode; everything else (text, EXIF, C2PA content credentials…) is dropped. */
|
|
4
|
+
const KEEP = new Set(["IHDR", "PLTE", "IDAT", "IEND", "tRNS", "gAMA", "cHRM", "sRGB", "iCCP", "sBIT", "pHYs", "acTL", "fcTL", "fdAT"]);
|
|
5
|
+
/**
|
|
6
|
+
* A PNG without its metadata chunks. Generated art often carries large ones (C2PA "caBX" content credentials, XMP)
|
|
7
|
+
* before the pixel data, and @napi-rs/canvas then fails to recognise the buffer ("Invalid SVG image"). Returns the
|
|
8
|
+
* input unchanged when it is not a PNG or has nothing to drop.
|
|
9
|
+
*/
|
|
10
|
+
export function stripPngMetadata(buf) {
|
|
11
|
+
if (buf.length < 8 || SIG.some((b, i) => buf[i] !== b))
|
|
12
|
+
return buf;
|
|
13
|
+
const parts = [buf.subarray(0, 8)];
|
|
14
|
+
let dropped = false;
|
|
15
|
+
for (let p = 8; p + 12 <= buf.length;) {
|
|
16
|
+
const len = buf.readUInt32BE(p);
|
|
17
|
+
const type = buf.toString("latin1", p + 4, p + 8);
|
|
18
|
+
const end = p + 12 + len;
|
|
19
|
+
if (end > buf.length)
|
|
20
|
+
return buf; // truncated: leave it to the decoder
|
|
21
|
+
if (KEEP.has(type))
|
|
22
|
+
parts.push(buf.subarray(p, end));
|
|
23
|
+
else
|
|
24
|
+
dropped = true;
|
|
25
|
+
p = end;
|
|
26
|
+
if (type === "IEND")
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
return dropped ? Buffer.concat(parts) : buf;
|
|
30
|
+
}
|
|
31
|
+
/** loadImage for image bytes, a data: URI, a URL or a path, robust to PNG metadata chunks. */
|
|
32
|
+
export async function decodeImage(src) {
|
|
33
|
+
if (typeof src === "string") {
|
|
34
|
+
const m = /^data:([^;,]+)?(;base64)?,(.*)$/s.exec(src);
|
|
35
|
+
if (m?.[2])
|
|
36
|
+
return loadImage(stripPngMetadata(Buffer.from(m[3], "base64")));
|
|
37
|
+
return loadImage(src);
|
|
38
|
+
}
|
|
39
|
+
return loadImage(stripPngMetadata(src));
|
|
40
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pixel operations for preparing art (process_image): key out a flat background, cut a layer out by comparing two
|
|
3
|
+
* versions of a picture, trim, crop, feather edges, flip. They work on straight-alpha RGBA buffers; resizing and file
|
|
4
|
+
* I/O happen in the tool with the canvas.
|
|
5
|
+
*/
|
|
6
|
+
export interface Pixels {
|
|
7
|
+
data: Uint8ClampedArray;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
}
|
|
11
|
+
/** Where the result sits in the input image (after crops and trims), in input pixels. */
|
|
12
|
+
export interface Box {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
}
|
|
18
|
+
export declare function parseHex(hex: string): [number, number, number];
|
|
19
|
+
/** Median colour of the image border (all edges, or only the top rows). */
|
|
20
|
+
export declare function borderColor(p: Pixels, edges?: "all" | "top", depth?: number): [number, number, number];
|
|
21
|
+
export interface KeyOptions {
|
|
22
|
+
/** Background colour; default: the border median. */
|
|
23
|
+
color?: string;
|
|
24
|
+
/** Where to sample the default colour: "all" edges or only the "top" (art that touches the other edges, like grass). */
|
|
25
|
+
sampleFrom?: "all" | "top";
|
|
26
|
+
/** Colour distance (0–441) still counted as background. */
|
|
27
|
+
tolerance?: number;
|
|
28
|
+
/** Distance over which pixels fade from background to opaque (soft, anti-aliased edges). */
|
|
29
|
+
softness?: number;
|
|
30
|
+
/** Pixels at least this bright (0–255 average) stay opaque, e.g. white flowers on a light grey background. */
|
|
31
|
+
keepBrighterThan?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Remove the key colour's cast from what is left (green/blue screen spill on edges): the key's dominant channel is
|
|
34
|
+
* limited to the average of the other two (the usual green-screen despill). Default: on when the key colour is strongly coloured (green, blue, magenta…).
|
|
35
|
+
*/
|
|
36
|
+
despill?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Remove a flat background colour everywhere in the image (also in closed gaps), with soft edges, and take the
|
|
40
|
+
* background tint out of the semi-transparent edge pixels so no fringe of it is left.
|
|
41
|
+
*/
|
|
42
|
+
export declare function keyColor(p: Pixels, o?: KeyOptions): {
|
|
43
|
+
color: [number, number, number];
|
|
44
|
+
};
|
|
45
|
+
/** Bounding box of pixels with alpha above `threshold`, grown by `padding`. Null when nothing is visible. */
|
|
46
|
+
export declare function alphaBounds(p: Pixels, threshold?: number, padding?: number): Box | null;
|
|
47
|
+
export declare function crop(p: Pixels, b: Box): Pixels;
|
|
48
|
+
/** Fade alpha to 0 over the given number of pixels at each side (so tiled copies overlap without seams). */
|
|
49
|
+
export declare function featherEdges(p: Pixels, o: {
|
|
50
|
+
left?: number;
|
|
51
|
+
right?: number;
|
|
52
|
+
top?: number;
|
|
53
|
+
bottom?: number;
|
|
54
|
+
}): void;
|
|
55
|
+
export declare function flip(p: Pixels, horizontal: boolean, vertical: boolean): Pixels;
|
|
56
|
+
/** Keep only the largest connected group of visible pixels (alpha > threshold), clearing stray specks. */
|
|
57
|
+
export declare function keepLargest(p: Pixels, threshold?: number): void;
|
|
58
|
+
export interface DifferenceOptions {
|
|
59
|
+
/** Channel difference (0–1) below which pixels count as unchanged. */
|
|
60
|
+
threshold?: number;
|
|
61
|
+
/** Range (0–1) over which alpha ramps up above the threshold. */
|
|
62
|
+
softness?: number;
|
|
63
|
+
/** Only look inside this box of the image (pixels outside become transparent). */
|
|
64
|
+
region?: Box;
|
|
65
|
+
/** Fade the matte out between these rows (e.g. where a tree meets repainted grass): {from, to}, alpha 1 at from, 0 at to. */
|
|
66
|
+
fadeRows?: {
|
|
67
|
+
from: number;
|
|
68
|
+
to: number;
|
|
69
|
+
};
|
|
70
|
+
/** Keep only the largest cut-out piece (default true). */
|
|
71
|
+
keepLargest?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Cut out what differs between `p` and `other` (the same picture with the object removed, e.g. an inpainted "clean
|
|
75
|
+
* plate"): `p` keeps its colours, alpha becomes how much the two differ. A median and a small dilation clean the matte.
|
|
76
|
+
*/
|
|
77
|
+
export declare function differenceMatte(p: Pixels, other: Pixels, o?: DifferenceOptions): void;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pixel operations for preparing art (process_image): key out a flat background, cut a layer out by comparing two
|
|
3
|
+
* versions of a picture, trim, crop, feather edges, flip. They work on straight-alpha RGBA buffers; resizing and file
|
|
4
|
+
* I/O happen in the tool with the canvas.
|
|
5
|
+
*/
|
|
6
|
+
const smooth = (t) => {
|
|
7
|
+
const c = Math.min(1, Math.max(0, t));
|
|
8
|
+
return c * c * (3 - 2 * c);
|
|
9
|
+
};
|
|
10
|
+
export function parseHex(hex) {
|
|
11
|
+
const h = hex.replace("#", "");
|
|
12
|
+
if (!/^[0-9a-f]{6}([0-9a-f]{2})?$/i.test(h))
|
|
13
|
+
throw new Error(`Not a colour: ${hex}`);
|
|
14
|
+
return [Number.parseInt(h.slice(0, 2), 16), Number.parseInt(h.slice(2, 4), 16), Number.parseInt(h.slice(4, 6), 16)];
|
|
15
|
+
}
|
|
16
|
+
/** Median colour of the image border (all edges, or only the top rows). */
|
|
17
|
+
export function borderColor(p, edges = "all", depth = 4) {
|
|
18
|
+
const rs = [];
|
|
19
|
+
const gs = [];
|
|
20
|
+
const bs = [];
|
|
21
|
+
const take = (x, y) => {
|
|
22
|
+
const i = (y * p.width + x) * 4;
|
|
23
|
+
if (p.data[i + 3] < 128)
|
|
24
|
+
return;
|
|
25
|
+
rs.push(p.data[i]);
|
|
26
|
+
gs.push(p.data[i + 1]);
|
|
27
|
+
bs.push(p.data[i + 2]);
|
|
28
|
+
};
|
|
29
|
+
for (let y = 0; y < Math.min(depth, p.height); y++)
|
|
30
|
+
for (let x = 0; x < p.width; x += 2)
|
|
31
|
+
take(x, y);
|
|
32
|
+
if (edges === "all") {
|
|
33
|
+
for (let y = Math.max(0, p.height - depth); y < p.height; y++)
|
|
34
|
+
for (let x = 0; x < p.width; x += 2)
|
|
35
|
+
take(x, y);
|
|
36
|
+
for (let y = 0; y < p.height; y += 2)
|
|
37
|
+
for (let d = 0; d < depth; d++) {
|
|
38
|
+
take(d, y);
|
|
39
|
+
take(p.width - 1 - d, y);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (!rs.length)
|
|
43
|
+
throw new Error("The border has no opaque pixels to take a background colour from: give `color`");
|
|
44
|
+
const med = (a) => a.sort((x, y) => x - y)[a.length >> 1];
|
|
45
|
+
return [med(rs), med(gs), med(bs)];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Remove a flat background colour everywhere in the image (also in closed gaps), with soft edges, and take the
|
|
49
|
+
* background tint out of the semi-transparent edge pixels so no fringe of it is left.
|
|
50
|
+
*/
|
|
51
|
+
export function keyColor(p, o = {}) {
|
|
52
|
+
const bg = o.color ? parseHex(o.color) : borderColor(p, o.sampleFrom ?? "all");
|
|
53
|
+
const tol = o.tolerance ?? 18;
|
|
54
|
+
const soft = Math.max(1, o.softness ?? 34);
|
|
55
|
+
const d = p.data;
|
|
56
|
+
for (let i = 0; i < d.length; i += 4) {
|
|
57
|
+
const dist = Math.hypot(d[i] - bg[0], d[i + 1] - bg[1], d[i + 2] - bg[2]);
|
|
58
|
+
let a = smooth((dist - tol) / soft);
|
|
59
|
+
if (o.keepBrighterThan !== undefined)
|
|
60
|
+
a = Math.max(a, smooth(((d[i] + d[i + 1] + d[i + 2]) / 3 - o.keepBrighterThan) / 20));
|
|
61
|
+
const a0 = d[i + 3] / 255;
|
|
62
|
+
if (a > 0 && a < 1)
|
|
63
|
+
for (let c = 0; c < 3; c++)
|
|
64
|
+
d[i + c] = Math.min(255, Math.max(0, (d[i + c] - (1 - a) * bg[c]) / a));
|
|
65
|
+
d[i + 3] = Math.round(a * a0 * 255);
|
|
66
|
+
}
|
|
67
|
+
const k = bg.indexOf(Math.max(...bg));
|
|
68
|
+
const others = [0, 1, 2].filter((c) => c !== k);
|
|
69
|
+
const saturated = bg[k] - Math.max(bg[others[0]], bg[others[1]]) > 80;
|
|
70
|
+
if (o.despill ?? saturated)
|
|
71
|
+
for (let i = 0; i < d.length; i += 4)
|
|
72
|
+
if (d[i + 3] > 0)
|
|
73
|
+
d[i + k] = Math.min(d[i + k], (d[i + others[0]] + d[i + others[1]]) / 2);
|
|
74
|
+
return { color: bg };
|
|
75
|
+
}
|
|
76
|
+
/** Bounding box of pixels with alpha above `threshold`, grown by `padding`. Null when nothing is visible. */
|
|
77
|
+
export function alphaBounds(p, threshold = 8, padding = 0) {
|
|
78
|
+
let x0 = p.width;
|
|
79
|
+
let y0 = p.height;
|
|
80
|
+
let x1 = -1;
|
|
81
|
+
let y1 = -1;
|
|
82
|
+
for (let y = 0; y < p.height; y++)
|
|
83
|
+
for (let x = 0; x < p.width; x++)
|
|
84
|
+
if (p.data[(y * p.width + x) * 4 + 3] > threshold) {
|
|
85
|
+
if (x < x0)
|
|
86
|
+
x0 = x;
|
|
87
|
+
if (x > x1)
|
|
88
|
+
x1 = x;
|
|
89
|
+
if (y < y0)
|
|
90
|
+
y0 = y;
|
|
91
|
+
if (y > y1)
|
|
92
|
+
y1 = y;
|
|
93
|
+
}
|
|
94
|
+
if (x1 < 0)
|
|
95
|
+
return null;
|
|
96
|
+
x0 = Math.max(0, x0 - padding);
|
|
97
|
+
y0 = Math.max(0, y0 - padding);
|
|
98
|
+
x1 = Math.min(p.width - 1, x1 + padding);
|
|
99
|
+
y1 = Math.min(p.height - 1, y1 + padding);
|
|
100
|
+
return { x: x0, y: y0, width: x1 - x0 + 1, height: y1 - y0 + 1 };
|
|
101
|
+
}
|
|
102
|
+
export function crop(p, b) {
|
|
103
|
+
const x = Math.max(0, Math.round(b.x));
|
|
104
|
+
const y = Math.max(0, Math.round(b.y));
|
|
105
|
+
const w = Math.max(1, Math.min(p.width - x, Math.round(b.width)));
|
|
106
|
+
const h = Math.max(1, Math.min(p.height - y, Math.round(b.height)));
|
|
107
|
+
const out = new Uint8ClampedArray(w * h * 4);
|
|
108
|
+
for (let r = 0; r < h; r++)
|
|
109
|
+
out.set(p.data.subarray(((y + r) * p.width + x) * 4, ((y + r) * p.width + x + w) * 4), r * w * 4);
|
|
110
|
+
return { data: out, width: w, height: h };
|
|
111
|
+
}
|
|
112
|
+
/** Fade alpha to 0 over the given number of pixels at each side (so tiled copies overlap without seams). */
|
|
113
|
+
export function featherEdges(p, o) {
|
|
114
|
+
for (let y = 0; y < p.height; y++)
|
|
115
|
+
for (let x = 0; x < p.width; x++) {
|
|
116
|
+
let f = 1;
|
|
117
|
+
if (o.left)
|
|
118
|
+
f = Math.min(f, smooth(x / o.left));
|
|
119
|
+
if (o.right)
|
|
120
|
+
f = Math.min(f, smooth((p.width - 1 - x) / o.right));
|
|
121
|
+
if (o.top)
|
|
122
|
+
f = Math.min(f, smooth(y / o.top));
|
|
123
|
+
if (o.bottom)
|
|
124
|
+
f = Math.min(f, smooth((p.height - 1 - y) / o.bottom));
|
|
125
|
+
if (f < 1)
|
|
126
|
+
p.data[(y * p.width + x) * 4 + 3] *= f;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export function flip(p, horizontal, vertical) {
|
|
130
|
+
const out = new Uint8ClampedArray(p.data.length);
|
|
131
|
+
for (let y = 0; y < p.height; y++)
|
|
132
|
+
for (let x = 0; x < p.width; x++) {
|
|
133
|
+
const sx = horizontal ? p.width - 1 - x : x;
|
|
134
|
+
const sy = vertical ? p.height - 1 - y : y;
|
|
135
|
+
out.set(p.data.subarray((sy * p.width + sx) * 4, (sy * p.width + sx) * 4 + 4), (y * p.width + x) * 4);
|
|
136
|
+
}
|
|
137
|
+
return { data: out, width: p.width, height: p.height };
|
|
138
|
+
}
|
|
139
|
+
/** Keep only the largest connected group of visible pixels (alpha > threshold), clearing stray specks. */
|
|
140
|
+
export function keepLargest(p, threshold = 12) {
|
|
141
|
+
const n = p.width * p.height;
|
|
142
|
+
const label = new Int32Array(n);
|
|
143
|
+
const stack = new Int32Array(n);
|
|
144
|
+
let best = 0;
|
|
145
|
+
let bestSize = 0;
|
|
146
|
+
let next = 0;
|
|
147
|
+
for (let s = 0; s < n; s++) {
|
|
148
|
+
if (label[s] || p.data[s * 4 + 3] <= threshold)
|
|
149
|
+
continue;
|
|
150
|
+
next++;
|
|
151
|
+
let size = 0;
|
|
152
|
+
let top = 0;
|
|
153
|
+
stack[top++] = s;
|
|
154
|
+
label[s] = next;
|
|
155
|
+
while (top) {
|
|
156
|
+
const i = stack[--top];
|
|
157
|
+
size++;
|
|
158
|
+
const x = i % p.width;
|
|
159
|
+
const nb = [x > 0 ? i - 1 : -1, x < p.width - 1 ? i + 1 : -1, i - p.width, i + p.width];
|
|
160
|
+
for (const j of nb)
|
|
161
|
+
if (j >= 0 && j < n && !label[j] && p.data[j * 4 + 3] > threshold) {
|
|
162
|
+
label[j] = next;
|
|
163
|
+
stack[top++] = j;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (size > bestSize) {
|
|
167
|
+
bestSize = size;
|
|
168
|
+
best = next;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Keep the faint edge pixels that touch the kept group (they carry the anti-aliasing).
|
|
172
|
+
for (let i = 0; i < n; i++) {
|
|
173
|
+
if (label[i] === best)
|
|
174
|
+
continue;
|
|
175
|
+
if (label[i] === 0 && p.data[i * 4 + 3] > 0) {
|
|
176
|
+
const x = i % p.width;
|
|
177
|
+
const near = [x > 0 ? i - 1 : -1, x < p.width - 1 ? i + 1 : -1, i - p.width, i + p.width].some((j) => j >= 0 && j < n && label[j] === best);
|
|
178
|
+
if (near)
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
p.data[i * 4 + 3] = 0;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Cut out what differs between `p` and `other` (the same picture with the object removed, e.g. an inpainted "clean
|
|
186
|
+
* plate"): `p` keeps its colours, alpha becomes how much the two differ. A median and a small dilation clean the matte.
|
|
187
|
+
*/
|
|
188
|
+
export function differenceMatte(p, other, o = {}) {
|
|
189
|
+
if (other.width !== p.width || other.height !== p.height)
|
|
190
|
+
throw new Error(`The other image is ${other.width}×${other.height}; it must be ${p.width}×${p.height} (resize it first)`);
|
|
191
|
+
const w = p.width;
|
|
192
|
+
const h = p.height;
|
|
193
|
+
const diff = new Float32Array(w * h);
|
|
194
|
+
for (let i = 0; i < w * h; i++) {
|
|
195
|
+
const a = i * 4;
|
|
196
|
+
diff[i] = Math.max(Math.abs(p.data[a] - other.data[a]), Math.abs(p.data[a + 1] - other.data[a + 1]), Math.abs(p.data[a + 2] - other.data[a + 2])) / 255;
|
|
197
|
+
}
|
|
198
|
+
// 5×5 median (drops repaint noise), then 3×3 max (closes thin gaps), then a light blur.
|
|
199
|
+
const med = filter(diff, w, h, 2, (v) => v.sort((x, y) => x - y)[v.length >> 1]);
|
|
200
|
+
const dil = filter(med, w, h, 1, (v) => Math.max(...v));
|
|
201
|
+
const blur = filter(dil, w, h, 1, (v) => v.reduce((s, x) => s + x, 0) / v.length);
|
|
202
|
+
const th = o.threshold ?? 0.06;
|
|
203
|
+
const soft = o.softness ?? 0.09;
|
|
204
|
+
const r = o.region;
|
|
205
|
+
for (let y = 0; y < h; y++)
|
|
206
|
+
for (let x = 0; x < w; x++) {
|
|
207
|
+
const i = y * w + x;
|
|
208
|
+
let a = smooth((blur[i] - th) / soft);
|
|
209
|
+
if (r && (x < r.x || y < r.y || x >= r.x + r.width || y >= r.y + r.height))
|
|
210
|
+
a = 0;
|
|
211
|
+
if (o.fadeRows) {
|
|
212
|
+
const { from, to } = o.fadeRows;
|
|
213
|
+
a *= to === from ? (y < from ? 1 : 0) : smooth((to - y) / (to - from));
|
|
214
|
+
}
|
|
215
|
+
p.data[i * 4 + 3] = Math.round(a * p.data[i * 4 + 3]);
|
|
216
|
+
}
|
|
217
|
+
if (o.keepLargest ?? true)
|
|
218
|
+
keepLargest(p);
|
|
219
|
+
}
|
|
220
|
+
function filter(src, w, h, r, f) {
|
|
221
|
+
const out = new Float32Array(src.length);
|
|
222
|
+
const v = [];
|
|
223
|
+
for (let y = 0; y < h; y++)
|
|
224
|
+
for (let x = 0; x < w; x++) {
|
|
225
|
+
v.length = 0;
|
|
226
|
+
for (let dy = -r; dy <= r; dy++)
|
|
227
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
228
|
+
const yy = Math.min(h - 1, Math.max(0, y + dy));
|
|
229
|
+
const xx = Math.min(w - 1, Math.max(0, x + dx));
|
|
230
|
+
v.push(src[yy * w + xx]);
|
|
231
|
+
}
|
|
232
|
+
out[y * w + x] = f(v);
|
|
233
|
+
}
|
|
234
|
+
return out;
|
|
235
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { createServer, snake } from "./server.js";
|
|
2
|
+
export { ProjectSession } from "./session.js";
|
|
3
|
+
export { startBridge } from "./bridge.js";
|
|
4
|
+
export { renderPng, renderGif, pose } from "./render.js";
|
|
5
|
+
export { GUIDE } from "./guide.js";
|
|
6
|
+
export { packAtlas } from "./render.js";
|
|
7
|
+
export { EXPORT_FORMATS, type ExportFormat, type ExportRequest, type ExportResult, type AtlasExportOptions, exportFile, exportBaseName } from "./export.js";
|
|
8
|
+
export { type AtlasSettings, type AtlasScaleVariant, atlasBuildOptions, parseScaleVariants } from "@openbone/render-canvas";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { createServer, snake } from "./server.js";
|
|
2
|
+
export { ProjectSession } from "./session.js";
|
|
3
|
+
export { startBridge } from "./bridge.js";
|
|
4
|
+
export { renderPng, renderGif, pose } from "./render.js";
|
|
5
|
+
export { GUIDE } from "./guide.js";
|
|
6
|
+
export { packAtlas } from "./render.js";
|
|
7
|
+
export { EXPORT_FORMATS, exportFile, exportBaseName } from "./export.js";
|
|
8
|
+
export { atlasBuildOptions, parseScaleVariants } from "@openbone/render-canvas";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function isLocalOrigin(origin: string | undefined): boolean;
|
|
2
|
+
export declare function isLocalHost(host: string | undefined): boolean;
|
|
3
|
+
/** Both checks, for an incoming request's headers. */
|
|
4
|
+
export declare function isLocalRequest(headers: {
|
|
5
|
+
origin?: string;
|
|
6
|
+
host?: string;
|
|
7
|
+
}): boolean;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who may talk to the local servers (the editor bridge and MCP over HTTP).
|
|
3
|
+
*
|
|
4
|
+
* Both listen on 127.0.0.1, so other machines can't reach them, but any web page open in a browser on this machine
|
|
5
|
+
* can still try: a page can open a WebSocket to ws://127.0.0.1:7717, and with DNS rebinding (a site whose name starts
|
|
6
|
+
* resolving to 127.0.0.1) it can send same-origin HTTP requests. Browsers always say where a request comes from
|
|
7
|
+
* (Origin) and which name it was sent to (Host), so:
|
|
8
|
+
* - Origin must be absent (CLI tools, MCP clients, scripts: not a browser page) or a local page: the editor's dev
|
|
9
|
+
* server on localhost/127.0.0.1/[::1] (any port) or the desktop app (tauri://localhost, http(s)://tauri.localhost).
|
|
10
|
+
* - Host must be a loopback name, which a DNS-rebinding site's name never is.
|
|
11
|
+
*/
|
|
12
|
+
const LOCAL_HOSTNAMES = new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
|
|
13
|
+
export function isLocalOrigin(origin) {
|
|
14
|
+
if (origin === undefined || origin === "")
|
|
15
|
+
return true;
|
|
16
|
+
let url;
|
|
17
|
+
try {
|
|
18
|
+
url = new URL(origin); // "null" (file://, sandboxed iframes) doesn't parse and is refused
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
if (url.protocol === "tauri:")
|
|
24
|
+
return url.hostname === "localhost";
|
|
25
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
26
|
+
return false;
|
|
27
|
+
return LOCAL_HOSTNAMES.has(url.hostname) || url.hostname === "tauri.localhost";
|
|
28
|
+
}
|
|
29
|
+
export function isLocalHost(host) {
|
|
30
|
+
if (!host)
|
|
31
|
+
return false;
|
|
32
|
+
const name = host.startsWith("[") ? host.slice(0, host.indexOf("]") + 1) : host.split(":")[0];
|
|
33
|
+
return LOCAL_HOSTNAMES.has(name.toLowerCase());
|
|
34
|
+
}
|
|
35
|
+
/** Both checks, for an incoming request's headers. */
|
|
36
|
+
export function isLocalRequest(headers) {
|
|
37
|
+
return isLocalOrigin(headers.origin) && isLocalHost(headers.host);
|
|
38
|
+
}
|
package/dist/media.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type DecodedAudio } from "@openbone/export";
|
|
2
|
+
/** The ffmpeg executable, or null when there is none. */
|
|
3
|
+
export declare function findFfmpeg(): string | null;
|
|
4
|
+
/**
|
|
5
|
+
* Decode an audio file: WAV in-process, anything else through ffmpeg (stereo float at `sampleRate`). Null when it
|
|
6
|
+
* can't be decoded (no ffmpeg, or a broken file).
|
|
7
|
+
*/
|
|
8
|
+
export declare function decodeAudio(bytes: Uint8Array, sampleRate?: number): Promise<DecodedAudio | null>;
|
|
9
|
+
export interface VideoFrame {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
/** RGBA, opaque (already flattened onto the background). */
|
|
13
|
+
data: Uint8ClampedArray | Uint8Array;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Encode frames (and an optional sound track) as MP4 (H.264 + AAC, yuv420p, plays everywhere) or WebM (VP9 + Opus)
|
|
17
|
+
* with ffmpeg. `quality` 0..1 maps to the encoder's constant-quality setting.
|
|
18
|
+
*/
|
|
19
|
+
export declare function encodeVideoFfmpeg(frames: VideoFrame[], o: {
|
|
20
|
+
format: "mp4" | "webm";
|
|
21
|
+
fps: number;
|
|
22
|
+
quality?: number;
|
|
23
|
+
audio?: {
|
|
24
|
+
sampleRate: number;
|
|
25
|
+
channels: Float32Array[];
|
|
26
|
+
} | null;
|
|
27
|
+
}): Promise<Uint8Array>;
|