# Loading placeholders

An avatar is not drawn until every item it wears has been read and built. Over a slow connection, or when a whole
lobby joins at once, that leaves holes in the scene and then avatars that pop in. With placeholders on, the core
draws a neutral figure in each loading avatar's place instead:

- **An original design.** A soft jointed mannequin made at run time from rounded parts on the avatar rig's joints, in a
  cool grey. No item art, nothing from the platform's own loading figure.
- **The avatar's height and body type.** Once the manifest is known the figure is posed by the avatar's own
  specialised rig (weight, height and gender), and the female figure has its own proportions. Before that (a player
  added with `sona_avatar_add_waiting`) it is a neutral figure.
- **It moves.** It plays the avatar's own clip as soon as that clip has been read, and a built-in idle before
  (breathing, a slow look about, a weight shift; seeded per avatar, so a crowd never moves as one). The idle blends
  into the clip over a fixed 0.3 s.
- **A crossfade at READY.** The avatar's clock takes the figure's time on the same clip, so the pose runs on. The
  figure, pulled a little toward the camera so it covers the avatar, fades out over the first part (set to 60%) of
  `reveal_seconds`; the avatar itself first shows in the figure's tint, which fades out over the whole crossfade,
  so the parts the figure did not cover (a coat, a hood) change shape before they change colour.
- **A clear, friendly failed state.** A FAILED avatar keeps a dimmed, slumped figure in a muted red with a question
  mark over its head, turned toward the camera. `SONA_PH_HIDE_FAILED` fades it out after `failed_seconds`.
- **Crowds.** One draw per figure, posed at the crowd's rates (3.10b), and past `sprite_px` a sprite of the
  figure's own look, baked into the crowd's atlas like an avatar's.

