# Scanlines

> The first custom-shader material: createMaterial takes one function, and the engine hands it the picture already read and tinted.

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

The first custom shader. `createMaterial` takes one function, and the engine hands it the picture **already read and already tinted**: return it unchanged and nothing looks different. `mu.time` is put there for you, so the bands scroll with no per-frame code at all. Left is the built-in shader, right is the same picture through the material.

The effect is written twice, once in WGSL and once in GLSL, because the engine will not pretend to translate one into the other. Delete the GLSL half and the effect becomes WebGPU only: WebGL2 then draws the plain sprite and says so once, rather than showing you nothing.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { Game } from './scenes/demo';

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

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

`components/comparison.ts`:

```ts
import { createMaterial, createSprite, useLoadTexture } from 'nacatamalon';

import { SCANLINES_WGSL, SCANLINES_GLSL } from '../shaders/scanlines';

export const Comparison = () => {
    const texture = useLoadTexture({
        src: '/example-assets/assets/gammix.png',
        key: 'gammix'
    });

    // Left: no material at all, which is the shader the engine has always used.
    createSprite({
        texture,
        width: 160,
        height: 160,
        transform: {
            x: 120,
            y: 160,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        }
    });

    // ONE material. The uniforms here are its own defaults, and its parameter types are read off
    // these values, so nothing is declared twice.
    const crt = createMaterial({
        name: 'scanlines',
        fragment: SCANLINES_WGSL,
        fragmentGlsl: SCANLINES_GLSL,
        uniforms: {
            lines: 40,
            speed: 6,
            depth: 0.7
        },
    });

    // Right: the same picture through it.
    createSprite({
        texture,
        material: crt,
        width: 160,
        height: 160,
        transform: {
            x: 300,
            y: 160,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        }
    });
};
```

`scenes/demo.ts`:

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

import { Comparison } from '../components/comparison';

export const Game = () => {
    useSpawn(Comparison)();

    return createScene();
};
```

`settings.ts`:

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

`shaders/scanlines.ts`:

```ts
export const SCANLINES_WGSL = `
fn effect(color: vec4f, uv: vec2f) -> vec4f {
    let band = 0.5 + 0.5 * sin(uv.y * mu.lines * 6.28318 + mu.time * mu.speed);
    return vec4f(color.rgb * (1.0 - mu.depth * (1.0 - band)), color.a);
}
`;

// The same arithmetic, spelled the way the older card spells it. Leave it out and the effect is
// WebGPU only: WebGL2 then draws the plain sprite and says so once in the console.
export const SCANLINES_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    float band = 0.5 + 0.5 * sin(uv.y * mu.lines * 6.28318 + mu.time * mu.speed);
    return vec4(color.rgb * (1.0 - mu.depth * (1.0 - band)), color.a);
}
`;
```
