# sprite-stacking

Adopt this skill: https://proskills.md/skills/other/danielwh2-sprite-stacking-skills-sprite-stacking

Page: https://proskills.md/skills/other/danielwh2-sprite-stacking-skills-sprite-stacking
This document is the public skill bundle for that page. It contains SKILL.md and every helper file the catalog carries. No login is required.

## File: skills/sprite-stacking/SKILL.md
size: 19628
sha: e40448b1655c6890a6beba390a8184940dad4f4c
upstream: https://github.com/danielwh2/sprite-stacking/blob/main/skills/sprite-stacking/SKILL.md

````text
---
name: sprite-stacking
description: Makes fake-3D rotatable pixel art with sprite stacking. Turns four orthographic views (top, side, front, back) into a visual-hull carved slice strip, checks it, and renders a crisp turntable for web Canvas, Godot, Unity or Phaser. Use when someone asks to "make a rotatable pixel art car", "sprite stacking", "fake 3D pixel art", "pixel art turntable", "stacked slices", "voxel-style 2D sprite", or wants a top-down vehicle, prop, tree or building that turns without drawing every angle.
---

# Sprite stacking

An object is a horizontal strip of flat top-down slices, ground slice on the left, roof on the right. Draw them bottom to top, each rotated by the same angle, squashed vertically for camera pitch, and lifted 1 px per slice. One small sheet gives every angle in crisp pixels.

Never ship a stack you have not rendered and looked at. Paths below are relative to this skill's folder.

## 0. Pick the path

```
What do you have?
├── Slice strip PNG or MagicaVoxel layer export ──▶ Step 3 (Render)
├── 4-view sheet (top, side, front, back) ────────▶ Step 1 gate ─▶ Step 2 (Carve)
└── A prompt or an idea ──────────────────────────▶ Step 1 ─▶ Step 2 ─▶ Step 3
```

In an existing project, read the renderer first: engine, pixel scale, how sprites are drawn and sorted, which objects turn. Fit the stack into that renderer. Never add a second canvas or render pipeline.

## 1. Get a 4-view sheet

The sheet: one 1x4 row in the order TOP, SIDE, FRONT, BACK, on flat white, with clear gaps between views. TOP looks straight down, front pointing down. SIDE has the front pointing left. Same scale in all four.

Pick a tier:

- **Chunky retro:** 15-20 slices, about 25×48 px. Bold silhouettes, 3-4 px stripes, shown at 8-10x.
- **Detailed hi-bit:** 26-44 slices, 36×72 to 46×107 px. Holds vents, logos, spoked rims, glass reflections, raised spoilers.
- **Showcase / 1080p video:** 36-56 slices, 30×30 to 148×148 px, shown at 4-6x integer scale. Avoids chunky silhouette stepping and rotational wobble when tiny sprites are blown up to 7-9x on HD video. Architectural micro-details that read cleanly across 360° (shutters, flower boxes, balcony railings, roof moss, lily pads, lantern posts, cliff tendrils, waterfalls) elevate dioramas without breaking palette discipline.

Then take the first source that applies.

**a. You have an image generation tool.** Use this prompt. Swap the subject, keep the rest verbatim:

```
Chunky pixel art orthographic model sheet of [SUBJECT] with rich 3-tone hue-shifted pixel shading (deep cool-shifted shadows, saturated midtones, bright warm top highlights along ridges and edges), bold 3-4px stripes/accents, and crisp specular pixels on glass. Four views in a clean 1x4 horizontal row (or 2x2 grid) on a completely flat solid white or pale gray background:
1. TOP view looking straight down, front pointing DOWN.
2. SIDE view, front pointing LEFT.
3. FRONT view.
4. BACK view.
Strictly orthographic: zero perspective, zero tilt, zero 3/4 angle. Identical scale across all four views so length, width, and height match pixel for pixel. Bold 1px dark outline around each view, solid flat color clusters, crisp internal panel shading, zero anti-aliasing, zero soft edges, zero cast ground shadows, zero grid lines, zero text, generous empty space between views.
```

**b. The user has a sheet.** Use it.

**c. Neither.** Draw the four views yourself as pixel art in a short Python/PIL script at native resolution, one pixel per art pixel (numpy arrays or `ImageDraw`). Give each view a 1px dark outline, keep length, width and height identical across views, upscale 8x with `Image.NEAREST`, and paste them in one row on flat white with at least 40 px between views. Carve it with `--slices` set to the SIDE view's native height including its outline, so the slices land back on your pixels.

The slicer keeps a voxel only where every view covers it, so for (c):

- Round objects (pots, canopies, domes, wheels seen from above) need a pixel circle in the TOP view. A square TOP carves a box. If a round part is narrower than the widest part (a tower on a wide base, a balloon over its basket), carve with `--round` too, or it comes out square.
- Fins, leaves, wings and any irregular plan need their real outline in the TOP view, or they carve as blocks.
- The SIDE and FRONT silhouettes set the profile: a rectangle makes a cylinder, so round them for a ball.
- Wall detail (windows, doors, panel lines) goes in SIDE, FRONT and BACK. Roof detail goes in TOP.
- Anything that sticks out of a wall (flower box, porch, sign) carves a ledge along that whole wall. Paint small protrusions flat, and give a chimney a flat strip of ridge to stand on.
- Shade with a 3-4 tone hue-shifted ramp: cool deep shadow, saturated mid, warm highlight. No gradients, no anti-aliasing.

- Centre the object: the stack spins about the middle of its bounding box, so a base that reaches further on one side makes it orbit off-axis.

### Gate: look at the sheet before slicing

1. **Orientation:** TOP front points down. SIDE front points left (if right, pass `--side-nose right`).
2. **Proportions:** TOP length = SIDE length, TOP width = FRONT/BACK width, SIDE height = FRONT/BACK height.
3. **Hard edges:** flat fills, crisp outlines. Soft airbrushed edges or thin 1px internal lines smear once slices rotate. Regenerate or redraw rather than salvage.
4. **Holes:** overhangs that show daylight in the SIDE view (a raised rear wing) survive. Cavities hidden from all four silhouettes (truck beds, hollow arches) fill in. Fix those slices by hand after carving.
5. **Lessons:** run down the Lessons table at the end of this file.

## 2. Carve and inspect

`scripts/views_to_slices.py` needs Python 3 with Pillow and NumPy. It removes the background (transparent or flat), strips the 1px view outlines, quantizes to one shared palette, carves the slices, and writes a check sheet and a standalone viewer.

```bash
python3 scripts/views_to_slices.py views.png slices.png \
  --slices 36 --shade 0.2 --preview check.png --html viewer.html
```

| Flag | Default | When to change it |
| --- | --- | --- |
| `--slices N` | auto (36 if the SIDE view is taller than 64 px, else native) | 15-20 chunky, 32-48 hi-bit, native height for drawn sheets, `0` keeps the sheet's raw pixels |
| `--order` | `auto` | `auto` picks TOP and SIDE by matching proportions (TOP length = SIDE length, TOP width = FRONT width, SIDE height = FRONT height); reading order wins ties. `reading` forces TOP, SIDE, FRONT, BACK |
| `--round` | off | Towers, balloons, pots, bottles: clips each slice to the ellipse its SIDE and FRONT spans describe. Leave off for fins, wings and boxes |
| `--side-nose` | `left` | `right` if the SIDE view faces right |
| `--top-nose` | `auto` | Auto reads car cabins. Pass `down` for anything that is not a vehicle, and `up` or `down` if it flips a car |
| `--shade` | `0.2` | 0.12-0.15 for richly shaded views, 0.25 for flat ones, 0 for none |
| `--colours` | `24` | 32-48 for detailed liveries, `0` to keep every colour |
| `--bg-tol` | `28` | Raise a little if JPEG noise leaves a fringe |
| `--dark-thresh` | `70` | `0` to keep outlines on very dark silhouettes |
| `--normals` | *(none)* | Save a matching 3D normal-map slice strip PNG |
| `--emissive` | *(none)* | Save an auto-detected emissive mask strip PNG |

