# When a shader is WebGPU only

> The degradation, shown on purpose: with only its WGSL half, on WebGL2 the object draws through the built-in shader with a warning in the console.

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

The other half of the story, shown on purpose. This material carries only its WGSL half, so the dissolve runs on the left and the right draws through the **built-in shader**: same object, same tint and lighting, no effect, and one line in the console naming the material.

Drawing nothing would leave a hole with no clue; refusing to start would let one shader take the whole game down. Degrading is the least bad of the three. Reach for it knowingly: if the effect matters on a phone, give it a GLSL half or compose it as a graph.

## Code

`index.ts`:

```ts
import { side } from './host/side';

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

if (root === null) {
    throw new Error('There is no #app to put the two canvases in.');
}

root.style.cssText = 'display:grid;grid-template-columns:1fr 1fr;gap:8px';

side(root, 'WEBGPU', 'WebGPU', 'effect runs');
side(root, 'WEBGL2', 'WebGL2', 'built-in shader');
```

`components/ball.ts`:

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

import { DISSOLVE_WGSL } from '../shaders';

export const Ball = () => {
    // No fragmentGlsl. That is the whole of this example.
    const material = createMaterial({
        name: 'dissolve',
        shader: 'mesh3d',
        tint: '#8fd4a0',
        fragment: DISSOLVE_WGSL,
    });

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

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

`host/side.ts`:

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

export const side = (root: HTMLElement, renderer: 'WEBGPU' | 'WEBGL2', label: string, note: string): void => {
    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 + '  ·  ' + note;
    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);

    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,
        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.ts`:

```ts
export const DISSOLVE_WGSL = `
fn hash(p: vec2<f32>) -> f32 {
    return fract(sin(dot(p, vec2<f32>(127.1, 311.7))) * 43758.5453);
}
fn effect(surface: vec4<f32>, ctx: FragContext) -> vec4<f32> {
    let t = fract(mu.time * 0.3);
    let e = hash(floor(ctx.uv * 16.0)) - t;
    if (e < 0.0) { return vec4<f32>(0.0, 0.0, 0.0, 0.0); }
    let burn = smoothstep(0.0, 0.18, e);
    let lit = surface.rgb * ctx.light;
    return vec4<f32>(mix(vec3<f32>(1.0, 0.5, 0.1), lit, burn), surface.a);
}
`;
```
