# 2D jelly

> LocoRoco style blobs: the vertex hook wobbles the rim and the fragment paints colour, gloss and a face.

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

A jelly blob's charm is that its silhouette deforms, and that is vertex work, not a fragment trick. So each blob is a flat disc facing the camera: the `vertex` hook wobbles the rim and squashes and stretches it (real soft-body geometry), while the `fragment` paints colour, gloss and a face.

`squash` and `center` are uniforms driven live from each blob's bounce.

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

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

`components/blob.ts`:

```ts
import { createMaterial, createMesh, getColor, useCircleGeometry, useUpdate } from 'nacatamalon';

import { JELLY_V, JELLY_V_GLSL, JELLY_F, JELLY_F_GLSL } from '../shaders/jelly';

export const Blob = ({ tint, rgb, i, geometry }: { tint: string; rgb: number[]; i: number; geometry: ReturnType<typeof useCircleGeometry>; }) => {
    const x = (i - 1.5) * 1.7;

    const uniforms = {
        blobColor: rgb,
        center: [x, 0],
        squash: 1
    };

    const material = createMaterial({
        name: 'jelly-' + i,
        shader: 'mesh3d',
        tint: getColor(tint),
        vertex: JELLY_V,
        vertexGlsl: JELLY_V_GLSL,
        fragment: JELLY_F,
        fragmentGlsl: JELLY_F_GLSL,
        uniforms,
    });

    // rotationX turns the flat disc to face the camera: it is built lying down in XZ.
    const blob = createMesh({
        geometry,
        material,
        transform: {
            x,
            y: -1.3,
            rotationX: Math.PI / 2
        },
    });

    useUpdate((_delta, time) => {
        const clock = time + i * 1.1;
        const bounce = Math.abs(Math.sin(clock * 3));

        blob.transform.y = -1.3 + bounce * 1.4;
        uniforms.squash = 1 + (1 - bounce) * 0.4;
        uniforms.center[1] = blob.transform.y;
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useCircleGeometry, useSpawn } from 'nacatamalon';
import * as Settings from '../settings';

import { Blob } from '../components/blob';

export const Game = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        transform: {
            z: 6
        }
    });

    const geometry = useCircleGeometry({
        radius: 0.5,
        segments: 48
    });

    Settings.BLOBS.forEach((blob, i) => useSpawn(Blob)({
        ...blob,
        i,
        geometry
    }));

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const BLOBS = [
    {
        tint: '#ffd21a',
        rgb: [1, 0.82, 0.1]
    },
    {
        tint: '#ff5aa0',
        rgb: [1, 0.35, 0.63]
    },
    {
        tint: '#4aa0ff',
        rgb: [0.29, 0.63, 1]
    },
    {
        tint: '#7ade4a',
        rgb: [0.48, 0.87, 0.29]
    },
];
```

`shaders/jelly.ts`:

```ts
export const JELLY_V = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    let r = length(vec2<f32>(pos.x, pos.z));
    if (r < 0.001) { return pos; }
    let angle = atan2(pos.z, pos.x);
    // The surface-tension ripple around the rim.
    let wobble = sin(angle * 6.0 + mu.time * 7.0) * 0.05 + sin(angle * 9.0 - mu.time * 5.0) * 0.03;
    let p = pos + vec3<f32>(pos.x, 0.0, pos.z) / r * wobble;
    // Squash and stretch, from the bounce: wide and short on landing.
    return vec3<f32>(p.x * mu.squash, p.y, p.z / mu.squash);
}
`;

export const JELLY_V_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    float r = length(vec2(pos.x, pos.z));
    if (r < 0.001) { return pos; }
    float angle = atan(pos.z, pos.x);
    float wobble = sin(angle * 6.0 + mu.time * 7.0) * 0.05 + sin(angle * 9.0 - mu.time * 5.0) * 0.03;
    vec3 p = pos + vec3(pos.x, 0.0, pos.z) / r * wobble;
    return vec3(p.x * mu.squash, p.y, p.z / mu.squash);
}
`;

export const JELLY_F = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let q = ctx.worldPos.xy - mu.center;
    // Soft body shading, lighter towards the top.
    var col = mu.blobColor * (0.8 + 0.35 * q.y - 0.1 * q.x);
    // A gloss up and to the left.
    col = col + smoothstep(0.16, 0.0, length(q - vec2<f32>(-0.15, 0.16))) * 0.7;
    // Two eyes and a smile, squashed vertically so they read as eyes.
    let eyeL = smoothstep(0.06, 0.048, length((q - vec2<f32>(-0.13, 0.10)) * vec2<f32>(1.0, 1.4)));
    let eyeR = smoothstep(0.06, 0.048, length((q - vec2<f32>(0.13, 0.10)) * vec2<f32>(1.0, 1.4)));
    let mouth = smoothstep(0.05, 0.038, abs(length(q - vec2<f32>(0.0, -0.02)) - 0.14)) * step(q.y, -0.02);
    col = mix(col, vec3<f32>(0.15, 0.05, 0.08), max(max(eyeL, eyeR), mouth));
    return vec4<f32>(col, surface.a);
}
`;

export const JELLY_F_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    vec2 q = ctx.worldPos.xy - mu.center;
    vec3 col = mu.blobColor * (0.8 + 0.35 * q.y - 0.1 * q.x);
    col = col + smoothstep(0.16, 0.0, length(q - vec2(-0.15, 0.16))) * 0.7;
    float eyeL = smoothstep(0.06, 0.048, length((q - vec2(-0.13, 0.10)) * vec2(1.0, 1.4)));
    float eyeR = smoothstep(0.06, 0.048, length((q - vec2(0.13, 0.10)) * vec2(1.0, 1.4)));
    float mouth = smoothstep(0.05, 0.038, abs(length(q - vec2(0.0, -0.02)) - 0.14)) * step(q.y, -0.02);
    col = mix(col, vec3(0.15, 0.05, 0.08), max(max(eyeL, eyeR), mouth));
    return vec4(col, surface.a);
}
`;
```