The API ([C reference: loading placeholders](../reference.md#loading-placeholders)): seven functions on the ctx and the avatar, three on waiting avatars, and
three sized structs. Placeholders are off on a new ctx: every existing picture stays the same.

## Quick start

```c
sona_open(&cfg, &ctx);
sona_placeholder_set(ctx, NULL);         /* on, with the defaults */
sona_lod_set(ctx, NULL);                 /* optional: far figures become sprites too */

/* ... add avatars as usual; they show figures until READY ... */

while (game_running()) {               /* your game's loop */
  sona_update(ctx, dt, &view, NULL);     /* phases, timers, posing */
  sona_draw(renderer, &view, &target, NULL);
}
```

```cpp
sona::Context ctx(cfg);
sona_placeholder_set(ctx.raw(), nullptr);   // on, with the defaults
ctx.set_lod(nullptr);                       // optional: far figures become sprites too

// ... add avatars as usual; they show figures until READY ...

while (game_running()) {                    // your game's loop
  ctx.update(dt, &view);                    // phases, timers, posing
  r.draw(view, target);
}
```

```csharp
ctx.SetPlaceholders();                   // on, with the defaults
ctx.SetLod();                            // optional: far figures become sprites too

// ... add avatars as usual; they show figures until READY ...
```

```rust
ctx.set_placeholders(None)?;             // on, with the defaults
ctx.set_lod(None)?;                      // optional: far figures become sprites too

// ... add avatars as usual; they show figures until READY ...
```

```python
ctx.set_placeholders()                   # on, with the defaults
ctx.set_lod()                            # optional: far figures become sprites too

# ... add avatars as usual; they show figures until READY ...
```

Nothing else changes for the host: figures are drawn by `sona_draw` and `sona_snapshot` with the avatars.

## A player whose profile has not arrived

```c
sona_avatar *seat;
sona_avatar_add_waiting(ctx, &opts, &seat);         /* QUEUED, the neutral figure */
/* ... later, when the network brings the manifest ... */
sona_avatar_set_manifest_base64(seat, b64);         /* now it loads like any avatar */
```

```cpp
sona_add_options opts;
sona_add_defaults(&opts);
sona_avatar *seat = nullptr;
sona_avatar_add_waiting(ctx.raw(), &opts, &seat);     // QUEUED, the neutral figure
// ... later, when the network brings the manifest ...
sona_avatar_set_manifest_base64(seat, b64);           // now it loads like any avatar
```

```csharp
var seat = ctx.AddWaiting();                          // queued, the neutral figure
// ... later, when the network brings the manifest ...
seat.SetManifestBase64(Convert.ToBase64String(manifest));   // now it loads like any avatar
```

```rust
let mut seat = ctx.add_waiting(&AddOptions::default())?;   // queued, the neutral figure
// ... later, when the network brings the manifest ...
seat.set_manifest_base64("...")?;                           // now it loads like any avatar
```

```python
seat = ctx.add_waiting()                  # queued, the neutral figure
# ... later, when the network brings the manifest ...
seat.set_manifest(b64)                    # now it loads like any avatar
```

`sona_avatar_set_manifest` works once, on an avatar added waiting (else `SONA_E_ARGUMENT`). Placement, `play`,
layers and the rest work on a waiting avatar as on any avatar; a bad manifest fails it later with
`SONA_E_MANIFEST`, as `sona_avatar_add` does.

## The options

```c
typedef struct sona_placeholder_options {
  uint32_t size;          /* sizeof; sona_placeholder_defaults sets it */
  uint32_t flags;         /* SONA_PH_HIDE_FAILED, _NO_CLIP, _NO_SPRITES, _NO_GLOW */
  float delay_seconds;    /* default 0: shown at once (never a hole) */
  float appear_seconds;   /* default 0.2: the figure fades in */
  float reveal_seconds;   /* default 0.4: the crossfade at READY; 0 a cut */
  float failed_seconds;   /* default 2: with SONA_PH_HIDE_FAILED */
  float tint[3];          /* default 0.16, 0.19, 0.25, linear albedo */
  float failed_tint[3];   /* default 0.25, 0.15, 0.15 */
  float glow;             /* default 0.5: the rim pulses while loading */
  uint32_t reserved[8];
} sona_placeholder_options;
```

| Flag | What it does |
|---|---|
| `SONA_PH_HIDE_FAILED` | a FAILED avatar's figure fades out after `failed_seconds` |
| `SONA_PH_NO_CLIP` | always the built-in idle, never the avatar's clip |
| `SONA_PH_NO_SPRITES` | figures stay meshes at every distance |
| `SONA_PH_NO_GLOW` | no pulse along the rim |

`delay_seconds` above 0 hides the figure of an avatar that loads quickly (one whose items are in memory pops in
without one), at the price of a short hole. `sona_placeholder_off` lets the figures shown fade out over
`appear_seconds`; `sona_placeholder_get` returns `SONA_E_NOT_FOUND` while placeholders are off.

Per avatar, `sona_avatar_placeholder(a, mode)`: `-1` follows the ctx (the default), `0` never shows a figure, `1`
always does (with the ctx's options, or the defaults when the ctx never set any).

## Phases

`sona_avatar_placeholder_info` reports, per avatar:

| Phase | When |
|---|---|
| `SONA_PLACEHOLDER_NONE` | no figure: off, before `delay_seconds`, after the crossfade, or hidden |
| `SONA_PLACEHOLDER_WAITING` | added with `sona_avatar_add_waiting`, no manifest given |
| `SONA_PLACEHOLDER_LOADING` | QUEUED or LOADING |
| `SONA_PLACEHOLDER_REVEAL` | READY: the crossfade |
| `SONA_PLACEHOLDER_FAILED` | FAILED |

with the figure's opacity, its height in metres, its gender (0 neutral), whether it plays the avatar's clip, the
seconds since the avatar was added and whether it is a sprite. `sona_placeholder_stats_get` counts the figures in
view by phase, the sprites, how many were posed and how long that took, and the figures' draws in the last frame.

A figure shows from the moment the ctx has read the rig (`avatar-rig.json`), the first data every avatar needs; a
renderer cannot be made before the lighting data either, so in practice the first frame drawn already has them.

## How it draws

- **Meshes.** Four meshes are made once per ctx from the rig's bind pose: the neutral, male and female figures
  (about 3,900 vertices and 6,700 triangles each: ellipsoids and tapered capsules, each bound to one joint) and the
  question mark (a bent tube and a dot on palette slot 71, which the rig never animates). They use the runtime
  vertex format and the cloth program, with a 1x1 tint texture and a parameter block per opacity and glow level,
  uploaded through the renderer's GPU cache; no shader changed. A figure is one draw (two when FAILED) with one cb
  block, in the opaque pass when whole and in the transparent pass while it fades in or out.
- **Posing.** On the owner thread after the avatars pose, in parallel on the workers from 8 figures up: the idle
  or the clip on the avatar's specialised rig (weight and height in steps of 1/8: at most 17 x 17 x 2 rigs, made
  once each and shared), at the crowd's rates: every frame when FULL, when the clip or the idle (at a rate of 15 frames a second) reaches
  a new frame when SAMPLED, and at most every 4th frame when QUARTER. A sprite is not posed.
- **Sprites.** With LOD on, a figure smaller than `sprite_px` becomes a sprite of its look (the shape, the gender,
  loading or failed), baked once per renderer into the crowd's atlas pages. When the avatar is READY its look
  changes: it starts over as a mesh (the crossfade runs there) and fades into its own sprite once that is baked.
- **Snapshots.** `sona_snapshot` draws loading and failed figures but leaves the READY crossfade out, so a still
  picture (and `sona_headless_*`, the 2D outputs and item previews, which wait for READY) shows the avatar alone.

## Cost

Measured with the sample's `--measure` mode, `placeholder_lobby_vk --measure 1000`: 1,000 players in a 40-wide grid, LOD
on, every item read through a throttled source. It ran on an RTX 4090 machine, three runs, medians:

| `placeholder_lobby_vk --measure 1000` | `sona_update`, ms a frame | `sona_draw` with the GPU's wait, ms a frame |
|---|---|---|
| While a tenth or fewer of the players are in | 0.61 | 0.6 |
| Once every player is in | 0.09 | 0.19 |

At most 968 figures were in view at once. With placeholders off every hook returns at
its first test (the ctx has no placeholder state), so the hot path is the one it was.

## Limits

- The figure is one design in three body shapes; it does not take the avatar's colours or props (a prop appears
  at READY, under the crossfade).
- Parts of an outfit far outside the figure (wings, a large prop) appear at READY in the figure's tint and fade to
  their colours; they are not covered by the figure itself.
- Waiting avatars take a manifest once; changing a READY avatar's outfit is not a placeholder matter.
- The built-in idle assumes the rig's T-pose bind (arms out along X, facing -Z), which is the avatar rig's.
