1// The world is a three.js scene drawn at exactly one texel per character cell, 2// then a single fullscreen pass quantizes it into glyphs of the amber ramp. 3// Two channels carry the shading: R = luminance (which glyph), G = palette index. 4import * as THREE from 'three'; 5 6export const GLYPHS = ' .:-=+*#%@'; 7// 0-7 is the amber ramp used for everything that flies. 8-12 are reserved for 8// celestial bodies, so a rust planet and a steel planet never read as each other. 9// 13-14 are damage: a hurt piece can never be mistaken for a healthy one. 15 is 10// the AI hull: neither the gold of another player nor the red of an enemy. 11export const PALETTE = [ 12 0x0c0803, 0x241806, 0x3d2709, 0x57370c, 0x754c11, 0x98631a, 0xc78a20, 0xffe9a8, 13 0x4a1d0f, 0x9c4f28, 0x1f4453, 0x7fc0d4, 0xfff6dd, 0x7a1512, 0xff4a3d, 0x5fbf8a, 14]; 15const ATLAS_CELL = 32; 16 17// R = luminance, G = palette index. 18export const lum = (v: number, idx = 7) => new THREE.Color(v, idx / (PALETTE.length - 1), 0); 19 20// One cell of white ink per character, in the same monospace face as the ramp: the 21// glyph atlas and any hud word are the same kind of texture. 22export function textAtlas(text: string): THREE.Texture { 23 const c = document.createElement('canvas'); 24 c.width = text.length * ATLAS_CELL; 25 c.height = ATLAS_CELL; 26 const g = c.getContext('2d')!; 27 g.font = `700 ${ATLAS_CELL - 1}px ui-monospace, SFMono-Regular, Menlo, monospace`; 28 g.textAlign = 'center'; 29 g.textBaseline = 'middle'; 30 g.fillStyle = '#fff'; 31 for (let i = 0; i < text.length; i++) { 32 g.fillText(text[i], i * ATLAS_CELL + ATLAS_CELL / 2, ATLAS_CELL / 2); 33 } 34 const t = new THREE.CanvasTexture(c); 35 t.minFilter = t.magFilter = THREE.LinearFilter; 36 t.generateMipmaps = false; 37 return t; 38} 39 40export function paletteTexture(): THREE.DataTexture { 41 const d = new Uint8Array(PALETTE.length * 4); 42 PALETTE.forEach((hex, i) => d.set([(hex >> 16) & 255, (hex >> 8) & 255, hex & 255, 255], i * 4)); 43 const t = new THREE.DataTexture(d, PALETTE.length, 1, THREE.RGBAFormat); 44 t.minFilter = t.magFilter = THREE.NearestFilter; 45 t.needsUpdate = true; 46 return t; 47} 48 49export const VERT = /* glsl */ `void main() { gl_Position = vec4(position.xy, 0.0, 1.0); }`; 50 51export const FRAG = /* glsl */ ` 52uniform sampler2D uScene; 53uniform sampler2D uAtlas; 54uniform sampler2D uPalette; 55uniform vec3 uBg; 56uniform vec2 uCells; 57uniform float uCellPx; 58uniform sampler2D uWord; // hud ink, in cells, drawn over the glyphs 59uniform vec4 uWordCell; // x, y, width, height of the word, in cells from the corner 60uniform vec3 uWordCol; 61uniform sampler2D uWord2; // a second word, on the same row as the first 62uniform vec4 uWord2Cell; 63uniform vec3 uWord2Col; 64 65// One word, spelled from its own atlas and placed in cells. A zero-sized rect 66// covers nothing, which is also how a word is hidden. 67vec3 spell(vec3 col, vec2 uv, sampler2D atlas, vec4 cell, vec3 ink) { 68 vec2 w = (uv - cell.xy) / cell.zw; 69 return (w.x >= 0.0 && w.x < 1.0 && w.y >= 0.0 && w.y < 1.0) 70 ? mix(col, ink, texture2D(atlas, w).a) 71 : col; 72} 73 74void main() { 75 vec2 uv = gl_FragCoord.xy / uCellPx; 76 vec3 s = texture2D(uScene, (floor(uv) + 0.5) / uCells).rgb; 77 float band = floor(s.r * ${GLYPHS.length - 1}.0 + 0.5); 78 vec3 col = texture2D(uPalette, vec2((s.g * ${PALETTE.length - 1}.0 + 0.5) / ${PALETTE.length}.0, 0.5)).rgb; 79 vec2 local = clamp(fract(uv), 0.02, 0.98); 80 float ink = texture2D(uAtlas, vec2((band + local.x) / ${GLYPHS.length}.0, local.y)).a; 81 vec3 outCol = mix(uBg, col, ink); 82 outCol = spell(outCol, uv, uWord, uWordCell, uWordCol); 83 outCol = spell(outCol, uv, uWord2, uWord2Cell, uWord2Col); 84 gl_FragColor = vec4(outCol, 1.0); 85} 86`;