# Composite cable

> ntsc() is the frame as it came down a composite cable: colour smeared sideways, crawling dots and rainbows.

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

`ntsc()` is the picture as it came down a composite cable. Brightness and colour travelled **mixed in one signal**, and the television could only part them roughly: colour smears sideways while brightness stays sharp (`bleed`), the colour that leaks back into brightness crawls a little every frame (`crawl`), and sharp edges in brightness are mistaken for colour, which makes rainbows on fine detail (`artifacts`).

Artists of the era drew for it: dithered stripes that a cable blended into a solid colour. Some old art only looks right through this.

It belongs **right before the tube**, which is the order the signal travelled in. Press **1** to **3** to toggle each part, **T** to put a `crt()` after it, and **Space** to switch it off.

## Code

`index.ts`:

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

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

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

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

`components/effect_controls.ts`:

```ts
import { createText, useKeyboard, useLoadBitmapFont, useUpdate, useScreenSpace } from 'nacatamalon';
import { crt, ntsc, usePostProcess, POST_EFFECTS } from 'nacatamalon/effects';

import { PARTS, EFFECT_SETTINGS } from '../settings';

export const EffectControls = () => {
    useScreenSpace();

    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
    });

    const label = createText({
        text: '',
        font,
        style: {
            fontSize: 8,
        },
        tint: '#ffffff',
        transform: {
            x: 12,
            y: 14,
        },
    });

    createText({
        text: '1-3 PARTS   T TUBE   SPACE OFF',
        font,
        style: {
            fontSize: 8,
        },
        tint: '#8c82a6',
        transform: {
            x: 12,
            y: 30,
        },
    });

    // The order effects are installed in is the order they run in: the cable first, then the tube.
    const effect = usePostProcess(ntsc(EFFECT_SETTINGS));

    const tube = usePostProcess({
        ...crt(POST_EFFECTS.CRT_PRESETS.composite),
        enabled: false,
    });

    const show = () => {
        if (!effect.enabled) {
            label.text = 'OFF  THE FRAME AS DRAWN';

            return;
        }

        const parts = PARTS.filter((part) => effect.uniforms[part.name] > 0).map((part) => part.name.toUpperCase());

        label.text = (parts.length > 0 ? parts.join(' ') : 'A CLEAN CABLE') + (tube.enabled ? '  ON A TUBE' : '');
    };

    show();

    const keys = useKeyboard();

    useUpdate(() => {
        for (const part of PARTS) {
            if (keys.justPressed(part.key)) {
                effect.uniforms[part.name] = effect.uniforms[part.name] > 0 ? 0 : part.on;
                show();
            }
        }

        if (keys.justPressed('t')) {
            tube.enabled = !tube.enabled;
            show();
        }

        // Switched off, not removed: it keeps its place in the chain for when it comes back.
        if (keys.justPressed(' ')) {
            effect.enabled = !effect.enabled;
            show();
        }
    });
};
```

`components/stage.ts`:

```ts
import { WIDTH, HEIGHT } from '../settings';
import { createMaterial, createMesh, createSprite, getColor, useCamera3d, useDirectionalLight, useLoadTexture, useUpdate, useSphereGeometry } from 'nacatamalon';

// The test card every post-processing example draws, so the only thing that changes between them
// is the effect. Each part is here because it shows something a screen effect does:
//
//   lit balls   smooth gradients that nobody drew, made by the light as it turns. Banding shows
//               there first, because a gradient is exactly what a few steps cannot hold.
//   hue ramp    every colour at once, which is where a palette has to make its hardest choices.
//   grey ramp   sixteen even steps: whether an effect keeps them even is visible at a glance.
//   gammix      pixel art, already limited, so it shows what an effect does to a sprite that was
//               drawn for a small palette in the first place.

export const Stage = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        transform: {
            z: 5,
            y: 0.4
        },
    });

    const light = useDirectionalLight({
        color: '#ffffff',
        intensity: 1.2,
        ambient: 0.25,
        transform: {
            rotationX: -0.5
        },
    });

    const geometry = useSphereGeometry();

    ['#ff7a3d', '#3dd6c6', '#9b5cff'].forEach((hex, i) => {
        const material = createMaterial({
            name: 'post-card-' + i,
            shader: 'mesh3d',
            tint: getColor(hex),
            specular: '#ffffff',
            shininess: 24,
        });

        createMesh({
            geometry,
            material,
            transform: {
                x: (i - 1) * 2.1,
                y: 0.6,
            },
        });
    });

    // The light swings from side to side in front of the balls, so the gradients move and a
    // static effect cannot hide in one lucky frame.
    useUpdate((_, time) => {
        light.transform.rotationY = Math.sin(time * 0.8) * 0.9;
    });

    for (let i = 0; i < 48; i++) {
        createSprite({
            width: WIDTH / 48,
            height: 18,
            tint: getColor('hsl(' + Math.round((i / 48) * 360) + ', 85%, 55%)'),
            transform: {
                x: (i + 0.5) * WIDTH / 48,
                y: HEIGHT - 38,
            },
        });
    }

    for (let i = 0; i < 16; i++) {
        const v = Math.round((i / 15) * 255);

        createSprite({
            width: WIDTH / 16,
            height: 18,
            tint: getColor('rgb(' + v + ',' + v + ',' + v + ')'),
            transform: {
                x: (i + 0.5) * WIDTH / 16,
                y: HEIGHT - 18,
            },
        });
    }

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

    createSprite({
        texture: gammix,
        width: 48,
        height: 48,
        transform: {
            x: WIDTH - 34,
            y: 34,
        },
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Stage } from '../components/stage';
import { EffectControls } from '../components/effect_controls';

export const Game = () => {
    useSpawn(Stage)();
    useSpawn(EffectControls)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;

export const HEIGHT = 300;

// ntsc() is the picture as it came down a composite cable. Brightness and colour travelled mixed
// in one signal, and the television could only part them roughly:
//
//   bleed      colour smears sideways over several pixels while brightness stays sharp
//   crawl      the colour that leaks back into brightness crawls a little every frame
//   artifacts  sharp edges in brightness are mistaken for colour: rainbows on fine detail
//
// Artists of the era drew for it, and some old art only looks right through it. It belongs right
// before the tube: T puts a crt() after it, which is the order the signal travelled in.

export const PARTS = [
    {
        key: '1',
        name: 'bleed',
        on: 0.8
    },
    {
        key: '2',
        name: 'crawl',
        on: 0.4
    },
    {
        key: '3',
        name: 'artifacts',
        on: 0.5
    },
] as const;

export const EFFECT_SETTINGS = {
    bleed: 0.8,
    crawl: 0.4,
    artifacts: 0.5,
};
```