Open `check.png` and look at it right after carving: all four extracted views, four turntable angles and the exploded view. Do this before writing any integration code. `viewer.html` opens in a browser with Turntable, **Scene** (Living Diorama: Neon Street / Meadow Island with procedural stacked props + multi-PNG prop drops), Slice scan, Exploded, and **Play** modes (Drive through infinite Street/Meadow/Grid worlds, Wind, Shatter), live Time of Day (Day → Sunset → Night with auto-emissive glow and projected 3D shadows), **Weather** (Rain with wavy wet reflections & ripples, Snow with `normZ` snow-cap accumulation), and 16-direction PNG export.

## 3. Render: the loop and its numbers

Every renderer runs the same loop, bottom to top:

```js
// sheet: horizontal strip of `count` slices, each w × h, ground slice at z = 0
// pos: ground contact centre, rounded to whole pixels
for (let z = 0; z < count; z++) {
  // 1. rotate the slice around its centre (-floor(w/2), -floor(h/2)) by `turn`
  // 2. scale y by `squash` (camera pitch)
  // 3. lift by -round(z * spacing)
  // 4. translate to whole-pixel `pos`, draw with nearest filtering
}
```

Do not guess these:

- **`spacing = 1.0`** screen px at the art-pixel scale.
- **`squash = 0.75`** for an angled top-down camera. 1.0 looks straight down; below 0.6 roofs flatten to slivers. For a tilting camera, `squash = sin(elevation)`, `spacing = cos(elevation)`.
- **Symmetric rotation centres.** In software/pixel-buffer rasterizers, sample around `(w - 1) * 0.5, (h - 1) * 0.5` with `Math.round(...)` nearest sampling. Using `Math.floor(... + w / 2)` on even-sized slices shifts the centre by 0.5 art px and creates visible rotational wobble across 90°/180°/270° turns.
- **Smooth 60 fps placement.** Keep continuous motion (bobbing, floating, orbiting, parent-to-child projection) in floating-point screen pixels and snap only once at the final 1-screen-pixel blit (`Math.round(dx), Math.round(dy)`) with an integer display `scale`. Never quantize motion to art-pixel steps (`Math.round(...) * scale`) or round child projections independently — both cause harsh multi-pixel jumps and prop-to-ground jitter.
- **Validate strip counts at load time.** Assert `sheet.width % count === 0` (and expected slice dimensions) when slicing a strip so upgrading a strip's slice count never silently slices mid-frame.
- **Nearest-neighbour filtering.** `imageSmoothingEnabled = false` and `image-rendering: pixelated` on the web, Nearest/Point filter in engines.
- **Ground shadow:** for vehicles, the union footprint at z = 0, offset +2, +3 art px, drawn before slice 0. For tall props, project the stacked slices along the sun vector per layer, with Bayer 4×4 dithered edges, not a generic oval.
- **Outline the finished stack once, never per slice.** Per-slice outlines draw seams and speckle through the body. Render into a 1x art-pixel buffer, trace a 1px outline around its alpha, then scale up.
- **Heading:** the sheet's front points down (+y), so `turn = atan2(vy, vx) - PI / 2`. For anything that reverses, drive `turn` from a stored heading, not raw velocity.
- **Y-sort whole stacks by ground point.** Never interleave slices of two objects.
- **Bake static stacks** into plain sprites at load time. 200 trees × 30 slices is 6,000 draws a frame.
- **Animate entrances with slices, not opacity fades.** Fading a stack in mid-air looks flat; grow grounded props bottom-to-top via per-slice `lifts` (slice-bloom with a gentle spring settle and ground ring), and glide airborne/waterborne stacks (clouds, boats) in along their travel arc. Extend the `lifts` reveal frontier past `count` by the per-slice blend width (`mix(start, count + width, ...)`) so the top slices reach full `spacing` before switching `lifts` back to `null` — otherwise the upper slices snap in a single frame. Never blur crisp stacks with `defocus` cuts in video — use continuous camera moves instead.
- **Organic sway, 3D pitch tilt & body undulation via per-slice offsets.** For anchored flexible props (kelp, grass, banners), offset each slice horizontally by `sin(t * freq - z * wave + phase) * amp * (z / (count - 1))²` so `z = 0` stays pinned while tips sway smoothly. For leaping creatures (porpoising dolphins pitching nose-up on breach → level at apex → nose-down on re-entry) or rolling vessels, apply per-slice longitudinal/lateral tilt offsets `(xOffsets, yOffsets)` proportional to `(z - centerZ) * sin(pitchAngle)` along the body axis so pitch and roll read as true 3D tilt rather than a flat vertical lift.
- **Verify native nose axis & depth-attenuate submerged stacks.** Confirm whether each strip's nose faces `+x`, `+y`, or `-y` by visual inspection before binding `turn = atan2(vy, vx) ± PI / 2` so vessels and creatures travel nose-first. For stacks submerged underwater (seafloor props, deep manta, submerged dolphin phase), apply Beer-Lambert depth tinting (desaturating reds toward the water hue) and soften or omit the hard outer outline; drive floating stacks' heave, pitch, and roll from a damped spring-mass response to the shared wave field (not instantaneous wave height).
- **Multi-frame articulated 3D animation strips.** For living creatures (fluke-beating dolphins, wing-flapping mantas, tail-swishing fish, flapping/gliding birds), generate a compact set of full-3D slice strips (`f0..fN-1`, plus distinct pose frames like `leap` or `glide`) that share the exact same `(sliceWidth, sliceHeight, count)` and symmetric pivot `(w - 1) * 0.5, (h - 1) * 0.5` across every frame so switching frames never shifts the center. Give articulated strips extra Z-slice headroom (e.g., 18–20 slices instead of 12–16) so vertical wingbeats and tail flukes never clip the top or bottom slice, and select frames deterministically from the subject's physical motion phase.
- **Spherical bodies, world-space lighting & planetary rings.** A naive equal-radius sphere (`R_z = R_xy`) stretches into a vertical egg when rendered with pitch `squash` and vertical slice `spacing`: compute the exact Cauchy-Schwarz vertical half-height `R_z = R_xy · √(1 − squash²) / spacing` so the projected silhouette is a true circle (or controlled oblateness). For rotating planets and moons, store pure albedo bands/craters in the strip and apply a directional world-space solar terminator at render time so surface features spin while day/night stays locked to the star. For ringed bodies, split planar ring particles into back (`y_local < 0`, drawn before the planet stack) and front (`y_local >= 0`, drawn after) and eclipse particles inside the planet's cylindrical shadow cone.

Open `references/engines.md` when wiring into web Canvas, React, Godot, Unity or Phaser. `scripts/viewer.html` is the complete web reference: buffer, outline, orbit, scan, exploded, export.

## 4. Done when

- [ ] **Straight down:** at `squash = 1`, `turn = 0`, the render is one slice wide and `sliceHeight + count - 1` px tall.
- [ ] **Pitch and 90°:** 0.75 is visibly shorter than 1.0; at 90° the width equals `sliceHeight`.
- [ ] **Spacing:** 0 draws flat (top slice only), 2.5 separates every layer cleanly.
- [ ] **Clean diagonals:** no horizontal stripes or dark speckle inside the body at 45°.
- [ ] **Heading:** driving a circle keeps the front on the path; reversing holds heading.
- [ ] **Slow turns:** if small sprites look choppy, raise the integer display scale first. Try RotSprite only if long straight edges still crawl, and compare by eye: it rounds off tiny sprites.
- [ ] **Full turn:** watch every angle, not four. A part that appears and disappears is a Lessons row.

## Lessons

When a stack comes out wrong, fix it, then fix the cause where it can't recur: change the slicer so the mistake can't happen, or make it warn with the fix in the message, and add a case to `tests/check.py` built from the real mistake that fails before the change. Only judgment calls code can't see become a rule. Add a row here; delete it once code makes the mistake impossible.

