# Affine cube

> Two cubes turning with the same picture: the right one is affine, and its picture bends and swims like on a PlayStation.

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

Two cubes with the same picture turn together. The left one is drawn as a modern card would, the picture held true to each face. The right one has `affine: true`: the picture is stretched straight across the screen, the way the PlayStation drew it.

Each face is two triangles, and each one stretches the picture its own way, so the lines break along the face's diagonal and swim as the cube turns. It shows most when a face is seen edge-on, because that is when its corners are at the most different depths. Press space to switch it on and off on the right cube.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Spin } from './scenes/demo';
import { ACTIONS, WIDTH, HEIGHT } from './settings';

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

// Declare Scenes
game({
    Spin
});
```

`components/hud.ts`:

```ts
import { createText, useAction, useGame, useLoadBitmapFont, useScreenSpace, useUpdate } from 'nacatamalon';
import type { TMesh } from 'nacatamalon';

export const Hud = (affine: TMesh) => {
    useScreenSpace();

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

    const correctedLabel = createText({
        text: 'CORRECTED',
        font,
        tint: '#ede6f5',
        transform: {
            y: 30
        }
    });

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

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

    const game = useGame();
    const toggle = useAction('toggle');

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

        correctedLabel.transform.x = middle - 116;
        label.transform.x = middle + 40;
        hint.transform.x = middle - 54;
    };

    // keep: 'height' can change the width. Layout runs on resize rather than every frame.
    place();
    game.onResized(place);

    useUpdate(() => {
        if (toggle.justPressed()) {
            affine.material.affine = !affine.material.affine;
            label.text = affine.material.affine ? 'AFFINE' : 'CORRECTED';
        }
    });
};
```

`components/textured_cube.ts`:

```ts
import { createMesh, useUpdate } from 'nacatamalon';
import type { TGeometry, TTexture } from 'nacatamalon';
import { SPIN } from '../settings';

type TTexturedCubeProps = {
    geometry: TGeometry;
    texture: TTexture;
    x: number;
    affine: boolean;
};

export const TexturedCube = ({ geometry, texture, x, affine }: TTexturedCubeProps) => {
    const mesh = createMesh({
        geometry,
        texture,
        affine,
        transform: {
            x
        }
    });

    useUpdate((delta) => {
        mesh.transform.rotationY += delta * SPIN.y;
        mesh.transform.rotationX += delta * SPIN.x;
    });

    return mesh;
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useCubeGeometry, useDirectionalLight, useLoadTexture, useSpawn } from 'nacatamalon';
import { TexturedCube } from '../components/textured_cube';
import { Hud } from '../components/hud';
import { CUBE_SIZE, SEPARATION } from '../settings';

export const Spin = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 65,
        transform: {
            z: 3.6
        }
    });
    useDirectionalLight({
        color: '#fff1dc',
        intensity: 0.8,
        ambient: 0.55,
        transform: {
            rotationX: -0.6,
            rotationY: 0.5
        }
    });

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

    const geometry = useCubeGeometry({
        width: CUBE_SIZE,
        height: CUBE_SIZE,
        depth: CUBE_SIZE
    });

    const spawnCube = useSpawn(TexturedCube);

    spawnCube({
        geometry,
        texture,
        x: -SEPARATION,
        affine: false
    });

    const affine = spawnCube({
        geometry,
        texture,
        x: SEPARATION,
        affine: true
    });

    useSpawn(Hud)(affine.api);

    return createScene();
};
```

`settings.ts`:

```ts
import type { TActionMap } from 'nacatamalon';

export const WIDTH = 320;
export const HEIGHT = 240;
export const CUBE_SIZE = 1.4;
export const SEPARATION = 1.35;

export const SPIN = {
    x: 0.35,
    y: 0.6
};

export const ACTIONS: TActionMap = [
    {
        name: 'toggle',
        bindings: [
            {
                type: 'key',
                key: ' '
            },
            {
                type: 'button',
                button: 'a'
            }
        ]
    }
];
```
