# Many lights

> Thirty-two lights a scene and eight a model, the era's ceiling. Only one casts a shadow, and the others keep lighting inside it.

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

An object gives off as many lights as it asks for, and a scene holds thirty-two at a time, each model lit by the eight that reach it most. Eight is the era's ceiling rather than an arbitrary budget: a GameCube had exactly that many in hardware for each thing it drew.

The sun casts the shadow, because only one light in a scene can and it is the first that asks. The three orbiting lamps **keep lighting the ball inside that shadow**, since a shadow takes away the casting light's share and nobody else's, and that is the whole difference between a shadow and a dark patch.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { Scene } from './scenes/demo';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#080a12',
    msaa: 4,
});

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

`components/exhibit.ts`:

```ts
import { createMesh, usePlaneGeometry, useSphereGeometry } from 'nacatamalon';

export const Exhibit = () => {
    createMesh({
        geometry: usePlaneGeometry({
            width: 14,
            depth: 14
        }),
        tint: '#3a3f4b',
        // It receives every shadow and casts none: a floor that cast would shadow itself.
        castShadow: false,
    });

    const ball = createMesh({
        geometry: useSphereGeometry({
            radius: 1,
            segments: 32,
            rings: 16
        }),
        tint: '#e8e8ee',
        transform: {
            y: 1.4
        },
    });

    return ball;
};
```

`scenes/demo.ts`:

```ts
import { createScene, getColor, useAmbientLight, useCamera3d, useDirectionalLight, usePointLight, useUpdate, useSpawn } from 'nacatamalon';

import { Exhibit } from '../components/exhibit';

export const Scene = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        near: 0.1,
        far: 100,
        transform: {
            y: 3.5,
            z: 7,
            rotationX: -0.4
        }
    });

    useAmbientLight({
        color: '#26304a',
        intensity: 0.35
    });

    useDirectionalLight({
        color: '#fff0d0',
        intensity: 0.9,
        transform: {
            rotationX: -0.9,
            rotationY: 0.4
        },
        castShadow: true,
        // How far the shadows reach in front of the camera. The camera stands seven back, so a
        // square of eight would stop short of the ball and there would be no shadow at all.
        shadowArea: 22,
        shadowDistance: 30,
    });

    // Three lamps, three calls: an object gives off as many lights as it asks for. Their records
    // are kept so the update can fly them around.
    const lamps: ReturnType<typeof usePointLight>[] = [];

    ['#ff4d6d', '#4dff88', '#4d9dff'].forEach((hex) => {
        lamps.push(usePointLight({
            color: getColor(hex),
            intensity: 1.6,
            range: 6,
            transform: {
                y: 1.4
            }
        }));
    });

    const ball = useSpawn(Exhibit)().api;

    useUpdate((delta, t) => {
        lamps.forEach((lamp, i) => {
            const a = t * 0.9 + (i * Math.PI * 2) / 3;

            lamp.transform.x = Math.cos(a) * 2.6;
            lamp.transform.z = Math.sin(a) * 2.6;
            lamp.transform.y = 1.4 + Math.sin(t * 1.5 + i) * 0.5;
        });
        ball.transform.rotationY += delta * 0.4;
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 560;
export const HEIGHT = 360;
```