| Date | Mistake | Rule | Enforced by |
| --- | --- | --- | --- |
| 2026-11-20 | 1px posts, ropes and antennas blink in and out as the stack turns | Draw thin parts at least 2px wide | Slicer warning (`thin_runs`), check case 5 |
| 2026-11-20 | A round part narrower than its base (tower, balloon, pot) carves square | Carve with `--round`; it keeps gapped rows like posts square | `--round`, check case 4 |
| 2026-11-20 | Auto `--top-nose` flips anything that is not a vehicle | Pass `--top-nose down` | Rule |
| 2026-11-20 | Something sticking out of a wall carves a ledge along the whole wall | Paint small protrusions flat; give a chimney a flat strip of ridge | Rule |
| 2026-11-20 | Background fully enclosed inside a view (between ropes, fins, legs) carves solid | Every gap must reach the outside of its view | Rule |
| 2026-11-20 | A base that reaches further on one side makes the stack orbit off-axis | Centre the object in its views | Rule |
| 2026-11-20 | Asymmetric `Math.floor(... + w / 2)` rotation sampling causes 0.5-art-px wobble across 90° boundaries | Use symmetric `(w - 1) * 0.5` centres with `Math.round(...)` nearest sampling | Rule |
| 2026-11-20 | Quantizing bobbing, orbiting, or child-prop projection to art-pixel steps (`Math.round(...) * scale`) creates jarring multi-pixel jumps at 60 fps | Keep motion and projection continuous in screen pixels; snap once at the final 1-screen-px blit with integer `scale` | Rule |
| 2026-11-20 | Tiny 12–24px props scaled 7–9× on 1080p video look overly chunky and exaggerate nearest-neighbor rotation stepping | Build denser strips (36–56 slices, 30–148px) and render at 4–6× integer scale for showcase video | Rule |
| 2026-11-20 | Updating a strip's slice count without updating the loader's count silently slices mid-frame | Validate `sheet.width % count === 0` and slice width at load time | Rule |
| 2026-11-20 | Fading stacks in mid-air or using `defocus` cuts blurs/flattens pixel-art dioramas | Bloom grounded props bottom-to-top via per-slice `lifts`, glide orbiters in along their arc, and use continuous camera moves | Rule |
| 2026-11-20 | Stopping a `lifts` reveal frontier at `count` leaves upper slices compressed when switching back to `null`, causing a single-frame top-slice jump | Extend the reveal frontier past `count` by the per-slice blend width (`count + width`) so top slices reach `1.0` before returning `null` | Rule |
| 2026-11-21 | Assuming every strip faces `+y` makes custom-carved ships or fish travel sideways/backward when driven by `atan2(vy, vx) - PI / 2` | Verify each strip's native nose axis (`+x`, `+y`, `-y`) before setting the `turn` offset | Rule |
| 2026-11-21 | Drawing deep underwater stacks with full surface saturation and hard ink outlines breaks depth hierarchy | Shift submerged stacks toward the water hue (Beer-Lambert red loss) and soften/drop hard outlines; couple floating stacks to the surface wave field | Rule |
| 2026-11-21 | Animating living creatures with a single rigid pose (or clipping flapping wings/flukes at slice 0 / `count - 1`) makes wildlife look stiff or truncated | Generate multi-frame strips (`f0..fN-1` + pose frames) with identical `(W, H, count)`, shared symmetric pivots, extra Z-slice headroom, and physics-synced frame selection | Rule |
| 2026-11-21 | Equal-radius spheres (`R_z = R_xy`) stretch vertically into eggs under pitch `squash` and slice `spacing` | Compensate sphere slice half-height with Cauchy-Schwarz projection `R_z = R_xy · √(1 − squash²) / spacing` | Rule |
| 2026-11-21 | Baking directional sun shading into rotating planet or moon strips makes the day/night shadow spin with the surface | Store pure albedo in spherical strips and evaluate world-space directional terminator shading per slice at render time | Rule |
| 2026-11-21 | Drawing planetary rings in a single pass causes rings to float completely in front of or behind a stacked planet | Split planar ring particles into back (`y_local < 0`) before the stack and front (`y_local >= 0`) after, and eclipse particles in the cylindrical shadow cone | Rule |

````

## File: skills/sprite-stacking/scripts/viewer.html
size: 102528
sha: 429e4b9cbd4413c586c3621ef7db694bbc514ab7
upstream: https://github.com/danielwh2/sprite-stacking/blob/main/skills/sprite-stacking/scripts/viewer.html

Content is not inlined in the catalog. Read the upstream file: https://github.com/danielwh2/sprite-stacking/blob/main/skills/sprite-stacking/scripts/viewer.html

## File: skills/sprite-stacking/scripts/views_to_slices.py
size: 28970
sha: 73972c1019988dcdb119bc8d9ba0ad55c0fcfd2d
upstream: https://github.com/danielwh2/sprite-stacking/blob/main/skills/sprite-stacking/scripts/views_to_slices.py

