# 2D and 3D

> Both halves in one scene, sorted by the same zIndex: a sprite behind the model and a panel in front.

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

Both halves in one scene, in one list, sorted by the same `zIndex`: a sprite behind the model, a panel in front of it. On a tie the model goes under, because a HUD is meant to be seen.

This scene has no 2D camera, so its sprites are in screen pixels and pinned: the 3D camera can go anywhere and the panel will not move.

## Code

`index.ts`:

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

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

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

`components/hud.ts`:

```ts
import { createSprite, useScreenSpace, useUpdate } from 'nacatamalon';
import { WIDTH, HEIGHT, HUD } from '../settings';

export const Hud = () => {
    // It stays pinned even if a 2D camera is added to the scene later.
    useScreenSpace();

    createSprite({
        tint: '#0c1420',
        width: WIDTH,
        height: HUD.height,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            y: HEIGHT - HUD.height
        },
        zIndex: 10
    });
    createSprite({
        tint: '#243247',
        width: HUD.barWidth,
        height: HUD.barHeight,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: HUD.inset,
            y: HEIGHT - HUD.height + HUD.inset
        },
        zIndex: 11
    });

    const bar = createSprite({
        tint: '#ff6b6b',
        width: HUD.barWidth,
        height: HUD.barHeight,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: HUD.inset,
            y: HEIGHT - HUD.height + HUD.inset
        },
        zIndex: 12
    });

    let elapsed = 0;

    useUpdate((delta) => {
        elapsed += delta;
        bar.width = HUD.barWidth / 2 * (1 + Math.sin(elapsed));
    });
};
```

`components/rotating_cube.ts`:

```ts
import { createMesh, useCubeGeometry, useUpdate } from 'nacatamalon';
import { CUBE } from '../settings';

export const RotatingCube = () => {
    const cube = createMesh({
        geometry: useCubeGeometry(),
        tint: '#5bd6a0',
        transform: {
            scaleX: CUBE.size,
            scaleY: CUBE.size,
            scaleZ: CUBE.size
        }
    });

    let elapsed = 0;

    useUpdate((delta) => {
        elapsed += delta;
        cube.transform.rotationY += delta * CUBE.turnSpeed;
        cube.transform.rotationX = Math.sin(elapsed * CUBE.tiltSpeed) * CUBE.tilt;
    });
};
```

`scenes/gallery.ts`:

```ts
import { createScene, createSprite, useCamera3d, useDirectionalLight, useSpawn } from 'nacatamalon';
import { Hud } from '../components/hud';
import { RotatingCube } from '../components/rotating_cube';
import { WIDTH } from '../settings';

export const Gallery = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        transform: {
            z: 4
        }
    });
    useDirectionalLight({
        intensity: 1.1,
        ambient: 0.3
    });

    // No 2D camera: sprites use screen pixels while the cube uses the perspective camera.
    createSprite({
        tint: '#12203a',
        width: WIDTH,
        height: 200,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            y: 40
        },
        zIndex: -10
    });

    useSpawn(RotatingCube)();
    useSpawn(Hud)();

    return createScene();
};
```

`settings.ts`:

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

export const HUD = {
    height: 64,
    barWidth: 220,
    barHeight: 16,
    inset: 20
};

export const CUBE = {
    size: 1.3,
    turnSpeed: 0.8,
    tiltSpeed: 0.6,
    tilt: 0.3
};
```
