# Portable by construction

> The same cel and rim look composed as a graph, with no WGSL or GLSL, drawn by both renderers at once.

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

The same look as *Portable shaders*, composed, with no WGSL and no GLSL anywhere. A graph is made of **typed** values, so the compiler writes whichever language a renderer speaks. The effect runs on both, side by side here, and the two halves cannot drift apart because nobody wrote them.

That is exactly what a `.shader` file is: nodes and wires as plain JSON, with no shader source in any language, loaded with `useLoadShader` like a `.wgsl`. Drawn in an editor or written in code as here, it is the same graph.

## Code

`index.ts`:

```ts
import { startPanel } from './host/start_panel';

const root = document.querySelector<HTMLElement>('#app');

if (root === null) {
    throw new Error('The example needs an #app container.');
}

root.style.cssText = 'display:grid;grid-template-columns:1fr 1fr;gap:8px';
startPanel(root, 'WEBGPU', 'WebGPU');
startPanel(root, 'WEBGL2', 'WebGL2');
```

`components/ball.ts`:

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

import { celGraph } from '../shaders/cel_graph';

export const Ball = () => {
    // What compileShader returns is exactly what a material wants, in BOTH languages, so nothing
    // here mentions a card.
    const material = createMaterial({
        name: 'cel-graph',
        tint: '#e8a03c',
        ...celGraph()
    });

    const ball = createMesh({
        geometry: useSphereGeometry({
            radius: 1.15,
            segments: 32,
            rings: 16
        }),
        material
    });

    useUpdate((delta) => {
        ball.transform.rotationY += delta * 0.5;
    });
};
```

`host/start_panel.ts`:

```ts
import { createGame } from 'nacatamalon';

import { Game } from '../scenes/demo';
import { WIDTH, HEIGHT } from '../settings';

export const startPanel = (root: HTMLElement, renderer: 'WEBGPU' | 'WEBGL2', label: string) => {
    const wrap = document.createElement('div');

    wrap.style.cssText = 'display:flex;flex-direction:column;gap:4px;min-width:0';

    const tag = document.createElement('div');

    tag.textContent = label;
    tag.style.cssText = 'font:11px ui-monospace,monospace;color:#8ee6a0;letter-spacing:.08em';

    const host = document.createElement('div');

    wrap.append(tag, host);
    root.appendChild(wrap);

    // A browser with no WebGPU cannot run the left half, and that is content rather than a failure.
    if (renderer === 'WEBGPU' && !('gpu' in navigator)) {
        tag.textContent = label + '  ·  unavailable here';
        tag.style.color = '#e0663c';
        host.style.cssText = 'height:230px;display:grid;place-items:center;border:1px dashed #3a3f4b;' +
            'color:#6b7280;font:12px ui-monospace,monospace';
        host.textContent = 'this browser has no WebGPU';

        return;
    }

    const game = createGame(host, {
        width: WIDTH,
        height: HEIGHT,
        background: '#0a0a12',
        renderer
    });

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

`scenes/demo.ts`:

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

import { Ball } from '../components/ball';

export const Game = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        near: 0.1,
        far: 100,
        transform: {
            y: 1.2,
            z: 4.2,
            rotationX: -0.18
        }
    });
    useDirectionalLight({
        color: '#fff0d0',
        intensity: 1.1,
        ambient: 0.2,
        transform: {
            rotationX: -0.5,
            rotationY: 0.4
        }
    });

    useSpawn(Ball)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 300;
export const HEIGHT = 230;
```

`shaders/cel_graph.ts`:

```ts
import { compileShader, composerAdd, composerClamp, composerDot, composerLight, composerMix, composerMul, composerNormalize, composerOneMinus, composerPow, composerSurface, composerSwizzle, composerUniform, composerVec3, composerVec4, composerViewDir, composerWorldNormal } from 'nacatamalon/shader';

export const celGraph = () => {
    const n = composerNormalize(composerWorldNormal());
    // The rim: 1 where the surface turns away from the camera, 0 where it faces it.
    const facing = composerClamp(composerDot(n, composerViewDir()), 0, 1);
    const rim = composerPow(composerOneMinus(facing), 2.5);

    // Two flat steps of light: mix and clamp make a ramp hard enough to read as bands.
    const term = composerSwizzle(composerLight(), 'g');
    const bands = composerClamp(composerMul(term, composerUniform('contrast', 1.6)), 0, 1);
    const stepped = composerMix(composerUniform('darkStep', 0.32), 1, composerClamp(composerMul(bands, 2), 0, 1));

    const lit = composerMul(composerSwizzle(composerSurface(), 'rgb'), stepped);
    const rimColor = composerMul(composerVec3(0.2, 0.6, 1.0), rim);

    return compileShader({
        shader: 'mesh3d',
        color: composerVec4(composerAdd(lit, rimColor), 1)
    });
};
```