```text
#!/usr/bin/env python3
"""Turn four orthographic pixel-art views (top, side, front, back) into a sprite-stacking slice sheet.

Works on transparent PNGs and on opaque flat-background sheets (generated or hand-drawn),
in either a 2x2 grid or a 1x4 horizontal row.

Examples:
  python views_to_slices.py sheet.png slices.png --shade 0.25 --preview check.png
  python views_to_slices.py sheet.png slices.png --slices 36 --shade 0.2 --preview check.png --html viewer.html
"""

from __future__ import annotations

import argparse
import base64
import io
import json
import math
from collections import deque
from pathlib import Path

import numpy as np
from PIL import Image, ImageDraw

def remove_background(rgba: np.ndarray, tol: int = 28) -> np.ndarray:
    """Return RGBA array with transparent background.

    Keeps existing alpha when the sheet is already transparent; otherwise flood-fills
    from the outer border against the median corner color so interior highlights stay intact.
    """
    h, w = rgba.shape[:2]
    if (rgba[..., 3] < 128).mean() > 0.05:
        out = rgba.copy()
        out[..., 3] = np.where(out[..., 3] >= 128, 255, 0)
        return out

    rgb = rgba[..., :3].astype(np.int16)
    corners = np.array([rgb[0, 0], rgb[0, w - 1], rgb[h - 1, 0], rgb[h - 1, w - 1]])
    bg = np.median(corners, axis=0)
    close = np.abs(rgb - bg).max(axis=-1) <= tol

    bg_mask = np.zeros((h, w), dtype=bool)
    q: deque[tuple[int, int]] = deque()
    for x in range(w):
        if close[0, x] and not bg_mask[0, x]:
            bg_mask[0, x] = True
            q.append((0, x))
        if close[h - 1, x] and not bg_mask[h - 1, x]:
            bg_mask[h - 1, x] = True
            q.append((h - 1, x))
    for y in range(1, h - 1):
        if close[y, 0] and not bg_mask[y, 0]:
            bg_mask[y, 0] = True
            q.append((y, 0))
        if close[y, w - 1] and not bg_mask[y, w - 1]:
            bg_mask[y, w - 1] = True
            q.append((y, w - 1))

    while q:
        y, x = q.popleft()
        if y > 0 and close[y - 1, x] and not bg_mask[y - 1, x]:
            bg_mask[y - 1, x] = True
            q.append((y - 1, x))
        if y + 1 < h and close[y + 1, x] and not bg_mask[y + 1, x]:
            bg_mask[y + 1, x] = True
            q.append((y + 1, x))
        if x > 0 and close[y, x - 1] and not bg_mask[y, x - 1]:
            bg_mask[y, x - 1] = True
            q.append((y, x - 1))
        if x + 1 < w and close[y, x + 1] and not bg_mask[y, x + 1]:
            bg_mask[y, x + 1] = True
            q.append((y, x + 1))

    out = rgba.copy()
    out[bg_mask, 3] = 0
    out[~bg_mask, 3] = 255
    return out

def find_views(sheet: np.ndarray, order: str = "auto") -> list[np.ndarray]:
    """Locate the four views [top, side, front, back], dropping tiny specks and text labels."""
    solid = sheet[..., 3] >= 128
    h, w = solid.shape
    dilate_steps = max(2, min(6, round(min(h, w) / 260)))
    grown = solid.copy()
    for _ in range(dilate_steps):
        g = grown.copy()
        g[1:] |= grown[:-1]
        g[:-1] |= grown[1:]
        g[:, 1:] |= grown[:, :-1]
        g[:, :-1] |= grown[:, 1:]
        grown = g

    seen = np.zeros_like(solid)
    boxes: list[tuple[int, int, int, int, int]] = []
    for y0, x0 in zip(*np.nonzero(grown)):
        if seen[y0, x0]:
            continue
        stack = [(int(y0), int(x0))]
        seen[y0, x0] = True
        min_x = max_x = int(x0)
        min_y = max_y = int(y0)
        count = 0
        while stack:
            y, x = stack.pop()
            count += 1
            if x < min_x:
                min_x = x
            if x > max_x:
                max_x = x
            if y < min_y:
                min_y = y
            if y > max_y:
                max_y = y
            if y + 1 < h and grown[y + 1, x] and not seen[y + 1, x]:
                seen[y + 1, x] = True
                stack.append((y + 1, x))
            if y > 0 and grown[y - 1, x] and not seen[y - 1, x]:
                seen[y - 1, x] = True
                stack.append((y - 1, x))
            if x + 1 < w and grown[y, x + 1] and not seen[y, x + 1]:
                seen[y, x + 1] = True
                stack.append((y, x + 1))
            if x > 0 and grown[y, x - 1] and not seen[y, x - 1]:
                seen[y, x - 1] = True
                stack.append((y, x - 1))
        if count > 40:
            boxes.append((min_x, min_y, max_x + 1, max_y + 1, count))

    if not boxes:
        raise ValueError("No foreground drawings found. Check --bg-tol or image alpha.")

    big = max((b[2] - b[0]) * (b[3] - b[1]) for b in boxes)
    boxes = [b for b in boxes if (b[2] - b[0]) * (b[3] - b[1]) >= 0.14 * big]
    # Reading order: cluster into rows by vertical midpoint, then left to right
    row_band = max(1.0, h / 2.2)
    boxes.sort(key=lambda b: (round((b[1] + b[3]) / 2 / row_band), b[0]))

    if len(boxes) != 4:
        summary = [(b[0], b[1], b[2] - b[0], b[3] - b[1]) for b in boxes]
        raise ValueError(
            f"Found {len(boxes)} drawings {summary}, expected 4 (top, side, front, back). "
            "Check spacing between views or adjust --bg-tol."
        )

    views: list[np.ndarray] = []
    for x0, y0, x1, y1, _ in boxes:
        v = sheet[y0:y1, x0:x1].copy()
        ys, xs = np.nonzero(v[..., 3] >= 128)
        v = v[ys.min() : ys.max() + 1, xs.min() : xs.max() + 1]
        v[..., 3] = np.where(v[..., 3] >= 128, 255, 0)
        views.append(v)

    if order == "reading":
        return views

    # Pick the (top, side) pair whose sizes agree: top length = side length, top width = front
    # width, side height = front height. Aspect alone misreads tall props (trees, towers).
    def err(t: int, s: int) -> float:
        tv, sv = views[t], views[s]
        rest = [views[i] for i in range(4) if i not in (t, s)]
        e = abs(math.log(tv.shape[0] / sv.shape[1]))
        e += sum(abs(math.log(tv.shape[1] / f.shape[1])) + abs(math.log(sv.shape[0] / f.shape[0])) for f in rest) / 2
        return e

    pairs = [(t, s) for t in range(4) for s in range(4) if t != s]
    best = min(err(t, s) for t, s in pairs)
    # ponytail: 0.1 log-tolerance ~ 10% size slack; reading order wins any near-tie
    t, s = next(p for p in pairs if err(*p) <= best + 0.1)
    rest = [views[i] for i in range(4) if i not in (t, s)]
    return [views[t], views[s], rest[0], rest[1]]

def strip_outline(v: np.ndarray, dark: int = 70) -> np.ndarray:
    """Replace 1px dark outer contour pixels with their interior neighbor's color."""
    if dark <= 0:
        return v
    m = v[..., 3] > 0
    pad = np.pad(m, 1)
    # 8-neighbour: an outline pixel in a concave corner (stem under a cap) touches empty space only diagonally
    edge = m & ~(
        pad[:-2, 1:-1] & pad[2:, 1:-1] & pad[1:-1, :-2] & pad[1:-1, 2:]
        & pad[:-2, :-2] & pad[:-2, 2:] & pad[2:, :-2] & pad[2:, 2:]
    )
    line = edge & (v[..., :3].astype(int).sum(-1) / 3 < dark)
    out = v.copy()
    for y, x in zip(*np.nonzero(line)):
        for dy, dx in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)):
            yy, xx = y + dy, x + dx
            if 0 <= yy < m.shape[0] and 0 <= xx < m.shape[1] and m[yy, xx] and not line[yy, xx]:
                out[y, x] = v[yy, xx]
                break
    return out

def one_palette(views: list[np.ndarray], n: int) -> list[np.ndarray]:
    """Quantize all four views onto one shared palette of n colors without dithering."""
    if n <= 0:
        return views
    px = np.concatenate([v[v[..., 3] > 0][:, :3] for v in views])
    px = np.unique(px // 6 * 6 + 3, axis=0).astype(np.uint8)
    pal_img = Image.fromarray(px.reshape(1, -1, 3)).quantize(
        min(n, len(px)), method=Image.MEDIANCUT, dither=Image.NONE
    )
    k = min(n, len(px))
    pal = np.array(pal_img.getpalette()[: k * 3]).reshape(-1, 3)
    out: list[np.ndarray] = []
    for v in views:
        q = v.copy()
        rgb = v[..., :3].astype(np.int32)
        dist = ((rgb[..., None, :] - pal[None, None, :, :]) ** 2).sum(-1)
        q[..., :3] = pal[dist.argmin(-1)]
        out.append(q)
    return out

def fit(v: np.ndarray, w: int, h: int) -> np.ndarray:
    return np.array(Image.fromarray(v).resize((max(1, w), max(1, h)), Image.NEAREST))

def edge_colour(view: np.ndarray, row: int, i: int):
    xs = np.nonzero(view[row, :, 3])[0]
    return None if len(xs) == 0 else view[row, min(max(i, int(xs[0])), int(xs[-1])), :3]

def match_top(top: np.ndarray, side: np.ndarray) -> str:
    """Return 'down' or 'up' so dark cabin/window pixels sit over the tallest span of the side view."""
    tall = (side[..., 3] > 0).sum(0)
    tall = np.interp(np.linspace(0, len(tall) - 1, top.shape[0]), np.arange(len(tall)), tall)
    tall_mask = tall >= 0.8 * tall.max()
    dark = ((top[..., :3].astype(int).sum(-1) < 240) & (top[..., 3] > 0)).mean(1)
    return "down" if (dark * tall_mask).sum() >= (dark[::-1] * tall_mask).sum() else "up"

def _edge_row(view: np.ndarray, row: int, length: int) -> np.ndarray | None:
    xs = np.nonzero(view[row, :, 3])[0]
    if len(xs) == 0:
        return None
    idx = np.clip(np.arange(length), int(xs[0]), int(xs[-1]))
    return view[row, idx, :3]

def carve(
    top: np.ndarray,
    side: np.ndarray,
    front: np.ndarray,
    back: np.ndarray,
    shade: float = 0.0,
    round_: bool = False,
) -> np.ndarray:
    """Visual-hull carve into slices [z, y, x, rgba], z=0 on the ground."""
    L, W = top.shape[:2]
    H = side.shape[0]
    top_mask = top[..., 3] > 0
    occ = np.zeros((H, L, W), dtype=bool)
    yy_grid = xx_grid = None
    for z in range(H):
        r = H - 1 - z
        along = side[r, :, 3] > 0
        across = (front[r, :, 3] > 0) | (back[r, :, 3] > 0)
        occ[z] = top_mask & along[:, None] & across[None, :]
        if round_ and along.any() and across.any():
            # Lathe: clip the row to the ellipse its side and front spans describe. The hull alone
            # gives (widest top disk) & (square), so a tower narrower than its base carves square.
            ys, xs = np.nonzero(along)[0], np.nonzero(across)[0]
            if not (along[ys[0] : ys[-1] + 1].all() and across[xs[0] : xs[-1] + 1].all()):
                continue  # a gap in the row means separate posts, ropes or legs: keep them square
            if yy_grid is None:
                yy_grid, xx_grid = np.mgrid[0:L, 0:W]
            ry, rx = (ys[-1] - ys[0]) / 2 + 0.5, (xs[-1] - xs[0]) / 2 + 0.5
            occ[z] &= ((yy_grid - (ys[0] + ys[-1]) / 2) / ry) ** 2 + ((xx_grid - (xs[0] + xs[-1]) / 2) / rx) ** 2 < 1

    vol = np.zeros((H, L, W, 4), dtype=np.uint8)
    covered = np.zeros((L, W), dtype=bool)
    ix = np.arange(W)[None, :].repeat(L, 0)
    iy = np.arange(L)[:, None].repeat(W, 1)
    top_rgb = top[..., :3]

    for z in range(H - 1, -1, -1):
        r = H - 1 - z
        above = covered.copy()
        o = occ[z]
        covered |= o
        if not o.any():
            continue
        d = np.stack(
            [
                ix - np.maximum.accumulate(np.where(~o, ix, -1), axis=1),
                np.minimum.accumulate(np.where(~o, ix, W)[:, ::-1], axis=1)[:, ::-1] - ix,
                iy - np.maximum.accumulate(np.where(~o, iy, -1), axis=0),
                np.minimum.accumulate(np.where(~o, iy, L)[::-1], axis=0)[::-1] - iy,
            ]
        )
        face, depth = d.argmin(0), d.min(0)
        # Higher slices within 2px, corners included: views disagree by 1-2px at colour boundaries.
        pad_a = np.pad(above, 2)
        near_above = np.zeros_like(above)
        for dy in range(5):
            for dx in range(5):
                near_above |= pad_a[dy : dy + L, dx : dx + W]

        # Vectorized per-voxel colouring (matches scalar carve logic exactly)
        is_steep_step = (~above) & (depth <= 2) & near_above
        use_top = (~above) & (~is_steep_step)
        if z + 1 < H:
            use_above = above & (depth > 2) & (vol[z + 1, :, :, 3] > 0)
        else:
            use_above = np.zeros((L, W), dtype=bool)

        side_row = _edge_row(side, r, L)
        back_row = _edge_row(back, r, W)
        front_row = _edge_row(front, r, W)

        edge_rgb = top_rgb.copy()
        if side_row is not None:
            m = face < 2
            edge_rgb[m] = np.broadcast_to(side_row[:, None, :], (L, W, 3))[m]
        if back_row is not None:
            m = face == 2
            edge_rgb[m] = np.broadcast_to(back_row[None, :, :], (L, W, 3))[m]
        if front_row is not None:
            m = face > 2
            edge_rgb[m] = np.broadcast_to(front_row[None, :, :], (L, W, 3))[m]

        slice_rgb = np.where(use_top[..., None], top_rgb, edge_rgb)
        if z + 1 < H:
            slice_rgb = np.where(use_above[..., None], vol[z + 1, :, :, :3], slice_rgb)

        if shade:
            shade_mask = (above | is_steep_step) & (depth <= 2) & o
            if shade_mask.any():
                factor = 1.0 - shade * (1.0 - z / max(H - 1, 1)) - shade * 0.4
                shaded = np.clip(slice_rgb[shade_mask].astype(float) * factor, 0, 255).astype(np.uint8)
                slice_rgb = slice_rgb.copy()
                slice_rgb[shade_mask] = shaded

        vol[z, o, :3] = slice_rgb[o]
        vol[z, o, 3] = 255
    return vol

def views_to_slices(
    sheet: np.ndarray,
    *,
    slices: int | None = None,
    top_nose: str = "auto",
    side_nose: str = "left",
    order: str = "auto",
    colours: int = 24,
    shade: float = 0.2,
    dark_thresh: int = 70,
    bg_tol: int = 28,
    round_: bool = False,
) -> tuple[np.ndarray, list[np.ndarray]]:
    clean = remove_background(sheet, tol=bg_tol)
    raw_views = find_views(clean, order=order)
    top, side, front, back = raw_views

    if side_nose == "left":
        side_aligned = side[:, ::-1]
    else:
        side_aligned = side

    # Determine target pixel grid (L, W, H)
    raw_h = side.shape[0]
    if slices is None:
        # Auto-downscale high-res AI sheets while leaving native pixel art untouched
        target_h = 36 if raw_h > 64 else raw_h
    elif slices == 0:
        target_h = raw_h
    else:
        target_h = slices

    if target_h != raw_h:
        L = max(4, round(target_h * side.shape[1] / side.shape[0]))
        W = max(4, round(L * top.shape[1] / top.shape[0]))
        H = target_h
        top = fit(top, W, L)
        side_aligned = fit(side_aligned, L, H)
        front = fit(front, W, H)
        back = fit(back, W, H)
    else:
        L, W = top.shape[:2]
        H = max(2, round(side.shape[0] * L / side.shape[1]))
        side_aligned = fit(side_aligned, L, H)
        front = fit(front, W, H)
        back = fit(back, W, H)

    # Back view is seen from behind (+y -> -y), mirror x to match top view's x axis
    back_aligned = back[:, ::-1]

    cleaned = [strip_outline(v, dark=dark_thresh) for v in (top, side_aligned, front, back_aligned)]
    top, side_aligned, front, back_aligned = one_palette(cleaned, colours)

    if top_nose == "auto":
        top_nose = match_top(top, side_aligned)
    if top_nose == "up":
        top = top[::-1]

    vol = carve(top, side_aligned, front, back_aligned, shade=shade, round_=round_)
    display_views = [
        top,
        side_aligned[:, ::-1] if side_nose == "left" else side_aligned,
        front,
        back_aligned[:, ::-1],
    ]
    return vol, display_views

def render_stack_image(
    vol: np.ndarray,
    angle_deg: float,
    squash: float = 0.75,
    spacing: float = 1.0,
    shadow: bool = True,
    outline: bool = True,
) -> Image.Image:
    """Render one crisp 1x art-pixel frame of the stacked volume for visual verification."""
    n, h, w = vol.shape[:3]
    diag = int(math.ceil(math.hypot(w, h))) + 8
    lift_max = int(math.ceil((n - 1) * spacing))
    cw = diag + 12
    ch = int(math.ceil(diag * squash)) + lift_max + 16
    cx = cw // 2
    base_y = ch - int(math.ceil(diag * squash / 2)) - 6

    rad = math.radians(angle_deg)
    ca, sa = math.cos(rad), math.sin(rad)

    canvas = np.zeros((ch, cw, 4), dtype=np.uint8)
    yy, xx = np.mgrid[0:ch, 0:cw]
    dx = xx - cx

    if shadow:
        # Union footprint of all slices projected flat on the ground, offset down-right
        footprint = (vol[..., 3] > 0).any(axis=0)
        foot_img = np.zeros((h, w, 4), dtype=np.uint8)
        foot_img[footprint] = (0, 0, 0, 255)
        dy_s = (yy - (base_y + 3)) / squash
        dx_s = (xx - (cx + 2))
        sx = np.floor(dx_s * ca + dy_s * sa + w / 2.0).astype(np.int32)
        sy = np.floor(-dx_s * sa + dy_s * ca + h / 2.0).astype(np.int32)
        valid = (sx >= 0) & (sx < w) & (sy >= 0) & (sy < h)
        vy, vx = np.nonzero(valid)
        hit = foot_img[sy[vy, vx], sx[vy, vx], 3] > 0
        canvas[vy[hit], vx[hit]] = (186, 198, 212, 255)

    body = np.zeros((ch, cw, 4), dtype=np.uint8)
    for z in range(n):
        lift_y = round(z * spacing)
        dy = (yy - (base_y - lift_y)) / squash
        sx = np.floor(dx * ca + dy * sa + w / 2.0).astype(np.int32)
        sy = np.floor(-dx * sa + dy * ca + h / 2.0).astype(np.int32)
        valid = (sx >= 0) & (sx < w) & (sy >= 0) & (sy < h)
        vy, vx = np.nonzero(valid)
        px = vol[z, sy[vy, vx], sx[vy, vx]]
        opaque = px[:, 3] > 0
        body[vy[opaque], vx[opaque]] = px[opaque]

    if outline:
        alpha = body[..., 3] > 0
        pad = np.pad(alpha, 1)
        ring = ~alpha & (pad[:-2, 1:-1] | pad[2:, 1:-1] | pad[1:-1, :-2] | pad[1:-1, 2:])
        canvas[ring] = (24, 28, 36, 255)

    solid = body[..., 3] > 0
    canvas[solid] = body[solid]
    return Image.fromarray(canvas)

def thin_runs(vol: np.ndarray, min_run: int = 3) -> list[tuple[int, int]]:
    """Slice ranges holding a part with no 2x2 block (a 1px post, rope, antenna or rail) for at
    least min_run slices in a row. Renderers sample each rotated slice per screen pixel, so such a
    part drops between pixels at some angles and blinks in and out as the stack turns."""
    thin = []
    for s in vol[..., 3] > 0:
        b = s[:-1, :-1] & s[1:, :-1] & s[:-1, 1:] & s[1:, 1:]
        block = np.zeros_like(s)
        for dy in (0, 1):
            for dx in (0, 1):
                block[dy : dy + b.shape[0], dx : dx + b.shape[1]] |= b
        seen, found = np.zeros_like(s), False
        for y0, x0 in zip(*np.nonzero(s)):
            if seen[y0, x0] or found:
                continue
            stack, seen[y0, x0], solid = [(y0, x0)], True, False
            while stack:
                y, x = stack.pop()
                solid |= bool(block[y, x])
                for yy, xx in ((y + 1, x), (y - 1, x), (y, x + 1), (y, x - 1)):
                    if 0 <= yy < s.shape[0] and 0 <= xx < s.shape[1] and s[yy, xx] and not seen[yy, xx]:
                        seen[yy, xx] = True
                        stack.append((yy, xx))
            found = not solid
        thin.append(found)
    runs, start = [], None
    for z, t in enumerate(thin + [False]):
        if t and start is None:
            start = z
        elif not t and start is not None:
            if z - start >= min_run:
                runs.append((start, z - 1))
            start = None
    return runs

def build_preview(vol: np.ndarray, views: list[np.ndarray], out_path: Path) -> None:
    """Save a crisp visual check sheet: 4 views + 4 turntable angles + exploded stack."""
    renders = [
        ("35 deg", render_stack_image(vol, 35, squash=0.75, spacing=1.0)),
        ("135 deg", render_stack_image(vol, 135, squash=0.75, spacing=1.0)),
        ("225 deg", render_stack_image(vol, 225, squash=0.75, spacing=1.0)),
        ("315 deg", render_stack_image(vol, 315, squash=0.75, spacing=1.0)),
        ("exploded", render_stack_image(vol, 35, squash=0.75, spacing=2.4)),
    ]
    scale = max(2, min(6, round(180 / max(renders[0][1].width, 1))))
    pad = 16
    cell_w = max(im.width * scale for _, im in renders) + pad
    cell_h = max(im.height * scale for _, im in renders) + pad + 20

    top_row_h = max(v.shape[0] for v in views) * scale + 36
    total_w = max(cell_w * len(renders) + pad, sum(v.shape[1] * scale + pad * 2 for v in views))
    total_h = top_row_h + cell_h + pad * 2

    bg = Image.new("RGBA", (total_w, total_h), (242, 245, 249, 255))
    draw = ImageDraw.Draw(bg)

    # Row 1: extracted views
    x = pad
    for label, v in zip(("TOP", "SIDE", "FRONT", "BACK"), views):
        im = Image.fromarray(v).resize((v.shape[1] * scale, v.shape[0] * scale), Image.NEAREST)
        bg.paste(im, (x, pad + 18), im)
        draw.text((x, pad), f"{label} ({v.shape[1]}x{v.shape[0]})", fill=(70, 80, 95, 255))
        x += im.width + pad * 2

    # Row 2: rendered stack angles + exploded
    y0 = top_row_h + pad
    for i, (label, im) in enumerate(renders):
        up = im.resize((im.width * scale, im.height * scale), Image.NEAREST)
        cx = pad + i * cell_w + (cell_w - up.width) // 2
        cy = y0 + 18 + (cell_h - 20 - up.height) // 2
        bg.paste(up, (cx, cy), up)
        draw.text((pad + i * cell_w + 4, y0), label, fill=(70, 80, 95, 255))

    bg.convert("RGB").save(out_path)

def compute_normals(vol: np.ndarray) -> np.ndarray:
    """Estimate outward 3D unit normals per occupied voxel, packed as RGBA uint8."""
    occ = (vol[..., 3] > 0).astype(np.float32)
    p = np.pad(occ, 1, mode="constant", constant_values=0.0)

    def smooth_x(a: np.ndarray) -> np.ndarray:
        return a[:, :, :-2] + 2.0 * a[:, :, 1:-1] + a[:, :, 2:]

    def smooth_y(a: np.ndarray) -> np.ndarray:
        return a[:, :-2, :] + 2.0 * a[:, 1:-1, :] + a[:, 2:, :]

    def smooth_z(a: np.ndarray) -> np.ndarray:
        return a[:-2, :, :] + 2.0 * a[1:-1, :, :] + a[2:, :, :]

    nz = smooth_x(smooth_y(p[:-2, :, :] - p[2:, :, :]))
    ny = smooth_x(smooth_z(p[:, :-2, :] - p[:, 2:, :]))
    nx = smooth_y(smooth_z(p[:, :, :-2] - p[:, :, 2:]))

    mag = np.sqrt(nx * nx + ny * ny + nz * nz)
    zero = mag < 1e-5
    nz = np.where(zero, 1.0, nz)
    mag = np.where(zero, 1.0, mag)
    nx /= mag
    ny /= mag
    nz /= mag

    out = np.zeros_like(vol)
    solid = vol[..., 3] > 0
    out[solid, 0] = np.clip(np.round((nx[solid] * 0.5 + 0.5) * 255.0), 0, 255).astype(np.uint8)
    out[solid, 1] = np.clip(np.round((ny[solid] * 0.5 + 0.5) * 255.0), 0, 255).astype(np.uint8)
    out[solid, 2] = np.clip(np.round((nz[solid] * 0.5 + 0.5) * 255.0), 0, 255).astype(np.uint8)
    out[solid, 3] = 255
    return out

def detect_emissive(vol: np.ndarray) -> np.ndarray:
    """Detect small bright/vivid surface accents (headlights, windows, neon) as an RGBA emissive volume."""
    solid = vol[..., 3] > 0
    out = np.zeros_like(vol)
    if not solid.any():
        return out

    p = np.pad(solid, 1, mode="constant", constant_values=False)
    surface = solid & ~(
        p[:-2, 1:-1, 1:-1]
        & p[2:, 1:-1, 1:-1]
        & p[1:-1, :-2, 1:-1]
        & p[1:-1, 2:, 1:-1]
        & p[1:-1, 1:-1, :-2]
        & p[1:-1, 1:-1, 2:]
    )
    surf_count = int(surface.sum())
    if surf_count == 0:
        return out

    rgb = vol[..., :3].astype(np.float32)
    r, g, b = rgb[..., 0], rgb[..., 1], rgb[..., 2]
    luma = 0.299 * r + 0.587 * g + 0.114 * b
    max_c = np.maximum(np.maximum(r, g), b)
    min_c = np.minimum(np.minimum(r, g), b)
    sat = (max_c - min_c) / np.maximum(max_c, 1.0)

    cand = surface & (
        ((luma >= 180.0) & ((sat >= 0.30) | (luma >= 232.0)))
        | ((max_c >= 215.0) & (sat >= 0.62) & (luma >= 110.0))
    )
    if not cand.any():
        return out

    # Exclude dominant body colors (> 14% of surface) so a bright red car or white wall doesn't glow.
    surf_rgb = vol[surface, :3]
    surf_keys = (
        (surf_rgb[:, 0].astype(np.uint32) << 16)
        | (surf_rgb[:, 1].astype(np.uint32) << 8)
        | surf_rgb[:, 2].astype(np.uint32)
    )
    uniq, counts = np.unique(surf_keys, return_counts=True)
    dominant = set(int(k) for k, c in zip(uniq, counts) if c / surf_count > 0.14)

    cz, cy, cx = np.nonzero(cand)
    cand_rgb = vol[cz, cy, cx, :3]
    cand_keys = (
        (cand_rgb[:, 0].astype(np.uint32) << 16)
        | (cand_rgb[:, 1].astype(np.uint32) << 8)
        | cand_rgb[:, 2].astype(np.uint32)
    )
    keep = np.array([int(k) not in dominant for k in cand_keys], dtype=bool)
    if keep.any():
        out[cz[keep], cy[keep], cx[keep]] = vol[cz[keep], cy[keep], cx[keep]]
    return out

def to_data_uri(arr: np.ndarray) -> str:
    buf = io.BytesIO()
    Image.fromarray(arr).save(buf, format="PNG")
    return "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode("ascii")

def build_html_viewer(vol: np.ndarray, views: list[np.ndarray], out_path: Path, title: str) -> None:
    template_path = Path(__file__).with_name("viewer.html")
    html = template_path.read_text(encoding="utf-8")
    strip = np.concatenate(list(vol), axis=1)
    payload = {
        "title": title,
        "slices": int(vol.shape[0]),
        "sliceWidth": int(vol.shape[2]),
        "sliceHeight": int(vol.shape[1]),
        "sheet": to_data_uri(strip),
        "top": to_data_uri(views[0]),
        "side": to_data_uri(views[1]),
        "front": to_data_uri(views[2]),
        "back": to_data_uri(views[3]),
    }
    marker = "/*__EMBEDDED_STACK__*/null"
    if marker not in html:
        raise RuntimeError(f"Template marker not found in {template_path}")
    out_path.write_text(html.replace(marker, json.dumps(payload), 1), encoding="utf-8")

def main() -> None:
    p = argparse.ArgumentParser(description="Carve 4 orthographic pixel-art views into a sprite-stacking slice strip.")
    p.add_argument("sheet", type=Path, help="Input 4-view PNG (transparent or solid background)")
    p.add_argument("out", type=Path, help="Output horizontal slice strip PNG (ground slice on left)")
    p.add_argument("--slices", type=int, default=None, help="Target slice count / height (default: auto, 0 = native)")
    p.add_argument("--top-nose", choices=["auto", "up", "down"], default="auto")
    p.add_argument("--side-nose", choices=["left", "right"], default="left")
    p.add_argument("--order", choices=["auto", "reading"], default="auto", help="auto = match views by proportion; reading = top,side,front,back")
    p.add_argument("--colours", type=int, default=24, help="Shared palette size (0 = keep original colors)")
    p.add_argument("--shade", type=float, default=0.2, help="Darken side faces near ground (0 = flat, 0.2-0.25 = subtle depth)")
    p.add_argument("--round", action="store_true", help="Lathe each slice to an ellipse: towers, balloons, pots, bottles")
    p.add_argument("--dark-thresh", type=int, default=70, help="Luminance cutoff for stripping 1px dark view outlines (0 = disable)")
    p.add_argument("--bg-tol", type=int, default=28, help="Color tolerance for flood-fill background removal on opaque sheets")
    p.add_argument("--preview", type=Path, default=None, help="Save a visual verification contact sheet PNG")
    p.add_argument("--html", type=Path, default=None, help="Save a self-contained interactive HTML turntable/scan viewer")
    p.add_argument("--normals", type=Path, default=None, help="Save a matching 3D normal-map slice strip PNG")
    p.add_argument("--emissive", type=Path, default=None, help="Save an auto-detected emissive mask slice strip PNG")
    a = p.parse_args()

    sheet = np.array(Image.open(a.sheet).convert("RGBA"))
    vol, views = views_to_slices(
        sheet,
        slices=a.slices,
        top_nose=a.top_nose,
        side_nose=a.side_nose,
        order=a.order,
        colours=a.colours,
        shade=a.shade,
        dark_thresh=a.dark_thresh,
        bg_tol=a.bg_tol,
        round_=a.round,
    )

    a.out.parent.mkdir(parents=True, exist_ok=True)
    strip = np.concatenate(list(vol), axis=1)
    Image.fromarray(strip).save(a.out)
    print(f"{vol.shape[0]} slices of {vol.shape[2]} x {vol.shape[1]} px -> {a.out}")
    for z0, z1 in thin_runs(vol):
        print(f"warning: slices {z0}-{z1} hold a 1px-thin part; it will blink as the stack turns. "
              "Draw posts, ropes and antennas at least 2px wide in the views.")

    if a.normals:
        a.normals.parent.mkdir(parents=True, exist_ok=True)
        norm_strip = np.concatenate(list(compute_normals(vol)), axis=1)
        Image.fromarray(norm_strip).save(a.normals)
        print(f"normals -> {a.normals}")

    if a.emissive:
        a.emissive.parent.mkdir(parents=True, exist_ok=True)
        emis_strip = np.concatenate(list(detect_emissive(vol)), axis=1)
        Image.fromarray(emis_strip).save(a.emissive)
        print(f"emissive -> {a.emissive}")

    if a.preview:
        a.preview.parent.mkdir(parents=True, exist_ok=True)
        build_preview(vol, views, a.preview)
        print(f"preview -> {a.preview}")

    if a.html:
        a.html.parent.mkdir(parents=True, exist_ok=True)
        build_html_viewer(vol, views, a.html, title=a.out.stem)
        print(f"viewer -> {a.html}")

if __name__ == "__main__":
    main()
```

