# Vertex snapping

> The PlayStation shiver: with vertexSnap, corners land on a coarse grid of the screen and models jump as they move.

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

The PlayStation shivered: every corner of a model landed on a coarse grid of the screen, so models jumped from step to step as they moved and the edges of a floor crawled as the camera turned. `vertexSnap` on a material (or on `createModel` and `createMesh`) is exactly that, and the number is how many rows the grid has.

The fox and board on the left are drawn as a modern card would; the ones on the right snap to an 80-row grid. Press space to switch it on and off live.

Why a grid coarser than the game's 240 rows? Because the picture is already drawn on those 240: snapping to them moves a corner less than half a pixel and nobody sees it (that is what `vertexSnap: true` does). The console did its maths with low-precision numbers and missed by more than a pixel, which is why a coarser grid is what reads as PlayStation. `120` is the classic look, `80` is easy to see and `60` takes the model apart.

In full screen it fills the whole monitor: `keep: 'height'` pins the 240 rows and lets the width follow the screen, so a wide monitor sees more to the sides instead of black bars.

## Code

`index.ts`:

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

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

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

`components/comparison.ts`:

```ts
import { useWatch, createMaterial, createMesh, createModel, createText, getColor, useGame, useKeyboard, useLoadBitmapFont, useLoadModel, usePlaneGeometry, useSkeletalAnimation, useUpdate } from 'nacatamalon';

import * as Settings from '../settings';

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

    const smooth = createModel({
        model: fox,
        transform: {
            x: -100,
        },
    });

    const snapped = createModel({
        model: fox,
        transform: {
            x: 100,
        },
        vertexSnap: Settings.SNAP,
    });

    // Each model has bones of its own, so each walks with an animator of its own.
    useSkeletalAnimation(smooth, {
        play: 'Walk',
    });
    useSkeletalAnimation(snapped, {
        play: 'Walk',
    });

    // A checkerboard under each: straight edges are where the crawl shows best, and a board is
    // nothing but straight edges. Each side has its own two materials, so the snap can be switched
    // for one side and not the other.
    const tile = usePlaneGeometry();
    const dark = getColor('#2a2340');
    const light = getColor('#5b4f86');

    const boardOf = (centre: number, vertexSnap: number | false) => {
        const materials = [dark, light].map((tint) => createMaterial({
            shader: 'mesh3d',
            tint,
            vertexSnap,
        }));

        for (let row = 0; row < 6; row++) {
            for (let column = 0; column < 6; column++) {
                createMesh({
                    geometry: tile,
                    material: materials[(row + column) % 2],
                    transform: {
                        x: centre + (column - 2.5) * 26,
                        z: (row - 2.5) * 26,
                        scaleX: 26,
                        scaleZ: 26,
                    },
                });
            }
        }

        return materials;
    };

    boardOf(-100, false);

    const snappedBoard = boardOf(100, Settings.SNAP);

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

    const tint = getColor('#ede6f5');

    const smoothLabel = createText({
        text: 'SMOOTH',
        font,
        tint,
        transform: {
            y: 30,
        },
    });

    const label = createText({
        text: 'SNAPPED',
        font,
        tint,
        transform: {
            y: 30,
        },
    });

    const hint = createText({
        text: 'SPACE ON OFF',
        font,
        tint: '#8c82a6',
        transform: {
            y: 204,
        },
    });

    const keys = useKeyboard();
    const game = useGame();

    const placeLabels = () => {
        const middle = game.getWidth() / 2;

        smoothLabel.transform.x = middle - 122;
        label.transform.x = middle + 36;
        hint.transform.x = middle - 48;
    };

    placeLabels();
    game.onResized(placeLabels);
    useWatch(() => {
        for (const part of snapped.parts) {
            part.material.vertexSnap = snappedBoard[0].vertexSnap;
        }
    }, [fox]);

    useUpdate((delta) => {
        // Slow on purpose: the shiver is a thing of small movements, and a fast one hides it.
        smooth.transform.rotationY += delta * 0.25;
        snapped.transform.rotationY += delta * 0.25;
        // Drifting the camera a little is what makes the floor's edges crawl.

        if (keys.justPressed(' ')) {
            const on = !snappedBoard[0].vertexSnap;

            for (const material of snappedBoard) {
                material.vertexSnap = on ? Settings.SNAP : false;
            }

            for (const part of snapped.parts) {
                part.material.vertexSnap = on ? Settings.SNAP : false;
            }

            label.text = on ? 'SNAPPED' : 'SMOOTH';
        }
    });
};
```

`scenes/demo.ts`:

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

import { Comparison } from '../components/comparison';

export const Level = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 45,
        transform: {
            y: 150,
            z: 460,
            rotationX: -0.3
        },
    });

    useDirectionalLight({
        color: '#fff2e0',
        intensity: 1.1,
        ambient: 0.4,
        transform: {
            rotationX: -0.6,
            rotationY: 0.5
        },
    });

    useSpawn(Comparison)();
    useUpdate((_delta, time) => {
        camera.transform.x = Math.sin(time * 0.4) * 18;
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 320;
export const HEIGHT = 240;
export const SNAP = 80;
```
