# Energy charge

> A uniform driven by the game: the object you hand to createMaterial is the one the renderer reads, so writing a number into it reaches the next frame.

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

A **uniform driven by the game**. The values handed to `createMaterial` are kept, not copied, so the object you passed in is the one the renderer reads: write a number into it and the next frame draws with it. That is how gameplay state (health, a combo meter, a held button) feeds straight into a shader.

Here `mu.time` drives the automatic throb while `charge` goes there and back between 0 and 1. The sprite on the left has no material at all, to compare against.

## 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/charged_sprite.ts`:

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

import type { TTexture } from 'nacatamalon';
import { ENERGY, ENERGY_GLSL } from '../shaders/energy';

export const ChargedSprite = ({ texture }: { texture: TTexture; }) => {
    // The uniforms, kept where the game can reach them. glowColor never changes; charge does.
    const uniforms = {
        charge: 0,
        glowColor: [1, 0.82, 0.28]
    };

    const energy = createMaterial({
        name: 'energy',
        fragment: ENERGY,
        fragmentGlsl: ENERGY_GLSL,
        uniforms,
    });

    // Right: its own picture, and the effect over it.
    createSprite({
        texture,
        material: energy,
        width: 180,
        height: 180,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 260,
            y: 60,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // Here it is a plain there-and-back over about two seconds. In a game it would be the health
    // bar, or how long the button has been held.
    let direction = 1;

    useUpdate((delta) => {
        uniforms.charge += direction * delta * 0.5;

        if (uniforms.charge >= 1) {
            uniforms.charge = 1;
            direction = -1;
        }

        if (uniforms.charge <= 0) {
            uniforms.charge = 0;
            direction = 1;
        }
    });
};
```

`scenes/demo.ts`:

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

import { ChargedSprite } from '../components/charged_sprite';

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

    // Left: the same sprite with no material at all, to have something to compare against. Both are
    // placed by their top left corner.
    createSprite({
        texture,
        width: 180,
        height: 180,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 40,
            y: 60,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        }
    });

    useSpawn(ChargedSprite)({
        texture
    });

    return createScene();
};
```

`settings.ts`:

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

`shaders/energy.ts`:

```ts
export const ENERGY = `
fn effect(color: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
    // The fast heartbeat, from the clock the engine puts there.
    let pulse = 0.5 + 0.5 * sin(mu.time * 12.0);
    // charge (0..1) is the one the game writes. At 0 the sprite is untouched; near 1 it runs hot.
    let heat = mu.charge * (0.55 + 0.45 * pulse);
    let glow = mix(color.rgb, mu.glowColor, heat);
    return vec4<f32>(glow * (1.0 + heat * 0.8), color.a);
}
`;

export const ENERGY_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    float pulse = 0.5 + 0.5 * sin(mu.time * 12.0);
    float heat = mu.charge * (0.55 + 0.45 * pulse);
    vec3 glow = mix(color.rgb, mu.glowColor, heat);
    return vec4(glow * (1.0 + heat * 0.8), color.a);
}
`;
```
