# Combined FX

> Four materials using both hooks at once: a portal, a ghost, a crystal and a jelly.

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

Both mesh hooks composing into one look. Each material sets `vertex` and `fragment`: a rippling energy **portal**, a bobbing dissolving **ghost**, a spiking iridescent **crystal** and a squashing translucent **jelly**.

The fragment sees the displaced geometry (`ctx.worldPos` and the normal come after the vertex stage), so the two halves stay in step. All from `mu.time`.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Game } from './scenes/demo';
import { WIDTH, HEIGHT } from './settings';

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

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

`components/gallery.ts`:

```ts
import { createMaterial, createMesh, createText, getColor, useIcoSphereGeometry, useLoadBitmapFont, useTorusGeometry, useUpdate, useSphereGeometry } from 'nacatamalon';
import * as Settings from '../settings';
import { PORTAL_V, PORTAL_V_GLSL, PORTAL_F, PORTAL_F_GLSL, GHOST_V, GHOST_V_GLSL, GHOST_F, GHOST_F_GLSL, CRYSTAL_V, CRYSTAL_V_GLSL, CRYSTAL_F, CRYSTAL_F_GLSL, JELLY_V, JELLY_V_GLSL, JELLY_F, JELLY_F_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: 'PORTAL',
            tint: '#9040ff',
            geometry: useTorusGeometry({
                radius: 0.65,
                tube: 0.22
            }),
            vertex: PORTAL_V,
            vertexGlsl: PORTAL_V_GLSL,
            fragment: PORTAL_F,
            fragmentGlsl: PORTAL_F_GLSL,
        },
        {
            label: 'GHOST',
            tint: '#a0ffd0',
            geometry: useSphereGeometry(),
            vertex: GHOST_V,
            vertexGlsl: GHOST_V_GLSL,
            fragment: GHOST_F,
            fragmentGlsl: GHOST_F_GLSL,
        },
        {
            label: 'CRYSTAL',
            tint: '#b0c0ff',
            geometry: useIcoSphereGeometry({
                subdivisions: 2
            }),
            vertex: CRYSTAL_V,
            vertexGlsl: CRYSTAL_V_GLSL,
            fragment: CRYSTAL_F,
            fragmentGlsl: CRYSTAL_F_GLSL,
        },
        {
            label: 'JELLY',
            tint: '#3adb5a',
            geometry: useSphereGeometry(),
            vertex: JELLY_V,
            vertexGlsl: JELLY_V_GLSL,
            fragment: JELLY_F,
            fragmentGlsl: JELLY_F_GLSL,
        },
    ];

    shapes.forEach(({ label, tint, geometry, vertex, vertexGlsl, fragment, fragmentGlsl }, i) => {
        const column = i % 2;
        const row = Math.floor(i / 2);

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

        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: 'PORTAL',
            x: 118,
            y: 148
        },
        {
            text: 'GHOST',
            x: 306,
            y: 148
        },
        {
            text: 'CRYSTAL',
            x: 128,
            y: 286
        },
        {
            text: 'JELLY',
            x: 308,
            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, useSpawn, useCamera3d, useDirectionalLight } 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
// Both hooks at once. Each of these four moves its corners AND colours its pixels, and the two
// compose into one look rather than sitting side by side.
//
// The colour hook sees the move: ctx.worldPos and ctx.normal come from the corner AFTER the vertex
// hook ran, and the facing was measured again from the shape it became. That is what lets the
// portal's ripple catch its own light and the crystal's spikes read as spikes.
//
// Written twice, once per language, so all four run on both cards.

export const PORTAL_V = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    let angle = atan2(pos.z, pos.x);
    return pos + ctx.normal * sin(angle * 8.0 + mu.time * 4.0) * 0.05;
}
`;
export const PORTAL_V_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    float angle = atan(pos.z, pos.x);
    return pos + ctx.normal * sin(angle * 8.0 + mu.time * 4.0) * 0.05;
}
`;
export const PORTAL_F = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let swirl = sin(ctx.uv.x * 30.0 - mu.time * 6.0 + ctx.uv.y * 10.0);
    let glow = 0.5 + 0.5 * swirl;
    let col = mix(vec3<f32>(0.35, 0.0, 0.8), vec3<f32>(0.2, 0.9, 1.0), glow);
    return vec4<f32>(col * (0.6 + glow), 1.0);
}
`;
export const PORTAL_F_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    float swirl = sin(ctx.uv.x * 30.0 - mu.time * 6.0 + ctx.uv.y * 10.0);
    float glow = 0.5 + 0.5 * swirl;
    vec3 col = mix(vec3(0.35, 0.0, 0.8), vec3(0.2, 0.9, 1.0), glow);
    return vec4(col * (0.6 + glow), 1.0);
}
`;
export const GHOST_V = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    var p = pos;
    p.y = p.y + sin(mu.time * 2.0) * 0.1;
    return p + ctx.normal * sin(pos.y * 6.0 + mu.time * 3.0) * 0.03;
}
`;
export const GHOST_V_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    vec3 p = pos;
    p.y = p.y + sin(mu.time * 2.0) * 0.1;
    return p + ctx.normal * sin(pos.y * 6.0 + mu.time * 3.0) * 0.03;
}
`;
export const GHOST_F = `
fn hash(p: vec2<f32>) -> f32 { return fract(sin(dot(p, vec2<f32>(127.1, 311.7))) * 43758.5453); }
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let fresnel = pow(1.0 - max(dot(normalize(ctx.normal), ctx.viewDir), 0.0), 2.0);
    let speck = hash(floor(ctx.uv * 30.0));
    let shimmer = 0.55 + 0.35 * sin(mu.time * 1.5 + speck * 6.28);
    let col = vec3<f32>(0.6, 1.0, 0.8) * (fresnel + 0.6);
    return vec4<f32>(col, shimmer * (0.5 + 0.5 * fresnel));
}
`;
export const GHOST_F_GLSL = `
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec4 effect(vec4 surface, FragContext ctx) {
    float fresnel = pow(1.0 - max(dot(normalize(ctx.normal), ctx.viewDir), 0.0), 2.0);
    float speck = hash(floor(ctx.uv * 30.0));
    float shimmer = 0.55 + 0.35 * sin(mu.time * 1.5 + speck * 6.28);
    vec3 col = vec3(0.6, 1.0, 0.8) * (fresnel + 0.6);
    return vec4(col, shimmer * (0.5 + 0.5 * fresnel));
}
`;
export const CRYSTAL_V = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    let spike = 0.08 * (0.5 + 0.5 * sin(mu.time * 3.0 + pos.y * 8.0));
    return pos + ctx.normal * spike;
}
`;
export const CRYSTAL_V_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    float spike = 0.08 * (0.5 + 0.5 * sin(mu.time * 3.0 + pos.y * 8.0));
    return pos + ctx.normal * spike;
}
`;
export const CRYSTAL_F = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let fresnel = 1.0 - max(dot(normalize(ctx.normal), ctx.viewDir), 0.0);
    let t = fresnel * 5.0 + mu.time;
    let sheen = 0.5 + 0.5 * cos(vec3<f32>(t, t + 2.094, t + 4.188));
    return vec4<f32>(surface.rgb * ctx.light * 0.5 + sheen * fresnel * 1.2 + sheen * 0.2, 1.0);
}
`;
export const CRYSTAL_F_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    float fresnel = 1.0 - max(dot(normalize(ctx.normal), ctx.viewDir), 0.0);
    float t = fresnel * 5.0 + mu.time;
    vec3 sheen = 0.5 + 0.5 * cos(vec3(t, t + 2.094, t + 4.188));
    return vec4(surface.rgb * ctx.light * 0.5 + sheen * fresnel * 1.2 + sheen * 0.2, 1.0);
}
`;
export const JELLY_V = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    let squash = 1.0 + 0.12 * sin(mu.time * 4.0);
    let p = vec3<f32>(pos.x / squash, pos.y * squash, pos.z / squash);
    return p + ctx.normal * sin(pos.x * 5.0 + pos.y * 5.0 + mu.time * 5.0) * 0.02;
}
`;
export const JELLY_V_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    float squash = 1.0 + 0.12 * sin(mu.time * 4.0);
    vec3 p = vec3(pos.x / squash, pos.y * squash, pos.z / squash);
    return p + ctx.normal * sin(pos.x * 5.0 + pos.y * 5.0 + mu.time * 5.0) * 0.02;
}
`;
export const JELLY_F = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let fresnel = pow(1.0 - max(dot(normalize(ctx.normal), ctx.viewDir), 0.0), 1.5);
    let core = surface.rgb * ctx.light + vec3<f32>(0.1, 0.3, 0.1);
    return vec4<f32>(core + vec3<f32>(0.4, 1.0, 0.3) * fresnel, 0.75 + 0.25 * fresnel);
}
`;
export const JELLY_F_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    float fresnel = pow(1.0 - max(dot(normalize(ctx.normal), ctx.viewDir), 0.0), 1.5);
    vec3 core = surface.rgb * ctx.light + vec3(0.1, 0.3, 0.1);
    return vec4(core + vec3(0.4, 1.0, 0.3) * fresnel, 0.75 + 0.25 * fresnel);
}
`;
```
