# Shader graph 3D

> One graph driving both stages of a model's material: colour and position.

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

One graph drives **both stages** of a model's material: its `color` becomes `fn effect` (the light cut into bands, plus a rim) and its `position` becomes `fn vertex` (a wave along the normal). `compileShader` writes both, and the result spreads into `createMaterial`.

A parameter is moved every frame, straight on the object the renderer reads.

## Code

`index.ts`:

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

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

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

`components/wobblyorb.ts`:

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

import * as Settings from '../settings';
import { wobblyCel } from '../shaders/wobbly_cel';

export const WobblyOrb = () => {
    const shader = wobblyCel();

    const material = createMaterial({
        tint: '#e08a3a',
        ...shader
    });

    const orb = createMesh({
        geometry: useIcoSphereGeometry({
            subdivisions: 3
        }),
        material
    });

    useUpdate((delta, time) => {
        // The parameters are kept, not copied: this is the object the renderer reads, so the next
        // frame already wobbles harder.
        shader.uniforms.amp = 0.1 + 0.07 * Math.sin(time * Settings.PULSE.speed);
        orb.transform.rotationY += delta * Settings.TURN.y;
        orb.transform.rotationX += delta * Settings.TURN.x;
    });
};
```

`scenes/demo.ts`:

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

import { WobblyOrb } from '../components/wobblyorb';

export const Orb = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        near: 0.1,
        far: 100,
        transform: {
            z: 4
        }
    });
    // A light shines along its own facing, so it is aimed by turning it: down and a little left.
    useDirectionalLight({
        intensity: 1.1,
        ambient: 0.25,
        transform: {
            rotationX: -0.6,
            rotationY: 0.5
        }
    });

    useSpawn(WobblyOrb)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const TURN = {
    y: 0.6,
    x: 0.2
};
export const PULSE = {
    speed: 2
};
```

`shaders/wobbly_cel.ts`:

```ts
import { compileShader, composerAdd, composerCelShade, composerFresnel, composerLight, composerMul, composerPipe, composerSin, composerSurface, composerSwizzle, composerTime, composerUniform, composerVec3, composerVec4, composerVertexNormal, composerVertexPosition } from 'nacatamalon/shader';

export const wobblyCel = ({ rim = [0.3, 0.7, 1.0], steps = 4 } = {
}) => {
    // wobble = sin(position.y * 6 + time) * amp. amp is a parameter, so the game can move it.
    const wobble = composerPipe(
        composerSwizzle(composerVertexPosition(), 'y'),
        composerMul(6),
        composerAdd(composerTime()),
        composerSin,
        composerMul(composerUniform('amp', 0.12)),
    );

    return compileShader({
        shader: 'mesh3d',
        color: composerVec4(composerAdd(
            composerMul(composerSwizzle(composerSurface(), 'rgb'), composerCelShade(composerAdd(composerLight(), 0.05), steps)),
            composerMul(composerVec3(rim[0], rim[1], rim[2]), composerFresnel(3)),
        ), 1),
        position: composerAdd(composerVertexPosition(), composerMul(composerVertexNormal(), wobble)),
    });
};
```
