# Extruded shapes

> useExtrudeGeometry pushes a diamond, a star with a hole, a frame, a gear and a letter out into solids, their edges sharp, cut at 45° or rounded.

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

Press **space** to change the edges: sharp, cut or rounded.

`useExtrudeGeometry` takes a flat outline, a list of points as its front face is seen, with the holes cut out of it, and pushes it out into a solid of the depth asked for. The outlines are in `data/outlines.ts`: a diamond, a star of five points with a hole in it, a frame, a gear of twelve teeth and a letter.

Its `bevel` cuts the edges inward, straight in one step or rounded in several, and its crease angle decides which edges stay hard and which are smooth. The studio lights them, and is what their plastic reflects.

## Code

`index.ts`:

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

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

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

`components/hud.ts`:

```ts
import { createText, useAction, useGame, useLoadBitmapFont, useScreenSpace, useUpdate, worldToScreen } from 'nacatamalon';
import type { TCamera3d } from 'nacatamalon';
import type { TPiece } from './piece';
import { EDGES, HEIGHT } 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 shape's name under it, and which cut of the edges is shown; space for the next one.
export const Hud = (camera: TCamera3d, pieces: { name: string; piece: TPiece }[]) => {
    useScreenSpace();

    const font = useLoadBitmapFont(FONT);
    const game = useGame();
    const next = useAction('edges');
    let edges = 0;

    const title = createText({
        text: 'EDGES: ' + EDGES[edges]!.name,
        font,
        transform: {
            x: 8,
            y: 8
        }
    });

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

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

    useUpdate(() => {
        if (next.justPressed()) {
            edges = (edges + 1) % EDGES.length;
            title.text = 'EDGES: ' + EDGES[edges]!.name;

            for (const { piece } of pieces) {
                piece.setEdges(edges);
            }
        }

        pieces.forEach(({ piece }, i) => {
            const at = worldToScreen(camera, game.getWidth(), game.getHeight(), {
                x: piece.mesh.transform.x,
                y: 0,
                z: 0
            });

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

`components/piece.ts`:

```ts
import { createMaterial, createMesh, plastic, useExtrudeGeometry, useUpdate } from 'nacatamalon';
import type { TTexture } from 'nacatamalon';
import type { OUTLINES } from '../data/outlines';
import { DEPTH, EDGES, SPACING, TURN_SPEED } from '../settings';

type TPieceProps = {
    shape: (typeof OUTLINES)[number];
    index: number;
    count: number;
    studio: TTexture;
};

// One outline pushed out into a solid, in each of the three cuts of its edges. Asking for the same
// outline again gives back the same shape, so all of them are made once, here.
export const Piece = ({ shape, index, count, studio }: TPieceProps) => {
    const shapes = EDGES.map(({ bevel }) => useExtrudeGeometry({
        outline: shape.outline,
        holes: shape.holes,
        depth: DEPTH,
        ...(bevel === null ? {
        } : {
            bevel
        })
    }));

    const mesh = createMesh({
        geometry: shapes[0]!,
        material: createMaterial(plastic({
            tint: shape.tint,
            environment: studio
        })),
        transform: {
            x: (index - (count - 1) / 2) * SPACING,
            y: 1.1,
            rotationY: index * 0.6
        }
    });

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

    return {
        mesh,
        setEdges: (which: number) => {
            mesh.geometry = shapes[which]!;
        }
    };
};

export type TPiece = ReturnType<typeof Piece>;
```

`data/outlines.ts`:

```ts
type TPoint = { x: number; y: number };

const circle = (radius: number, points: number, turn = 0): TPoint[] => Array.from({
    length: points
}, (_, i) => ({
    x: Math.cos(turn + (i / points) * Math.PI * 2) * radius,
    y: Math.sin(turn + (i / points) * Math.PI * 2) * radius
}));

const square = (half: number): TPoint[] => [
    {
        x: -half,
        y: -half
    },
    {
        x: half,
        y: -half
    },
    {
        x: half,
        y: half
    },
    {
        x: -half,
        y: half
    }
];

