# Per-pixel light

> Two glossy balls with few triangles: the right one has perPixel, so its shine is a round spot that slides across it instead of being lost between corners.

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

The same glossy ball twice, with few triangles. The left one is lit at its corners and the colour is spread between them, as the consoles of the era did it: its shine flickers and is lost when it falls between two corners. The right one has `perPixel: true` and is lit at every pixel, as the computer graphics of the era did it: its shine is a round spot that slides smoothly across it.

`perPixel` is per material, and a render style can give it to every material that does not say (`RENDER.STYLES.cgi`). **Space** turns it on and off.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Showroom } 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({
    Showroom
});
```

`components/glossy_ball.ts`:

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

type TGlossyBallProps = {
    geometry: TGeometry;
    x: number;
    perPixel: boolean;
};

export const GlossyBall = ({ geometry, x, perPixel }: TGlossyBallProps) => {
    const mesh = createMesh({
        geometry,
        tint: PLASTIC.tint,
        specular: PLASTIC.specular,
        shininess: PLASTIC.shininess,
        perPixel,
        transform: {
            x
        }
    });

    // Turning slowly, so the corners pass under the shine as well.
    useUpdate((delta) => {
        mesh.transform.rotationY += delta * 0.3;
    });

    return mesh;
};
```

`components/hud.ts`:

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

export const Hud = (right: 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 leftLabel = createText({
        text: 'CORNERS',
        font,
        tint: '#ede6f5',
        transform: {
            y: 30
        }
    });

    const label = createText({
        text: 'PIXELS',
        font,
        tint: '#ede6f5',
        transform: {
            y: 30
        }
    });

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

    const game = useGame();
    const toggle = useAction('toggle');

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

        leftLabel.transform.x = middle - 108;
        label.transform.x = middle + 48;
        hint.transform.x = middle - 54;
    };

    // keep: 'height' can change the width. Layout runs on resize rather than every frame.
    place();
    game.onResized(place);

    useUpdate(() => {
        if (toggle.justPressed()) {
            right.material.perPixel = !right.material.perPixel;
            label.text = right.material.perPixel ? 'PIXELS' : 'CORNERS';
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useAmbientLight, useCamera3d, usePointLight, useSphereGeometry, useSpawn, useUpdate } from 'nacatamalon';
import { GlossyBall } from '../components/glossy_ball';
import { Hud } from '../components/hud';
import { BALL, LAMP } from '../settings';

export const Showroom = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 60,
        transform: {
            z: 4.2
        }
    });

    useAmbientLight({
        color: '#9fb0d0',
        intensity: 0.25
    });

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

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

    const spawnBall = useSpawn(GlossyBall);

    // The same ball twice: the left one lit at its corners, as the consoles of the era did it; the
    // right one at every pixel, as the computer graphics of the era did it.
    spawnBall({
        geometry,
        x: -BALL.separation,
        perPixel: false
    });

    const right = spawnBall({
        geometry,
        x: BALL.separation,
        perPixel: true
    });

    useSpawn(Hud)(right.api);

    useUpdate((_delta, time) => {
        lamp.transform.x = Math.cos(time * LAMP.speed) * LAMP.radius;
        lamp.transform.y = LAMP.height + Math.sin(time * LAMP.speed * 1.3) * 0.8;
    });

    return createScene();
};
```

`settings.ts`:

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

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

// Few triangles on purpose: the fewer corners a ball has, the more a shine worked out at them loses.
export const BALL = {
    radius: 1,
    segments: 12,
    rings: 8,
    separation: 1.35
};

// Glossy red plastic: a white shine, tight.
export const PLASTIC = {
    tint: '#d8322a',
    specular: '#ffffff',
    shininess: 60
};

// The lamp goes round in front of the balls, so the shine slides across them.
export const LAMP = {
    color: '#fff3e0',
    intensity: 1.6,
    range: 14,
    radius: 3.2,
    height: 1.6,
    z: 2.4,
    speed: 0.7
};

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