# Shader effects

> A gallery of materials: dissolve, hologram, posterize, ghost, flash, hue, wave and chromatic aberration, each a single function.

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

A gallery of custom-material effects: dissolve, hologram, posterize, ghost fade, hit flash and hue cycle, plus **wave** and **chromatic** distortion, which call `sampleTexture(uv)` to read the texture again at shifted coordinates.

Each is a single `fn effect(color, uv)` passed to `createMaterial`, and all animate purely from the automatically injected `mu.time`: no `useUpdate`, no per-frame wiring. An effect is a function; reusing it is reusing the string.

## 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: '#08080f',
});

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

`components/gallery.ts`:

```ts
import { createMaterial, createSprite, createText, useLoadBitmapFont, useLoadTexture } from 'nacatamalon';

import { EFFECTS } from '../data/effects';

export const Gallery = () => {
    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',
    });

    // Four across and two down. Each cell is a sprite with its own effect, and a label.
    EFFECTS.forEach(({ label, wgsl, glsl }, i) => {
        const x = 8 + (i % 4) * 118;
        const y = 30 + Math.floor(i / 4) * 150;

        const material = createMaterial({
            name: label,
            fragment: wgsl,
            fragmentGlsl: glsl
        });

        createSprite({
            texture,
            material,
            width: 96,
            height: 96,
            // Laid out by its corner, like the labels under it. The default is the middle, which is
            // what a character standing somewhere wants and not what a grid of panels does.
            anchor: {
                x: 0,
                y: 0
            },
            transform: {
                x,
                y,
                rotation: 0,
                scaleX: 1,
                scaleY: 1
            },
        });

        createText({
            text: label,
            font,
            tint: '#8fb7ff',
            transform: {
                x: x + 2,
                y: y + 102,
                rotation: 0,
                scaleX: 1,
                scaleY: 1
            },
        });
    });
};
```

`data/effects.ts`:

```ts
import { DISSOLVE, DISSOLVE_GLSL, HOLOGRAM, HOLOGRAM_GLSL, POSTERIZE, POSTERIZE_GLSL, GHOST, GHOST_GLSL, FLASH, FLASH_GLSL, HUE, HUE_GLSL, WAVE, WAVE_GLSL, CHROMATIC, CHROMATIC_GLSL } from '../shaders/effects';

export const EFFECTS = [
    {
        label: 'DISSOLVE',
        wgsl: DISSOLVE,
        glsl: DISSOLVE_GLSL
    },
    {
        label: 'HOLOGRAM',
        wgsl: HOLOGRAM,
        glsl: HOLOGRAM_GLSL
    },
    {
        label: 'POSTERIZE',
        wgsl: POSTERIZE,
        glsl: POSTERIZE_GLSL
    },
    {
        label: 'GHOST',
        wgsl: GHOST,
        glsl: GHOST_GLSL
    },
    {
        label: 'FLASH',
        wgsl: FLASH,
        glsl: FLASH_GLSL
    },
    {
        label: 'HUE',
        wgsl: HUE,
        glsl: HUE_GLSL
    },
    {
        label: 'WAVE',
        wgsl: WAVE,
        glsl: WAVE_GLSL
    },
    {
        label: 'CHROMATIC',
        wgsl: CHROMATIC,
        glsl: CHROMATIC_GLSL
    },
];
```

`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;
```

`shaders/effects.ts`:

