# Reflection

> Plastic, ceramic and chrome under one studio: reflection lays a round picture of the surroundings over each, by how much it reflects in front and at the edge.

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

Plastic, ceramic and chrome under the same light. A material's `reflection` lays a picture of its surroundings over it, read round from the way each pixel faces, the way the pre-rendered graphics of the era made a model look polished. `front` is how much it reflects where it faces the camera and `edge` where it turns away, so plastic shows its surroundings only at its rim and chrome everywhere; `metal` tints what it reflects with its own colour.

The surroundings here are `studioEnvironment`, a bright sky above and a warm floor below, drawn in code. **Space** turns the reflections on and off.

## Code

`index.ts`:

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

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

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

`components/hud.ts`:

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

export const Hud = (balls: 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 labels = SURFACES.map((surface) => createText({
        text: surface.label,
        font,
        tint: '#ede6f5',
        transform: {
            y: 40
        }
    }));

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

    const game = useGame();
    const toggle = useAction('toggle');
    let reflecting = true;

    // How much each one reflects, as its recipe made it, to give back when switched on again.
    const amounts = balls.map((ball) => ({
        front: ball.material.uniforms!.reflectionFront as number,
        edge: ball.material.uniforms!.reflectionEdge as number
    }));

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

        labels.forEach((label, i) => {
            label.transform.x = middle + (i - 1) * 96 - label.text.length * 4;
        });
        hint.transform.x = middle - 76;
    };

    place();
    game.onResized(place);

    // The two numbers are uniforms of the material, so they change while the game runs.
    useUpdate(() => {
        if (toggle.justPressed()) {
            reflecting = !reflecting;
            balls.forEach((ball, i) => {
                ball.material.uniforms!.reflectionFront = reflecting ? amounts[i]!.front : 0;
                ball.material.uniforms!.reflectionEdge = reflecting ? amounts[i]!.edge : 0;
            });
            hint.text = reflecting ? 'SPACE REFLECTION ON' : 'SPACE REFLECTION OFF';
        }
    });
};
```

`components/shiny_ball.ts`:

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

type TShinyBallProps = {
    geometry: TGeometry;
    studio: TTexture;
    surface: (typeof SURFACES)[number];
    x: number;
};

export const ShinyBall = ({ geometry, studio, surface, x }: TShinyBallProps) => {
    const mesh = createMesh({
        geometry,
        material: createMaterial(surface.make({
            environment: studio
        })),
        transform: {
            x
        }
    });

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

    return mesh;
};
```

`scenes/demo.ts`:

```ts
import { createScene, useAmbientLight, useCamera3d, usePointLight, useSphereGeometry, useSpawn, useUpdate } from 'nacatamalon';
import { createPixelTexture, studioEnvironment } from 'nacatamalon/pixels';
import { ShinyBall } from '../components/shiny_ball';
import { Hud } from '../components/hud';
import { BALL, LAMP, STUDIO, SURFACES } from '../settings';

export const Studio = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        transform: {
            z: 5.6
        }
    });

    // A studio's light from all around: a bright sky above and a warm floor below.
    useAmbientLight({
        color: '#dce6ff',
        ground: '#6a4632',
        intensity: 0.45
    });

    const lamp = usePointLight({
        color: LAMP.color,
        intensity: LAMP.intensity,
        range: LAMP.range,
        transform: {
            y: LAMP.height,
            z: LAMP.z
        }
    });

    const studio = createPixelTexture(studioEnvironment(STUDIO));

    const geometry = useSphereGeometry({
        radius: BALL.radius,
        segments: BALL.segments,
        rings: BALL.rings
    });

    const spawnBall = useSpawn(ShinyBall);

    const balls = SURFACES.map((surface, i) => spawnBall({
        geometry,
        studio,
        surface,
        x: (i - 1) * BALL.spacing
    }).api);

    useSpawn(Hud)(balls);

    useUpdate((_delta, time) => {
        lamp.transform.x = Math.cos(time * LAMP.speed) * LAMP.radius;
    });

    return createScene();
};
```

`settings.ts`:

```ts
import { ceramic, chrome, plastic } from 'nacatamalon';
import type { TActionMap } from 'nacatamalon';

export const WIDTH = 320;
export const HEIGHT = 240;

export const BALL = {
    radius: 0.8,
    segments: 16,
    rings: 12,
    spacing: 2
};

// Three of the engine's recipes under the same studio. Each is only options of a material, so
// each reflects the same picture by its own amounts.
export const SURFACES = [
    {
        label: 'PLASTIC',
        make: plastic
    },
    {
        label: 'CERAMIC',
        make: ceramic
    },
    {
        label: 'CHROME',
        make: chrome
    }
];

// The studio it reflects: a bright sky, a dark floor and two softboxes.
export const STUDIO = {
    top: '#f4f7ff',
    horizon: '#a9b4c8',
    ground: '#3a2a22',
    panels: 2
};

export const LAMP = {
    color: '#fff3e0',
    intensity: 1.4,
    range: 16,
    radius: 4,
    height: 2.4,
    z: 3,
    speed: 0.5
};

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