## File: skills/sprite-stacking/references/engines.md
size: 4884
sha: 99f51b35a342883dca095052b7472f95ccf01003
upstream: https://github.com/danielwh2/sprite-stacking/blob/main/skills/sprite-stacking/references/engines.md

````text
# Engine recipes

All of these run the loop from SKILL.md step 3: rotate each slice around its centre, scale y by `squash`, lift by `-round(z * spacing)`, whole-pixel position, nearest filtering.

## Web (Canvas 2D, React)

For a crisp outer outline, render into an offscreen canvas at 1x art-pixel size, outline its alpha once, then draw it onto the display canvas at an integer scale (`4`, `6`, etc.) with `imageSmoothingEnabled = false`. In software pixel-buffer rasterizers, rotate around symmetric centres `(w - 1) * 0.5, (h - 1) * 0.5` with `Math.round(...)` nearest sampling to avoid 0.5-art-px rotational drift across 90° turns. For smooth 60 fps animation (bobbing, orbiting, child props on a spinning parent), keep positions in continuous screen-space floats and round only once at the final 1-screen-pixel `drawImage` destination (`Math.round(dx), Math.round(dy)`). Animate grounded prop entrances bottom-to-top via per-slice `lifts` (extending the reveal frontier past `count` by the blend width so top slices finish easing before returning `null`), add per-slice lateral `xOffsets` scaled by `(z / (count - 1))²` for anchored swaying props like kelp or banners, and apply per-slice `(xOffsets, yOffsets)` proportional to `(z - centerZ) * sin(pitchAngle)` for 3D pitch/roll tilt on leaping creatures or rolling hulls. Validate `sheet.width % count === 0` at load time, verify each strip's native nose axis (`+x`, `+y`, or `-y`) visually before computing `turn` from velocity, and depth-tint submerged stacks toward the water hue (omitting hard outlines) so surface and underwater layers separate cleanly. For multi-frame articulated creatures (fluke-beats, wing-waves, tail-swishes, bird flaps/glides), load `f0..fN-1` strips with identical `(w, h, count)`, extra Z-slice headroom, and shared symmetric pivots, selecting frames deterministically from the creature's physical phase. `scripts/viewer.html` implements the full reference pipeline, plus orbit, slice scan, exploded view and 16-direction export.

