# Procedural Animal Studio · Plan (PLAN)

> Goal: take the pure-code cat from the original prototype (zero glb, zero textures, SDF + marching meshes, GPU fur, 27 bones)
> and refactor it into a **generic quadruped engine** that produces **10** interactive 3D animal assets.

---

## 1. Baseline inventory (the cat pipeline)

| Layer | Prototype module | Engine module | Key capability |
|---|---|---|---|
| Distance-field primitives | `core/sdf-math.js` | `demo/src/engine/sdf.js` | smin / ssub / ellipsoid / roundCone / capsule / analytic normals |
| Torso implicit | `core/loft.js` | `demo/src/engine/loft.js` | elliptical sweep along the spine + Catmull-Rom + end-cap closure (anti-"caterpillar") |
| Meshing | `core/surface-nets.js` | `demo/src/engine/surface-nets.js` | adaptive axis-warped Surface Nets + Taubin volume-preserving smoothing + normal reprojection |
| Anatomy spec | `anatomy/feline-spec.js` | `demo/src/engine/prepare.js`, `demo/src/species/cat.js` | head/spine/limbs/tail/bounds — the single source of truth for both sculpting and rigging |
| Sculpting | `anatomy/feline-bust-sdf.js` | `demo/src/engine/head-kit.js`, `demo/src/engine/body-kit.js` | cranium/zygomatic/brow ridge/muzzle/whisker pad/orbit/auricle + body smin assembly |
| Organs | `organs/feline-organs.js` | `demo/src/engine/organs.js` | dual-layer refractive iris eyeballs, wet nose, whiskers, tongue |
| Paws | `organs/feline-paws.js` | `demo/src/engine/paws.js` | padded toes + toe beans + fur tufts |
| Fur | `groom/strand-groomer.js` | `demo/src/engine/fur.js` | 160k instanced strands, zoned grooming flow field, coat-genetics GLSL, Kajiya-Kay |
| Rig | `rig/feline-rig.js` + `ik.js` + `motion.js` | `demo/src/engine/rig.js` + `ik.js` + `motion.js` | 27 bones, skinning gated by sculpt region, analytic two-bone IK, walk/run/groom |
| Studio | `studio/photo-stage.js` | `demo/src/engine/studio.js` | procedural window-light environment map |

Conclusion: **the rig, the fur and the meshing are species-agnostic**; the genuinely "cat" parts are only the proportion table, the head sculpt, the coat, the organ parameters and the gait.
So the extension strategy is "extract a generic engine + one declarative module per species" rather than copying the cat ten times.

---

## 2. Target species (10, silhouette difference first)

| # | Species | Scientific name | Silhouette cues | Fur / colorway | Special parts |
|---|---|---|---|---|---|
| 1 | Cat | Felis catus | slender body, S-curve tail, pointed ears, short muzzle | orange tabby / mackerel tabby / calico / tuxedo | reference species |
| 2 | Shiba Inu | Canis familiaris | curled tail, triangular prick ears, thick chest, short muzzle | red / black-and-tan / sesame + urajiro | urajiro chest and belly |
| 3 | Red fox | Vulpes vulpes | very long muzzle, large ears, huge bushy tail, slender legs | red with white tail tip / black gloves / silver | white tail tip |
| 4 | Long-eared rabbit | Oryctolagus | long upright ears, round rump, thick hind legs | cream / agouti grey-brown / black | short pom-pom tail |
| 5 | Giant panda | Ailuropoda | round and rolling, short legs, round ears | black-and-white eye patches / black shoulder band | eye patches / black limbs |
| 6 | Pig | Sus scrofa | barrel body, snout disc, drooping ears, curled tail | pink / spotted | snout disc |
| 7 | Sheep | Ovis aries | fluffy wool, black face, short legs | cream fleece + black face and hooves | long wool |
| 8 | Horse | Equus ferus | tall long legs, long neck, long face, mane and long tail | chestnut / black / grey | mane, long tail, single hoof |
| 9 | Sika deer | Cervus nippon | antlers, slender legs, long neck, white spots | chestnut red + white spots + white rump | antlers |
| 10 | Hamster | Mesocricetus | tiny round body, cheek pouches, short tail, small round ears | golden / three-lined | cheek pouches, short tail |

---

## 3. Architecture (`demo`)