```ts
// A small library of effects. Each one is a single function:
//
//   fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32>
//
// and 'color' arrives already read from the picture and already multiplied by the sprite's own
// colour, so the ordinary case is one line. The last two go further and call sampleTexture(uv) to
// read the picture AGAIN at a moved place, which is the only way to bend an image from a fragment.
//
// They all animate from mu.time, which the engine fills in, so none of them needs a line of
// per-frame code. Note that the picture is on each SPRITE and the effect is on the material: the
// material is the effect and nothing else, and that is what lets one be shared.
//
// Each is written twice, once per language, so the gallery is the same on both cards.

export const DISSOLVE = `
fn hash(p: vec2<f32>) -> f32 {
    return fract(sin(dot(p, vec2<f32>(127.1, 311.7))) * 43758.5453);
}
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let t = fract(mu.time * 0.35);
    let e = hash(floor(uv * 22.0)) - t;
    if (e < 0.0) { return vec4<f32>(0.0, 0.0, 0.0, 0.0); }
    let burn = smoothstep(0.0, 0.14, e);
    return vec4<f32>(mix(vec3<f32>(1.0, 0.55, 0.12), color.rgb, burn), color.a);
}
`;
export const DISSOLVE_GLSL = `
float hash(vec2 p) {
    return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
vec4 effect(vec4 color, vec2 uv) {
    float t = fract(mu.time * 0.35);
    float e = hash(floor(uv * 22.0)) - t;
    if (e < 0.0) { return vec4(0.0); }
    float burn = smoothstep(0.0, 0.14, e);
    return vec4(mix(vec3(1.0, 0.55, 0.12), color.rgb, burn), color.a);
}
`;
export const HOLOGRAM = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let scan = 0.6 + 0.4 * sin(uv.y * 80.0 - mu.time * 8.0);
    let flicker = 0.8 + 0.2 * sin(mu.time * 50.0);
    let lum = dot(color.rgb, vec3<f32>(0.299, 0.587, 0.114));
    return vec4<f32>(vec3<f32>(0.25, 0.9, 1.0) * lum * scan * flicker, color.a * 0.85);
}
`;
export const HOLOGRAM_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    float scan = 0.6 + 0.4 * sin(uv.y * 80.0 - mu.time * 8.0);
    float flicker = 0.8 + 0.2 * sin(mu.time * 50.0);
    float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));
    return vec4(vec3(0.25, 0.9, 1.0) * lum * scan * flicker, color.a * 0.85);
}
`;
export const POSTERIZE = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let levels = floor(2.0 + 3.0 * (0.5 + 0.5 * sin(mu.time)));
    return vec4<f32>(floor(color.rgb * levels) / levels, color.a);
}
`;
export const POSTERIZE_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    float levels = floor(2.0 + 3.0 * (0.5 + 0.5 * sin(mu.time)));
    return vec4(floor(color.rgb * levels) / levels, color.a);
}
`;
export const GHOST = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    return vec4<f32>(color.rgb, color.a * (0.5 + 0.5 * sin(mu.time * 1.5)));
}
`;
export const GHOST_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    return vec4(color.rgb, color.a * (0.5 + 0.5 * sin(mu.time * 1.5)));
}
`;
export const FLASH = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let f = pow(0.5 + 0.5 * sin(mu.time * 8.0), 3.0);
    return vec4<f32>(mix(color.rgb, vec3<f32>(1.0), f), color.a);
}
`;
export const FLASH_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    float f = pow(0.5 + 0.5 * sin(mu.time * 8.0), 3.0);
    return vec4(mix(color.rgb, vec3(1.0), f), color.a);
}
`;
// Turning the colour about the grey axis, which is what a hue shift is.
export const HUE = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let k = vec3<f32>(0.57735);
    let a = mu.time;
    let rgb = color.rgb * cos(a) + cross(k, color.rgb) * sin(a) + k * dot(k, color.rgb) * (1.0 - cos(a));
    return vec4<f32>(clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0)), color.a);
}
`;
export const HUE_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    vec3 k = vec3(0.57735);
    float a = mu.time;
    vec3 rgb = color.rgb * cos(a) + cross(k, color.rgb) * sin(a) + k * dot(k, color.rgb) * (1.0 - cos(a));
    return vec4(clamp(rgb, 0.0, 1.0), color.a);
}
`;
export const WAVE = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let offset = vec2<f32>(sin(uv.y * 14.0 + mu.time * 3.0) * 0.03, 0.0);
    return sampleTexture(uv + offset);
}
`;
export const WAVE_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    vec2 offset = vec2(sin(uv.y * 14.0 + mu.time * 3.0) * 0.03, 0.0);
    return sampleTexture(uv + offset);
}
`;
export const CHROMATIC = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let d = vec2<f32>(0.02 * (0.5 + 0.5 * sin(mu.time * 2.0)), 0.0);
    let middle = sampleTexture(uv);
    return vec4<f32>(sampleTexture(uv + d).r, middle.g, sampleTexture(uv - d).b, middle.a);
}
`;
export const CHROMATIC_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    vec2 d = vec2(0.02 * (0.5 + 0.5 * sin(mu.time * 2.0)), 0.0);
    vec4 middle = sampleTexture(uv);
    return vec4(sampleTexture(uv + d).r, middle.g, sampleTexture(uv - d).b, middle.a);
}
`;
```
