# Doom fire

> The fire of Doom on the PlayStation and the Nintendo 64, with a word burning in it, computed on the graphics card.

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

**Drag the mouse** to pour heat and press **F** to put the fire out and light it again.

Each pixel is a heat: every frame it takes the heat of the pixel below, a little to the side, and loses a little. It runs on the graphics card between two `createSpriteTexture`s that take turns, and anything written into them in white (the word) is a source of heat.

The colour ramp is cut into the original's 37 steps.

## Code

`index.ts`:

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

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

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

`components/fire.ts`:

```ts
import * as Settings from '../settings';
import { createMaterial, createSprite, createSpriteTexture, createText, useGame, useKeyboard, useLoadBitmapFont, usePointer, useUpdate, type TSprite } from 'nacatamalon';
import { BURN, BURN_GLSL, SHOW, SHOW_GLSL } from '../shaders/fire';

export const Fire = () => {
    const game = useGame();
    const keys = useKeyboard();
    const pointer = usePointer();
    const W = game.getWidth();
    const H = game.getHeight();

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

    // The heat lives in the red of a picture. A picture cannot be drawn from itself, so two take
    // turns: NEXT is drawn from LAST through the burn, and LAST copies NEXT back.
    const burn = {
        texel: [1 / Settings.FIRE_W, 1 / Settings.FIRE_H],
        now: 0,
        floor: 1,
        brush: [-1, -1],
        pouring: 0
    };

    const material = createMaterial({
        name: 'doom-burn',
        fragment: BURN,
        fragmentGlsl: BURN_GLSL,
        uniforms: burn
    });

    const whole = {
        width: Settings.FIRE_W,
        height: Settings.FIRE_H,
        transform: {
            x: Settings.FIRE_W / 2,
            y: Settings.FIRE_H / 2
        },
        smooth: false
    };

    const burning: TSprite[] = [];

    const next = createSpriteTexture({
        width: Settings.FIRE_W,
        height: Settings.FIRE_H,
        background: '#000000'
    }, () => {
        burning.push(createSprite({
            ...whole,
            material
        }));
        // Anything drawn on top in pure white is a source of heat: the word burns because it is
        // written into the fire itself, over the burn, every frame.
        createText({
            text: 'NACATAMALON',
            font,
            tint: '#ffffff',
            transform: {
                x: Settings.FIRE_W / 2 - 98,
                y: 70,
                scaleX: 2,
                scaleY: 2
            },
            zIndex: 1
        });
    });

    const last = createSpriteTexture({
        width: Settings.FIRE_W,
        height: Settings.FIRE_H,
        background: '#000000'
    }, () => {
        createSprite({
            ...whole,
            texture: next
        });
    });

    burning[0].texture = last;

    // What you see: the heat, through Doom's own ramp from black through red and yellow to white.
    createSprite({
        texture: next,
        material: createMaterial({
            name: 'doom-show',
            fragment: SHOW,
            fragmentGlsl: SHOW_GLSL
        }),
        width: W,
        height: H,
        transform: {
            x: W / 2,
            y: H / 2
        },
        smooth: false,
    });

    let down = false;

    const aim = (x: number, y: number) => {
        burn.brush[0] = x / W;
        burn.brush[1] = y / H;
    };

    pointer.onDown((info) => {
        down = true;
        aim(info.screenX, info.screenY);
    });
    pointer.onMove((info) => aim(info.screenX, info.screenY));
    pointer.onUp(() => {
        down = false;
    });

    useUpdate((delta) => {
        burn.now += delta;

        if (keys.justPressed('f')) {
            burn.floor = burn.floor > 0.5 ? 0 : 1;
        }

        burn.pouring = down ? 1 : 0;
    });
};
```

`scenes/demo.ts`:

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

import { Fire } from '../components/fire';

export const Hell = () => {
    useSpawn(Fire)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const FIRE_W = 240;
export const FIRE_H = 160;
```

`shaders/fire.ts`:

```ts
// One step of the burn, per pixel: take the heat of the pixel below, from up to a pixel to either
// side (that is the flicker and the wind), and lose a random little. The bottom row is the source,
// white hot while the fire is lit.

export const BURN = `
fn hash(p: vec2<f32>) -> f32 {
    return fract(sin(dot(p, vec2<f32>(12.9898, 78.233))) * 43758.5453);
}

fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let r = hash(uv * 811.0 + fract(mu.now * 7.13));
    // Mostly up and a little to the left: the wind the original blew.
    let side = floor(r * 3.0) - 1.4;
    let below = sampleTexture(uv + vec2<f32>(side * mu.texel.x, mu.texel.y)).r;
    var heat = max(0.0, below - r * 0.03);

    if (uv.y > 1.0 - mu.texel.y) {
        heat = mu.floor;
    }
    let away = (uv - mu.brush) / mu.texel;
    if (mu.pouring > 0.5 && dot(away, away) < 20.0) {
        heat = 1.0;
    }
    return vec4<f32>(heat, 0.0, 0.0, 1.0);
}
`;

export const BURN_GLSL = `
float hash(vec2 p) {
    return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}

vec4 effect(vec4 color, vec2 uv) {
    float r = hash(uv * 811.0 + fract(mu.now * 7.13));
    float side = floor(r * 3.0) - 1.4;
    float below = sampleTexture(uv + vec2(side * mu.texel.x, mu.texel.y)).r;
    float heat = max(0.0, below - r * 0.03);

    if (uv.y > 1.0 - mu.texel.y) {
        heat = mu.floor;
    }
    vec2 away = (uv - mu.brush) / mu.texel;
    if (mu.pouring > 0.5 && dot(away, away) < 20.0) {
        heat = 1.0;
    }
    return vec4(heat, 0.0, 0.0, 1.0);
}
`;

// Doom's ramp had 37 steps; this is the same walk, black to deep red to orange to yellow to white,
// cut into 37 so the bands of the original show.
export const SHOW = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    let h = floor(sampleTexture(uv).r * 36.0) / 36.0;
    let rgb = vec3<f32>(
        clamp(h * 2.6, 0.0, 1.0),
        clamp(h * 2.2 - 0.55, 0.0, 1.0),
        clamp(h * 3.0 - 2.1, 0.0, 1.0));
    return vec4<f32>(rgb, 1.0);
}
`;

export const SHOW_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    float h = floor(sampleTexture(uv).r * 36.0) / 36.0;
    vec3 rgb = vec3(
        clamp(h * 2.6, 0.0, 1.0),
        clamp(h * 2.2 - 0.55, 0.0, 1.0),
        clamp(h * 3.0 - 2.1, 0.0, 1.0));
    return vec4(rgb, 1.0);
}
`;
```
