# Rounded boxes

> useCubeGeometry({ bevel }): one box with its edges sharp, cut straight, rounded in steps and rounded into a ball, on a table with a rounded edge.

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

Press **space** to swap the plastic for a picture on every face.

The same box, its edges cut further each time: `bevel: { size, segments }` cuts `size` in from every face, in `segments` steps across each edge. One step is a straight cut; more steps round it; at half the box it is a ball. The table top is a box too, its edges rounded off as a cushioned edge is.

The picture shows how each face is laid: four squares with a dark band along the top, so it shows where each face's picture starts, which way up it is, and that it reaches round to the middle of each rounded edge.

## Code

`index.ts`:

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

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#121418',
    actions: ACTIONS
});

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

`components/checker.ts`:

```ts
import { createPixels, createPixelTexture, fillRect } from 'nacatamalon/pixels';

// A picture of four squares with a dark band along its top: where each face's picture starts and
// which way up it is, and that it reaches round to the middle of each rounded edge.
export const Checker = () => {
    const pixels = createPixels(16, 16, '#f2efe6');

    fillRect(pixels, 0, 0, 8, 8, '#e05a5a');
    fillRect(pixels, 8, 8, 8, 8, '#4a7fd6');
    fillRect(pixels, 0, 0, 16, 2, '#1d1d24');

    return createPixelTexture(pixels);
};
```

`components/hud.ts`:

```ts
import { createText, useAction, useGame, useLoadBitmapFont, useScreenSpace, useUpdate, worldToScreen } from 'nacatamalon';
import type { TCamera3d } from 'nacatamalon';
import type { TRow } from './row';
import { HEIGHT, TABLE } from '../settings';

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

// Each box's cut under it; space to see the picture each face is given.
export const Hud = (camera: TCamera3d, row: TRow) => {
    useScreenSpace();

    const font = useLoadBitmapFont(FONT);
    const game = useGame();
    const swap = useAction('picture');
    let picture = false;

    createText({
        text: 'useCubeGeometry({ bevel })',
        font,
        transform: {
            x: 8,
            y: 8
        }
    });

    createText({
        text: 'SPACE: PLASTIC OR PICTURE',
        font,
        tint: '#8c82a6',
        transform: {
            x: 8,
            y: HEIGHT - 18
        }
    });

    const labels = row.boxes.map(({ name }) => createText({
        text: name,
        font,
        tint: '#e8e4f0',
        anchor: {
            x: 0.5,
            y: 0
        }
    }));

    useUpdate(() => {
        if (swap.justPressed()) {
            picture = !picture;
            row.showPicture(picture);
        }

        row.boxes.forEach(({ mesh }, i) => {
            const at = worldToScreen(camera, game.getWidth(), game.getHeight(), {
                x: mesh.transform.x,
                y: 0,
                z: TABLE.depth / 2
            });

            labels[i]!.transform.x = Math.round(at.x);
            labels[i]!.transform.y = Math.round(at.y) + 6;
        });
    });
};
```

`components/row.ts`:

```ts
import { createMaterial, createMesh, plastic, useCubeGeometry, useUpdate } from 'nacatamalon';
import type { TMesh, TTexture } from 'nacatamalon';
import { BOXES, SIZE, SPACING, TABLE, TURN_SPEED } from '../settings';

// One box of each cut, standing on the table and turning, each with a plastic look and a picture
// look to swap between.
export const Row = (studio: TTexture, picture: TTexture) => {
    const boxes: { name: string; mesh: TMesh }[] = [];

    const looks = {
        plain: BOXES.map((_, i) => createMaterial(plastic({
            tint: ['#e8c547', '#ff7a8a', '#7fb3ff', '#9be37a', '#c9a0ff'][i]!,
            environment: studio
        }))),
        picture: createMaterial({
            shader: 'mesh3d',
            texture: picture,
            perPixel: true
        })
    };

    BOXES.forEach(({ name, bevel }, i) => {
        const mesh = createMesh({
            geometry: useCubeGeometry({
                width: SIZE,
                height: SIZE,
                depth: SIZE,
                ...(bevel === null ? {
                } : {
                    bevel
                })
            }),
            material: looks.plain[i]!,
            transform: {
                x: (i - (BOXES.length - 1) / 2) * SPACING,
                y: TABLE.height + SIZE / 2,
                rotationY: 0.5,
                rotationX: 0.3
            }
        });

        boxes.push({
            name,
            mesh
        });
    });

    useUpdate((delta) => {
        for (const { mesh } of boxes) {
            mesh.transform.rotationY += delta * TURN_SPEED;
        }
    });

    return {
        boxes,
        showPicture: (on: boolean) => {
            boxes.forEach(({ mesh }, i) => {
                mesh.material = on ? looks.picture : looks.plain[i]!;
            });
        }
    };
};

export type TRow = ReturnType<typeof Row>;
```

`components/table.ts`:

```ts
import { createMaterial, createMesh, useCubeGeometry } from 'nacatamalon';
import { TABLE } from '../settings';

// A table top with its edges rounded off, as a cushioned edge or a worn bench is.
export const Table = () => {
    createMesh({
        geometry: useCubeGeometry(TABLE),
        material: createMaterial({
            shader: 'mesh3d',
            tint: '#6b4a32',
            specular: '#3a3028',
            shininess: 18,
            perPixel: true
        }),
        transform: {
            y: TABLE.height / 2
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { cameraLookAt, createScene, useCamera3d, useSpawn, useStudio } from 'nacatamalon';
import { Checker } from '../components/checker';
import { Hud } from '../components/hud';
import { Row } from '../components/row';
import { Table } from '../components/table';

export const Boxes = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 42,
        transform: {
            y: 3.4,
            z: 8
        }
    });

    cameraLookAt(camera, {
        x: 0,
        y: 0.6,
        z: 0
    });

    const studio = useStudio({
        ground: '#1e2026'
    });

    useSpawn(Table)();

    const row = useSpawn(Row)(studio.environment, Checker()).api;

    useSpawn(Hud)(camera, row);

    return createScene();
};
```

`settings.ts`:

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

export const WIDTH = 640;
export const HEIGHT = 360;
export const SIZE = 1;
export const SPACING = 1.7;
export const TURN_SPEED = 0.45;

// The same box, its edges cut further each time: `size` in from every face, in `segments` steps
// across each edge. One step is a straight cut; at half the box, a ball.
export const BOXES = [
    {
        name: 'SHARP',
        bevel: null
    },
    {
        name: '1 STEP',
        bevel: {
            size: 0.15,
            segments: 1
        }
    },
    {
        name: '2 STEPS',
        bevel: {
            size: 0.15,
            segments: 2
        }
    },
    {
        name: '4 STEPS',
        bevel: {
            size: 0.25,
            segments: 4
        }
    },
    {
        name: 'HALF: A BALL',
        bevel: {
            size: 0.5,
            segments: 8
        }
    }
] as const;

export const TABLE = {
    width: 9.5,
    height: 0.3,
    depth: 3,
    bevel: {
        size: 0.12,
        segments: 4
    }
};

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