# Vertex effects

> The vertex half of a mesh material: PS1-style jitter, inflate, twist and wave, with lighting untouched.

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

The vertex half of a mesh material. Where `effect` recolours pixels, `fn vertex(pos, ctx)` moves geometry, returning a changed local position. Here: PS1-style **jitter**, normal-based **inflate**, a height-driven **twist**, and a **wave**.

Each sets only `vertex` (no `fragment`), so the surface still lights normally while the shape deforms. All 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: '#07070d',
});

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

`components/gallery.ts`:

```ts
import { createMaterial, createMesh, createText, getColor, useCubeGeometry, useIcoSphereGeometry, useLoadBitmapFont, useTorusGeometry, useUpdate, useSphereGeometry } from 'nacatamalon';
import * as Settings from '../settings';
import { JITTER, JITTER_GLSL, INFLATE, INFLATE_GLSL, TWIST, TWIST_GLSL, WAVE, WAVE_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: 'JITTER',
            wgsl: JITTER,
            glsl: JITTER_GLSL,
            tint: '#ff6b6b',
            geometry: useIcoSphereGeometry({
                subdivisions: 2
            })
        },
        {
            label: 'INFLATE',
            wgsl: INFLATE,
            glsl: INFLATE_GLSL,
            tint: '#4cff91',
            geometry: useSphereGeometry()
        },
        {
            label: 'TWIST',
            wgsl: TWIST,
            glsl: TWIST_GLSL,
            tint: '#60a5fa',
            geometry: useCubeGeometry({
                width: 0.9,
                height: 1,
                depth: 0.9
            })
        },
        {
            label: 'WAVE',
            wgsl: WAVE,
            glsl: WAVE_GLSL,
            tint: '#ffb347',
            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);

        // A material with only a corner hook: the shape moves, the surface lights as it always did.
        const material = createMaterial({
            name: label,
            shader: 'mesh3d',
            tint: getColor(tint),
            vertex: wgsl,
            vertexGlsl: 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.6;
            mesh.transform.rotationX += delta * 0.2;
        });
    });

    const labels = [
        {
            text: 'JITTER',
            x: 120,
            y: 148
        },
        {
            text: 'INFLATE',
            x: 288,
            y: 148
        },
        {
            text: 'TWIST',
            x: 140,
            y: 286
        },
        {
            text: 'WAVE',
            x: 316,
            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
// The corner hook, on its own. Where 'effect' decides what colour a place ends up,
//
//   fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32>
//
// decides where a corner really goes, and hands back a position in the model's own space. ctx
// carries which way the surface faces and its place on the picture; mu.time animates it.
//
// None of these four writes a colour hook, so the engine's own stands in and the surface lights
// exactly as an ordinary model does. The engine also re-measures which way the surface faces after
// the corners moved, by sampling the hook twice more a hair away, so what you see is the new shape
// being lit and not the old one.
//
// Written twice, once per language, so all four run on both cards.

export const JITTER = `
fn hash3(p: vec3<f32>) -> vec3<f32> {
    let q = vec3<f32>(
        dot(p, vec3<f32>(127.1, 311.7, 74.7)),
        dot(p, vec3<f32>(269.5, 183.3, 246.1)),
        dot(p, vec3<f32>(113.5, 271.9, 124.6)));
    return fract(sin(q) * 43758.5453) - 0.5;
}
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    let t = floor(mu.time * 12.0);
    return pos + hash3(pos * 8.0 + t) * 0.06;
}
`;
export const JITTER_GLSL = `
vec3 hash3(vec3 p) {
    vec3 q = vec3(
        dot(p, vec3(127.1, 311.7, 74.7)),
        dot(p, vec3(269.5, 183.3, 246.1)),
        dot(p, vec3(113.5, 271.9, 124.6)));
    return fract(sin(q) * 43758.5453) - 0.5;
}
vec3 vertex(vec3 pos, VertContext ctx) {
    float t = floor(mu.time * 12.0);
    return pos + hash3(pos * 8.0 + t) * 0.06;
}
`;
export const INFLATE = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    let amount = 0.18 * (0.5 + 0.5 * sin(mu.time * 2.0));
    return pos + ctx.normal * amount;
}
`;
export const INFLATE_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    float amount = 0.18 * (0.5 + 0.5 * sin(mu.time * 2.0));
    return pos + ctx.normal * amount;
}
`;
export const TWIST = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    let angle = pos.y * 3.0 * sin(mu.time);
    let c = cos(angle);
    let s = sin(angle);
    return vec3<f32>(pos.x * c - pos.z * s, pos.y, pos.x * s + pos.z * c);
}
`;
export const TWIST_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    float angle = pos.y * 3.0 * sin(mu.time);
    float c = cos(angle);
    float s = sin(angle);
    return vec3(pos.x * c - pos.z * s, pos.y, pos.x * s + pos.z * c);
}
`;
export const WAVE = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    let d = sin(pos.x * 4.0 + mu.time * 3.0) * 0.12 + sin(pos.z * 5.0 + mu.time * 2.0) * 0.08;
    return vec3<f32>(pos.x, pos.y + d, pos.z);
}
`;
export const WAVE_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    float d = sin(pos.x * 4.0 + mu.time * 3.0) * 0.12 + sin(pos.z * 5.0 + mu.time * 2.0) * 0.08;
    return vec3(pos.x, pos.y + d, pos.z);
}
`;
```
