# Shader file

> An effect in its own file: the scene keeps the key and never the source, so it can be shared between scenes.

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

An effect in its own file, which is how one stops belonging to the scene that first needed it. A scene keeps the **key**, never the source, so the same effect can be shared between scenes, opened in an editor that colours it, and changed without touching a line of game code.

The file is ordinary WGSL with a few comment lines on top: `@shader` says which family it is for, `@uniform` declares a uniform and its default, and `@wgsl` / `@glsl` open the two halves. The header is read from the whole file and never from inside a half, because a contract written twice is two things that can drift.

Left is the file as written. Middle is the same file with the scene having the last word on two of its uniforms. Right is a second file, for models.

## Code

`index.ts`:

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

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

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

`components/gallery.ts`:

```ts
import { createMaterial, createMesh, createSprite, createText, useLoadBitmapFont, useLoadTexture, useUpdate, useSphereGeometry } from 'nacatamalon';
import { useLoadShader } from 'nacatamalon/shader';

export const Gallery = () => {
    // The file. Two materials below share this one load.
    const dissolve = useLoadShader({
        src: '/example-assets/shaders/dissolve.wgsl',
        key: 'dissolve'
    });

    const cel = useLoadShader({
        src: '/example-assets/shaders/cel.wgsl',
        key: 'cel'
    });

    const texture = useLoadTexture({
        src: '/example-assets/assets/gammix.png'
    });

    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
    });

    // Left: the file's own numbers, untouched.
    const slow = createMaterial({
        name: 'dissolve-slow',
        effect: dissolve
    });

    // Middle: the same file, this time with the scene having the last word on two of its uniforms.
    // The file says what a uniform usually is; the scene says what it is here. And it works whichever
    // way round they arrive, because what was asked for is kept apart from what is in use.
    const fast = createMaterial({
        name: 'dissolve-fast',
        effect: dissolve,
        uniforms: {
            speed: 0.9,
            ember: [0.3, 0.8, 1]
        },
    });

    [slow, fast].forEach((material, i) => {
        createSprite({
            texture,
            material,
            width: 110,
            height: 110,
            transform: {
                x: 24 + i * 130,
                y: 72,
                rotation: 0,
                scaleX: 1,
                scaleY: 1
            },
        });
    });

    const ball = createMesh({
        geometry: useSphereGeometry({
            radius: 1.1,
            segments: 32,
            rings: 16
        }),
        material: createMaterial({
            name: 'cel',
            shader: 'mesh3d',
            tint: '#e8a03c',
            effect: cel
        }),
        transform: {
            x: 2.55
        },
    });

    useUpdate((delta) => {
        ball.transform.rotationY += delta * 0.5;
    });

    const labels = [
        {
            text: 'AS WRITTEN',
            x: 24,
            y: 196
        },
        {
            text: 'OVERRIDDEN',
            x: 154,
            y: 196
        },
        {
            text: 'MESH3D FILE',
            x: 300,
            y: 196
        },
    ];

    labels.forEach(({ text, x, y }) => createText({
        text,
        font,
        tint: '#8fb7ff',
        transform: {
            x,
            y,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    }));
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, useSpawn } from 'nacatamalon';

import { Gallery } from '../components/gallery';

export const Game = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 45,
        transform: {
            x: 2.55,
            z: 4.6
        }
    });
    useDirectionalLight({
        color: '#fff0d0',
        intensity: 1.2,
        ambient: 0.18,
        transform: {
            rotationX: -0.5,
            rotationY: 0.5
        }
    });

    useSpawn(Gallery)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 260;
```