```
demo/
  index.html                 interactive gallery (species switching / motion / fur / layer staging / skeleton)
  gallery.html               10-species overview wall (for QC and promo images)
  PLAN.md  README.md  CONTRACT.md
  src/
    engine/
      sdf.js                 primitives (+ rounded box and friends)
      loft.js                torso sweep
      surface-nets.js        meshing (+ per-species vertex coloring callback)
      studio.js              window-light environment
      rig.js                 generic 27 bones (fixed template, coordinates from spec)
      ik.js                  generic two-bone IK / foot locking
      motion.js              generic gait: idle / walk / run / groom
      fur.js                 generic fur system (species supply groom / flow / coat GLSL)
      body-kit.js            body primitives generated from spec + standard smin assembly
      head-kit.js            parametric head sculpt (cranium/brow/cheek/muzzle/nose pad/chin/orbit/ear) + ear shapes
      organs.js              parametric eye / nose / whiskers / tongue
      paws.js                parametric paw / hoof (paw | hoof | cloven)
      viewer.js              studio + orbit camera + UI wiring
    species/
      registry.js            species registry
      cat.js shiba.js fox.js rabbit.js panda.js pig.js sheep.js horse.js deer.js hamster.js
  tools/
    preview.html             6-view QC contact sheet (?s=<id>&stage=&coat=&motion=)
    shot.mjs                 headless Chrome screenshot
```

### 3.1 Species contract (see CONTRACT.md for details)

Each species is **one self-contained ES module** with a default export:

```js
export default {
  id, name, en, emoji, accent,
  spec,        // proportion source of truth: head/spine/chest/rump/neck/front legs/hind legs/tail/bounds/ear root and tip
  headLocal,   // head-local SDF (can be generated with head-kit)
  bodyParts,   // body primitive distance field (can be generated with body-kit)
  assemble,    // optional: custom smin assembly (default: body-kit.assembleStanding)
  groom,       // fur zoning: density/length/weight/zone
  flow,        // fur flow direction (per zone)
  coatGLSL,    // coat-genetics GLSL: evalCoatColor(...)
  coats,       // coat list (including eye hue)
  organs,      // eye / nose / whisker / tongue parameters
  paws,        // paw / hoof parameters
  gait,        // gait parameters
  vertexTint,  // optional: vertex colors (pink inner ear, etc.)
  extras,      // optional: extra meshes such as antlers or a mane
}
```

### 3.2 Genericity boundary

- All species share the **27-bone template** (spine 4 + neck 2 + head 1 + ears 2 + tail 4 + front legs 3x2 + hind legs 4x2),
  so GPU skinning, IK, the motion engine and the skeleton overlay are all reused.
- The differences are numeric: `spec` sets leg length / neck length / tail shape; `headLocal` sets the face; `groom/coat` sets the fur;
  `paws` selects paw or hoof.
- Special parts (antlers, snout disc, mane) may be built inside a species module and must not leak into the engine.

---

## 4. Phases

- **P0 engine**: extract the generic engine while keeping all of the cat's detail expressiveness (head-kit's default parameters must reproduce the cat head).
- **P1 cat port**: `demo/src/species/cat.js`; compare QC against the original cat screenshots and confirm there are no regressions.
- **P2 species**: shiba / red fox / rabbit first (to validate head-shape differences), then panda / pig / sheep (to validate roundness and fur volume),
  and finally horse / deer / hamster (to validate long legs and neck, antlers, miniature body). Every species must pass 6-view QC.
- **P3 gallery**: species cards, motion, coat, layer staging (clay / organs / fur), skeleton, strand density, lighting, auto-rotate.
- **P4 overview**: `demo/gallery.html` with the 10-species wall plus an overview capture; the README documents how to run it and the asset specs.

## 5. Acceptance criteria

1. `demo/index.html` opens under a local HTTP server, all 10 species can be switched, with no console errors and no NaN.
2. For every species: front / 3-4 view / side / head / rear / top 6 views are recognizable, proportions do not collapse, and feet do not sink through the ground.
3. The clay silhouette holds; the fully furred state does not blur into a single mass; organs (eyes / nose) do not float.
4. Under motion (walk/run/groom) the paws stay locked, with no ground penetration and no floating.
5. A 10-species overview image and per-species QC images are provided.

## 6. Risks and mitigations

| Risk | Mitigation |
|---|---|
| Uneven head-sculpt quality across 10 species | One shared head-kit + 3 rounds of screenshot iteration per species, silhouette first |
| Stiff rig motion on long-legged / long-necked species | Gait parameters scale with leg length; IK keeps the feet locked |
| Fur smothering the facial features | Zero density around eyes / nose / mouth (gated by groom zone) |
| Slow rebuild when switching species | On-demand geometry build + loading indicator; the overview wall uses low-resolution clay |
| Shared engine broken by concurrent edits | Species developers only write `demo/src/species/<id>.js`; engine changes go through the mainline |
