# 3D effects II

> Another surface batch: procedural lava, a scan band, a thermal palette and a 1-bit Bayer dither.

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

A second batch of `mesh3d` surface shaders: procedural lava from `ctx.worldPos`, a scan band riding `ctx.uv.y`, a thermal palette from the `ctx.light` term, and an ordered 4×4 Bayer dither for a Game Boy 1-bit look. All animate 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: '#07070f',
});

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

`components/gallery.ts`:

```ts
import { createMaterial, createMesh, createText, getColor, useCylinderGeometry, useIcoSphereGeometry, useLoadBitmapFont, useTorusGeometry, useUpdate, useSphereGeometry } from 'nacatamalon';
import * as Settings from '../settings';
import { LAVA, LAVA_GLSL, SCAN, SCAN_GLSL, HEAT, HEAT_GLSL, DITHER, DITHER_GLSL } from '../shaders/effects';

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

    const shapes = [
        {
            label: 'LAVA',
            wgsl: LAVA,
            glsl: LAVA_GLSL,
            tint: '#ff5522',
            geometry: useIcoSphereGeometry({
                subdivisions: 2
            })
        },
        {
            label: 'SCAN',
            wgsl: SCAN,
            glsl: SCAN_GLSL,
            tint: '#88aadd',
            geometry: useCylinderGeometry({
                radius: 0.5,
                height: 1.2
            })
        },
        {
            label: 'HEAT',
            wgsl: HEAT,
            glsl: HEAT_GLSL,
            tint: '#dddddd',
            geometry: useSphereGeometry()
        },
        {
            label: 'DITHER',
            wgsl: DITHER,
            glsl: DITHER_GLSL,
            tint: '#cccccc',
            geometry: useTorusGeometry({
                radius: 0.6,
                tube: 0.24
            })
        },
    ];

    shapes.forEach(({ label, wgsl, glsl, tint, geometry }, i) => {
        const column = i % 2;
        const row = Math.floor(i / 2);

        const material = createMaterial({
            name: label,
            shader: 'mesh3d',
            tint: getColor(tint),
            fragment: wgsl,
            fragmentGlsl: glsl,
        });

        const mesh = createMesh({
            geometry,
            material,
            transform: {
                x: (column - 0.5) * Settings.SPACING,
                y: (0.5 - row) * Settings.SPACING
            },
        });

        useUpdate((delta) => {
            mesh.transform.rotationY += delta * 0.7;
            mesh.transform.rotationX += delta * 0.25;
        });
    });

    const labels = [
        {
            text: 'LAVA',
            x: 124,
            y: 148
        },
        {
            text: 'SCAN',
            x: 304,
            y: 148
        },
        {
            text: 'HEAT',
            x: 144,
            y: 286
        },
        {
            text: 'DITHER',
            x: 300,
            y: 286
        },
    ];

    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: 50,
        transform: {
            z: 5.5
        }
    });
    useDirectionalLight({
        color: '#fff0d8',
        intensity: 1.1,
        ambient: 0.2,
        transform: {
            rotationX: -0.5,
            rotationY: 0.6
        }
    });

    useSpawn(Gallery)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const SPACING = 2;
```

`shaders/effects.ts`:

