gw-canvas
Version:
Library for rendering colorized bitmap fonts. Very fast, suitable for applications where the whole canvas needs frequent redrawing.
59 lines (45 loc) • 1.34 kB
JavaScript
export const VS = `
in vec2 position;
in uvec2 offset;
in uint fg;
in uint bg;
in uint glyph;
out vec2 fsOffset;
out vec4 fgRgb;
out vec4 bgRgb;
flat out uvec2 fontPos;
uniform int depth;
void main() {
float fdepth = float(depth) / 255.0;
gl_Position = vec4(position, fdepth, 1.0);
float fgr = float((fg & uint(0xF000)) >> 12);
float fgg = float((fg & uint(0x0F00)) >> 8);
float fgb = float((fg & uint(0x00F0)) >> 4);
float fga = float((fg & uint(0x000F)) >> 0);
fgRgb = vec4(fgr, fgg, fgb, fga) / 15.0;
float bgr = float((bg & uint(0xF000)) >> 12);
float bgg = float((bg & uint(0x0F00)) >> 8);
float bgb = float((bg & uint(0x00F0)) >> 4);
float bga = float((bg & uint(0x000F)) >> 0);
bgRgb = vec4(bgr, bgg, bgb, bga) / 15.0;
uint glyphX = (glyph & uint(0xF));
uint glyphY = (glyph >> 4);
fontPos = uvec2(glyphX, glyphY);
fsOffset = vec2(offset);
}`.trim();
export const FS = `
precision highp float;
in vec2 fsOffset;
in vec4 fgRgb;
in vec4 bgRgb;
flat in uvec2 fontPos;
out vec4 fragColor;
uniform sampler2D font;
uniform uvec2 tileSize;
void main() {
uvec2 fontPx = (tileSize * fontPos) + uvec2(vec2(tileSize) * fsOffset);
vec4 texel = texelFetch(font, ivec2(fontPx), 0).rgba;
fragColor = vec4(mix(bgRgb.rgb, fgRgb.rgb, texel.rgb), mix(bgRgb.a, fgRgb.a, texel.r));
}`.trim();