Without an outline, draw straight to the canvas:

```ts
function drawStack(
  ctx: CanvasRenderingContext2D,
  sheet: HTMLImageElement | HTMLCanvasElement,
  count: number,
  x: number,
  y: number,
  turn: number,
  squash = 0.75,
  spacing = 1,
) {
  const w = sheet.width / count;
  const h = sheet.height;
  const ox = -(w - 1) * 0.5;
  const oy = -(h - 1) * 0.5;
  ctx.imageSmoothingEnabled = false;
  for (let z = 0; z < count; z++) {
    ctx.save();
    ctx.translate(Math.round(x), Math.round(y - z * spacing));
    ctx.scale(1, squash);
    ctx.rotate(turn);
    ctx.drawImage(sheet, z * w, 0, w, h, ox, oy, w, h);
    ctx.restore();
  }
}
```

## Godot 4 (`Node2D`)

Set the default texture filter to Nearest and keep the node on whole pixels.

```gdscript
extends Node2D

@export var sheet: Texture2D
@export var slices := 15
@export var turn := 0.0:
	set(v):
		turn = v
		queue_redraw()
@export_range(0.1, 1.0) var squash := 0.75
@export var spacing := 1.0

func _draw() -> void:
	if sheet == null:
		return
	var w := float(sheet.get_width()) / slices
	var h := float(sheet.get_height())
	var rect := Rect2(-floorf(w / 2.0), -floorf(h / 2.0), w, h)
	var spin := Transform2D(turn, Vector2.ZERO)
	for z in slices:
		var lift := Transform2D(Vector2(1, 0), Vector2(0, squash), Vector2(0, -roundf(z * spacing)))
		draw_set_transform_matrix(lift * spin)
		draw_texture_rect_region(sheet, rect, Rect2(z * w, 0, w, h))
```

