Hit Flash Shader for Pixel Art: White Blinks After a Hit
A hit flash turns a figure white for a moment after she is hit, often in two or three short blinks, and then gives her colours back. It tells the player that a hit landed and on whom, faster than a number above her head or a bar going down. The rule is short, and the one thing it must get right is when the flash starts.

Only the beekeeper is given to the rule. The game counts the ticks since the sting and hands them over; from that number alone the rule makes her white, then her own colours, then white again, and stops after 30 ticks.
Also in this lesson 2 things to read up on 3 questions to check yourself
The idea
In a game, many things happen in the same second: a wasp darts in, a swarm hums around a hive, the beekeeper takes a step. A hit is over in a single tick, and without a sign the player may not see it at all. The flash is that sign. For half a second she blinks as a white shape, and a figure that is white all over looks like nothing else in the scene, so the eye finds her at once.
It is a sprite effect: only the figure that was hit flashes, the world around her keeps its colours. The game does not draw a white copy of her. It hands her picture to the rule and tells it one more number: how many ticks ago she was hit.
How it works
The rule looks at the number at its own place. If it is 0, nothing was drawn there, and the answer is 0: the flash does not fill the empty space around her.
Then it looks at the ticks since the hit. Before any hit, and from 30 ticks after it on, she has her own colours. In between, the rule cuts the ticks since the hit into blinks of 6 ticks: in the blinks 0, 2 and 4 every drawn pixel is white, in the blinks 1 and 3 she has her own colours. That makes three white blinks with two pauses, half a second in all. The answer is always white, her own colour at that place, or nothing, so the rule cannot invent a colour.
The rule never looks at the game's clock. Whatever the clock says, a hit is white on its own tick and blinks the same way as every other hit.
In the picture above, the game counts the ticks since each sting for you, and the slider for them follows. As soon as you touch a slider, the picture stops counting and holds the numbers you set:
- ticks since the hit: 0 is the tick of the hit.
- length of the flash: how many ticks after the hit she can be white.
- length of one blink: how many ticks a white blink lasts, and the pause after it.
What goes wrong
Asked for a blinking flash, an AI often writes the blink from the game's time: "while the flash is on, be white whenever the time is in an even blink". The shader has a clock built in, and blinking by the clock is the textbook way to blink, so the AI takes it. In a quick test it looks right: she blinks.
But the blinks now follow the clock, not the hit. A hit that lands in one of the clock's pauses shows her own colours at the moment of impact, and the white comes only when the clock says so. Switch the picture to "With the mistake" and watch both stings. At the first one she keeps her own colours for 3 ticks after the sting, while the wasp is already flying off, and the last white blink is cut to 3 ticks. At the second she is white for only 2 ticks before a pause, and the next white blinks come 4 ticks before the rule's. Every hit in a game would blink a little differently, and some would seem to come late.
Two tests catch it: "on the tick of the hit she is white, whatever the game's clock says" asks for white on 50 different ticks, and "two hits at different times of the game blink alike" compares two hits tick by tick.
The mistake, line by line
- /* Hit flash, with the mistake: the blinks are counted from the game's clock instead of from the hit.
- While the flash lasts she is white whenever the clock is in a white blink, so the flash starts at a
- moment that has nothing to do with the hit. Hit on the wrong tick, she shows her own colours at the
- moment of impact and turns white only later, and no two hits blink alike.
+ /* Hit flash: for a few ticks after a hit, every drawn pixel of the sprite turns white, in blinks, and
+ then she has her own colours again. The player sees at once that the hit landed and on whom, even
+ in a crowd, even when the hit is small.
+ The flash is counted from the hit. The game says how many ticks ago she was hit, and the rule
+ answers from that number alone: on the tick of the hit she is white, whatever the game's clock
+ shows, and every hit blinks the same way.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
}
- /** The rule, wrong: white in blinks for `flash` ticks after the hit, but the blinks follow the clock. */
- export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
+ /** The rule: white in blinks for `flash` ticks after the hit, counted from the hit; her own colours before and after. */
+ export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
if (n === 0) return 0; // nothing stays nothing, also in the flash
if (p.since < 0 || p.since >= p.flash) return n; // not hit yet, or the flash is over
const each = Math.max(1, p.blink);
- // wrong: the blink is worked out from the game's clock, not from the hit
- return Math.floor(tick / each) % 2 === 0 ? WHITE : n;
+ // counted from the hit: blink 0 is white, blink 1 her own colours, blink 2 white again, and so on
+ return Math.floor(p.since / each) % 2 === 0 ? WHITE : n;
}What the test says about the mistake
✔ before the hit and after the flash she has her own colours
✔ an empty pixel stays empty, also while she is white
✖ on the tick of the hit she is white, whatever the game's clock says
hit on tick 6
got [
0, 1, 1, 1, 0, 1,
3, 8, 3, 1, 0, 2,
9, 2, 0
], expected [
0, 5, 5, 5, 0, 5,
5, 5, 5, 5, 0, 5,
5, 5, 0
]
✖ the flash blinks: white for as long as a blink lasts, then her own colours for as long
got 'WW------WWWWWW------WWWWWW----', expected 'WWWWWW------WWWWWW------WWWWWW'
✖ two hits at different times of the game blink alike
tick 1 after each hit
got [
0, 5, 5, 5, 0, 5,
5, 5, 5, 5, 0, 5,
5, 5, 0
], expected [
0, 1, 1, 1, 0, 1,
3, 8, 3, 1, 0, 2,
9, 2, 0
]
✖ a shorter flash and a faster blink are set by their numbers
got '---WWW---WWW----', expected 'WWW---WWW-------'
✔ every answer is white, her own colour there, or nothing
7 tests: 3 passed, 4 failedTell your AI
Write a hit flash shader for my pixel art sprite: after a hit, the sprite blinks white for a short time, then shows its own colours again.
- Work on whole pixels and whole ticks (60 a second). No smoothing, no blending, no fading: each pixel is either pure white or its own colour.
- Empty pixels (alpha 0) stay empty, also during the flash.
- The game counts the ticks since the last hit and gives that number to the shader as a setting. Count the blinks from that number, never from the game's clock or time: on the tick of the hit the sprite is white.
- The flash lasts [30] ticks. One white blink lasts [6] ticks, then [6] ticks of its own colours, then white again.
- If the sprite is hit again during a flash, the count starts again at 0.
Before you write the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = the colours of my palette), a place, the game's tick and the ticks since the hit, and returns the number for that one pixel. Test it: on the tick of the hit the sprite is white whatever the game's tick is, two hits at different ticks blink alike, and after the flash every pixel has its own colour again. Show me the test's output. Then write the shader so that it does exactly what the function does.
The code
Three times the same rule. The first is the one we test; the two shaders are that rule in a shader language, which is what your game runs.
The rule, tested: shader.ts
/* Hit flash: for a few ticks after a hit, every drawn pixel of the sprite turns white, in blinks, and
then she has her own colours again. The player sees at once that the hit landed and on whom, even
in a crowd, even when the hit is small.
The flash is counted from the hit. The game says how many ticks ago she was hit, and the rule
answers from that number alone: on the tick of the hit she is white, whatever the game's clock
shows, and every hit blinks the same way.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** Ticks since the hit, counted by the game: 0 on the tick she is hit. */
since: 60,
/** How long the whole flash lasts, in ticks (60 are a second). */
flash: 30,
/** How long one white blink lasts, and the pause after it, in ticks. */
blink: 6,
};
export type Params = typeof DEFAULTS;
/** The number of white in the palette. */
const WHITE = 5;
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** The rule: white in blinks for `flash` ticks after the hit, counted from the hit; her own colours before and after. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
if (n === 0) return 0; // nothing stays nothing, also in the flash
if (p.since < 0 || p.since >= p.flash) return n; // not hit yet, or the flash is over
const each = Math.max(1, p.blink);
// counted from the hit: blink 0 is white, blink 1 her own colours, blink 2 white again, and so on
return Math.floor(p.since / each) % 2 === 0 ? WHITE : n;
}
The same rule as a shader: GLSL
#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;
uniform sampler2D SRC; // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK; // the time in ticks, 60 a second, counted by the game
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int since; // ticks since the hit, counted by the game: 0 on the tick she is hit
uniform int flash; // how long the whole flash lasts, in ticks (60 are a second)
uniform int blink; // how long one white blink lasts, and the pause after it, in ticks
// the number of white in the palette
const int WHITE = 5;
// white in blinks for `flash` ticks after the hit, counted from the hit; her own colours before and after
int shade(ivec2 p) {
int n = at(p);
if (n == 0) return 0; // nothing stays nothing, also in the flash
if (since < 0 || since >= flash) return n; // not hit yet, or the flash is over
int each = max(1, blink);
// counted from the hit: blink 0 is white, blink 1 her own colours, blink 2 white again, and so on
return (since / each) % 2 == 0 ? WHITE : n;
}
// ---- end of the rule ----
out vec4 FRAGMENT;
void main() {
// the pixel's place, counted from the top left as in the picture
ivec2 p = ivec2(int(gl_FragCoord.x), textureSize(SRC, 0).y - 1 - int(gl_FragCoord.y));
int n = shade(p);
FRAGMENT = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
The same rule for Godot 4
shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on the sprite, and give it the sprite's own picture once:
// material.set_shader_parameter("SRC", texture)
uniform sampler2D SRC : filter_nearest;
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
// tick += 1
// material.set_shader_parameter("TICK", tick)
uniform int TICK;
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int since; // ticks since the hit, counted by the game: 0 on the tick she is hit
uniform int flash; // how long the whole flash lasts, in ticks (60 are a second)
uniform int blink; // how long one white blink lasts, and the pause after it, in ticks
// the number of white in the palette
const int WHITE = 5;
// white in blinks for `flash` ticks after the hit, counted from the hit; her own colours before and after
int shade(ivec2 p) {
int n = at(p);
if (n == 0) return 0; // nothing stays nothing, also in the flash
if (since < 0 || since >= flash) return n; // not hit yet, or the flash is over
int each = max(1, blink);
// counted from the hit: blink 0 is white, blink 1 her own colours, blink 2 white again, and so on
return (since / each) % 2 == 0 ? WHITE : n;
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(UV * vec2(textureSize(SRC, 0)));
int n = shade(p);
COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
The test: shader.test.ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { DEFAULTS, type Frame, shade } from "./shader.ts";
/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. 5 is white, so it is left out. */
function picture(rows: string[]): Frame {
return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}
const WHITE = 5;
const BEETLE = picture([
"01110",
"13831",
"02920",
]);
/** The beetle's whole picture at a moment: `since` ticks after the hit, on the game's tick `tick`. */
function whole(since: number, tick: number, params = DEFAULTS): number[] {
const out: number[] = [];
for (let y = 0; y < BEETLE.height; y++) for (let x = 0; x < BEETLE.width; x++) out.push(shade(x, y, tick, BEETLE, { ...params, since }));
return out;
}
const own = [...BEETLE.index];
const white = own.map((n) => (n === 0 ? 0 : WHITE));
test("before the hit and after the flash she has her own colours", () => {
assert.deepEqual(whole(-1, 7), own, "not hit yet");
assert.deepEqual(whole(DEFAULTS.flash, 7), own, "the tick the flash ends");
assert.deepEqual(whole(DEFAULTS.flash + 100, 7), own, "long after");
});
test("an empty pixel stays empty, also while she is white", () => {
for (let since = 0; since < DEFAULTS.flash; since++) {
assert.equal(shade(0, 0, since, BEETLE, { ...DEFAULTS, since }), 0, `tick ${since} after the hit`);
}
});
test("on the tick of the hit she is white, whatever the game's clock says", () => {
for (let tick = 0; tick < 50; tick++) assert.deepEqual(whole(0, tick), white, `hit on tick ${tick}`);
});
test("the flash blinks: white for as long as a blink lasts, then her own colours for as long", () => {
const seen = [];
for (let since = 0; since < DEFAULTS.flash; since++) seen.push(whole(since, 1000 + since)[2] === WHITE ? "W" : "-");
// blink 6, flash 30: three white blinks with two pauses between
assert.equal(seen.join(""), "WWWWWW------WWWWWW------WWWWWW");
});
test("two hits at different times of the game blink alike", () => {
for (let since = 0; since < 40; since++) {
assert.deepEqual(whole(since, 100 + since), whole(since, 257 + since), `tick ${since} after each hit`);
}
});
test("a shorter flash and a faster blink are set by their numbers", () => {
const seen = [];
for (let since = 0; since < 16; since++) seen.push(whole(since, 3 + since, { ...DEFAULTS, flash: 12, blink: 3 })[7] === WHITE ? "W" : "-");
assert.equal(seen.join(""), "WWW---WWW-------");
});
test("every answer is white, her own colour there, or nothing", () => {
for (let since = -2; since < 40; since++) {
whole(since, 11 * since + 5).forEach((n, i) => assert.ok(n === own[i] || (n === WHITE && own[i] !== 0), `pixel ${i}, tick ${since} after the hit: ${n}`));
}
});
- 7 tests pass on the rule.
- With the mistake put in, 4 of them fail: "on the tick of the hit she is white, whatever the game's clock says", "the flash blinks: white for as long as a blink lasts, then her own colours for as long", "two hits at different times of the game blink alike", "a shorter flash and a faster blink are set by their numbers".
- The mistake shows in the picture above: switch to "With the mistake".
- The shader ran in a browser (Chrome/158.0.8127.37) on 14 pictures and gave the rule's picture in 453,600 of 453,600 pixels.
Try it
- Drag "ticks since the hit" slowly from 0 to 60. She is white from 0 to 5, coloured from 6 to 11, white again from 12, and from 30 on she keeps her colours however far you drag.
- Set "ticks since the hit" to 0 and switch to "With the mistake". With the rule she stays white, because the number does not change. With the mistake she blinks by herself, because the clock goes on.
- Set the length of the flash to 6, the same as one blink, and drag "ticks since the hit" again. There are no pauses any more: one white flash of 6 ticks, a tenth of a second, then her colours. Whether your flash should blink or not is your choice; the rule does both.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
The rule that often comes with the flash: a short time after a hit in which further hits change nothing, why a hit it ignores must not start it again, and why the blink must never decide whether a hit lands.
The other half of a hit that lands: the game's clock stops for a split second while input and sound go on, and a second hit during the pause takes the longer time instead of adding up.
Check yourself: three questions
Each answer is checked and explained at once. The questions are part of the quiz, so they count for your Game Coder IQ like any other.
For every drawn pixel the hit flash rule decides whether she is white right now. Which number does it count her blinks from?
Next lesson Palette Swap Shader for Pixel Art: New Clothes, Same Sprite
Give the whole lesson to your AI
The button copies what to tell it together with the tested rule, its test and both shaders, so your AI has everything to build this in your game and to show you that it works.
This is a lesson from the course "Shaders for Pixel Art" on spielerando.com: https://spielerando.com/en/courses/pixel-shaders/hit-flash
Write a hit flash shader for my pixel art sprite: after a hit, the sprite blinks white for a short time, then shows its own colours again.
- Work on whole pixels and whole ticks (60 a second). No smoothing, no blending, no fading: each pixel is either pure white or its own colour.
- Empty pixels (alpha 0) stay empty, also during the flash.
- The game counts the ticks since the last hit and gives that number to the shader as a setting. Count the blinks from that number, never from the game's clock or time: on the tick of the hit the sprite is white.
- The flash lasts [30] ticks. One white blink lasts [6] ticks, then [6] ticks of its own colours, then white again.
- If the sprite is hit again during a flash, the count starts again at 0.
Before you write the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = the colours of my palette), a place, the game's tick and the ticks since the hit, and returns the number for that one pixel. Test it: on the tick of the hit the sprite is white whatever the game's tick is, two hits at different ticks blink alike, and after the flash every pixel has its own colour again. Show me the test's output. Then write the shader so that it does exactly what the function does.
The rule, tested (shader.ts):
```ts
/* Hit flash: for a few ticks after a hit, every drawn pixel of the sprite turns white, in blinks, and
then she has her own colours again. The player sees at once that the hit landed and on whom, even
in a crowd, even when the hit is small.
The flash is counted from the hit. The game says how many ticks ago she was hit, and the rule
answers from that number alone: on the tick of the hit she is white, whatever the game's clock
shows, and every hit blinks the same way.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** Ticks since the hit, counted by the game: 0 on the tick she is hit. */
since: 60,
/** How long the whole flash lasts, in ticks (60 are a second). */
flash: 30,
/** How long one white blink lasts, and the pause after it, in ticks. */
blink: 6,
};
export type Params = typeof DEFAULTS;
/** The number of white in the palette. */
const WHITE = 5;
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** The rule: white in blinks for `flash` ticks after the hit, counted from the hit; her own colours before and after. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
if (n === 0) return 0; // nothing stays nothing, also in the flash
if (p.since < 0 || p.since >= p.flash) return n; // not hit yet, or the flash is over
const each = Math.max(1, p.blink);
// counted from the hit: blink 0 is white, blink 1 her own colours, blink 2 white again, and so on
return Math.floor(p.since / each) % 2 === 0 ? WHITE : n;
}
```
Its test (shader.test.ts):
```ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { DEFAULTS, type Frame, shade } from "./shader.ts";
/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. 5 is white, so it is left out. */
function picture(rows: string[]): Frame {
return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}
const WHITE = 5;
const BEETLE = picture([
"01110",
"13831",
"02920",
]);
/** The beetle's whole picture at a moment: `since` ticks after the hit, on the game's tick `tick`. */
function whole(since: number, tick: number, params = DEFAULTS): number[] {
const out: number[] = [];
for (let y = 0; y < BEETLE.height; y++) for (let x = 0; x < BEETLE.width; x++) out.push(shade(x, y, tick, BEETLE, { ...params, since }));
return out;
}
const own = [...BEETLE.index];
const white = own.map((n) => (n === 0 ? 0 : WHITE));
test("before the hit and after the flash she has her own colours", () => {
assert.deepEqual(whole(-1, 7), own, "not hit yet");
assert.deepEqual(whole(DEFAULTS.flash, 7), own, "the tick the flash ends");
assert.deepEqual(whole(DEFAULTS.flash + 100, 7), own, "long after");
});
test("an empty pixel stays empty, also while she is white", () => {
for (let since = 0; since < DEFAULTS.flash; since++) {
assert.equal(shade(0, 0, since, BEETLE, { ...DEFAULTS, since }), 0, `tick ${since} after the hit`);
}
});
test("on the tick of the hit she is white, whatever the game's clock says", () => {
for (let tick = 0; tick < 50; tick++) assert.deepEqual(whole(0, tick), white, `hit on tick ${tick}`);
});
test("the flash blinks: white for as long as a blink lasts, then her own colours for as long", () => {
const seen = [];
for (let since = 0; since < DEFAULTS.flash; since++) seen.push(whole(since, 1000 + since)[2] === WHITE ? "W" : "-");
// blink 6, flash 30: three white blinks with two pauses between
assert.equal(seen.join(""), "WWWWWW------WWWWWW------WWWWWW");
});
test("two hits at different times of the game blink alike", () => {
for (let since = 0; since < 40; since++) {
assert.deepEqual(whole(since, 100 + since), whole(since, 257 + since), `tick ${since} after each hit`);
}
});
test("a shorter flash and a faster blink are set by their numbers", () => {
const seen = [];
for (let since = 0; since < 16; since++) seen.push(whole(since, 3 + since, { ...DEFAULTS, flash: 12, blink: 3 })[7] === WHITE ? "W" : "-");
assert.equal(seen.join(""), "WWW---WWW-------");
});
test("every answer is white, her own colour there, or nothing", () => {
for (let since = -2; since < 40; since++) {
whole(since, 11 * since + 5).forEach((n, i) => assert.ok(n === own[i] || (n === WHITE && own[i] !== 0), `pixel ${i}, tick ${since} after the hit: ${n}`));
}
});
```
The same rule as a GLSL shader (WebGL 2, OpenGL ES 3.0), checked against the rule pixel for pixel:
```glsl
#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;
uniform sampler2D SRC; // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK; // the time in ticks, 60 a second, counted by the game
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int since; // ticks since the hit, counted by the game: 0 on the tick she is hit
uniform int flash; // how long the whole flash lasts, in ticks (60 are a second)
uniform int blink; // how long one white blink lasts, and the pause after it, in ticks
// the number of white in the palette
const int WHITE = 5;
// white in blinks for `flash` ticks after the hit, counted from the hit; her own colours before and after
int shade(ivec2 p) {
int n = at(p);
if (n == 0) return 0; // nothing stays nothing, also in the flash
if (since < 0 || since >= flash) return n; // not hit yet, or the flash is over
int each = max(1, blink);
// counted from the hit: blink 0 is white, blink 1 her own colours, blink 2 white again, and so on
return (since / each) % 2 == 0 ? WHITE : n;
}
// ---- end of the rule ----
out vec4 FRAGMENT;
void main() {
// the pixel's place, counted from the top left as in the picture
ivec2 p = ivec2(int(gl_FragCoord.x), textureSize(SRC, 0).y - 1 - int(gl_FragCoord.y));
int n = shade(p);
FRAGMENT = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
```
The same rule for Godot 4 (not run by the authors):
```gdshader
shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on the sprite, and give it the sprite's own picture once:
// material.set_shader_parameter("SRC", texture)
uniform sampler2D SRC : filter_nearest;
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
// tick += 1
// material.set_shader_parameter("TICK", tick)
uniform int TICK;
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int since; // ticks since the hit, counted by the game: 0 on the tick she is hit
uniform int flash; // how long the whole flash lasts, in ticks (60 are a second)
uniform int blink; // how long one white blink lasts, and the pause after it, in ticks
// the number of white in the palette
const int WHITE = 5;
// white in blinks for `flash` ticks after the hit, counted from the hit; her own colours before and after
int shade(ivec2 p) {
int n = at(p);
if (n == 0) return 0; // nothing stays nothing, also in the flash
if (since < 0 || since >= flash) return n; // not hit yet, or the flash is over
int each = max(1, blink);
// counted from the hit: blink 0 is white, blink 1 her own colours, blink 2 white again, and so on
return (since / each) % 2 == 0 ? WHITE : n;
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(UV * vec2(textureSize(SRC, 0)));
int n = shade(p);
COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
```
How useful was this page?
Nobody has rated this page yet.