# Spot light

> useSpotLight is a point light narrowed to a cone, sweeping a row of cubes on a floor.

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

`useSpotLight` is a point light narrowed to a cone: a direction, `angle` (the outer half-angle) and `penumbra` (how soft the edge is), on top of a place and a range.

Here it sweeps across a row of cubes standing on a `usePlaneGeometry` floor, lighting each cube, and the floor behind it, as the beam passes over.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';

import { WIDTH, HEIGHT } from './settings';
import { Scene } from './scenes/demo';

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

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

`components/display.ts`:

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

export const Display = () => {
    const floor = usePlaneGeometry({
        width: 10,
        depth: 6,
        widthSegments: 24,
        depthSegments: 16
    });

    createMesh({
        geometry: floor,
        transform: {
            y: 0
        },
        tint: '#2c2c3a'
    });

    const cube = useCubeGeometry();
    const meshes: TMesh[] = [];

    for (let i = -2; i <= 2; i++) {
        meshes.push(createMesh({
            geometry: cube,
            transform: {
                x: i * 1.4,
                y: 0.5
            },
            tint: '#e8e8f0'
        }));
    }

    useUpdate((delta) => {
        for (const mesh of meshes) {
            mesh.transform.rotationY += delta * Settings.TURN;
        }
    });
};
```

`scenes/demo.ts`:

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

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

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

    // A lamp is AIMED and not given a direction vector: it shines along its own -Z, so
    // pointing it down and slightly back is a rotation about X. That is also why a lamp
    // carries its own facing rather than taking its box's: a lantern held by someone who
    // spins should not sweep the room.
    const light = useSpotLight({
        transform: {
            x: 0,
            y: 3,
            z: 2.5,
            rotationX: -1.19
        },
        angle: Math.PI / 8,
        penumbra: 0.3,
        color: '#ffe488',
        intensity: 1.8,
        ambient: 0.08,
        range: 10,
    });

    useSpawn(Display)();
    useUpdate((_delta, time) => {
        light.transform.rotationY = Math.sin(time * Settings.SWING.speed) * Settings.SWING.angle;
    });

    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,
    near: 0.1,
    far: 100,
    transform: {
        y: 2,
        z: 6,
        rotationX: -0.32
    },
} satisfies TCamera3dOptions;
export const TURN = 0.3;
export const SWING = {
    speed: 0.8,
    angle: 0.6
};
```
