# Fog

> useFog: models fade into a colour as they get further from the camera, the Nintendo 64's signature and the classic way to hide where the world ends.

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

The further a thing is from the camera, the more it fades into one colour. It is what the Nintendo 64 is remembered by, and the way every game of the era hid how near it stopped drawing.

`useFog` belongs to the scene, like its camera, and covers every model in it, one with a material of its own included. The trick is the colour: the same as the background, so the end of the avenue melts into the sky instead of stopping at a wall. It is worked out per corner and smeared across each triangle, like the light, as that hardware did.

Space switches it on and off; up and down move where it is complete. Everything `useFog` returns is read every frame, so a change shows on the next one.

## Code

`index.ts`:

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

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    keep: 'height',
    fullscreenScaling: 'contain',
    background: Settings.SKY,
});

// Declare Scenes
game({
    Avenue,
});
```

`components/floor.ts`:

```ts
import * as Settings from '../settings';
import { createMaterial, createMesh, usePlaneGeometry } from 'nacatamalon';

/** A checkerboard down the middle of the avenue, as long as the pillars run. */
export const Floor = () => {
    const tiles = [
        createMaterial({
            shader: 'mesh3d',
            tint: '#3b3350',
        }),
        createMaterial({
            shader: 'mesh3d',
            tint: '#6b5f8a',
        }),
    ];

    const tile = usePlaneGeometry();

    // Two tiles deep per row of pillars and four across, alternating like a board.
    for (let row = 0; row < Settings.ROWS * 2; row++) {
        for (let column = 0; column < 4; column++) {
            createMesh({
                geometry: tile,
                material: tiles[(row + column) % 2],
                transform: {
                    x: column * 4 - 6,
                    z: -row * (Settings.ROW / 2) + Settings.ROW / 4,
                    scaleX: 4,
                    scaleZ: Settings.ROW / 2,
                },
            });
        }
    }
};
```

`components/hud.ts`:

```ts
import { createText, useKeyboard, useLoadBitmapFont, useUpdate, type TFog } from 'nacatamalon';
import { clamp } from 'nacatamalon/math';

/**
 * The two lines of text, and the keys that drive the fog: space switches it on and off, up and down
 * move where it is complete. Every field of the fog is read every frame, so writing one is enough.
 */
export const Hud = (fog: TFog) => {
    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
    });

    const label = createText({
        text: '',
        font,
        tint: '#ede6f5',
        transform: {
            x: 12,
            y: 12,
        },
    });

    createText({
        text: 'SPACE ON OFF   UP DOWN DISTANCE',
        font,
        tint: '#8c82a6',
        transform: {
            x: 12,
            y: 26,
        },
    });

    const keys = useKeyboard();

    useUpdate((delta) => {
        if (keys.justPressed(' ')) {
            fog.enabled = !fog.enabled;
        }

        if (keys.isDown('ArrowUp')) {
            fog.far = clamp(fog.far + delta * 40, fog.near + 5, 300);
        }

        if (keys.isDown('ArrowDown')) {
            fog.far = clamp(fog.far - delta * 40, fog.near + 5, 300);
        }

        label.text = fog.enabled ? 'FOG UNTIL ' + Math.round(fog.far) : 'NO FOG';
    });
};
```

`components/pillars.ts`:

```ts
import * as Settings from '../settings';
import { createMaterial, createMesh, useCubeGeometry, useCylinderGeometry, type TMeshMaterial } from 'nacatamalon';

/** What every pillar is made of, shared: one stone and one glow for the whole avenue. */
type TPillarLook = { stone: TMeshMaterial; glow: TMeshMaterial; };

/** One pillar with a lantern on top, standing at (x, z). */
export const Pillar = (x: number, z: number, look: TPillarLook) => {
    createMesh({
        geometry: useCylinderGeometry(),
        material: look.stone,
        transform: {
            x,
            y: 4,
            z,
            scaleX: 1.6,
            scaleY: 8,
            scaleZ: 1.6,
        },
    });
    createMesh({
        geometry: useCubeGeometry(),
        material: look.glow,
        transform: {
            x,
            y: 8.6,
            z,
            scaleX: 0.9,
            scaleY: 0.9,
            scaleZ: 0.9,
        },
    });
};

/**
 * Two rows of pillars running away from the camera. The materials are made once here and handed to
 * every pillar, so a hundred pillars are one stone and one glow, not a hundred of each.
 */
export const Colonnade = () => {
    const look: TPillarLook = {
        stone: createMaterial({
            shader: 'mesh3d',
            tint: '#c9b98f',
        }),
        // The glow is its own light: it needs no lamp to be seen, and it still fades into the fog.
        glow: createMaterial({
            shader: 'mesh3d',
            tint: '#ffcf7a',
            emissive: '#ff9d3a',
        }),
    };

    for (let row = 0; row < Settings.ROWS; row++) {
        Pillar(-7, -row * Settings.ROW, look);
        Pillar(7, -row * Settings.ROW, look);
    }
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useFog, useDirectionalLight, useUpdate, useSpawn } from 'nacatamalon';

import * as Settings from '../settings';
import { Colonnade } from '../components/pillars';
import { Floor } from '../components/floor';
import { Hud } from '../components/hud';

export const Avenue = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 60,
        transform: {
            y: 4
        },
        near: 0.1,
        far: 400,
    });

    useDirectionalLight({
        color: '#fff1dc',
        intensity: 1,
        ambient: 0.45,
        transform: {
            rotationX: -0.7,
            rotationY: 0.4
        },
    });

    const fog = useFog({
        color: Settings.SKY,
        near: 6,
        far: 55,
    });

    useSpawn(Colonnade)();
    useSpawn(Floor)();
    useSpawn(Hud)(fog);

    // Walking forever: the camera steps back one row whenever it has gone one forward.
    useUpdate((delta) => {
        camera.transform.z -= delta * 6;

        if (camera.transform.z < -Settings.ROW) {
            camera.transform.z += Settings.ROW;
        }
    });

    return createScene();
};

// The background is the fog's own colour, which is the whole trick. In full screen it fills the
// monitor: keep: 'height' pins the rows and lets a wide screen see more to the sides.
```

`settings.ts`:

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

export const WIDTH = 480;
export const HEIGHT = 320;
// Fog: the further a thing is from the camera, the more it fades into one colour. It is what the
// Nintendo 64 is remembered by, and the way every game of the era hid how near it stopped drawing.
//
// useFog belongs to the scene, like its camera, and covers every model in it. The trick is the
// colour: the same as the background, so the end of the avenue melts into the sky instead of
// stopping at a wall. It is worked out per corner and smeared, like the light, as that hardware did.
//
// The scene only says what is in it. Each piece is built in its own file: the pillars, the floor,
// and the text with the keys that drive the fog.

export const SKY = getColor('#2a2238');
/** How far apart one pair of pillars is from the next, and how many pairs there are. */
export const ROW = 14;
export const ROWS = 14;
```
