# Morphing

# Morphing

`@ferret/morph` lets a light figure change pose and an aeroplane fly across the screen and bank, open its flaps and change its livery without a model for each moment. A **bank** holds
*states* of one topology (positions, normals, colours of the same vertices); an **object** wears a bank and is a weighted mix of up to four of its states; a program of the
motion language moves the mix. The module keeps the numbers; the views draw them.

## The pieces

| piece | file | what it is |
|---|---|---|
| bank | `bank.ts` | the states of one topology: positions, normals, colours, scale, pivot; JSON and the binary `FMRB` file; a content hash; `validate()` names a fault |
| object | `sim.ts` | up to four weighted states; eased moves from the current blend; holds; a pin (an exact mix); a tint; snapshots; `version` changes only when the blend does |
| states from | `fromclip.ts`, `babylon.ts`, `source.ts` | frames of a clip (`bankFromClip`), of a model's animation groups skinned by the engine (`bankFromGroups`), the capture of a mesh, a function of the vertex, a colour change, a mirror, a part turned about a hinge (`rotatedPartState`) |
| retopology | `conform.ts` | a state of the bank's topology that looks like a mesh of another topology: align, closest point with normals, smoothing, landmarks; a residual and a folded-triangle report, a `ConformError` when it fails |
| CPU view | `view.ts` `CpuMorphView` | one mesh rewritten when the blend changed (never otherwise): the reference, exact for any bank, for the few important objects |
| GPU view | `view.ts` `GpuMorphBatch` | one mesh and one draw call for all objects of a bank: the states are in a float texture (`layout.ts`), the vertex shader mixes four per instance |
| far | `far.ts` | impostors baked at the states and blended by colour with the perspective ladder's cross-fade |
| budget | `budget.ts` | which objects take the CPU path and which the GPU one |
| host | `host.ts`, `service.ts`, `ops.ts` | `MorphHost` is the world service: the `morph.*` commands and the words of the motion language |

## A bank from what exists

```ts
const bank = bankFromGroups(root, [group], { times: [0], prefix: 'Sit' }, await averageColors(root)).bank;   // a character, a frame of a clip
const planeBank = bankFromMesh('level', arrays.positions, arrays.indices, arrays.colors);                       // a model: one state
rotatedPartState(planeBank, 'roll-right', 'level', part, hingePoint, [0, 0, 1], 0.35);                          // a part on its hinge
const all = mergeBanks([bankA, bankB]);                                                                         // same topology only
```
A figure of light weight takes one colour per vertex (`averageColors` takes the mean of a textured part). A state of a model of another topology goes through `conform` first.

## Moving objects: the motion language

`MorphHost` puts the morph words on the same bridge as the other motion words; `move-along` and `turn-to` are general words of `@ferret/motion` that move any node:

```lisp
(par (move-along (list (list -150 30 40) (list 150 30 40)) 60 :face #t)     ; fly the route at 60 m/s, nose along the path
     (seq (morph-to roll-right 1s) (morph-to level 1s))                      ; bank and level out
     (tint-to #3a4a6a 6s))                                                   ; and go to the night livery
```
`(morph-to state seconds [:curve ease] [:on name])` lasts until arrival; `morph-set`, `morph-stop`, `morph-weight`/`morph-blend` (holds), `tint`, `tint-to`, `morph-state`. The words
name an object by the program's `target` parameter or by `:on`. Run a program with the command `morph.run { source, target?, params? }`.

## Drawing

`new GpuMorphBatch(scene, bank)` then `batch.add(object, node)`: one draw call, the CPU writes four state numbers, four weights and a tint per object, and only when its blend changed. The batch reads the bank
texture by the vertex id (`vertex-id`, WebGL2) or by a baked index in a vertex attribute (`uv-index`); they draw the same picture. `new CpuMorphView(scene, bank, object)` is the reference.
The test `tests/e2e/morph.spec.ts` draws one object by each path and compares the pictures pixel by pixel.

## Commands

`morph.list`, `morph.spawn`, `morph.remove`, `morph.state`, `morph.set`, `morph.to`, `morph.stop`, `morph.hold`, `morph.release`, `morph.tint`, `morph.from-clip`, `morph.conform`, `morph.validate`, `morph.run`,
`morph.programs`, `morph.stop-program`. The `?morph[&lib=<catalog url>]` viewer adds `morph.bake`, `morph.show`, `morph.hide`, `morph.camera`, `morph.screenshot`.

## In UE

The game runs whole inside Chromium (CEF) hosted by UE, so the module, its shaders and its views are the browser's own and the picture is the same; nothing here is specific to UE. A bank is also a
file (`MorphBank.toBinary()`, `fromBinary`) and a float texture layout (`layout.ts`) for a host that draws it some other way.
