# A HUD over the effects

> useOverlay() draws a HUD after the CGI style's bloom, so it does not glow, and { overlay: true } in the chain puts it under a CRT that comes after.

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

Press **1**, **2** and **3** to switch between the looks: the bloom; a CRT after it; and the HUD's place between the two.

The level is in the CGI style with its bloom turned up, and has a white panel of its own pinned to the screen: it goes through the effects with the world, so it glows and bends in the CRT. The HUD is a scene of its own, launched over the level, that calls `useOverlay()`: everything it draws is drawn after the screen effects, untouched by them, so the same white panel does not glow.

Sometimes part of the chain should still reach the HUD: a CRT should bend it as it bends the game. `{ overlay: true }` in a style's chain says where the HUD goes: the effects before it leave the HUD alone, and those after it are drawn over it too.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Hud } from './scenes/hud';
import { Level } from './scenes/level';
import { HEIGHT, WIDTH } from './settings';

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

// Declare Scenes
game({
    Level,
    Hud
});
```

`components/neon_pieces.ts`:

```ts
import { createMaterial, createMesh, neon, useCubeGeometry, useTorusGeometry, useUpdate } from 'nacatamalon';

const COLORS = ['#ff3cac', '#3cf0ff', '#ffe23c'];

// Three neon rings and a bar under them: neon glows its own colour, bright enough for the bloom to
// spread it round.
export const NeonPieces = () => {
    const ring = useTorusGeometry({
        radius: 0.7,
        tube: 0.08
    });

    const rings = COLORS.map((tint, i) => createMesh({
        geometry: ring,
        material: createMaterial(neon({
            tint
        })),
        transform: {
            x: (i - 1) * 2,
            y: 1.2
        }
    }));

    createMesh({
        geometry: useCubeGeometry({
            width: 6,
            height: 0.08,
            depth: 0.08
        }),
        material: createMaterial(neon({
            tint: '#ffffff'
        })),
        transform: {
            y: 0.2
        }
    });

    useUpdate((delta) => {
        rings.forEach((piece, i) => {
            piece.transform.rotationY += delta * (0.6 + i * 0.2);
            piece.transform.rotationX += delta * 0.3;
        });
    });
};
```

`components/panel.ts`:

```ts
import { createShapes, createText, useLoadBitmapFont, useScreenSpace } from 'nacatamalon';
import { FONT } from '../settings';

// A white frame and white letters on the screen, the brightest there can be: whatever goes through
// the bloom glows. The same panel is drawn by the level, under the effects, and by the HUD, over them.
export const Panel = (x: number, y: number, width: number, title: string, lines: number) => {
    useScreenSpace();

    const font = useLoadBitmapFont(FONT);

    createShapes({
        shapes: [
            {
                type: 'rect',
                x: 0,
                y: 0,
                width,
                height: 32 + lines * 16,
                radius: 4,
                fill: '#101018',
                stroke: {
                    width: 3,
                    color: '#ffffff',
                    align: 'inside'
                }
            }
        ],
        transform: {
            x,
            y
        }
    });

    createText({
        text: title,
        font,
        style: {
            fontSize: 16
        },
        tint: '#ffffff',
        transform: {
            x: x + 10,
            y: y + 10
        }
    });

    return Array.from({
        length: lines
    }, (_, i) => createText({
        text: '',
        font,
        tint: '#ffffff',
        transform: {
            x: x + 10,
            y: y + 34 + i * 16
        }
    }));
};
```

`scenes/hud.ts`:

```ts
import { createScene, useKeyboard, useOverlay, useSpawn, useUpdate } from 'nacatamalon';
import type { TRenderStyleHandle } from 'nacatamalon';
import { Panel } from '../components/panel';
import { HEIGHT, LOOKS } from '../settings';

// Everything this scene draws is drawn over the screen effects, untouched by them: the same white
// panel as the level's, and it does not glow. The level hands it its style (`look`) to change.
export const Hud = ({ look }: { look?: TRenderStyleHandle }) => {
    useOverlay();

    const [chain, about, keys] = useSpawn(Panel)(16, HEIGHT - 96, 420, 'OVER THE EFFECTS', 3).api;

    const keyboard = useKeyboard();
    let current = 0;

    const show = () => {
        chain!.text = 'POST: ' + LOOKS[current]!.name;
        about!.text = LOOKS[current]!.about;
        keys!.text = '1 2 3: THE LOOK';
    };

    show();

    // A style is changed by giving another: its effects leave the chain and the new ones come in.
    useUpdate(() => {
        LOOKS.forEach((_, i) => {
            if (keyboard.justPressed(String(i + 1)) && i !== current) {
                current = i;
                look?.set(LOOKS[i]!.style);
                show();
            }
        });
    });

    return createScene();
};
```

`scenes/level.ts`:

```ts
import { cameraLookAt, createScene, useCamera3d, useReady, useRenderStyle, useScene, useSpawn, useStudio } from 'nacatamalon';
import { NeonPieces } from '../components/neon_pieces';
import { Panel } from '../components/panel';
import { LOOKS } from '../settings';

export const Level = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 45,
        transform: {
            y: 1.4,
            z: 6.5
        }
    });

    cameraLookAt(camera, {
        x: 0,
        y: 0.9,
        z: 0
    });

    useStudio({
        top: '#1a1830',
        ground: '#07060d'
    });

    // The look of the whole game: the style of the scene at the top that has one.
    const look = useRenderStyle(LOOKS[0]!.style);

    useSpawn(NeonPieces)();

    // The level's own panel, pinned to the screen but not an overlay: it goes through the effects
    // with the world, so it glows in the bloom and bends in the CRT.
    useSpawn(Panel)(16, 16, 330, 'UNDER THE EFFECTS', 1).api[0]!.text = 'THE LEVEL, PINNED';

    // The HUD in a scene of its own, started once the level is up so it is drawn after it, over it.
    const scenes = useScene();

    useReady(() => scenes.launch('Hud', {
        props: {
            look
        }
    }));

    return createScene();
};
```

`settings.ts`:

```ts
import { RENDER } from 'nacatamalon';
import type { TRenderStyle } from 'nacatamalon';

// The CGI style's size: a render of the era, smooth.
export const WIDTH = 640;
export const HEIGHT = 480;

export const FONT = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};

// The CGI style, its bloom turned up so the glow is plain to see.
const BLOOM = {
    builtin: 'bloom',
    uniforms: {
        threshold: 0.6,
        intensity: 1.4,
        radius: 10
    }
};

const CGI: TRenderStyle = {
    ...RENDER.STYLES.cgi,
    post: [BLOOM]
};

// The three looks the keys 1, 2 and 3 switch between: the bloom; a CRT after it; and the HUD's place
// between the two.
export const LOOKS: ReadonlyArray<{ name: string; about: string; style: TRenderStyle }> = [
    {
        name: 'BLOOM',
        about: 'THE HUD IS DRAWN AFTER THE BLOOM',
        style: CGI
    },
    {
        name: 'BLOOM, CRT',
        about: 'AFTER EVERY EFFECT: FLAT OVER THE TUBE',
        style: {
            ...CGI,
            post: [
                ...CGI.post!,
                {
                    builtin: 'crt'
                }
            ]
        }
    },
    {
        name: 'BLOOM, { OVERLAY: TRUE }, CRT',
        about: 'AFTER THE BLOOM, UNDER THE CRT',
        style: {
            ...CGI,
            post: [
                ...CGI.post!,
                {
                    overlay: true
                },
                {
                    builtin: 'crt'
                }
            ]
        }
    }
];
```
