# Lighting

> Three kinds of light: a sun with a direction and no place, a lamp that fades with distance, and a torch narrowed to a cone.

Example `lighting-3d`, running at https://nacatamalon.com/en/examples/lighting-3d. The whole catalogue: https://nacatamalon.com/llms/examples.md.

Three kinds of light: a **sun** with a direction and no place, a **lamp** with a place that fades out with distance, and a **torch**, a lamp narrowed to a cone.

An object gives off as many lights as it asks for, each placed by its own `transform`, so these are three calls in the scene. Each model is lit by the eight of the scene's lights that reach it most, which is what the GameCube offered for each thing it drew. The sun sweeps round while the blue lamp only reaches what is near it.

## Code

`index.ts`:

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

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

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

`components/display.ts`:

```ts
import { createMesh, useCubeGeometry, useUpdate, useSphereGeometry } from 'nacatamalon';
import * as Settings from '../settings';

export const Display = () => {
    // A wall of cubes and a ball, so the falling off is visible across a surface.
    const cube = useCubeGeometry({
        width: 0.6,
        height: 0.6,
        depth: 0.6
    });

    for (let x = -3; x <= 3; x++) {
        for (let y = -1; y <= 1; y++) {
            createMesh({
                geometry: cube,
                tint: '#dfe7f5',
                transform: {
                    x: x * 0.8,
                    y: y * 0.8,
                    z: -1
                }
            });
        }
    }

    const ball = createMesh({
        geometry: useSphereGeometry({
            radius: 0.7,
            segments: 24
        }),
        tint: '#ffffff',
        specular: '#ffffff',
        shininess: 48,
        transform: {
            z: 0.8
        },
    });

    useUpdate((delta) => {
        ball.transform.rotationY += delta * Settings.TURN.ball;
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useAmbientLight, useCamera3d, useDirectionalLight, usePointLight, useSpotLight, useUpdate, useSpawn } from 'nacatamalon';
import * as Settings from '../settings';

import { Display } from '../components/display';

export const Level = () => {
    useCamera3d(Settings.CAMERA);

    // Turning a light is how it is aimed, exactly as with a camera.
    const sun = useDirectionalLight({
        color: '#ffb454',
        intensity: 1.1,
        ambient: 0.12
    });

    // Placed by its own transform: this one hangs to the left and close in.
    usePointLight({
        color: '#4cc9ff',
        intensity: 1.6,
        transform: {
            x: -2.2,
            y: 0.6,
            z: 2
        },
        range: 5
    });

    // A torch pointing down at the middle from above.
    useSpotLight({
        color: '#ff6bd6',
        intensity: 2,
        transform: {
            y: 3,
            z: 1
        },
        range: 8,
        angle: 0.5,
        penumbra: 0.5
    });

    // Said out loud, this wins over whatever the lamps would have settled on.
    useAmbientLight({
        color: '#101830',
        intensity: 1
    });

    useUpdate((_delta, time) => {
        sun.transform.rotationY = time * Settings.TURN.sun;
    });

    useSpawn(Display)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
import type { TCamera3dOptions } from 'nacatamalon';

export const CAMERA = {
    projection: 'perspective',
    fov: 60,
    transform: {
        y: 1,
        z: 6,
        rotationX: -0.15
    }
} satisfies TCamera3dOptions;
export const TURN = {
    sun: 0.8,
    ball: 0.2
};
```