`_draw()` only runs again after `queue_redraw()`, which is why `turn` has a setter. For an outer outline, draw the stack into a `SubViewport` and run a 1px silhouette outline shader on its texture once.

## Unity

One child `SpriteRenderer` per slice with ascending sorting order, Point filter mode, parent snapped to whole pixels. To outline, render all slices into a `RenderTexture` once and outline that.

## Phaser, Pygame, anything else

The same loop: rotate each slice around its centre, scale y by `squash`, lift by `-round(z * spacing)`, nearest filter.

## Spherical bodies & world-space terminator lighting

Under orthographic pitch `squash` and vertical slice lift `spacing`, a sphere with horizontal radius `R_xy` projects to an exact circle only when its vertical slice half-height satisfies the Cauchy-Schwarz envelope:

```ts
// Vertical half-height in slices so the projected silhouette is circular
const R_z = (R_xy * Math.sqrt(1 - squash * squash)) / spacing;
```

For rotating planets or moons, keep the strip in pure albedo (cloud bands, storms, craters) and evaluate `dot(normal, sunDir)` in world space when blitting each slice so the surface rotates smoothly through a fixed day/night terminator. For planetary rings, draw back-half particles (`y_local < 0`) before the planet stack, front-half particles (`y_local >= 0`) after, and darken particles inside the cylindrical shadow cone cast away from `sunDir`.
````

