# Affine textures

> The PlayStation's swimming textures: with affine, the picture is stretched straight across the screen without correcting for depth.

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

The PlayStation drew a picture on a triangle by stretching it straight across the screen, without correcting for depth. That is "affine" mapping, and it is why the floors of the time bent and swam as the camera moved. `affine: true` on a material (or on `createModel` and `createMesh`) is exactly that.

The warp grows with how big a triangle is on screen. The left half of this corridor is made of whole squares and swims; the right half is the same squares cut into eight by eight and barely moves. Cutting was what the games of the time did to keep it down. Press space to switch it off and see the picture held true.

If you write a material shader of your own, its fragment receives the `uv` already stretched when the material is affine, so it works there too.

## Code

`index.ts`:

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

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

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

`components/corridor.ts`:

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

/**
 * One half of the corridor: its strip of floor and its wall, in squares of STEP. The only thing that
 * tells the two halves apart is how finely each square is cut, `cuts` pieces a side.
 *
 * The left wall faces right and the right wall faces left, so both are seen from inside.
 */
const Half = (side: -1 | 1, cuts: number, material: TMeshMaterial) => {
    const square = usePlaneGeometry({
        width: Settings.STEP,
        depth: Settings.STEP,
        widthSegments: cuts,
        depthSegments: cuts,
    });

    for (let step = 0; step < Settings.STEPS; step++) {
        const z = -step * Settings.STEP;

        createMesh({
            geometry: square,
            material,
            transform: {
                x: side * Settings.STEP / 2,
                z,
            },
        });
        createMesh({
            geometry: square,
            material,
            transform: {
                x: side * Settings.STEP,
                y: Settings.STEP / 2,
                z,
                rotation: side * Math.PI / 2,
            },
        });
    }
};

/**
 * The corridor: the left half made of whole squares, two triangles each, and the right half of the
 * same squares cut into eight by eight. Each half has its own material, so the stretching can be
 * switched for both at once and compared side by side. Returns the two materials.
 */
export const Corridor = (texture: TTexture) => {
    const whole = createMaterial({
        shader: 'mesh3d',
        texture,
        affine: true,
    });

    const cut = createMaterial({
        shader: 'mesh3d',
        texture,
        affine: true,
    });

    Half(-1, 1, whole);
    Half(1, 8, cut);

    return [whole, cut];
};
```

`components/hud.ts`:

```ts
import { createText, getColor, useGame, useKeyboard, useLoadBitmapFont, useUpdate, type TMeshMaterial } from 'nacatamalon';

/**
 * What each half is, whether the stretching is on, and the key that switches it. A material is read
 * every frame, so writing `affine` on it is the whole switch.
 */
export const Hud = (materials: TMeshMaterial[]) => {
    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 whole = createText({
        text: 'WHOLE',
        font,
        tint,
        transform: {
            y: 12,
        },
    });

    const cut = createText({
        text: 'CUT',
        font,
        tint,
        transform: {
            y: 12,
        },
    });

    const state = createText({
        text: '',
        font,
        tint,
        transform: {
            y: 204,
        },
    });

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

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

    useUpdate(() => {
        if (keys.justPressed(' ')) {
            for (const material of materials) {
                material.affine = !material.affine;
            }
        }

        state.text = materials[0].affine ? 'AFFINE' : 'CORRECTED';

        // The width follows the screen (keep: 'height' in index.ts), so the labels are placed from
        // the middle of whatever the game is this frame.
        const middle = game.getWidth() / 2;

        whole.transform.x = middle - 100;
        cut.transform.x = middle + 60;
        state.transform.x = middle - state.text.length * 4.5;
        hint.transform.x = middle - 54;
    });
};
```

`scenes/demo.ts`:

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

import * as Settings from '../settings';
import { Corridor } from '../components/corridor';
import { Hud } from '../components/hud';

export const Hall = () => {
    // Low and close, but in front of the first square: the warp is worst on the triangles nearest
    // the camera.
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 70,
        transform: {
            y: 1.4
        },
        near: 0.1,
        far: 80,
    });

    useDirectionalLight({
        color: '#fff1dc',
        intensity: 0.6,
        ambient: 0.7,
        transform: {
            rotationX: -0.8,
            rotationY: 0.3
        },
    });

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

    const materials = useSpawn(Corridor)(tiles).api;

    useSpawn(Hud)(materials);

    // Leaning in and back at the mouth, and looking from side to side: the moving is what makes the
    // picture swim. The nearest it comes is a little in front of the first square's edge.
    useUpdate((_delta, time) => {
        camera.transform.z = Settings.STEP / 2 + 2.6 + Math.sin(time * 0.5) * 0.8;
        camera.transform.rotationY = Math.sin(time * 0.6) * 0.3;
    });

    return createScene();
};

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

`settings.ts`:

```ts
export const WIDTH = 320;
export const HEIGHT = 240;
/** How long one stretch of the corridor is, floor tile and wall panel alike. */
export const STEP = 4;
/** How many stretches it runs on. */
export const STEPS = 16;
```
