# Portable shaders

> WebGL2 cannot compile WGSL and the engine will not pretend to translate it: a material may carry a second half in GLSL.

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

A custom material is WGSL, and WebGL2 cannot compile WGSL: the engine will not pretend to translate it. So a material may carry a **second half** in GLSL ES 300 (`fragmentGlsl`, `vertexGlsl`), and each renderer takes the one it speaks. Here the same cel-shaded sphere runs on both.

The halves are one shader in two spellings, so they share one parameter set: `mu.steps` is generated once and read by both. A shader file can hold both under a single `@shader`/`@uniform` header, which is the better shape: with two files the header lives twice and the halves can drift. Better still, a `.shader` graph: see *Portable by construction*.

## Code

`index.ts`:

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

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

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

`components/ball.ts`:

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

import { CEL_WGSL, CEL_GLSL } from '../shaders';

export const Ball = () => {
    const cel = createMaterial({
        name: 'cel',
        shader: 'mesh3d',
        tint: '#e8a03c',
        fragment: CEL_WGSL,
        fragmentGlsl: CEL_GLSL,
        uniforms: {
            steps: 4,
            rim: [0.2, 0.6, 1, 1]
        },
    });

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

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

`scenes/demo.ts`:

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

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

export const Game = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        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 = 480;
export const HEIGHT = 320;
```

`shaders.ts`:

```ts
export const CEL_WGSL = `
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    // Brightest where the surface turns away from the eye.
    let rim = pow(1.0 - max(dot(normalize(ctx.normal), ctx.viewDir), 0.0), 2.5);
    // The light cut into hard steps, which is the whole of the toon look.
    let bands = floor(ctx.light.g * mu.steps) / mu.steps;
    return vec4<f32>(surface.rgb * bands + mu.rim.rgb * rim, surface.a);
}
`;

export const CEL_GLSL = `
vec4 effect(vec4 surface, FragContext ctx) {
    float rim = pow(1.0 - max(dot(normalize(ctx.normal), ctx.viewDir), 0.0), 2.5);
    float bands = floor(ctx.light.g * mu.steps) / mu.steps;
    return vec4(surface.rgb * bands + mu.rim.rgb * rim, surface.a);
}
`;

// A file can hold both instead, with the header written once and the halves under it:
//
//   // @shader mesh3d
//   // @uniform steps: f32 = 4
//   // @uniform rim: vec4<f32> = 0.2, 0.6, 1.0, 1.0
//   // @wgsl
//   fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> { ... }
//   // @glsl
//   vec4 effect(vec4 surface, FragContext ctx) { ... }
//
// then useLoadShader({ src: '/example-assets/shaders/cel.wgsl' }) and createMaterial({ effect: '...' }). The
// header is read from the whole file and never from inside a half, because a contract written twice
// is two things that can drift.
```