// A star of five points: the points out, the dents in.
const star = Array.from({
    length: 10
}, (_, i) => {
    const radius = i % 2 === 0 ? 0.9 : 0.38;
    const angle = Math.PI / 2 + (i * Math.PI) / 5;

    return {
        x: Math.cos(angle) * radius,
        y: Math.sin(angle) * radius
    };
});

// A gear: twelve teeth, each rising from the rim, flat across its top and back down.
const gear = Array.from({
    length: 12
}, (_, tooth) => {
    const step = (Math.PI * 2) / 12;
    const at = tooth * step;

    return [[at, 0.68], [at + step * 0.12, 0.88], [at + step * 0.45, 0.88], [at + step * 0.57, 0.68]].map(([angle, radius]) => ({
        x: Math.cos(angle!) * radius!,
        y: Math.sin(angle!) * radius!
    }));
}).flat();

// Each outline as its front face is seen, `x` across and `y` up, and what is cut out of it.
export const OUTLINES: { name: string; tint: string; outline: TPoint[]; holes: TPoint[][] }[] = [
    {
        name: 'DIAMOND',
        tint: '#e8c547',
        outline: [
            {
                x: 0,
                y: 0.9
            },
            {
                x: 0.5,
                y: 0
            },
            {
                x: 0,
                y: -0.9
            },
            {
                x: -0.5,
                y: 0
            }
        ],
        holes: []
    },
    {
        name: 'STAR',
        tint: '#7fb3ff',
        outline: star,
        holes: [circle(0.16, 16)]
    },
    {
        name: 'FRAME',
        tint: '#c98a55',
        outline: square(0.8),
        holes: [square(0.5)]
    },
    {
        name: 'GEAR',
        tint: '#9be37a',
        outline: gear,
        holes: [circle(0.25, 20)]
    },
    {
        name: 'LETTER A',
        tint: '#ff7a8a',
        outline: [
            {
                x: -0.75,
                y: -0.9
            },
            {
                x: -0.4,
                y: -0.9
            },
            {
                x: -0.27,
                y: -0.5
            },
            {
                x: 0.27,
                y: -0.5
            },
            {
                x: 0.4,
                y: -0.9
            },
            {
                x: 0.75,
                y: -0.9
            },
            {
                x: 0.18,
                y: 0.9
            },
            {
                x: -0.18,
                y: 0.9
            }
        ],
        holes: [
            [
                {
                    x: -0.15,
                    y: -0.2
                },
                {
                    x: 0,
                    y: 0.35
                },
                {
                    x: 0.15,
                    y: -0.2
                }
            ]
        ]
    }
];
```

`scenes/demo.ts`:

```ts
import { cameraLookAt, createScene, useCamera3d, useSpawn, useStudio } from 'nacatamalon';
import { Hud } from '../components/hud';
import { Piece } from '../components/piece';
import { OUTLINES } from '../data/outlines';

export const Shapes = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 45,
        transform: {
            y: 2.4,
            z: 8.5
        }
    });

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

    // The studio lights them, and is what their plastic reflects.
    const studio = useStudio({
        ground: '#26222e'
    });

    const spawnPiece = useSpawn(Piece);

    const pieces = OUTLINES.map((shape, index) => ({
        name: shape.name,
        piece: spawnPiece({
            shape,
            index,
            count: OUTLINES.length,
            studio: studio.environment
        }).api
    }));

    useSpawn(Hud)(camera, pieces);

    return createScene();
};
```

`settings.ts`:

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

export const WIDTH = 640;
export const HEIGHT = 360;
export const DEPTH = 0.35;
export const SPACING = 2.3;
export const TURN_SPEED = 0.5;

// The three ways their edges are cut, one after the other with space.
export const EDGES = [
    {
        name: 'SHARP',
        bevel: null
    },
    {
        name: 'CUT AT 45',
        bevel: {
            size: 0.06,
            segments: 1
        }
    },
    {
        name: 'ROUNDED',
        bevel: {
            size: 0.08,
            segments: 4
        }
    }
] as const;

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