# Two cameras

> The 2D and 3D layers look through different cameras: WASD moves one, the arrows the other, and neither disturbs the other layer.

Example `two-cameras`, running at https://nacatamalon.com/en/examples/two-cameras. The whole catalogue: https://nacatamalon.com/llms/examples.md.

The 2D and 3D layers look through **different cameras**, and neither can reach the other layer. WASD moves the 3D camera and only the cubes shift; the arrows move the 2D camera, in pixels, and only the markers shift.

The readout lives in an object that calls `useScreenSpace()`, so it ignores the 2D camera as well and never moves at all. A scene that declares no `useCamera2d` draws all its 2D that way, which is what a HUD over a 3D world wants.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT } from './settings';
import { Scene } from './scenes/demo';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#0e1118'
});

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

`components/hud.ts`:

```ts
import { createSprite, createText, getColor, useScreenSpace } from 'nacatamalon';
import * as Settings from '../settings';
import type { TBitmapFont } from 'nacatamalon';

export const Hud = ({ font }: { font: TBitmapFont }) => {
    useScreenSpace();

    const panel = getColor('#0b0f18');

    createSprite({
        zIndex: 100,
        tint: {
            ...panel,
            a: 0.85
        },
        width: Settings.WIDTH,
        height: 44,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 0,
            y: 0
        }
    });

    const readout3d = createText({
        text: 'WASD   CAM3D',
        font,
        style: {
            fontSize: 8
        },
        tint: '#5bd6a0',
        zIndex: 101,
        transform: {
            x: 12,
            y: 8
        }
    });

    const readout2d = createText({
        text: 'ARROWS CAM2D',
        font,
        style: {
            fontSize: 8
        },
        tint: '#ffb84f',
        zIndex: 101,
        transform: {
            x: 12,
            y: 24
        }
    });

    return {
        readout3d,
        readout2d
    };
};
```

`components/world2d.ts`:

```ts
import { createSprite, getColor } from 'nacatamalon';

export const World2D = () => {
    // Opacity is the tint's own fourth channel: a sprite has no separate alpha.
    const marker = getColor('#ffb84f');

    for (let gy = 0; gy < 8; gy++) {
        for (let gx = 0; gx < 12; gx++) {
            createSprite({
                zIndex: 5,
                tint: {
                    ...marker,
                    a: 0.5
                },
                width: 10,
                height: 10,
                anchor: {
                    x: 0,
                    y: 0
                },
                transform: {
                    x: gx * 80 + 20,
                    y: gy * 80 + 20
                },
            });
        }
    }
};
```

`components/world3d.ts`:

```ts
import { createMesh, getColor, useCubeGeometry } from 'nacatamalon';

export const World3D = () => {
    const cube = useCubeGeometry();

    for (let x = -2; x <= 2; x++) {
        for (let z = -2; z <= 2; z++) {
            createMesh({
                geometry: cube,
                zIndex: 0,
                tint: getColor((x + z) % 2 === 0 ? '#5bd6a0' : '#3f9d78'),
                transform: {
                    x: 6 + x * 1.6,
                    y: 0,
                    z: 6 + z * 1.6,
                    scaleX: 0.6,
                    scaleY: 0.6,
                    scaleZ: 0.6
                },
            });
        }
    }
};
```

`scenes/demo.ts`:

```ts
import { createScene, useLoadBitmapFont, useCamera3d, useCamera2d, useDirectionalLight, useKeyboard, useSpawn, useUpdate } from 'nacatamalon';
import * as Settings from '../settings';
import { clamp } from 'nacatamalon/math';
import { World3D } from '../components/world3d';
import { World2D } from '../components/world2d';
import { Hud } from '../components/hud';

export const Scene = () => {
    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
    });

    // One camera per layer. Neither can reach the other layer.
    const cam3d = useCamera3d({
        projection: 'perspective',
        fov: 55,
        near: 0.1,
        far: 100,
        transform: {
            x: 6,
            y: 4,
            z: 14,
            rotationX: -0.42
        }
    });

    const cam2d = useCamera2d();

    // A lamp is aimed, not given a direction vector: it shines along its own -Z, so the
    // old { x: -0.4, y: -0.7, z: -0.5 } is this pair of rotations.
    useDirectionalLight({
        color: '#ffffff',
        intensity: 1.1,
        ambient: 0.35,
        transform: {
            rotationX: -0.83,
            rotationY: 0.675
        }
    });

    useSpawn(World3D)();
    useSpawn(World2D)();

    const { readout3d, readout2d } = useSpawn(Hud)({
        font
    }).api;

    const keys = useKeyboard();
    const axis = (neg: string, pos: string) => (keys.isDown(pos) ? 1 : 0) - (keys.isDown(neg) ? 1 : 0);

    useUpdate((dt) => {
        // WASD drives the 3D camera, in WORLD UNITS.
        cam3d.transform.x = clamp(cam3d.transform.x + axis('a', 'd') * dt * Settings.SPEED.world, 0, 14);
        cam3d.transform.z = clamp(cam3d.transform.z + axis('w', 's') * dt * Settings.SPEED.world, 4, 22);

        // Arrows drive the 2D camera, in PIXELS. Its x/y is the world point that lands
        // on the viewport's TOP-LEFT corner, not its centre.
        cam2d.transform.x = clamp(cam2d.transform.x + axis('ArrowLeft', 'ArrowRight') * dt * Settings.SPEED.pixels, 0, 500);
        cam2d.transform.y = clamp(cam2d.transform.y + axis('ArrowUp', 'ArrowDown') * dt * Settings.SPEED.pixels, 0, 340);

        // Live proof that the two numbers move independently — and, because this text sits in
        // the screen-space box, that a HUD can change every frame without drifting a pixel.
        // A text is a record: you change it by changing its field.
        readout3d.text = 'WASD   CAM3D ' + Math.round(cam3d.transform.x) + ' ' + Math.round(cam3d.transform.z);
        readout2d.text = 'ARROWS CAM2D ' + Math.round(cam2d.transform.x) + ' ' + Math.round(cam2d.transform.y);
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const SPEED = {
    world: 5,
    pixels: 240
};
```
