# Glow models

> A shield, a hologram and a magic circle with blend: 'additive': they add their colour to what is behind them, cast no shadow and fade into the fog.

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

Models that add their colour. A shield, a hologram and a magic circle in a dark vault are drawn with `blend: 'additive'`, so they brighten whatever is behind them, cast no shadow and fade into the fog. The floor darkens in bands by how far it is from the camera, and a sonar ring goes out from it, both from `ctx.cameraPos` in the material's colour hook, with nothing moved by hand.

**Space** makes the glows cover what is behind them instead, as alpha does.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Vault } from './scenes/vault';
import { HEIGHT, NIGHT, WIDTH } from './settings';

// Models that add their colour. A shield, a hologram and a magic circle in a dark vault are drawn
// with blend: 'additive', so they brighten whatever is behind them, cast no shadow and fade into the
// fog. The floor darkens in bands by how far it is from the camera and a sonar ring goes out from
// it, both from ctx.cameraPos, with nothing moved by hand.
//
//   space   add their colour, or cover with it as alpha does
const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: NIGHT,
    actions: [
        {
            name: 'blend',
            bindings: [
                {
                    type: 'key',
                    key: ' '
                }
            ]
        }
    ]
});

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

`scenes/vault.ts`:

```ts
import {
    cameraLookAt, createMaterial, createMesh, createScene, createText, useActions, useAmbientLight, useCamera3d, useCubeGeometry,
    useCylinderGeometry, useDirectionalLight, useFog, usePlaneGeometry, useSphereGeometry, useTorusGeometry, useUpdate,
    useIcoSphereGeometry
} from 'nacatamalon';
import { BEAM, CIRCLE, FLOOR, HOLOGRAM, SHIELD } from '../shaders/glow';
import { HEIGHT, NIGHT, ORBIT } from '../settings';

/**
 * A dark vault with three glows in it, seen from a camera going slowly round. Space changes how the
 * glows land on what is behind them: added, or covering as alpha does.
 */
export const Vault = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 55,
        far: 80
    });

    useAmbientLight({
        color: '#8d9ac8',
        intensity: 0.55
    });
    useDirectionalLight({
        color: '#c8d4ff',
        intensity: 0.6,
        castShadow: true,
        shadowArea: 30,
        transform: {
            rotationX: -0.9,
            rotationY: 0.5
        }
    });
    useFog({
        color: NIGHT,
        near: 14,
        far: 34
    });

    // The floor: lit by the engine, darker in bands away from the camera, and the sonar.
    createMesh({
        geometry: usePlaneGeometry({ width: 80, depth: 80, widthSegments: 40, depthSegments: 40 }),
        material: createMaterial({ ...FLOOR, tint: '#9098b8' }),
        castShadow: false
    });

    // Solid crates, which cast and hide: the glows add onto them.
    const crate = useCubeGeometry();
    for (const [x, z, size] of [[-2.2, 0.4, 1.2], [2.8, -1.5, 1.6], [-5, -4, 2], [5.5, 3.5, 1.4], [0.5, -6, 1.8]] as const) {
        createMesh({
            geometry: crate,
            tint: '#8a6a4a',
            transform: { x, y: size / 2, z, rotationY: x * 0.4, scaleX: size, scaleY: size, scaleZ: size }
        });
    }

    // The shield, over the first crate: seen from inside and out, and casting nothing.
    const shield = createMaterial({ ...SHIELD, blend: 'additive', cull: 'none' });
    createMesh({
        geometry: useSphereGeometry({ radius: 2.1, segments: 40, rings: 24 }),
        material: shield,
        transform: { x: -2.2, y: 0.4, z: 0.4 }
    });

    // The hologram: a stone turning in a beam on its pad.
    const hologram = createMaterial({ ...HOLOGRAM, blend: 'additive', cull: 'none' });
    const beam = createMaterial({ ...BEAM, blend: 'additive', cull: 'none' });
    createMesh({
        geometry: useCylinderGeometry({ radius: 0.9, height: 0.2, segments: 32 }),
        tint: '#3a4258',
        transform: { x: 3.5, y: 0.1, z: 2 }
    });
    createMesh({
        geometry: useCylinderGeometry({ radius: 0.8, height: 4, segments: 32 }),
        material: beam,
        transform: { x: 3.5, y: 2.2, z: 2 }
    });
    const stone = createMesh({
        geometry: useIcoSphereGeometry({ radius: 0.7, subdivisions: 1 }),
        material: hologram,
        transform: { x: 3.5, y: 1.6, z: 2 }
    });

    // The magic circle, flat on the floor and turning.
    const circle = createMaterial({ ...CIRCLE, blend: 'additive', cull: 'none' });
    const ring = createMesh({
        geometry: useTorusGeometry({ radius: 1.6, tube: 0.06, radialSegments: 64, tubularSegments: 8 }),
        material: circle,
        transform: { x: -0.5, y: 0.05, z: 4.5 }
    });

    createText({
        text: 'SPACE: ADD THEIR COLOUR / COVER WITH IT',
        tint: '#c8d4ff',
        zIndex: 100,
        transform: { x: 8, y: 8 }
    });
    const state = createText({
        text: '',
        tint: '#c8d4ff',
        zIndex: 100,
        transform: { x: 8, y: 22 }
    });
    createText({
        text: 'GLOW MODELS · NACATAMALON',
        tint: '#c8d4ff',
        zIndex: 100,
        transform: { x: 8, y: HEIGHT - 16 }
    });

    const glows = [shield, hologram, beam, circle];
    const input = useActions();
    let adding = true;
    state.text = 'BLEND: ADDITIVE, THEY ONLY BRIGHTEN';

    useUpdate((_delta, time) => {
        if (input.justPressed('blend')) {
            adding = !adding;
            for (const glow of glows) {
                glow.blend = adding ? 'additive' : 'alpha';
            }
            state.text = adding ? 'BLEND: ADDITIVE, THEY ONLY BRIGHTEN' : 'BLEND: ALPHA, THEY COVER WHAT IS BEHIND';
        }

        stone.transform.rotationY = time * 0.8;
        stone.transform.rotationX = time * 0.5;
        ring.transform.rotationY = time * 0.6;

        const angle = time * ORBIT.speed * Math.PI * 2;
        camera.transform.x = Math.sin(angle) * ORBIT.distance;
        camera.transform.y = ORBIT.height;
        camera.transform.z = Math.cos(angle) * ORBIT.distance;
        cameraLookAt(camera, { x: 0, y: 1, z: 0 });
    });

    return createScene();
};
```

