# Chrome hall

> A stone hall of the era's pre-rendered graphics: a polished floor mirroring it all with useMirror, columns carved with normal maps, flickering torches and a figure of chrome reflecting the hall live with useEnvironment.

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

A stone hall in the look of the era's pre-rendered graphics, in the CGI style. The polished floor mirrors the whole hall with `useMirror`: the scene drawn a second time from the camera reflected across its plane, softened a little with `blur`, and more of it where it is seen low, as a real floor does. Any flat model can be a mirror, a floor or a wall.

The columns and walls are carved with normal maps made from heights with `normalsFromHeight`, so the torches catch every joint and chisel mark. The figure of chrome reflects the hall it is really in with `useEnvironment`: the scene captured round it as the six faces of a cube, here every frame (`live: true`), so the flickering torches are in it too. The camera moves slowly from side to side, so the floor's mirror and the figure's reflections move.

## Code

`index.ts`:

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

// The CGI style: every material lit at every pixel, a soft bloom round the torches, 640 x 480.
const game = createGame('#app', {
    style: RENDER.STYLES.cgi,
    scaling: 'contain',
    fullscreenScaling: 'contain',
    background: '#0c0a08',
    msaa: 4
});

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

`components/figure.ts`:

```ts
import { createModel, useEnvironment, useLoadModel, useSkeletalAnimation, useUpdate } from 'nacatamalon';
import { FIGURE } from '../settings';

// A figure of chrome that reflects the hall it is really in: its surroundings captured round it
// every frame, so the torches flicker in it and the columns slide across it as it turns.
export const Figure = () => {
    const room = useEnvironment({
        height: 0.8,
        live: true,
        size: 128
    });

    const figure = createModel({
        model: useLoadModel({
            src: FIGURE.src
        }),
        tint: '#d0d4dc',
        specular: '#ffffff',
        shininess: 140,
        perPixel: true,
        reflection: {
            ...room,
            front: 0.9
        },
        transform: {
            scaleX: FIGURE.scale,
            scaleY: FIGURE.scale,
            scaleZ: FIGURE.scale
        }
    });
    // Seen from the figure, which is left out of its own surroundings.
    room.around = figure;

    useSkeletalAnimation(figure, {
        play: 'Survey'
    });

    useUpdate((delta) => {
        figure.transform.rotationY += delta * 0.3;
    });
};
```

`components/room.ts`:

```ts
import { createMesh, neon, createMaterial, useCylinderGeometry, useMirror, usePlaneGeometry, usePointLight, useSphereGeometry, useUpdate } from 'nacatamalon';
import { createPixelTexture, normalsFromHeight } from 'nacatamalon/pixels';
import { COLUMN, COLUMNS, TORCH } from '../settings';
import { blocks, slabs } from './stone';

// The hall: a polished floor that mirrors it all, columns and a back wall of carved stone, and a
// torch on each column.
export const Room = () => {
    // Slabs two units square.
    const floor = slabs(8, 10);
    createMesh({
        geometry: usePlaneGeometry({
            width: 16,
            depth: 20
        }),
        texture: createPixelTexture(floor),
        // The joints sunk a little, so the light picks out each slab.
        normalMap: {
            texture: createPixelTexture(normalsFromHeight(floor, {
                depth: 3
            })),
            strength: 0.6
        },
        specular: '#ffffff',
        shininess: 60,
        // Polished: what stands on it is mirrored in it, softly, more of it where it is seen low.
        reflection: useMirror({
            front: 0.25,
            blur: 2,
            scale: 0.5
        }),
        castShadow: false
    });

    // Blocks about a unit long: three round a column, and ten across the wall.
    const column = blocks(3, 5);
    const wall = blocks(10, 6);

    // The back wall, stood up behind the columns.
    createMesh({
        geometry: usePlaneGeometry({
            width: 16,
            depth: COLUMN.height + 1
        }),
        texture: createPixelTexture(wall),
        normalMap: createPixelTexture(normalsFromHeight(wall, {
            depth: 6
        })),
        specular: '#806a50',
        shininess: 12,
        transform: {
            y: (COLUMN.height + 1) / 2,
            z: -8,
            rotationX: Math.PI / 2
        },
        castShadow: false
    });

    const texture = createPixelTexture(column);
    const relief = createPixelTexture(normalsFromHeight(column, {
        depth: 6
    }));
    const flames = COLUMNS.map(({ x, z }) => {
        createMesh({
            geometry: useCylinderGeometry({
                radius: COLUMN.radius,
                height: COLUMN.height,
                segments: 24
            }),
            texture,
            normalMap: relief,
            specular: '#806a50',
            shininess: 12,
            transform: {
                x,
                y: COLUMN.height / 2,
                z
            }
        });

        // A torch on the side of the column that faces the middle: its flame glows, and lights the hall.
        const side = x < 0 ? 1 : -1;
        const at = {
            x: x + side * (COLUMN.radius + 0.15),
            y: 2.6,
            z
        };
        const light = usePointLight({
            color: TORCH.color,
            intensity: TORCH.intensity,
            range: TORCH.range,
            transform: at
        });
        const flame = createMesh({
            geometry: useSphereGeometry({
                radius: 0.12,
                segments: 12,
                rings: 8
            }),
            material: createMaterial(neon({
                tint: '#ffb050'
            })),
            transform: at
        });
        return { light, flame };
    });

    // Each flame flickers on its own.
    useUpdate((_delta, time) => {
        flames.forEach(({ light, flame }, i) => {
            const flicker = 0.85 + Math.sin(time * 11 + i * 1.7) * 0.08 + Math.sin(time * 23 + i) * 0.07;
            light.intensity = TORCH.intensity * flicker;
            flame.transform.scaleY = 0.9 + flicker * 0.3;
        });
    });
};
```

