# clip editing

**Cutting a clip down to the part you want, and making it loop.** A motion
capture runs on: a BVH from a suit, a webcam take, ten seconds of someone
waving. What a character plays is usually a piece of it, and an idle or a
wave has to come round again without a pop. These are the edits, on the clip
format every source shares ([[glb-clips]]'s `GlbClip`), so they serve BVH,
GLB clips and captures alike:

- `trimClip(clip, from, to)` keeps the part between two times (seconds), as a
  clip of its own starting at 0.
- `clipFrame(clip, t)` keeps one moment: a POSE, as a clip that holds it.
  Scrub to the frame you want and keep it; more precise than holding still.
- `bestLoop(source, clip, { from, to })` moves a trim's two ends, each within
  `reach` seconds of where it is, to the pair of frames whose poses are
  closest, so the loop's seam is as small as the clip allows.
- `loopBlend(source, clip, seconds)` cross-fades the clip's last `seconds`
  into its first, so the end flows into the start. The clip is `seconds`
  shorter: its first `seconds` are what the tail fades into.

```javascript
import { readBvh } from 'tosijs-3d/bvh'
import { trimClip, bestLoop, loopBlend } from 'tosijs-3d/clip-edit'

const source = readBvh(text)
const take = source.clips[0]
const { from, to } = bestLoop(source, take, { from: 2, to: 4.5 })
const loop = loopBlend(source, trimClip(take, from, to), 0.25)
```

The [character builder](/character-builder/) does this from its playback row:
drag the trim handles, **best loop**, **loop blend**, then keep the result as
a clip of your own.

## How the seam is measured

Two poses are as far apart as the sum, over every turning joint, of the angle
between them, plus how differently the hips are moving (their velocity, in
m/s, times `velocityWeight`, default 2): a walk whose legs match but whose
body is stopping at one end and moving at the other still pops. Positions
are not compared, since a clip that travels ends somewhere else by design.

## How the blend keeps travel

Rotations are slerped and the hips' position lerped, eased in and out. A clip
that travels (root motion) would be dragged back to where it began by a
plain blend, so the head it fades into is moved on by how far the hips went
across the loop, sideways only: their height blends straight, so the end
meets the start.

Pure: no DOM, no Babylon.
