# Lava sea

> A Wave Race style rolling ocean, made of lava: one material using both mesh hooks, vertex and effect.

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

A Wave Race style rolling ocean, as lava. One material uses both mesh hooks: a `vertex` sum of sines swells a subdivided plane, and an `effect` reads `ctx.worldPos.y` (the height after displacement) to glow molten on the crests and cool to dark basalt in the troughs. All from `mu.time`, no `useUpdate`.

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

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

`components/gallery.ts`:

```ts
import { createMaterial, createMesh, usePlaneGeometry } from 'nacatamalon';

import { WAVES, WAVES_GLSL, LAVA, LAVA_GLSL } from '../shaders/effects';

export const Gallery = () => {
    const lava = createMaterial({
        name: 'lava-sea',
        shader: 'mesh3d',
        tint: '#ff4400',
        vertex: WAVES,
        vertexGlsl: WAVES_GLSL,
        fragment: LAVA,
        fragmentGlsl: LAVA_GLSL,
    });

    createMesh({
        geometry: usePlaneGeometry({
            width: 60,
            depth: 60,
            widthSegments: 90,
            depthSegments: 90
        }),
        material: lava,
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useSpawn } from 'nacatamalon';

import { Gallery } from '../components/gallery';

export const Game = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 60,
        transform: {
            y: 2.4,
            z: 9,
            rotationX: -0.16
        }
    });

    useSpawn(Gallery)();

    return createScene();
};
```

`settings.ts`:

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

`shaders/effects.ts`:

```ts
// A rolling sea, in the Wave Race sense, except it is lava. One material, both hooks:
//
//   vertex(pos, ctx)      a sum of sines rolls a flat plane into swells
//   effect(surface, ctx)  colours it molten, reading ctx.worldPos.y, which is the height AFTER the
//                         corner hook ran, so the crests glow and the troughs go to dark basalt
//
// There is no useUpdate anywhere: it all rides mu.time. The plane is cut into many pieces because a
// corner hook can only move corners, and a flat quad has four. The camera sits low over the surface
// looking at the horizon.

export const WAVES = `
fn vertex(pos: vec3<f32>, ctx: VertContext) -> vec3<f32> {
    var p = pos;
    p.y = p.y
        + sin(pos.x * 0.6 + mu.time * 1.5) * 0.5
        + sin(pos.z * 0.8 - mu.time * 1.1) * 0.4
        + sin((pos.x + pos.z) * 0.4 + mu.time * 0.9) * 0.3
        + sin((pos.x - pos.z) * 1.1 + mu.time * 2.0) * 0.15;
    return p;
}
`;
export const WAVES_GLSL = `
vec3 vertex(vec3 pos, VertContext ctx) {
    vec3 p = pos;
    p.y = p.y
        + sin(pos.x * 0.6 + mu.time * 1.5) * 0.5
        + sin(pos.z * 0.8 - mu.time * 1.1) * 0.4
        + sin((pos.x + pos.z) * 0.4 + mu.time * 0.9) * 0.3
        + sin((pos.x - pos.z) * 1.1 + mu.time * 2.0) * 0.15;
    return p;
}
`;
export const LAVA = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let h = ctx.worldPos.y;
    let flow = 0.5 + 0.5 * sin(ctx.worldPos.x * 1.5 - ctx.worldPos.z * 1.2 + mu.time * 2.5);
    let heat = clamp((h * 0.45 + 0.5) * 0.85 + flow * 0.2, 0.0, 1.0);
    // Black crust, then red, orange, yellow, white hot.
    let iron = clamp(vec3<f32>(heat * 3.0, heat * 3.0 - 1.2, heat * 3.0 - 2.3), vec3<f32>(0.0), vec3<f32>(1.0));
    let crust = smoothstep(0.25, 0.55, heat);
    return vec4<f32>(mix(vec3<f32>(0.04, 0.01, 0.0), iron, crust), 1.0);
}
`;
export const LAVA_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    float h = ctx.worldPos.y;
    float flow = 0.5 + 0.5 * sin(ctx.worldPos.x * 1.5 - ctx.worldPos.z * 1.2 + mu.time * 2.5);
    float heat = clamp((h * 0.45 + 0.5) * 0.85 + flow * 0.2, 0.0, 1.0);
    vec3 iron = clamp(vec3(heat * 3.0, heat * 3.0 - 1.2, heat * 3.0 - 2.3), 0.0, 1.0);
    float crust = smoothstep(0.25, 0.55, heat);
    return vec4(mix(vec3(0.04, 0.01, 0.0), iron, crust), 1.0);
}
`;
```
