# Per-sprite uniforms

> One material shared by fifteen sprites: the shader compiles once and each sprite sets its own uniforms.

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

**One** material, fifteen sprites, fifteen different settings of it. The effect compiles once; each sprite passes its own `uniforms`, which are laid over the material's every frame. Without this, varying a single number would mean a material per sprite, and a material per sprite is a compile of the identical shader per sprite.

It is not free, and the cost is worth knowing: a sprite that brings its own numbers is a **draw of its own**, because a batch writes one set of them. Fifteen is nothing. The title is a text carrying the same material, and all of its letters carry the same one, so it stays a single draw rather than one per character.

## Code

`index.ts`:

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

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

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

`components/gallery.ts`:

```ts
import { createMaterial, createSprite, createText, useLoadBitmapFont, useLoadTexture } from 'nacatamalon';
import * as Settings from '../settings';
import { HUE_WGSL, HUE_GLSL } from '../shaders/hue';

export const Gallery = () => {
    const texture = useLoadTexture({
        src: '/example-assets/assets/gammix.png',
        key: 'gammix'
    });

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

    // The material declares the uniforms and what they are when nobody says otherwise.
    const shimmer = createMaterial({
        name: 'shimmer',
        fragment: HUE_WGSL,
        fragmentGlsl: HUE_GLSL,
        uniforms: {
            hue: 0,
            phase: 0
        },
    });

    for (let row = 0; row < Settings.ROWS; row++) {
        for (let col = 0; col < Settings.COLS; col++) {
            const at = row * Settings.COLS + col;

            createSprite({
                texture,
                material: shimmer,
                // This sprite's own settings of the shared effect.
                uniforms: {
                    hue: at / (Settings.COLS * Settings.ROWS),
                    phase: at * 0.6
                },
                width: 56,
                height: 56,
                transform: {
                    x: 80 + col * 80,
                    y: 110 + row * 70,
                    rotation: 0,
                    scaleX: 1,
                    scaleY: 1
                },
            });
        }
    }

    // A text can carry a material too, and every one of its letters carries the SAME one, so a
    // shimmering title is still a single draw rather than one per character.
    createText({
        text: 'PROPERTY BLOCKS',
        font,
        material: shimmer,
        uniforms: {
            hue: 0.55,
            phase: 2
        },
        style: {
            fontSize: 16
        },
        transform: {
            x: 100,
            y: 30,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });
};
```

`scenes/demo.ts`:

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

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

export const Game = () => {
    useSpawn(Gallery)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const COLS = 5;
export const ROWS = 3;
```

`shaders/hue.ts`:

```ts
export const HUE_WGSL = `
fn hueRotate(rgb: vec3f, angle: f32) -> vec3f {
    let k = vec3f(0.57735);
    return rgb * cos(angle) + cross(k, rgb) * sin(angle) + k * dot(k, rgb) * (1.0 - cos(angle));
}
fn effect(color: vec4f, uv: vec2f) -> vec4f {
    let pulse = 0.55 + 0.45 * sin(mu.time * 3.0 + mu.phase);
    return vec4f(hueRotate(color.rgb, mu.hue * 6.28318) * pulse, color.a);
}
`;

export const HUE_GLSL = `
vec3 hueRotate(vec3 rgb, float angle) {
    vec3 k = vec3(0.57735);
    return rgb * cos(angle) + cross(k, rgb) * sin(angle) + k * dot(k, rgb) * (1.0 - cos(angle));
}
vec4 effect(vec4 color, vec2 uv) {
    float pulse = 0.55 + 0.45 * sin(mu.time * 3.0 + mu.phase);
    return vec4(hueRotate(color.rgb, mu.hue * 6.28318) * pulse, color.a);
}
`;
```