## File: README.md
upstream: https://github.com/danielwh2/sprite-stacking/blob/main/README.md

````text
# sprite-stacking

An agent skill that turns four pixel art views into fake-3D sprites you can spin. By Daniel White.

![MIT](https://img.shields.io/badge/licence-MIT-111?style=flat-square)
![Agent skill](https://img.shields.io/badge/agent-skill-111?style=flat-square)
![Python 3 + Pillow + NumPy](https://img.shields.io/badge/Python%203-Pillow%20%2B%20NumPy-111?style=flat-square)
![Works with](https://img.shields.io/badge/works%20with-Claude%20Code%20%C2%B7%20Codex%20%C2%B7%20Cursor%20%C2%B7%20OpenCode%20%C2%B7%20Gemini%20CLI%20%C2%B7%20Pi-111?style=flat-square)

![Six sprite-stacked assets spinning](media/showcase.gif)

## Install

```bash
npx skills add danielwh2/sprite-stacking
```

Installs to the current project. Add `-g` for every project, `-a claude-code` (or `codex`, `cursor`, ...) to pick an agent. Uses the [skills CLI](https://github.com/vercel-labs/skills), Node 22.20 or later.

Or copy `skills/sprite-stacking` into your agent's skills folder:

| Agent | Folder |
| --- | --- |
| Claude Code | `~/.claude/skills/` |
| Codex, Cursor, OpenCode, Gemini CLI, Pi | `~/.agents/skills/` |

## Use

Ask your agent:

- "Make a rotatable pixel art car."
- "Sprite stack this 4-view sheet and give me a turntable."
- "Add a fake 3D pixel art tree to my Godot game."

No image generator? The agent draws the views itself in a short Python script.

## How it works

![Sheet to slices to render](media/pipeline.png)

1. **Views:** top, side, front and back, orthographic, one row.
2. **Carve:** a voxel survives only where all four views cover it.
3. **Slices:** the volume is cut into a strip of flat layers, ground first.
4. **Render:** each layer is rotated, squashed for pitch and lifted 1 px.

## Slicer CLI

```bash
python3 skills/sprite-stacking/scripts/views_to_slices.py views.png slices.png \
  --slices 36 --preview check.png --html viewer.html
```

- `--slices N`: layer count. 15-20 chunky, 32-48 detailed.
- `--order reading`: force TOP, SIDE, FRONT, BACK. Default matches views by proportion.
- `--round`: towers, balloons, pots carve round, not square.
- `--side-nose right`: the side view faces right.
- `--colours N`: shared palette size, `0` keeps every colour.
- `--normals path.png`: save a matching 3D normal-map slice strip.
- `--emissive path.png`: save an auto-detected emissive mask strip (headlights, windows, neon).

## Viewer

`--html` writes a standalone page:
- **Modes:** Turntable, **Scene** (Living Diorama with **Neon Street** and **Meadow Island** procedural stacked props, unified Y-sorting, and multi-PNG drop / `+ Add Prop`), Slice scan, Exploded view, and **Play** mode (**Drive** with WASD/arrows + drift across infinite Street/Meadow/Grid worlds, **Wind** sway, **Shatter** & rebuild).
- **Lighting & Weather:** Live **Time of Day** (Day → Sunset → Night with auto-emissive glow and ground light pools), draggable sun (`Shift+drag`) with projected 3D shadows, and **Weather** (**Rain** with pixel streaks, ground ripples, and wavy upside-down wet reflections; **Snow** with real-time snow cap accumulation on upward-facing voxels via `normZ`).
- **Performance:** Vectorized NumPy carving, zero-allocation typed-array software rasterizer, and angle/lighting-keyed prop sprite caching (~0.2–1.5 ms/frame in Chrome).
- **Export:** 16-direction baked PNG sprite sheet export. `skills/sprite-stacking/scripts/viewer.html` also opens on its own and accepts dropped slice strips.

## Requirements

Python 3 with Pillow and NumPy (`pip install pillow numpy`).

## Contributing

```bash
python3 tests/check.py
```

Prints `ok`.

## Prior art

Sprite stacking is a long-standing indie game technique, not invented here. This skill adds the four-view carving and the agent workflow.

## Licence

MIT
````
