# 1997

> Every PlayStation trait at once: shivering vertices, affine textures, fog and dithering. Switch them off to see the same scene today.

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

A game that looks like 1997, with every trait of that look switched on at once:

- **Shiver** (`vertexSnap`): corners land on a coarse grid of the screen and models shiver.
- **Affine** (`affine`): pictures are stretched across the screen without depth and floors swim.
- **Fog** (`useFog`): everything fades into the dusk with distance, which is how the end of the world was hidden.
- **Dither** (`dither` with `POST_EFFECTS.COLOR_LEVELS.ps1`): 32 levels a channel with an ordered pattern over them, the PlayStation's colour.

Press 1 to 4 to switch each one off, and space to switch them all and see the same scene as a modern card draws it. None of them costs anything to switch: each is a field read every frame.

## Code

`index.ts`:

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

import * as Settings from './settings';
import { Courtyard } from './scenes/demo';

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

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

`components/hud.ts`:

```ts
import { createText, useKeyboard, useLoadBitmapFont, useUpdate } from 'nacatamalon';
import type { TSwitch } from '../hooks/switches';

/**
 * What is on, and the keys: one to four switch one trait each, space switches all four together.
 */
export const Hud = (switches: TSwitch[]) => {
    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
    });

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

    const lines = [0, 1].map((i) => createText({
        text: '',
        font,
        tint: '#b8aed0',
        transform: {
            x: 10,
            y: 24 + i * 11,
        },
    }));

    createText({
        text: '1 2 3 4  SPACE ALL',
        font,
        tint: '#8c82a6',
        transform: {
            x: 10,
            y: 222,
        },
    });

    const flip = (s: TSwitch, on: boolean) => {
        s.on = on;
        s.set(on);
    };

    const keys = useKeyboard();

    useUpdate(() => {
        switches.forEach((s, i) => {
            if (keys.justPressed(String(i + 1))) {
                flip(s, !s.on);
            }
        });

        if (keys.justPressed(' ')) {
            const on = !switches.every((s) => s.on);

            switches.forEach((s) => flip(s, on));
        }

        const all = switches.every((s) => s.on);
        const none = switches.every((s) => !s.on);

        title.text = all ? '1997' : none ? 'TODAY' : 'MIXED';

        const state = (s: TSwitch, i: number) => i + 1 + ' ' + s.name + (s.on ? ' ON' : ' OFF');

        lines[0].text = state(switches[0], 0) + '  ' + state(switches[1], 1);
        lines[1].text = state(switches[2], 2) + '  ' + state(switches[3], 3);
    });
};
```

`components/ruins.ts`:

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

/**
 * The courtyard's floor: four by four whole tiles, two triangles each, with nothing cut finer, the
 * way a 1997 floor was built and why it swam. The camera goes round outside it: a tile under the
 * camera would be cut at the eye and smeared beyond what the console ever showed.
 */
const Ground = (material: TMeshMaterial) => {
    const tile = usePlaneGeometry({
        width: Settings.TILE,
        depth: Settings.TILE,
    });

    for (let row = 0; row < 4; row++) {
        for (let column = 0; column < 4; column++) {
            createMesh({
                geometry: tile,
                material,
                transform: {
                    x: (column - 1.5) * Settings.TILE,
                    z: (row - 1.5) * Settings.TILE,
                },
            });
        }
    }
};

/**
 * One pillar: eight sides, as few as the era could afford, with a block on top.
 */
const Pillar = (x: number, z: number, stone: TMeshMaterial) => {
    createMesh({
        geometry: useCylinderGeometry({
            radius: 0.5,
            height: 4,
            segments: 8,
        }),
        material: stone,
        transform: {
            x,
            y: 2,
            z,
        },
    });
    createMesh({
        geometry: useCubeGeometry({
            width: 1.4,
            height: 0.5,
            depth: 1.4,
        }),
        material: stone,
        transform: {
            x,
            y: 4.25,
            z,
        },
    });
};

/**
 * The ruins: a ring of pillars around the middle of the floor and a few fallen blocks. Every surface
 * shares the same two materials, so switching an effect is writing it on two materials.
 *
 * Returns those materials, for whoever switches the effects.
 */
export const Ruins = (texture: TTexture): TMeshMaterial[] => {
    const look = {
        texture,
        vertexSnap: Settings.SNAP,
        affine: true,
    };

    const floor = createMaterial({
        shader: 'mesh3d',
        ...look,
    });

    const stone = createMaterial({
        shader: 'mesh3d',
        ...look,
        tint: '#d8c8b8',
    });

    Ground(floor);

    const pillars = 8;

    for (let i = 0; i < pillars; i++) {
        const angle = (i / pillars) * Math.PI * 2;

        Pillar(Math.cos(angle) * 5, Math.sin(angle) * 5, stone);
    }

    const block = useCubeGeometry({
        width: 1.2,
        height: 1.2,
        depth: 1.2,
    });

    for (const [x, z, turn] of [[2.5, 6.6, 0.4], [-6.6, -1.8, 1.1], [5.4, -5.2, 2.2]]) {
        createMesh({
            geometry: block,
            material: stone,
            transform: {
                x,
                y: 0.6,
                z,
                rotationY: turn,
            },
        });
    }

    return [floor, stone];
};
```

`hooks/switches.ts`:

```ts
import * as Settings from '../settings';
import { type TFog, type TMeshMaterial } from 'nacatamalon';
import { type TPostEffect } from 'nacatamalon/effects';

