# Templates and starters

A template is one program you copy and change, short enough to read in a sitting. The sources are in
[360sona-templates.tar.gz](../360sona-templates.tar.gz) ([getting it](../install.md#starters-and-templates)); unpack it into your SDK download's folder, so `starters/`
and `templates/` sit beside `include/`, `lib/` and `bindings/`. Each one builds with a single compiler command,
given in its tutorial.

- [Plaza starter](../tutorials/starter-plaza.md): The same bare scene everywhere: avatars walking about a plaza. Source: `starters/`, one folder per platform. Links: the core.
- [Plaza demo](../tutorials/plaza-demo.md): A park of avatars you pick, edit, create and save. Source: `templates/plaza-demo`. Links: the core, `sona_editor`, SDL3.
- [Lobby](../tutorials/template-lobby.md): Sign in with 360sona, gamertag lookup, network sync, emotes with root motion, gamepad. Source: `templates/lobby`. Links: the core; SDL3 and libcurl when wanted.
- [VTuber kit](../tutorials/template-vtuber.md): Webcam and mic drive the face; a transparent window for OBS. Source: `templates/vtuber`. Links: the core, `sona_track`.
- [Bot and server](../tutorials/template-bot.md): Gamerpics and outfit cards over HTTP, for bots and sites. Source: `templates/bot`. Links: the core, `stb_truetype.h`.
- [VR](../tutorials/template-vr.md): OpenXR, a first-person body, a mirror, other players. Source: `templates/xr`. Links: the core, `sona_xr`, OpenXR, Vulkan.
- [Web crowd](../tutorials/template-web-crowd.md): 1,000 avatars in one canvas, every one different. Source: `examples/crowd.html` in the web download. Links: the web SDK.

## The plaza

The SDK draws avatars; the world is always the host's. Every template and starter stands its avatars in the same
small place, `templates/place`: a paved square with a brick ring round a fountain, low stone walls, planters,
lamp posts and trees, under a sky with clouds and a sun. It is ray-traced, so it needs no meshes or textures:
`place.h` on the CPU (the starters, the lobby, the VTuber backdrop and the VR pictures), `place.glsl` on the GPU (the
VR eye) and `starters/python/place.py` with numpy. Change the tables at the top of `place.h` to change the place.

- `place_paint(view, rgb, w, h, samples, mirror, glass)` paints it as a `sona_view` sees it.
- `place_look(renderer, turned)` gives the avatars its light: the sun as the key light (the same direction the place
  shades with), a sky fill, the promo style, and their soft shadows on the ground (`SONA_LIGHTING_GROUND`).
- `place_under(rgba, rgb, w, h)` lays avatars drawn on nothing (`sona_snapshot` with a clear of zero) over it.

The middle of the square is open, so avatars there are never behind anything and the avatars-over-place composite
is exact. A real game draws its own world with its own depth instead.

## 360sona branding

Every picture the library draws carries the "Powered by 360sona" badge (the tab style), built into the library at
one, two and three times its size; a snapshot gets the badge at its own scale. The templates and starters show
360sona's square logo on their loading screen and use it as their window or app icon: `templates/brand/brand.h`
(`brand_logo`, `brand_loading`) for C and C++, `brand.py`, `Brand.cs` and `brand.rs` for the language starters,
and the logo file for the web, Godot, Unity and Android.

## Lobby

```text
sona_lobby --dir "<asset folder>" --sign-in --serve 7777            # host, signed in with your own saved avatar
sona_lobby --dir "<asset folder>" --gamertag "Your Gamertag" --join 10.0.0.20:7777
sona_lobby --dir "<asset folder>" --avatar me.b64                    # alone, with an avatar of your own from a file
```

- **Sign in with 360sona** (`templates/lobby/signin.h`): OAuth 2.0 authorization code with PKCE over a loopback
  redirect. The game listens on `127.0.0.1` at a free port, opens the sign-in page in the player's browser, checks
  the state when the browser comes back, and swaps the code and its verifier at `/sona/v1/token`. Register
  `http://127.0.0.1/callback` (no port) as your app's return address. Then `GET /sona/v1/slots` with the token gives
  the player's saved avatars.
- **Gamertags**: a player who joins by gamertag sends only the tag (`sona_net_sender_look`); everyone looks it up
  with `sona_players`, cached.
- **Sync**: each player's avatar goes to every other with `sona_net` over UDP, the host relaying; remote players are
  shown three ticks behind, so a late packet is still in time.
- **Moving**: the stick walks (the walk clip steps in place, the game moves the walker at its pace, foot locking keeps
  the feet still); A runs (`run_off`, sideways, about 12 m; the room's edges stop it), and the run moves the avatar
  by its own root motion; B, X and Y cheer, wave and clap.
- **The room** is the plaza, seen from eye height at its open side. The window draws the avatars into a texture with
  `sona_draw` and a small GL pass lays them over the plaza, painted once for the window's size.

`--headless --script "walk 1 0 2; run; wave" --frames 720 --out <folder>` plays the pad and draws on the headless
device, a picture every two seconds; the script and `--frames` start when your avatar stands in the plaza, so a slow
load does not eat the run.

## VTuber kit

```text
python3 templates/vtuber/mediapipe_track.py --webcam 0 --model face_landmarker.task --out - |
  sona_vtuber --dir "<asset folder>" --avatar me.b64 --face - --mic
```

The face (head turns, brows, eyes, blinks, moods) comes from `sona_track`; while you speak, `sona_lipsync` shapes the
mouth from the microphone. The window is transparent around the avatar: in OBS add a Window Capture of "360sona
VTuber" with transparency kept. `--headless --out <folder>` writes PNGs with alpha instead; with `--backdrop` it also
writes each one with the plaza behind you, softly out of focus, for a stream with no scene of its own.

## Bot and server

```text
sona_bot --dir "<asset folder>" --catalogue sona-catalogue.bin --port 8360
curl http://127.0.0.1:8360/card/Some%20Gamertag.png > card.png
```

`GET /gamerpic/<gamertag>.png` (256 x 256, transparent), `GET /card/<gamertag>.png` (720 x 420: the avatar, the
gamertag and what it wears) and `?avatar=<base64>` to draw a manifest you already have. One warm headless device
answers one request at a time; run more processes behind your bot's queue for more. It listens on `127.0.0.1` only.

## VR and XR

```text
XR_RUNTIME_JSON=<runtime>.json sona_vr --dir "<asset folder>" --cast starters/showcase-cast.json --member 46 --serve 7777
sona_vr --bot 33 --join 127.0.0.1:7777 --cast starters/showcase-cast.json --spot 0
```

You stand in the plaza, turned to face the fountain, with a standing mirror in front of you. Other players join over
UDP (`--serve`): each sends its avatar once and then its head and hands every frame, and `sona_xr` gives each a whole
body. `--bot` is such a player with no headset, for tests. Each eye: sona draws the avatars upright with a symmetric
field of view just wide enough to hold the eye, and, when the mirror is in view, the mirrored eye's avatars; one
full-screen pass (`shaders/composite.frag`, which includes `place.glsl`) follows the eye's own rays (its pose with
roll, its field of view as the runtime gives it, each side its own angle), ray-traces the plaza and the mirror's
glass, finds each ray's pixel in the avatar images, then lays the badge in the corner. While your avatar loads the
pass shows 360sona's logo. `shaders/spv.py` remakes `composite_spv.h` after a shader change. `--out` writes `eye.png`
(the pass itself, read back from the GPU), `eye-cpu.png` (the same worked out on the CPU with `place.h`),
`loading.png` and `mirror.png`, 1600 x 900 by default.

## Plaza starters

The starter on every platform is the same scene and nothing more: twelve avatars in costumes walking
about the plaza, the near ones a few steps from a camera at eye height, most of them coming towards you. A loading
screen with 360sona's logo shows while they load. [The plaza starter tutorial](../tutorials/starter-plaza.md) has
the commands for C, C++, C#, Rust, Python, the web, Unity, Godot, Unreal, Android and iOS.

The headless ones (C, C++, C#, Rust, Python) take `--out plaza.png`, `--loading loading.png` and
`--layer avatars.png` (the avatars alone, on nothing).
