# Cast shadows

> A light with castShadow throws the shape of things onto what is behind them; the fox casts the pose it is in.

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

The other half of what `useBlobShadow` starts: not a smudge under a thing, but the shape of the thing thrown onto what is behind it. One light in a scene opts in with `castShadow`, and from then on the engine draws every caster into a depth picture taken from where that light stands.

The fox casts **the pose it is in**, because the bones are applied again while the map is drawn. The floor opts out of casting and still receives, since being in the map is about casting and never about receiving. And only that one light's share is taken away, which is what keeps a shadow from reading as a black smudge.

## 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: '#0a0a18',
});

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

`components/casters.ts`:

```ts
import { createMesh, createModel, useCubeGeometry, useLoadModel, useSkeletalAnimation, useUpdate } from 'nacatamalon';

export const Casters = () => {
    const cube = useCubeGeometry();

    const box = createMesh({
        geometry: cube,
        tint: '#6bb6ff',
        transform: {
            x: -130,
            y: 32,
            scaleX: 64,
            scaleY: 64,
            scaleZ: 64
        },
    });

    createMesh({
        geometry: cube,
        tint: '#b6ff6b',
        transform: {
            x: 120,
            y: 60,
            z: -40,
            scaleX: 22,
            scaleY: 120,
            scaleZ: 22
        },
    });

    const fox = useLoadModel({
        src: '/example-assets/assets/3d/gltf/Fox/Fox.gltf'
    });

    const walker = createModel({
        model: fox
    });

    useSkeletalAnimation(walker, {
        play: 'Walk',
        fade: 0.35
    });

    useUpdate((delta) => {
        box.transform.rotationY += delta * 0.6;
        walker.transform.rotationY += delta * 0.5;
    });
};
```

`scenes/demo.ts`:

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

import { Casters } from '../components/casters';

export const Scene = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 45,
        near: 0.1,
        far: 2000,
        transform: {
            y: 150,
            z: 300,
            rotationX: -0.45
        }
    });

    const sun = useDirectionalLight({
        color: '#fff1d0',
        intensity: 1.2,
        ambient: 0.1,
        transform: {
            rotationX: -0.7,
            rotationY: 0.5
        },
        castShadow: true,
        // Shadows reach from the camera out to this far. The camera stands 300 back, so a square
        // that only reached 300 would stop short of the far half of the scene and those shadows
        // would simply not be there, with nothing said.
        shadowArea: 700,
        shadowDistance: 700,
    });

    // A lamp beside the sun, a call of its own. It does not cast:
    // there is one map, so the first light that asked for it has it. What it does do is keep
    // lighting what the sun cannot see, which is the claim in the third paragraph above.
    usePointLight({
        color: '#4cc9ff',
        intensity: 0.8,
        transform: {
            x: 190,
            y: 110,
            z: 150
        },
        range: 520
    });

    createMesh({
        geometry: usePlaneGeometry({
            width: 700,
            depth: 700
        }),
        tint: '#41414f',
        // It receives every shadow in the scene and casts none of its own.
        castShadow: false,
    });

    useSpawn(Casters)();
    useUpdate((_delta, time) => {
        sun.transform.rotationY = 0.35 + Math.sin(time * 0.4) * 0.45;
    });

    return createScene();
};
```

`settings.ts`:

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