`settings.ts`:

```ts
/**
 * The game's size, in its own pixels.
 */
export const WIDTH = 640;
export const HEIGHT = 360;

/**
 * The night, and the haze the far side of the vault fades into.
 */
export const NIGHT = '#05070f';

/**
 * The glows: the shield's, the hologram's and the circle's colour.
 */
export const GLOW = {
    shield: [0.25, 0.75, 1.0] as [number, number, number],
    hologram: [0.3, 1.0, 0.65] as [number, number, number],
    circle: [1.0, 0.55, 0.15] as [number, number, number]
};

/**
 * The floor's bands: how far apart they are, in units from the camera, and how much darker each one
 * is than the one before. And the sonar: how far it reaches and how many times a second it goes out.
 */
export const BANDS = {
    width: 4,
    step: 0.15,
    reach: 40,
    pulse: 0.35
};

/**
 * How fast the camera goes round, in turns a second, and how far out and up it stays.
 */
export const ORBIT = {
    speed: 0.025,
    distance: 10,
    height: 4.5
};
```

`shaders/glow.ts`:

```ts
import {
    compileShader, composerAbs, composerAdd, composerCameraPos, composerClamp, composerDistance, composerFloor, composerFract,
    composerFresnel, composerLight, composerMax, composerMul, composerSin, composerSmoothstep, composerSub, composerSurface,
    composerSwizzle, composerTime, composerVec3, composerVec4, composerWorldPos, composerY
} from 'nacatamalon/shader';
import { BANDS, GLOW } from '../settings';

// The vault's shaders, written with the shader composer so they draw on both cards. The glows are
// not lit: their colour is the light they give, and how much of it is their alpha, which an
// additive model adds and an alpha one covers with.

const time = composerTime();
const height = composerY(composerWorldPos());

// The shield: bright at its rim, where it is seen edge on, with bands of light running up it.
const rim = composerFresnel(2.5);
const bands = composerMul(composerSmoothstep(0.85, 1, composerSin(composerSub(composerMul(height, 9), composerMul(time, 3)))), 0.35);
const shieldAmount = composerClamp(composerAdd(composerMul(rim, 1.1), bands), 0, 1);

export const SHIELD = compileShader({
    shader: 'mesh3d',
    color: composerVec4(composerVec3(...GLOW.shield), shieldAmount)
});

// The hologram: lines that crawl up it and a flicker, fading towards its top.
const lines = composerSmoothstep(0.4, 0.6, composerFract(composerSub(composerMul(height, 14), composerMul(time, 2))));
const flicker = composerAdd(0.85, composerMul(composerSin(composerMul(time, 37)), 0.15));
const holoAmount = composerMul(composerAdd(0.25, composerMul(lines, 0.55)), flicker);

export const HOLOGRAM = compileShader({
    shader: 'mesh3d',
    color: composerVec4(composerVec3(...GLOW.hologram), composerClamp(holoAmount, 0, 1))
});

// The beam it stands in: strongest at the floor and gone four units up.
export const BEAM = compileShader({
    shader: 'mesh3d',
    color: composerVec4(composerVec3(...GLOW.hologram), composerMul(composerClamp(composerSub(1, composerMul(height, 0.25)), 0, 1), 0.35))
});

// The magic circle: its own colour, pulsing.
export const CIRCLE = compileShader({
    shader: 'mesh3d',
    color: composerVec4(composerVec3(...GLOW.circle), composerAdd(0.6, composerMul(composerSin(composerMul(time, 4)), 0.3)))
});

// The floor, lit as the engine lights it, then darker in bands by how far it is from the camera, and
// a sonar ring going out from the camera. Both are distance(camera, point): composerCameraPos() is
// where the camera stands, so nothing has to be handed to the shader every frame.
const far = composerDistance(composerCameraPos(), composerWorldPos());
const band = composerFloor(composerMul(far, 1 / BANDS.width));
const shade = composerMax(composerSub(1, composerMul(band, BANDS.step)), 0.25);
const lit = composerMul(composerMul(composerSwizzle(composerSurface(), 'rgb'), composerLight()), shade);

const front = composerMul(composerFract(composerMul(time, BANDS.pulse)), BANDS.reach);
const ring = composerSmoothstep(0.8, 0, composerAbs(composerSub(far, front)));
const fading = composerClamp(composerSub(1, composerMul(far, 1 / BANDS.reach)), 0, 1);

export const FLOOR = compileShader({
    shader: 'mesh3d',
    color: composerVec4(composerAdd(lit, composerMul(composerVec3(...GLOW.shield), composerMul(ring, fading))), 1)
});
```
