---
name: ferret-morph
description: Use to make light figures change pose and aircraft fly, bank and change colour in Ferret without re-modelling (@ferret/morph): states from clip frames or hinged parts, blends by the motion language, GPU batches, far colour morphs, retopology, ?morph viewer.
---

# Morphing

## When to use
A figure that must move between poses without a skeleton per instance (soldiers in a row that sit, stand and crouch), an aeroplane that banks, drops flaps and changes livery while it flies a route, a colour or shape change of a far object. Not for the full-detail player or NPC (`ferret-animation`), not for crowds of skinned characters (`ferret-crowd`, whose far rungs are impostors; this module morphs *between* states).

## The model
A **bank** is the states of one topology; an **object** wears a bank and is a weighted mix of at most four states; programs of the motion language move the mix; a **view** draws it (CPU reference, or GPU: one draw call for all objects of a bank). Everything computed is plain numbers in the fixed step, the same on every peer.

## Steps
1. States: `bankFromGroups(root, [animationGroup], { times: [0] | every | keyPoses, prefix }, averageColors(root))` for a character; `bankFromMesh(name, positions, indices, colors)` for a model, then `rotatedPartState` for each part on a hinge and `colorState`/`tintedState` for liveries; `mergeBanks` joins banks of one topology. Another topology: `conform(bank, state, mesh)` (read the `ConformReport`; a `ConformError` names the cause).
2. `const host = world.addService(new MorphHost({ seed, node: name => handle }))`; `host.service.addBank('man', bank)`; `host.service.spawn('man0', 'man', 'Idle@0')`.
3. Nodes: `nodeHandle(transformNode, forwardAxis)` (a model with its nose along +x says `[1, 0, 0]`) and put it where the program can find it (`node` callback).
4. Draw: `new GpuMorphBatch(scene, bank)`, `batch.add(object, node)`; the few important objects: `new CpuMorphView(scene, bank, object)` parented to a node. `chooseViews` splits them by budget.
5. Move: `world.commands.execute('morph.run', { source: '(par (move-along route 60 :face #t) (morph-to roll-right 1s) (tint-to #3a4a6a 6s))', target: 'plane0' })`.
6. Look: `node tools/build.mjs viewer`, open `?morph&lib=<catalog url>`, `morph.bake`, `morph.spawn`, `morph.show`, `morph.camera`, `morph.screenshot`.
7. In UE the game runs in CEF: nothing to do. A baked bank is a file: `bank.toBinary()`, `MorphBank.fromBinary`.

## Check afterwards
`morph.validate` has no errors; the sample (`examples/morph`) and `tests/e2e/morph.spec.ts` show the GPU picture equal to the CPU one (max diff 0 on a real card); `object.version` stops growing when the blend has arrived (a morph that arrived costs nothing); a program's `morph.programs` shows no `error`.

## What usually goes wrong
- `bankFromGroups` on a model that is not at its rest pose or not in the scene: the states come out in the wrong place; bake before hiding the root, and hide it after.
- `mergeBanks` throws "differ in topology": the banks come from different meshes; use `conform`.
- A part moves with its neighbour: the part's meshes are not next to each other in the model's vertex order, or a node of several primitives was named by one primitive: group by the base name.
- A hinged part swings about the wrong line: the hinge point comes from the part's bounds (`partBounds`); check which end is the leading edge.
- `(morph-to ...)` does nothing: the object name is not the program's `target` (or `:on`).
- On a very old device the GPU batch throws "cannot read float textures in the vertex shader": use `CpuMorphView`.

## Files

- `reference/morph.md` — Geometric and colour morphing of light figures and vehicles. States are frames of existing clips or poses of a model's parts; objects blend between them under the motion language; the GPU draws thousands in one call, far objects morph by colour.
- `reference/api-morph.md` — every export of `@ferret/morph` with signature and doc comment
- `reference/commands.md` — 14 commands of these packages with their arguments