`components/stone.ts`:

```ts
import { createPixels, fillNoise, fillRect } from 'nacatamalon/pixels';
import type { TPixels } from 'nacatamalon/pixels';

/**
 * Floor slabs of a warm stone with dark joints, painted in code: `across` by `down` of them, each 48
 * pixels square, so a big floor gets slabs of a sensible size.
 */
export const slabs = (across: number, down: number): TPixels => {
    const size = 48;
    const pixels = fillNoise(createPixels(across * size, down * size), {
        seed: 7,
        scale: 20,
        octaves: 4,
        from: '#5a4a3c',
        to: '#a08a72'
    });
    for (let i = 0; i < down; i++) {
        fillRect(pixels, 0, i * size, across * size, 2, '#2a221c');
    }
    for (let i = 0; i < across; i++) {
        fillRect(pixels, i * size, 0, 2, down * size, '#2a221c');
    }
    return pixels;
};

/**
 * Courses of rough stone blocks, every other course moved half a block: `across` blocks to a course
 * and `courses` of them, each block 64 by 32 pixels.
 */
export const blocks = (across: number, courses: number): TPixels => {
    const width = across * 64;
    const pixels = fillNoise(createPixels(width, courses * 32), {
        seed: 3,
        scale: 12,
        octaves: 4,
        from: '#4a463e',
        to: '#8c8676'
    });
    for (let row = 0; row < courses; row++) {
        const y = row * 32;
        fillRect(pixels, 0, y, width, 3, '#1c1a16');
        for (let x = row % 2 === 0 ? 0 : 32; x < width; x += 64) {
            fillRect(pixels, x, y, 3, 32, '#1c1a16');
        }
    }
    return pixels;
};
```

`scenes/demo.ts`:

```ts
import { cameraLookAt, createScene, useAmbientLight, useCamera3d, useSpawn, useUpdate } from 'nacatamalon';
import { Figure } from '../components/figure';
import { Room } from '../components/room';

export const Hall = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 45,
        far: 60,
        transform: {
            y: 2.6,
            z: 10.5
        }
    });

    // Dim stone all round, warmer from below, where the torchlight bounces off the floor.
    useAmbientLight({
        color: '#2a2a34',
        ground: '#3a2a1c',
        intensity: 1
    });

    useSpawn(Room)();
    useSpawn(Figure)();

    // Slowly from side to side, so the floor's mirror and the figure's reflections move.
    useUpdate((_delta, time) => {
        const angle = Math.sin(time * 0.2) * 0.35;
        camera.transform.x = Math.sin(angle) * 10.5;
        camera.transform.z = Math.cos(angle) * 10.5;
        cameraLookAt(camera, { x: 0, y: 1.3, z: 0 });
    });

    return createScene();
};
```

`settings.ts`:

```ts
// A stone hall of the era's pre-rendered computer graphics: columns either side, torches on them, a
// polished floor and a figure of chrome in the middle.

/** Where the columns stand, either side of the middle, front to back. */
export const COLUMNS: readonly { x: number; z: number }[] = [
    { x: -3.5, z: -5 },
    { x: 3.5, z: -5 },
    { x: -3.5, z: 0 },
    { x: 3.5, z: 0 },
    { x: -3.5, z: 5 },
    { x: 3.5, z: 5 }
];

/** How tall a column is, and how far round. */
export const COLUMN = {
    height: 5,
    radius: 0.55
};

/** The torches' warm light, and how far it reaches. */
export const TORCH = {
    color: '#ff9a3c',
    intensity: 1.8,
    range: 7
};

/** The figure of chrome: its file and its size. */
export const FIGURE = {
    src: '/example-assets/assets/3d/gltf/Fox/Fox.gltf',
    scale: 0.02
};
```
