# Material catalog

> Every material recipe on one shelf under useStudio, in the CGI style: gold, copper, brass, anodized and steel; glass, neon, hologram and ice; chrome, plastic, ceramic, rubber, car paint and pearl; felt, wood, leather and paper.

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

Every material recipe on one shelf: the metals at the front, `gold`, `copper`, `brass`, `anodized` and `steel`; `glass`, `neon`, `hologram` and `ice`, which light goes through or comes from; the glossy `chrome`, `plastic`, `ceramic`, `rubber`, `carPaint` and `pearl`; and the soft surfaces of a table at the back, `felt`, `wood`, `leather` and `paper`. A recipe is a function that gives back a material's options, ready for `createMaterial`, so anything it sets can be changed or added to: `createMaterial(plastic({ tint: '#d02020' }))`.

The whole game is in the CGI style, with its own 640 × 480, and `useStudio` lights the shelf and is what the metals reflect. The camera moves a little from side to side, so what each reflects slides across it.

## Code

`index.ts`:

```ts
import { createGame, RENDER } from 'nacatamalon';
import { Catalog } from './scenes/demo';

// The CGI style for the whole game: every material at every pixel, a soft bloom, and its own
// 640 x 480, smooth, so no width or height is said here.
const game = createGame('#app', {
    style: RENDER.STYLES.cgi,
    scaling: 'contain',
    fullscreenScaling: 'contain',
    background: '#10131a',
    msaa: 4
});

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

`components/labels.ts`:

```ts
import { createText, useGame, useLoadBitmapFont, useScreenSpace, useUpdate, worldToScreen } from 'nacatamalon';
import type { TCamera3d, TMesh } from 'nacatamalon';
import { RADIUS } from '../settings';

// Each recipe's name under its ball, where the camera sees it this frame.
export const Labels = (camera: TCamera3d, placed: { name: string; ball: 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 game = useGame();
    const labels = placed.map(({ name }) => createText({
        text: name,
        font,
        tint: '#e8e4f0',
        // Over the balls, whichever is in front.
        zIndex: 100,
        anchor: {
            x: 0.5,
            y: 0
        }
    }));

    useUpdate(() => {
        placed.forEach(({ ball }, i) => {
            const at = worldToScreen(camera, game.getWidth(), game.getHeight(), {
                x: ball.transform.x,
                y: 0,
                z: ball.transform.z + RADIUS
            });
            labels[i]!.transform.x = Math.round(at.x);
            labels[i]!.transform.y = Math.round(at.y) + 4;
        });
    });
};
```

`components/shelf.ts`:

```ts
import { createMaterial, createMesh, usePlaneGeometry, useSphereGeometry, useUpdate } from 'nacatamalon';
import type { TMesh, TTexture } from 'nacatamalon';
import { RADIUS, ROW_SPACING, ROWS, SPACING } from '../settings';

// A ball of each recipe on a dark floor, all reflecting the studio they stand in. Gives back each
// ball with its name, for the labels.
export const Shelf = (studio: TTexture, placed: { name: string; ball: TMesh }[]) => {
    createMesh({
        geometry: usePlaneGeometry({
            width: 18,
            depth: 17
        }),
        transform: {
            z: -1.5
        },
        tint: '#2a2e38',
        specular: '#505560',
        shininess: 20,
        perPixel: true,
        // It takes the balls' shadows; it has nothing to cast one on.
        castShadow: false
    });

    ROWS.forEach((row, r) => {
        const z = (1 - r) * ROW_SPACING;
        row.forEach(({ name, make }, i) => {
            const ball = createMesh({
                geometry: useSphereGeometry({
                    radius: RADIUS,
                    segments: 32,
                    rings: 24
                }),
                material: createMaterial(make({
                    environment: studio
                })),
                transform: {
                    // Wider further back, where the view makes them smaller, so the names fit.
                    x: (i - (row.length - 1) / 2) * SPACING * (1 + r * 0.25),
                    y: RADIUS,
                    z
                }
            });
            placed.push({ name, ball });
        });
    });

    // Turning, so a rim, a shine and a reflection are seen to move with the light and not the ball.
    useUpdate((delta) => {
        for (const { ball } of placed) {
            ball.transform.rotationY += delta * 0.4;
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { cameraLookAt, createScene, useCamera3d, useSpawn, useStudio, useUpdate } from 'nacatamalon';
import type { TMesh } from 'nacatamalon';
import { Labels } from '../components/labels';
import { Shelf } from '../components/shelf';

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

    // The studio lights them, and is what they reflect.
    const studio = useStudio({
        ground: '#2a2420',
        panels: 3
    });
    // The shelf is deeper than the studio's own reach: its key light's shadows cover all of it.
    studio.key.shadowArea = 24;

    const placed: { name: string; ball: TMesh }[] = [];
    useSpawn(Shelf)(studio.environment, placed);
    useSpawn(Labels)(camera, placed);

    // A little from side to side, so what each reflects slides across it.
    useUpdate((_delta, time) => {
        camera.transform.x = Math.sin(time * 0.3) * 1.2;
        cameraLookAt(camera, { x: 0, y: 0.3, z: -1.4 });
    });

    return createScene();
};
```

`settings.ts`:

```ts
import { anodized, brass, carPaint, ceramic, chrome, copper, felt, glass, gold, hologram, ice, leather, neon, paper, pearl, plastic, rubber, steel, wood } from 'nacatamalon';
import type { TMaterialRecipe, TMaterialRecipeOptions } from 'nacatamalon';

// Every recipe, by row from the front: the metals, the ones light goes through or comes from, the
// glossy ones, and the soft ones of a table at the back, the rows wider where the view is wider.
export const ROWS: readonly (readonly { name: string; make: (options: TMaterialRecipeOptions) => TMaterialRecipe }[])[] = [
    [
        { name: 'GOLD', make: gold },
        { name: 'COPPER', make: copper },
        { name: 'BRASS', make: brass },
        { name: 'ANODIZED', make: anodized },
        { name: 'STEEL', make: steel }
    ],
    [
        { name: 'GLASS', make: glass },
        { name: 'NEON', make: neon },
        { name: 'HOLOGRAM', make: hologram },
        { name: 'ICE', make: ice },
        { name: 'CHROME', make: chrome }
    ],
    [
        { name: 'PLASTIC', make: plastic },
        { name: 'CERAMIC', make: ceramic },
        { name: 'RUBBER', make: rubber },
        { name: 'CAR PAINT', make: carPaint },
        { name: 'PEARL', make: pearl }
    ],
    [
        { name: 'FELT', make: felt },
        { name: 'WOOD', make: wood },
        { name: 'LEATHER', make: leather },
        { name: 'PAPER', make: paper }
    ]
];

/** How far apart the balls stand across, and the rows back, room for each name under its ball. */
export const SPACING = 1.6;
export const ROW_SPACING = 3;
export const RADIUS = 0.55;
```
