# Spatial audio 3D

> The ears are in the perspective camera and face where it looks: a sound moving round it is heard all the way round.

Example `spatial-audio-3d`, running at https://nacatamalon.com/en/examples/spatial-audio-3d. The whole catalogue: https://nacatamalon.com/llms/examples.md.

The ears are in the **perspective camera** and face where it looks, so a moving sound is heard **all the way round** and fades with distance. The birds ride a sphere carried by a turning arm, and the sound is heard where the sphere really is. **Click** the canvas to turn the sound on (and fire a coin).

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { Scene } from './scenes/demo';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#07070d'
});

// Declare Scenes
game({
    Scene
});
```

`components/arm.ts`:

```ts
import { useSpawn, useTransform, useUpdate } from 'nacatamalon';

import { Bird } from './bird';

export const Arm = () => {
    const arm = useTransform();

    useSpawn(Bird)();
    useUpdate((delta) => {
        arm.rotationY += delta;
    });
};
```

`components/bird.ts`:

```ts
import { createMesh, useIcoSphereGeometry, useSound, useTransform } from 'nacatamalon';

export const Bird = () => {
    useTransform({
        x: 2.2
    });
    createMesh({
        geometry: useIcoSphereGeometry({
            subdivisions: 2
        }),
        tint: '#f0a848',
        transform: {
            scaleX: 0.4,
            scaleY: 0.4,
            scaleZ: 0.4
        }
    });
    // Distances fit the orbit, so the swell in front and the fade behind are easy to hear.
    useSound({
        src: '/example-assets/audio/birds.mp3'
    }, {
        loop: true,
        volume: 0.6,
        spatial: true,
        refDistance: 2,
        maxDistance: 8,
        autoplay: true
    });
};
```

`scenes/demo.ts`:

```ts
import { createMesh, createScene, useCamera3d, useIcoSphereGeometry, useDirectionalLight, usePointer, useSound, useSpawn } from 'nacatamalon';

import { Arm } from '../components/arm';

export const Scene = () => {
    // Camera at z = 5 looking down -Z: this is where the game is heard from.
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        near: 0.1,
        far: 100,
        transform: {
            z: 5
        }
    });
    useDirectionalLight({
        color: '#ffd9a0',
        intensity: 1.2,
        ambient: 0.2
    });

    // A dim marker at the middle of the orbit.
    createMesh({
        geometry: useIcoSphereGeometry({
            subdivisions: 2
        }),
        tint: '#33384a',
        transform: {
            scaleX: 0.35,
            scaleY: 0.35,
            scaleZ: 0.35
        }
    });
    useSpawn(Arm)();

    // A plain effect, the same wherever it is.
    const coin = useSound({
        src: '/example-assets/audio/coin.mp3'
    }, {
        volume: 0.7
    });

    usePointer().onDown(() => coin.play());

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
```