/**
 * One of the four traits, with what switching it does.
 */
export type TSwitch = { name: string; on: boolean; set: (on: boolean) => void; };

/**
 * The four traits of 1997, each one a switch. A material, the fog and a post effect are all read
 * every frame, so switching is just writing a field.
 *
 * The models are asked for as a function, not a list: a model from a file has no parts until the
 * file lands, so the list is taken at the moment of the switch, never before.
 */
export const Switches = (materials: () => TMeshMaterial[], fog: TFog, dither: TPostEffect): TSwitch[] => [
    {
        name: 'SNAP',
        on: true,
        set: (on) => materials().forEach((m) => (m.vertexSnap = on ? Settings.SNAP : false)),
    },
    {
        name: 'AFFINE',
        on: true,
        set: (on) => materials().forEach((m) => (m.affine = on)),
    },
    {
        name: 'FOG',
        on: true,
        set: (on) => (fog.enabled = on),
    },
    {
        name: 'DITHER',
        on: true,
        set: (on) => (dither.enabled = on),
    },
];
```

`scenes/demo.ts`:

```ts
import { cameraLookAt, createModel, createScene, useCamera3d, useFog, useDirectionalLight, useLoadModel, useLoadTexture, useSkeletalAnimation, useUpdate, useSpawn } from 'nacatamalon';
import { dither, usePostProcess, POST_EFFECTS } from 'nacatamalon/effects';

import * as Settings from '../settings';
import { Ruins } from '../components/ruins';
import { Switches } from '../hooks/switches';
import { Hud } from '../components/hud';

export const Courtyard = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 50,
        near: 0.1,
        far: 60,
    });

    useDirectionalLight({
        color: '#ffd9b0',
        intensity: 0.9,
        ambient: 0.45,
        transform: {
            rotationX: -0.7,
            rotationY: 0.8
        },
    });

    const fog = useFog({
        color: Settings.DUSK,
        near: 9,
        far: 26,
    });

    const ps1 = usePostProcess(dither({
        levels: POST_EFFECTS.COLOR_LEVELS.ps1,
    }));

    const ruins = useSpawn(Ruins)(useLoadTexture({
        src: '/example-assets/assets/3d/textures/tiles.png',
        key: 'tiles',
    })).api;

    const gltf = useLoadModel({
        src: '/example-assets/assets/3d/gltf/Fox/Fox.gltf',
    });

    const fox = createModel({
        model: gltf,
        vertexSnap: Settings.SNAP,
        affine: true,
        transform: {
            scaleX: 0.035,
            scaleY: 0.035,
            scaleZ: 0.035,
        },
    });

    useSkeletalAnimation(fox, {
        play: 'Walk',
    });

    const switches = useSpawn(Switches)(() => [...ruins, ...fox.parts.map((part) => part.material)], fog, ps1).api;

    useSpawn(Hud)(switches);

    useUpdate((_delta, time) => {
        // The fox walks a circle inside the pillars, facing the way it goes.
        const walk = time * 0.35;

        fox.transform.x = Math.cos(walk) * 2.3;
        fox.transform.z = Math.sin(walk) * 2.3;
        fox.transform.rotationY = -walk;

        // The camera goes slowly round the other way, outside the floor, looking at the middle.
        const orbit = -time * 0.12;

        camera.transform.x = Math.cos(orbit) * 12.5;
        camera.transform.z = Math.sin(orbit) * 12.5;
        camera.transform.y = 4;
        cameraLookAt(camera, {
            x: 0,
            y: 1,
            z: 0,
        });
    });

    return createScene();
};

// PlayStation resolution, filling the screen in full screen as vertex-snap does.
```

`settings.ts`:

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

export const WIDTH = 320;
export const HEIGHT = 240;
// A game that looks like 1997, with every trait the look is made of switched on at once:
//
//   SNAP     the corners land on a coarse grid of the screen, so models shiver (vertexSnap)
//   AFFINE   pictures are stretched across the screen without depth, so floors swim (affine)
//   FOG      everything fades into the dusk with distance, the way the far end was hidden (useFog)
//   DITHER   32 levels a channel and an ordered pattern over them, the PlayStation's colour (dither)
//
// Press 1 to 4 to switch one, and space to switch all four and see the same scene as a modern card
// draws it. None of them costs anything to switch: each is a field read every frame.

export const DUSK = getColor('#2a1a3a');
/**
 * How many rows the shiver's grid has: the classic PlayStation look at 240 lines, two pixels a cell.
 */
export const SNAP = 120;
/** How big one floor tile is. Big on purpose: the affine warp lives on big triangles. */
export const TILE = 4;
```