```ts
// A second batch, on the same contract: fn effect(surface, ctx). What changes is which part of ctx
// each one leans on. Lava reads where it is in the world, so the pattern belongs to the object and
// not to the screen. Scan reads its place on the picture. Heat reads what the lamps added up to.
// Dither reads the picture place again, and throws away everything but one bit.
//
// All four ride mu.time, and each is written twice so it runs on both cards.

export const LAVA = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let p = ctx.worldPos;
    let n = sin(p.x * 6.0 + mu.time) + sin(p.y * 5.0 - mu.time * 1.3) + sin(p.z * 7.0 + mu.time * 0.7);
    let v = 0.5 + 0.5 * sin(n * 1.5 + mu.time);
    return vec4<f32>(vec3<f32>(1.0, v * 0.55, v * v * 0.25) * (0.5 + 0.7 * v), surface.a);
}
`;
export const LAVA_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    vec3 p = ctx.worldPos;
    float n = sin(p.x * 6.0 + mu.time) + sin(p.y * 5.0 - mu.time * 1.3) + sin(p.z * 7.0 + mu.time * 0.7);
    float v = 0.5 + 0.5 * sin(n * 1.5 + mu.time);
    return vec4(vec3(1.0, v * 0.55, v * v * 0.25) * (0.5 + 0.7 * v), surface.a);
}
`;
export const SCAN = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let lit = surface.rgb * ctx.light;
    let sweep = fract(mu.time * 0.35);
    let band = smoothstep(0.06, 0.0, abs(ctx.uv.y - sweep));
    let reveal = step(ctx.uv.y, sweep) * 0.5 + 0.5;
    return vec4<f32>(lit * reveal + vec3<f32>(0.2, 1.0, 0.6) * band, surface.a);
}
`;
export const SCAN_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    vec3 lit = surface.rgb * ctx.light;
    float sweep = fract(mu.time * 0.35);
    float band = smoothstep(0.06, 0.0, abs(ctx.uv.y - sweep));
    float reveal = step(ctx.uv.y, sweep) * 0.5 + 0.5;
    return vec4(lit * reveal + vec3(0.2, 1.0, 0.6) * band, surface.a);
}
`;
export const HEAT = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let t = clamp(dot(surface.rgb * ctx.light, vec3<f32>(0.333)) * 1.4 + 0.1 * sin(mu.time * 2.0), 0.0, 1.0);
    let iron = clamp(vec3<f32>(t * 3.0, t * 3.0 - 1.0, t * 3.0 - 2.0), vec3<f32>(0.0), vec3<f32>(1.0));
    return vec4<f32>(iron, surface.a);
}
`;
export const HEAT_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    float t = clamp(dot(surface.rgb * ctx.light, vec3(0.333)) * 1.4 + 0.1 * sin(mu.time * 2.0), 0.0, 1.0);
    vec3 iron = clamp(vec3(t * 3.0, t * 3.0 - 1.0, t * 3.0 - 2.0), 0.0, 1.0);
    return vec4(iron, surface.a);
}
`;
// An ordered 4x4 threshold, which is the trick a Game Boy used to fake shades it did not have.
export const DITHER = `
fn bayer(p: vec2<f32>) -> f32 {
    let x = i32(p.x) & 3;
    let y = i32(p.y) & 3;
    let m = array<f32, 16>(
        0.0, 8.0, 2.0, 10.0,
        12.0, 4.0, 14.0, 6.0,
        3.0, 11.0, 1.0, 9.0,
        15.0, 7.0, 13.0, 5.0);
    return m[y * 4 + x] / 16.0;
}
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let l = dot(surface.rgb * ctx.light, vec3<f32>(0.299, 0.587, 0.114));
    let bit = step(bayer(ctx.uv * 64.0), l);
    return vec4<f32>(mix(vec3<f32>(0.06, 0.22, 0.06), vec3<f32>(0.55, 0.9, 0.35), bit), surface.a);
}
`;
export const DITHER_GLSL = `
float bayer(vec2 p) {
    int x = int(p.x) & 3;
    int y = int(p.y) & 3;
    float m[16] = float[16](
        0.0, 8.0, 2.0, 10.0,
        12.0, 4.0, 14.0, 6.0,
        3.0, 11.0, 1.0, 9.0,
        15.0, 7.0, 13.0, 5.0);
    return m[y * 4 + x] / 16.0;
}
vec4 effect(vec4 surface, FragContext ctx) {
    float l = dot(surface.rgb * ctx.light, vec3(0.299, 0.587, 0.114));
    float bit = step(bayer(ctx.uv * 64.0), l);
    return vec4(mix(vec3(0.06, 0.22, 0.06), vec3(0.55, 0.9, 0.35), bit), surface.a);
}
`;
```
