# Xbox 360 avatars in three.js, Babylon.js and React

To put Xbox 360 avatars in a three.js scene, install `@360sona/three`, put the web SDK's `sona-renderer.js` beside
your code and load each avatar with `SonaAvatar.load`. Babylon.js has `@360sona/babylon` and React has
`@360sona/react`, and all three work with the same web SDK download.

![The three.js wrapper: an avatar waving in a three.js scene](../img/web-three.webp)

## Steps

1. Get a free app id that lists your page's origin, and download the web SDK ([getting it](../web-api.md#sdk-download)).
2. Install the package with its engine.
   The three packages are not published on npm; the web SDK draws avatars into your own canvas by itself
   ([Xbox 360 avatars on a web page](xbox-360-avatars-on-the-web.md)).
3. Put `sona-renderer.js` beside your code, or name it `sona-renderer` in the page's import map. The SDK is not on
   npm, because each download carries its app id.
4. Connect with `SonaRenderer.connect`, load the avatar with `SonaAvatar.load`, add it to the scene and call
   `update(dt)` every frame.

```bash
npm install @360sona/three three                 # three.js 0.150 or later
npm install @360sona/babylon @babylonjs/core
npm install @360sona/react                       # React 17, 18 or 19
```

## three.js

The package's sample page, whole: it connects, takes the first sample avatar, loads it with two clips and draws it in
a three.js scene; the button waves.

```js
import * as THREE from 'three';
import { SonaRenderer } from 'sona-renderer';
import { SonaAvatar, credit } from '@360sona/three';

// ?app=your-app-id&host=... ; the app id must list this page's origin.
const q = new URLSearchParams(location.search);
const sona = await SonaRenderer.connect({ appId: q.get('app') || 'your-app-id', host: q.get('host') || undefined });
const [first] = await sona.samples();

const canvas = document.getElementById('view');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.outputColorSpace = THREE.SRGBColorSpace;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdde3ea);
const camera = new THREE.PerspectiveCamera(30, canvas.width / canvas.height, 0.1, 100);
camera.position.set(0, 1.0, 4.2);
camera.lookAt(0, 0.85, 0);

const avatar = await SonaAvatar.load(sona, first.manifest, { clips: ['Stand0', 'GenericWave'] });
scene.add(avatar);
avatar.play('Stand0');
credit(sona, renderer);

const button = document.getElementById('wave');
button.disabled = false;
button.onclick = () => avatar.play('GenericWave', { loop: false }).then(() => avatar.play('Stand0'));

const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
  avatar.update(clock.getDelta());
  renderer.render(scene, camera);
});
```

The avatar is a `THREE.Group` with skinned meshes and an `AnimationMixer`, drawn by three.js's own renderer, so it
shares the scene's depth, shadows and post-processing. `first.manifest` is a sample avatar; in a game it is the
player's own (from sign-in, a gamertag or your save).

## Babylon.js and React

In Babylon.js, `SonaAvatar` is a `TransformNode` with one `AnimationGroup` per clip, and there is no `update(dt)`. In
React, `<SonaCanvas>` makes one SDK scene and each `<SonaAvatar>` inside it is an avatar in that scene;
`<SonaAvatarEditor>` opens the avatar editor and hands back the new manifest.

## More

- [three.js, Babylon.js and React](../guides/web-wrappers.md): the Babylon sample, the React components and
  how to run each sample.
- [The web tutorial](../tutorials/web.md): one live avatar with no key, then many in the SDK's own canvas.
- Credit: `credit(sona, renderer)` puts a small "Powered by 360sona" badge on the canvas; in React the SDK's renderer
  shows it itself.
- 360sona is not affiliated with or endorsed by Microsoft.
