# Shader effects II

> A second batch on sampleTexture(uv): glitch, swirl, blur, ripples, CRT warp, edges, old film and kaleidoscope.

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

A second batch, built on `sampleTexture(uv)`: glitch tears, swirl, a 5×5 box blur (sampling in a loop), water ripples, CRT barrel warp, Sobel edge detection, old-film grain and a kaleidoscope.

Each is one `fn effect(color, uv)` in `createMaterial`, all animating from `mu.time`.

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

    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 { GLITCH, GLITCH_GLSL, SWIRL, SWIRL_GLSL, BLUR, BLUR_GLSL, RIPPLE, RIPPLE_GLSL, FILM, FILM_GLSL, CRT, CRT_GLSL, EDGE, EDGE_GLSL, KALEIDO, KALEIDO_GLSL } from '../shaders/effects';

export const EFFECTS = [
    {
        label: 'GLITCH',
        wgsl: GLITCH,
        glsl: GLITCH_GLSL
    },
    {
        label: 'SWIRL',
        wgsl: SWIRL,
        glsl: SWIRL_GLSL
    },
    {
        label: 'BLUR',
        wgsl: BLUR,
        glsl: BLUR_GLSL
    },
    {
        label: 'RIPPLE',
        wgsl: RIPPLE,
        glsl: RIPPLE_GLSL
    },
    {
        label: 'FILM',
        wgsl: FILM,
        glsl: FILM_GLSL
    },
    {
        label: 'CRT',
        wgsl: CRT,
        glsl: CRT_GLSL
    },
    {
        label: 'EDGE',
        wgsl: EDGE,
        glsl: EDGE_GLSL
    },
    {
        label: 'KALEIDO',
        wgsl: KALEIDO,
        glsl: KALEIDO_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 second batch, leaning on sampleTexture(uv), which reads the picture again at a place of your
// choosing. That one call is what makes bending an image possible from a fragment: tears, a twist,
// a blur (reading in a loop), ripples, the bulge of a tube, an outline, a kaleidoscope.
//
// Nothing here needs a line of per-frame code: they all ride mu.time. Each is written twice, once
// per language, so the gallery is the same on both cards.

export const GLITCH = `
fn hash(p: f32) -> f32 { return fract(sin(p * 78.233) * 43758.5453); }
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let row = floor(uv.y * 12.0);
    let t = floor(mu.time * 8.0);
    let shift = (hash(row + t) - 0.5) * 0.12 * step(0.7, hash(row * 1.3 + t));
    let d = 0.012;
    let base = uv + vec2<f32>(shift, 0.0);
    let middle = sampleTexture(base);
    return vec4<f32>(
        sampleTexture(base + vec2<f32>(d, 0.0)).r,
        middle.g,
        sampleTexture(base - vec2<f32>(d, 0.0)).b,
        middle.a);
}
`;
export const GLITCH_GLSL = `
float hash(float p) { return fract(sin(p * 78.233) * 43758.5453); }
vec4 effect(vec4 color, vec2 uv) {
    float row = floor(uv.y * 12.0);
    float t = floor(mu.time * 8.0);
    float shift = (hash(row + t) - 0.5) * 0.12 * step(0.7, hash(row * 1.3 + t));
    float d = 0.012;
    vec2 base = uv + vec2(shift, 0.0);
    vec4 middle = sampleTexture(base);
    return vec4(
        sampleTexture(base + vec2(d, 0.0)).r,
        middle.g,
        sampleTexture(base - vec2(d, 0.0)).b,
        middle.a);
}
`;
export const SWIRL = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let c = uv - vec2<f32>(0.5);
    let r = length(c);
    let angle = atan2(c.y, c.x) + (0.6 - r) * 4.0 * sin(mu.time);
    return sampleTexture(vec2<f32>(0.5) + vec2<f32>(cos(angle), sin(angle)) * r);
}
`;
export const SWIRL_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    vec2 c = uv - vec2(0.5);
    float r = length(c);
    float angle = atan(c.y, c.x) + (0.6 - r) * 4.0 * sin(mu.time);
    return sampleTexture(vec2(0.5) + vec2(cos(angle), sin(angle)) * r);
}
`;
// Twenty-five reads for one pixel. Cheap on one sprite, and the reason a full-screen blur belongs
// in a post pass rather than on every sprite that wants one.
export const BLUR = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let radius = 0.004 * (1.5 + sin(mu.time));
    var sum = vec4<f32>(0.0);
    for (var i = -2; i <= 2; i = i + 1) {
        for (var j = -2; j <= 2; j = j + 1) {
            sum = sum + sampleTexture(uv + vec2<f32>(f32(i), f32(j)) * radius);
        }
    }
    return sum / 25.0;
}
`;
export const BLUR_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    float radius = 0.004 * (1.5 + sin(mu.time));
    vec4 sum = vec4(0.0);
    for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
            sum = sum + sampleTexture(uv + vec2(float(i), float(j)) * radius);
        }
    }
    return sum / 25.0;
}
`;
export const RIPPLE = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let c = uv - vec2<f32>(0.5);
    let r = length(c);
    let direction = c / max(r, 0.0001);
    let wave = sin(r * 40.0 - mu.time * 5.0) * 0.02 * (1.0 - r);
    return sampleTexture(uv + direction * wave);
}
`;
export const RIPPLE_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    vec2 c = uv - vec2(0.5);
    float r = length(c);
    vec2 direction = c / max(r, 0.0001);
    float wave = sin(r * 40.0 - mu.time * 5.0) * 0.02 * (1.0 - r);
    return sampleTexture(uv + direction * wave);
}
`;
export const FILM = `
fn hash2(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 lum = dot(color.rgb, vec3<f32>(0.299, 0.587, 0.114));
    let sepia = vec3<f32>(lum) * vec3<f32>(1.1, 0.95, 0.72);
    let grain = (hash2(uv * 400.0 + mu.time) - 0.5) * 0.25;
    let flicker = 0.9 + 0.1 * sin(mu.time * 30.0);
    return vec4<f32>((sepia + grain) * flicker, color.a);
}
`;
export const FILM_GLSL = `
float hash2(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec4 effect(vec4 color, vec2 uv) {
    float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));
    vec3 sepia = vec3(lum) * vec3(1.1, 0.95, 0.72);
    float grain = (hash2(uv * 400.0 + mu.time) - 0.5) * 0.25;
    float flicker = 0.9 + 0.1 * sin(mu.time * 30.0);
    return vec4((sepia + grain) * flicker, color.a);
}
`;
export const CRT = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let c = uv - vec2<f32>(0.5);
    let w = uv + c * dot(c, c) * 0.4;
    if (w.x < 0.0 || w.x > 1.0 || w.y < 0.0 || w.y > 1.0) { return vec4<f32>(0.0, 0.0, 0.0, color.a); }
    let read = sampleTexture(w);
    let banded = read.rgb * (0.7 + 0.3 * sin(w.y * 120.0));
    return vec4<f32>(banded * (1.0 - dot(c, c) * 1.2), read.a);
}
`;
export const CRT_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    vec2 c = uv - vec2(0.5);
    vec2 w = uv + c * dot(c, c) * 0.4;
    if (w.x < 0.0 || w.x > 1.0 || w.y < 0.0 || w.y > 1.0) { return vec4(0.0, 0.0, 0.0, color.a); }
    vec4 read = sampleTexture(w);
    vec3 banded = read.rgb * (0.7 + 0.3 * sin(w.y * 120.0));
    return vec4(banded * (1.0 - dot(c, c) * 1.2), read.a);
}
`;
export const EDGE = `
fn lum(p: vec2<f32>) -> f32 { return dot(sampleTexture(p).rgb, vec3<f32>(0.299, 0.587, 0.114)); }
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let e = 0.006;
    let gx = lum(uv + vec2<f32>(e, 0.0)) - lum(uv - vec2<f32>(e, 0.0));
    let gy = lum(uv + vec2<f32>(0.0, e)) - lum(uv - vec2<f32>(0.0, e));
    let edge = clamp(sqrt(gx * gx + gy * gy) * 4.0, 0.0, 1.0);
    let pulse = 0.5 + 0.5 * sin(mu.time * 2.0);
    return vec4<f32>(mix(color.rgb * 0.12, vec3<f32>(0.2, 1.0, 0.5), edge * pulse), color.a);
}
`;
export const EDGE_GLSL = `
float lum(vec2 p) { return dot(sampleTexture(p).rgb, vec3(0.299, 0.587, 0.114)); }
vec4 effect(vec4 color, vec2 uv) {
    float e = 0.006;
    float gx = lum(uv + vec2(e, 0.0)) - lum(uv - vec2(e, 0.0));
    float gy = lum(uv + vec2(0.0, e)) - lum(uv - vec2(0.0, e));
    float edge = clamp(sqrt(gx * gx + gy * gy) * 4.0, 0.0, 1.0);
    float pulse = 0.5 + 0.5 * sin(mu.time * 2.0);
    return vec4(mix(color.rgb * 0.12, vec3(0.2, 1.0, 0.5), edge * pulse), color.a);
}
`;
export const KALEIDO = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let c = uv - vec2<f32>(0.5);
    let r = length(c);
    let segment = 6.2831853 / 6.0;
    var a = atan2(c.y, c.x) + mu.time * 0.3;
    a = abs(a - segment * floor(a / segment + 0.5));
    return sampleTexture(vec2<f32>(0.5) + vec2<f32>(cos(a), sin(a)) * r);
}
`;
export const KALEIDO_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    vec2 c = uv - vec2(0.5);
    float r = length(c);
    float segment = 6.2831853 / 6.0;
    float a = atan(c.y, c.x) + mu.time * 0.3;
    a = abs(a - segment * floor(a / segment + 0.5));
    return sampleTexture(vec2(0.5) + vec2(cos(a), sin(a)) * r);
}
`;
```
