# Unreal Engine

The `Sona360` plugin draws Xbox 360 avatars from a 360sona manifest with Unreal's own renderer: one procedural mesh
per avatar mesh, skinned by the core, in the plugin's material. It needs Unreal Engine 5.8 or later, on
Win64, Linux, Mac, Android and iOS, and is built and run against 5.8 on Linux. Use it when your game is an Unreal
project; [the Unreal tutorial](../tutorials/unreal.md) goes through it step by step.

An avatar on an actor, from the tutorial's game mode:

```cpp
USonaAvatarComponent* Avatar = NewObject<USonaAvatarComponent>(A, TEXT("Avatar"));
Avatar->Manifest = Manifest;
Avatar->Clip = TEXT("idle");
A->SetRootComponent(Avatar);
A->AddInstanceComponent(Avatar);
Avatar->SetWorldTransform(T);
Avatar->RegisterComponent();
```

Credit: the plugin shows the "Powered by 360sona" badge in a corner of the game viewport by default.

## Install

1. Copy `Sona360` from the Unreal download into your project's `Plugins` folder and build the project.
2. Put `sona-licence.json` at `Content/Sona360/sona-licence.json` and your app id in `Config/DefaultGame.ini`.

The plugin names no `EngineVersion`, so a newer engine loads it without asking. Its materials, `M_SonaAvatar` and
`M_SonaAvatarTranslucent`, are in the plugin's `Content`, saved by Unreal Engine 5.8, which is why an older engine
cannot load it.

## What it has

| Part | Class | What it does |
|---|---|---|
| The component | `USonaAvatarComponent` | Manifest, clip, play, crossfade, queue, joints and overrides, expressions, clip events and marks, root motion, LOD, item previews, content filters |
| IK | on the component | The core's solver ([IK](ik.md)): `SetLimbTarget`, `ClearLimbTarget`, `SetFeetPlanting`, `SetLookAt`, `ClearLookAt`, `ClearIK`; `GetJointParent` and `GetJointCount` for the rig's tree |
| Blueprint nodes | every component and subsystem call | **Load 360sona Avatar** (`USonaLoadAvatarAction`) is a latent node that spawns an actor with one avatar and fires On Ready or On Failed |
| Control Rig IK | module `Sona360ControlRig` | The **360sona Control Rig** component keeps a Control Rig whose 71 bones follow the avatar each tick and whose five controls (`ik_hand_l`, `ik_hand_r`, `ik_foot_l`, `ik_foot_r`, `look`) are the avatar's IK targets |
| The player editor | `USonaEditorWidget` (UMG), `USonaEditorSession` | [The editor](editor.md) in Full, Quick or Wardrobe mode, with gamepad, keyboard, mouse and touch |

## Control Rig IK

Move a control from Blueprint, from Sequencer, or from a rig graph of your own (`ControlRigClass`), and the core
solves the limb or the head towards it inside the joint limits. A rig graph's construction event resets its
hierarchy to the rig's own; the component puts the avatar's bones back after it runs, so a graph can read them in
its Forwards Solve.

## The player editor

`USonaEditorWidget` runs the editor in a context of its own and draws it on the library's headless device
([headless](headless.md)) into a texture, item and look pictures included. On Done gives the manifest for
`SetManifest`. `GetControls` and `GetFocus` give what is on screen (for a screen reader or your own input), with
`GetScreen`, `SetScreen` and `GetAvatarRect`. The plugin carries the `sona_editor` library beside the core for
this.

## Things to know

- In an `.ini`, quote a base64 manifest: `//` starts a comment in an unquoted value.
- From Unreal Engine 5.7, `GetMaterialResource` takes a shader platform; the plugin handles both forms.
- On Win64, Mac, Android and iOS the plugin compiles against the engine's build tool; it is run on Linux.
