Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

fix(face): a line waits after the clip's own mouth for the first frame whose switch to the rest is within the jump line, and its voice with it; sprites leave out the feather's outermost 2 px

Soohong Kim90c3858d3a05
diff --git a/sprite_gen/video/face_bake.py b/sprite_gen/video/face_bake.py
index f4835cc..e0dc8c2 100644
--- a/sprite_gen/video/face_bake.py
+++ b/sprite_gen/video/face_bake.py
@@ -166,13 +166,12 @@ def bake(pack_dir: Path, voice_path: Path, out: Path, *, play: str | None = None
if s["comp"] > s["body"] + s["layer"]:
over_sum.append(r["t"])
- own = plan["line_start"] or 0
- sound_parts = []
- if play is not None and own:
- src = bodies[play].get("sound")
- sound_parts.append(decode(pack_dir / src, own / fps) if src else np.zeros(int(round(own / fps * RATE)), np.float32))
- sound_parts.append(padded)
- track_audio = np.concatenate(sound_parts)
+ # the clip's own sound for its own_mouth span, silence while the line waits for the layer, the padded voice
+ own, start_at = plan["own_mouth"], plan["line_start"]
+ src = bodies[play].get("sound") if play is not None else None
+ head = decode(pack_dir / src, own / fps) if src and own else np.zeros(int(round(own / fps * RATE)), np.float32)
+ head = np.concatenate([head, np.zeros(int(round(start_at / fps * RATE)) - len(head), np.float32)])
+ track_audio = np.concatenate([head, padded])
frames = len(rows)
track_audio = np.concatenate([track_audio, np.zeros(max(0, int(round(frames / fps * RATE)) - len(track_audio)), np.float32)])
write_wav(out / "voice-baked.wav", track_audio)
@@ -182,11 +181,9 @@ def bake(pack_dir: Path, voice_path: Path, out: Path, *, play: str | None = None
refused.append(f"frames {over_sum} step over the jump line and over the body's and the layer's steps together")
if any(r["outside_changed_px"] for r in rows):
refused.append("pixels changed outside the layer's weight")
- if plan["layer_from"] is not None and plan["layer_from"] >= frames:
- refused.append("the layer found no frame within the jump line of its rest to come on")
report = {"pack": str(pack_dir), "voice": str(voice_path), "play": play, "start_cell": start, "tags": tags, "lead_s": lead, "tail_s": tail,
- "fps": fps, "frames": frames, "set": plan["set"], "rest": plan["rest"], "line_start": plan["line_start"],
- "layer_from": plan["layer_from"], "levels": track["levels"], "syllables": len(track["syllables"]),
+ "fps": fps, "frames": frames, "set": plan["set"], "rest": plan["rest"], "own_mouth_frames": plan["own_mouth"],
+ "line_start": plan["line_start"], "wait_frames": plan["wait"], "levels": track["levels"], "syllables": len(track["syllables"]),
"talk_frames": sum(1 for r in rows if r["layer"] == "talk"), "rest_frames": sum(1 for r in rows if r["layer"] == "rest"),
"off_frames": sum(1 for r in rows if r["layer"] == "off"),
"outside_changed_px_max": max((r["outside_changed_px"] for r in rows), default=0),
@@ -259,7 +256,7 @@ def run(**kwargs: object) -> int:
if kwargs.get("beside") is not None:
made.append(str(video(out, report, Path(str(kwargs["beside"])), kwargs.get("video_height"), str(kwargs.get("matte") or MATTE)))) # type: ignore[arg-type]
print(json.dumps({"out": str(out), "frames": report["frames"], "set": report["set"], "rest": report["rest"],
- "line_start": report["line_start"], "layer_from": report["layer_from"], "talk_frames": report["talk_frames"],
+ "line_start": report["line_start"], "wait_frames": report["wait_frames"], "talk_frames": report["talk_frames"],
"over_line": report["over_line"], "over_sum": report["over_sum"], "outside_changed_px_max": report["outside_changed_px_max"],
"verdict": report["verdict"], "videos": made}, ensure_ascii=False, indent=2))
if report["verdict"] != "pass":
diff --git a/sprite_gen/video/face_layer.py b/sprite_gen/video/face_layer.py
index 242428c..62bd65c 100644
--- a/sprite_gen/video/face_layer.py
+++ b/sprite_gen/video/face_layer.py
@@ -20,10 +20,10 @@ grown from (`Geometry`).
through their anchors, joined (`union`; a frame adds nothing past `REACH` px of the reference's own lower face, so a
hand or sleeve the skin grows into stays out), and holes filled; flat 1 over it grown by `MARGIN`, a cos² feather
over `FEATHER` px outside it, 0 above the top edge rising to 1 over `RAMP`, the eyes cut out.
-- **Sprite** (`face_sprite`): a talking frame's lower face at the clip's own resolution, its alpha the weight less
- what must stay the body's where the face meets it — the dark grey and black that reach in from the boundary (the
- headset's boom, the choker) and the lines drawn in the feather — its colour moved onto the reference's by the median
- over the feather.
+- **Sprite** (`face_sprite`): a talking frame's lower face at the clip's own resolution, its alpha the weight (less
+ its outermost EDGE px, which the resampling would spread past it) less what must stay the body's where the face
+ meets it — the dark grey and black that reach in from the boundary (the headset's boom, the choker) and the lines
+ drawn in the feather — its colour moved onto the reference's by the median over the feather.
- **Keep** (`body_keep`): per body frame, what of the body stays on top of the layer: its own dark grey and black
reaching in from the boundary, its lines in the feather, and a hand (`hand_reach`): what differs from the reference
face and reaches in from outside the layer far into the face above the chin.
@@ -54,6 +54,8 @@ LUMA = np.array([0.299, 0.587, 0.114], np.float32)
SKIN_LUMA = 175 # skin is opaque and lighter than this
RAMP = 14 # the weight rises from 0 at the top edge to 1 this far below it
MARGIN, FEATHER = 6, 10 # the union grown by MARGIN px is flat; the weight falls to 0 over FEATHER px outside it
+EDGE = 2 # a sprite leaves out the feather's outermost px (weight under 0.1): mapped to the clip's
+ # pixels and back, its edge spreads a pixel, and nothing may change outside the weight
EYE_GROW = 6
REACH = 12 # a frame's lower face counts this far past the reference's own: a dropped jaw, not a hand
BAND = 30 # the anchor band: this far outside the flat part, above band_bottom, less the eyes
@@ -586,7 +588,7 @@ def face_sprite(native: Image.Image, to_native: np.ndarray, w0: np.ndarray, refe
size = (w0.shape[1], w0.shape[0])
face_ref = warp(nat, to_native, size)
hole = np.maximum(dark_reach(face_ref, w0), line_band(face_ref, w0))
- w_face = w0 * (1 - hole)
+ w_face = w0 * (1 - hole) * parts_mod._erode(w0 > 0, EDGE)
off = colour_offset(reference, face_ref, w_face)
back = np.linalg.inv(to_native)
w_nat = warp(w_face, back, native.size)
diff --git a/sprite_gen/video/sprite-gen-face-v1.mjs b/sprite_gen/video/sprite-gen-face-v1.mjs
index fdc75e5..ef96a1f 100644
--- a/sprite_gen/video/sprite-gen-face-v1.mjs
+++ b/sprite_gen/video/sprite-gen-face-v1.mjs
@@ -4,7 +4,7 @@
// frames. docs/face-parts.md owns the rules; the numbers are the pack's (`face-pack.json` rules).
//
// mouthTrack(samples, sampleRate, rules, { fps, tags }) voice -> a mouth level per frame
-// pickFaces(pack, levels, set, rest, { from }) levels -> a talking frame (or the rest) per frame
+// pickFaces(pack, levels, set, rest) levels -> a talking frame (or the rest) per frame
// planScene(pack, { play, start, levels, tags }) a body clip and a line -> what each frame shows
// loadFacePack(url), createFacePlayer(canvas, pack, { audioContext }) the live page
@@ -86,8 +86,8 @@ export function mouthTrack(samples, sampleRate, rules, { fps = 24, tags = [] } =
// frame within the jump line of the clip's next, an entry from the rest and an exit to it (both within the enter
// line: the clip's own largest step) cost the pack's numbers. A frame costs how far its open area is from the
// level's (a mouth opened less than wanted under_weight times more), in one level's area; the rest costs the area
-// wanted. Before `from` only the rest is allowed (the layer is not on yet).
-export function pickFaces(pack, levels, set, rest, { from = 0 } = {}) {
+// wanted.
+export function pickFaces(pack, levels, set, rest) {
const rules = pack.rules;
const D = pack.face.steps;
const n = D.length;
@@ -109,7 +109,6 @@ export function pickFaces(pack, levels, set, rest, { from = 0 } = {}) {
const arg = new Array(S + 1).fill(-1);
for (let k = 0; k < S; k++) {
const j = frames[k];
- if (t < from) continue;
let best = Infinity, how = -1;
if (j > 0 && member.has(j - 1) && cost[pos.get(j - 1)] < best) { best = cost[pos.get(j - 1)]; how = pos.get(j - 1); }
if (cost[k] + rules.hold_cost < best) { best = cost[k] + rules.hold_cost; how = k; }
@@ -156,16 +155,15 @@ function mod(a, n) {
}
// What every frame shows for a body clip played from its start (`play`), or the loop from a cell (`start`), with a
-// line said at once (its mouth `levels`, one a frame). The line waits for the end of the clip's own_mouth span: its
-// own mouth and sound play till then, the layer off. The layer comes on in the set and at the rest the body names at
-// that frame (`entry`), at the first frame from there whose switch to that rest is within the jump line — or at once,
-// in its own set and rest, when it is already on (`layer`) — and the line's frames are picked from there. A clip
-// that plays once goes on to the loop from its first cell.
+// line said at once (its mouth `levels`, one a frame). The line waits for the end of the clip's own_mouth span (its
+// own mouth and sound play till then, the layer off), and then for the first frame whose switch to the rest the body
+// names there (`entry`) is within the jump line: a voice never plays on a frame whose mouth cannot move. A layer
+// already on (`layer`) goes on in its own set and rest at once. A clip that plays once goes on to the loop from its
+// first cell; a line with no frame to come on at within the clip and one round of the loop is refused.
export function planScene(pack, { play = null, start = 0, levels = [], layer = null } = {}) {
const loop = loopBody(pack);
const first = play ? body(pack, play) : loop;
const own = play && first.own_mouth ? first.own_mouth.end_frame : 0;
- const total = (levels.length ? own : first.cells.length) + levels.length;
const cellAt = (t) => {
if (!play) return { body: loop.name, cell: mod(start + t, loop.cells.length) };
if (t < first.cells.length) return { body: first.name, cell: t };
@@ -173,26 +171,28 @@ export function planScene(pack, { play = null, start = 0, levels = [], layer = n
};
const frames = [];
if (!levels.length) {
- for (let t = 0; t < total; t++) frames.push({ ...cellAt(t), face: null, layer: "off", level: 0 });
- return { frames, set: null, rest: null, line_start: null, layer_from: null };
+ for (let t = 0; t < first.cells.length; t++) frames.push({ ...cellAt(t), face: null, layer: "off", level: 0 });
+ return { frames, set: null, rest: null, own_mouth: own, line_start: null, wait: null };
}
const { set, rest } = layer ?? body(pack, cellAt(own).body).entry;
- let from = 0;
+ let lineStart = own;
if (!layer) {
- while (from < levels.length) {
- const c = cellAt(own + from);
+ const limit = (play ? first.cells.length : 0) + loop.cells.length;
+ while (lineStart < limit) {
+ const c = cellAt(lineStart);
if (body(pack, c.body).switch[String(rest)][c.cell] <= pack.rules.jump_line) break;
- from++;
+ lineStart++;
}
+ if (lineStart >= limit) throw new RangeError(`the face layer has no frame within the jump line of its rest ${rest} to come on`);
}
- const pick = pickFaces(pack, levels, set, rest, { from });
- for (let t = 0; t < total; t++) {
+ const pick = pickFaces(pack, levels, set, rest);
+ for (let t = 0; t < lineStart + levels.length; t++) {
const c = cellAt(t);
- if (t < own + from) { frames.push({ ...c, face: null, layer: "off", level: t >= own ? levels[t - own] : 0 }); continue; }
- const j = pick.path[t - own];
- frames.push({ ...c, face: j ?? rest, layer: j === null ? "rest" : "talk", level: levels[t - own] });
+ if (t < lineStart) { frames.push({ ...c, face: null, layer: "off", level: 0 }); continue; }
+ const j = pick.path[t - lineStart];
+ frames.push({ ...c, face: j ?? rest, layer: j === null ? "rest" : "talk", level: levels[t - lineStart] });
}
- return { frames, set, rest, line_start: own, layer_from: own + from, want: pick.want, cost: pick.cost };
+ return { frames, set, rest, own_mouth: own, line_start: lineStart, wait: lineStart - own, want: pick.want, cost: pick.cost };
}
// --- the live page --------------------------------------------------------------------------------------------------
@@ -360,7 +360,7 @@ export function createFacePlayer(canvas, pack, { audioContext } = {}) {
}
return run((t0) => {
const plan = planScene(pack, { start: mod(t0 - loopPhase, loop.cells.length), levels, layer: resting });
- sound(data, t0);
+ sound(data, t0 + plan.line_start);
return { plan };
});
},