space-browser / commits / af072af
fix: raster fine tuning
@@ -10,6 +10,11 @@ const SHAPES: Shape[] = ["triangle", "square", "circle", "pentagon", "star"];
1010 // here or points would render as vertical ovals.
1111 const CELL_ASPECT = CELL_H / CELL_W;
1212
13+// Scene texels per glyph cell side. The world is drawn SS times finer than the
14+// glyph grid and every cell is averaged down to one character, so geometry
15+// smaller than a cell still reads instead of flickering in and out.
16+const SS = 3;
17+
1318 // Fleet contacts share the gold accent; the mark shape tells them apart:
1419 // a solid disc is a rogue fleet, a hollow ring a friendly one.
1520 const FLEET_COLOR = "#ffcf6b";
@@ -39,13 +44,25 @@ const KIND_SHAPE: Record<string, number> = {
3944 wreck: 0,
4045 };
4146
47+// Smallest a body is ever drawn, in glyph cells: below that it would drop out
48+// of the raster at mid zoom.
49+const KIND_MIN: Record<string, number> = {
50+ star: 2.2,
51+ planet: 1.8,
52+ asteroid: 1.4,
53+ comms: 1.8,
54+ shipyard: 1.8,
55+ wreck: 1.4,
56+};
4257 const POINT_VERT = /* glsl */ `
4358 attribute float aSize;
4459 attribute vec3 aColor;
4560 attribute float aShape;
4661 attribute float aPhase;
62+attribute float aMin;
4763 uniform float uZoom;
4864 uniform float uAspect;
65+uniform float uCellTexels;
4966 uniform float uTime;
5067 uniform sampler2D tFog;
5168 uniform float uFogRadius;
@@ -59,9 +76,13 @@ void main() {
5976 vColor = aColor * mix(1.0, uFogDim + (1.0 - uFogDim) * known, uFogged);
6077 vShape = aShape;
6178 vTw = 0.7 + 0.3 * sin(uTime * 1.3 + aPhase);
79+ // A mark that shrinks under one glyph cell stops reading: one scene texel is
80+ // a ninth of a cell, so a sub-cell point fades to nothing. Lift it to its
81+ // floor size instead.
82+ float px = max(aSize * 2.0 * uZoom * uAspect, aMin * uCellTexels);
6283 vec4 mv = modelViewMatrix * vec4(position, 1.0);
6384 gl_Position = projectionMatrix * mv;
64- gl_PointSize = clamp(aSize * 2.0 * uZoom * uAspect, 1.0, 900.0);
85+ gl_PointSize = clamp(px, 1.0, ${900 * SS}.0);
6586 }
6687 `;
6788
@@ -80,7 +101,7 @@ void main() {
80101 } else if (vShape < 1.5) {
81102 a = smoothstep(0.45, 0.72, r) * (1.0 - smoothstep(0.78, 1.0, r));
82103 } else {
83- a = max(1.0 - smoothstep(0.05, 0.30, abs(d.x)), 1.0 - smoothstep(0.05, 0.30, abs(d.y)));
104+ a = max(1.0 - smoothstep(0.18, 0.55, abs(d.x)), 1.0 - smoothstep(0.18, 0.55, abs(d.y)));
84105 }
85106 if (a <= 0.001) discard;
86107 gl_FragColor = vec4(vColor * vTw, a);
@@ -101,6 +122,7 @@ const FOG_FRAG = /* glsl */ `
101122 uniform sampler2D tFog;
102123 uniform float uRadius;
103124 uniform float uTime;
125+uniform float uCellWorld;
104126 uniform vec3 uMist;
105127 varying vec2 vWorld;
106128 float hash(vec2 p) {
@@ -113,16 +135,31 @@ float noise(vec2 p) {
113135 return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x),
114136 mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
115137 }
138+// One octave anchored in world units, faded out once its lobes get smaller
139+// than a few glyph cells. Without the fade the fine octaves alias into a
140+// regular moiré when zoomed out; without the coarse ones the mist is flat when
141+// zoomed in. Drift is proportional to the period, so no octave shears.
142+float band(vec2 p, float period, vec2 drift) {
143+ return noise((p + drift * (period / 30000.0)) / period);
144+}
116145 void main() {
117146 float unknown = texture2D(tFog, vWorld / (2.0 * uRadius) + 0.5).r;
118147 if (unknown < 0.01) discard;
119- // Two octaves sized in world units, so the cloud keeps its place at every
120- // zoom and only the slow drift moves it. Density rides on a base veil, so
121- // thin spots still hide space instead of punching holes in the layer.
122- vec2 drift = vec2(uTime * 120.0, uTime * -90.0);
123- float lobe = noise((vWorld + drift) * 0.000025);
124- float puff = noise((vWorld - drift * 0.6) * 0.00007);
125- float n = lobe * 0.62 + puff * 0.38;
148+ vec2 drift = vec2(uTime * 400.0, uTime * -300.0);
149+ float lo = 2.0 * uCellWorld;
150+ float hi = 6.0 * uCellWorld;
151+ float wa = smoothstep(lo, hi, 1600.0);
152+ float wb = smoothstep(lo, hi, 7000.0);
153+ float wc = smoothstep(lo, hi, 30000.0);
154+ float wd = smoothstep(lo, hi, 130000.0);
155+ float ws = wa * 0.30 + wb * 0.30 + wc * 0.25 + wd * 0.35;
156+ float n = 0.5;
157+ if (ws > 0.0001) {
158+ n = (wa * 0.30 * band(vWorld, 1600.0, drift) +
159+ wb * 0.30 * band(vWorld, 7000.0, drift) +
160+ wc * 0.25 * band(vWorld, 30000.0, drift) +
161+ wd * 0.35 * band(vWorld, 130000.0, drift)) / ws;
162+ }
126163 float a = unknown * mix(0.30, 0.95, smoothstep(0.25, 0.75, n));
127164 if (a < 0.01) discard;
128165 gl_FragColor = vec4(uMist, a);
@@ -143,8 +180,16 @@ void main() {
143180 vec2 g = vUv * uCells;
144181 vec2 cell = floor(g);
145182 vec2 inCell = fract(g);
146- vec2 uv = (cell + 0.5) / uCells;
147- vec3 sc = texture2D(tScene, uv).rgb;
183+ // Average the cell's scene texels: point sampling one of them makes every
184+ // moving edge pick a new glyph, which is what the picture flickered with.
185+ vec3 sc = vec3(0.0);
186+ for (int i = 0; i < ${SS}; i++) {
187+ for (int j = 0; j < ${SS}; j++) {
188+ vec2 uv = (cell + (vec2(float(i), float(j)) + 0.5) / float(${SS})) / uCells;
189+ sc += texture2D(tScene, uv).rgb;
190+ }
191+ }
192+ sc /= float(${SS * SS});
148193 float lum = dot(sc, vec3(0.299, 0.587, 0.114));
149194 float n = (hash(cell) - 0.5) * 0.10;
150195 float t = clamp(lum + n, 0.0, 1.0);
@@ -361,6 +406,7 @@ export class Engine {
361406 tFog: { value: this.fogTex },
362407 uRadius: { value: cfg.galaxy.radius },
363408 uTime: { value: 0 },
409+ uCellWorld: { value: 1 },
364410 uMist: { value: new THREE.Color(FOG_MIST) },
365411 },
366412 vertexShader: FOG_VERT,
@@ -391,11 +437,13 @@ export class Engine {
391437 const projSize = new Float32Array(4096);
392438 const projShape = new Float32Array(4096);
393439 const projPhase = new Float32Array(4096);
440+ const projMin = new Float32Array(4096);
394441 this.projGeom.setAttribute("position", new THREE.BufferAttribute(this.projPos, 3));
395442 this.projGeom.setAttribute("aColor", new THREE.BufferAttribute(this.projCol, 3));
396443 this.projGeom.setAttribute("aSize", new THREE.BufferAttribute(projSize, 1));
397444 this.projGeom.setAttribute("aShape", new THREE.BufferAttribute(projShape, 1));
398445 this.projGeom.setAttribute("aPhase", new THREE.BufferAttribute(projPhase, 1));
446+ this.projGeom.setAttribute("aMin", new THREE.BufferAttribute(projMin, 1));
399447 const projPoints = new THREE.Points(this.projGeom, this.pointMat(false, false));
400448 projPoints.frustumCulled = false;
401449 this.scene.add(projPoints);
@@ -455,6 +503,7 @@ export class Engine {
455503 uniforms: {
456504 uZoom: { value: 1 },
457505 uAspect: { value: CELL_ASPECT },
506+ uCellTexels: { value: SS },
458507 uTime: { value: 0 },
459508 tFog: { value: this.fogTex },
460509 uFogRadius: { value: this.cfg.galaxy.radius },
@@ -485,6 +534,7 @@ export class Engine {
485534 const shape = new Float32Array(n);
486535 const phase = new Float32Array(n);
487536 const c = new THREE.Color("#2a1d10");
537+ const min = new Float32Array(n);
488538 for (let k = 0; k < n; k++) {
489539 const a = rnd() * Math.PI * 2;
490540 const r = radius * Math.sqrt(rnd());
@@ -507,6 +557,7 @@ export class Engine {
507557 g.setAttribute("aColor", new THREE.BufferAttribute(col, 3));
508558 g.setAttribute("aShape", new THREE.BufferAttribute(shape, 1));
509559 g.setAttribute("aPhase", new THREE.BufferAttribute(phase, 1));
560+ g.setAttribute("aMin", new THREE.BufferAttribute(min, 1));
510561 return g;
511562 }
512563
@@ -523,12 +574,14 @@ export class Engine {
523574 const col = new Float32Array(n * 3);
524575 const shape = new Float32Array(n);
525576 const phase = new Float32Array(n);
577+ const min = new Float32Array(n);
526578 const c = new THREE.Color();
527579 list.forEach((p, i) => {
528580 pos[i * 3] = p.x;
529581 pos[i * 3 + 1] = p.y;
530582 pos[i * 3 + 2] = -1;
531583 size[i] = Math.max(3, p.r);
584+ min[i] = KIND_MIN[p.kind] ?? 0;
532585 c.set(KIND_COLOR[p.kind] ?? "#c18f45");
533586 col[i * 3] = c.r;
534587 col[i * 3 + 1] = c.g;
@@ -541,6 +594,7 @@ export class Engine {
541594 this.poiGeom.setAttribute("aColor", new THREE.BufferAttribute(col, 3));
542595 this.poiGeom.setAttribute("aShape", new THREE.BufferAttribute(shape, 1));
543596 this.poiGeom.setAttribute("aPhase", new THREE.BufferAttribute(phase, 1));
597+ this.poiGeom.setAttribute("aMin", new THREE.BufferAttribute(min, 1));
544598 }
545599
546600 // setFog loads the shared explored-space snapshot: one bit per texel, 1
@@ -601,14 +655,14 @@ export class Engine {
601655 updateShips(players: PlayerView[], npcs: NPCView[], selfId: string) {
602656 const counts: Record<Shape, number> = { triangle: 0, square: 0, circle: 0, pentagon: 0, star: 0 };
603657 const seen = new Set<string>();
604- const add = (id: string, pieces: PlayerView["ship"]["pieces"], x: number, y: number, a: number) => {
658+ const add = (id: string, pieces: PlayerView["ship"]["pieces"], x: number, y: number, a: number, floor: boolean) => {
605659 seen.add(id);
606660 const jig = this.jiggle(id, x, y);
607661 const hull = hullRadius(pieces, this.cfg.ship.pieceRadius);
608662 // zoomed out the hull would vanish between two glyph cells: grow it into
609- // an icon of itself instead, so the player never loses their ship.
663+ // an icon of itself instead, so a ship is never lost at any zoom.
610664 const minHull = (ICON_CELLS * this.viewHeight) / this.cells.y;
611- const k = id === selfId ? Math.max(1, minHull / Math.max(hull, 1)) : 1;
665+ const k = floor ? Math.max(1, minHull / Math.max(hull, 1)) : 1;
612666 if (id === selfId) this.selfScale = k;
613667 for (const key in pieces) {
614668 const p = pieces[key];
@@ -625,8 +679,9 @@ export class Engine {
625679 counts[p.shape] = n + 1;
626680 }
627681 };
628- for (const p of players) add(p.id, p.ship.pieces, p.x, p.y, p.a);
629- for (const n of npcs) add(n.id, n.ship.pieces, n.x, n.y, n.a);
682+ for (const p of players) add(p.id, p.ship.pieces, p.x, p.y, p.a, true);
683+ // fleets are carried by their contact mark, so their hull stays true size
684+ for (const n of npcs) add(n.id, n.ship.pieces, n.x, n.y, n.a, false);
630685 for (const id of [...this.jitters.keys()]) if (!seen.has(id)) this.jitters.delete(id);
631686 for (const s of SHAPES) {
632687 const mesh = this.shipMeshes[s];
@@ -643,8 +698,12 @@ export class Engine {
643698 hover: Vec2 | null,
644699 ) {
645700 const c = new THREE.Color();
701+ const attr = (name: string) => this.projGeom.getAttribute(name) as THREE.BufferAttribute;
702+ const zoom = (this.cells.y * SS) / this.viewHeight;
646703 let i = 0;
647- const put = (x: number, y: number, size: number, shape: number, color: string) => {
704+ // minCells is the smallest a mark is allowed to get, in glyph cells: below
705+ // that it would sit between two cells and blink in and out while zooming.
706+ const put = (x: number, y: number, size: number, shape: number, color: string, minCells = 0) => {
648707 if (i >= 4096) return;
649708 this.projPos[i * 3] = x;
650709 this.projPos[i * 3 + 1] = y;
@@ -653,29 +712,25 @@ export class Engine {
653712 this.projCol[i * 3] = c.r;
654713 this.projCol[i * 3 + 1] = c.g;
655714 this.projCol[i * 3 + 2] = c.b;
656- (this.projGeom.getAttribute("aSize") as THREE.BufferAttribute).setX(i, size);
657- (this.projGeom.getAttribute("aShape") as THREE.BufferAttribute).setX(i, shape);
658- (this.projGeom.getAttribute("aPhase") as THREE.BufferAttribute).setX(i, i * 1.7);
715+ const px = size * 2 * zoom * CELL_ASPECT;
716+ const minPx = minCells * SS;
717+ attr("aSize").setX(i, Math.max(size, (size * minPx) / px));
718+ attr("aShape").setX(i, shape);
719+ attr("aPhase").setX(i, i * 1.7);
659720 i++;
660721 };
661- for (const p of projs) put(p.x, p.y, 8, 0, "#ffcf6b");
722+ for (const p of projs) put(p.x, p.y, 8, 0, "#ffcf6b", 1.2);
662723 for (const n of fleets) {
663724 // fleets read as a glyph block at any zoom: a hot filled disc for rogue
664725 // AI, a hollow ring for a friendly fleet.
665- const floor = (2 * ICON_CELLS * this.viewHeight) / this.cells.y;
666- const size = Math.max(1.6 * hullRadius(n.ship.pieces, this.cfg.ship.pieceRadius), floor);
667- put(n.x, n.y, size, n.hostile ? 0 : 1, FLEET_COLOR);
726+ put(n.x, n.y, 1.6 * hullRadius(n.ship.pieces, this.cfg.ship.pieceRadius), n.hostile ? 0 : 1, FLEET_COLOR, 2 * ICON_CELLS);
668727 }
669728 for (const it of items)
670- put(it.x, it.y, 22, it.item.kind === "ore" ? 1 : 2, it.item.kind === "ore" ? "#8a5f2c" : "#ffcf6b");
671- for (const m of markers ?? []) put(m.x, m.y, 7, 2, "#ffcf6b");
672- if (hover) put(hover.x, hover.y, 9, 1, "#ffcf6b");
729+ put(it.x, it.y, 22, it.item.kind === "ore" ? 1 : 2, it.item.kind === "ore" ? "#8a5f2c" : "#ffcf6b", 2);
730+ for (const m of markers ?? []) put(m.x, m.y, 7, 0, "#ffcf6b", 6);
731+ if (hover) put(hover.x, hover.y, 9, 1, "#ffcf6b", 2.5);
673732 this.projGeom.setDrawRange(0, i);
674- (this.projGeom.getAttribute("position") as THREE.BufferAttribute).needsUpdate = true;
675- (this.projGeom.getAttribute("aColor") as THREE.BufferAttribute).needsUpdate = true;
676- (this.projGeom.getAttribute("aSize") as THREE.BufferAttribute).needsUpdate = true;
677- (this.projGeom.getAttribute("aShape") as THREE.BufferAttribute).needsUpdate = true;
678- (this.projGeom.getAttribute("aPhase") as THREE.BufferAttribute).needsUpdate = true;
733+ for (const name of ["position", "aColor", "aSize", "aShape", "aPhase"]) attr(name).needsUpdate = true;
679734 }
680735
681736 setView(cx: number, cy: number, viewHeight: number) {
@@ -714,7 +769,7 @@ export class Engine {
714769 const cols = Math.max(1, Math.round(w / CELL_W));
715770 const rows = Math.max(1, Math.round(h / CELL_H));
716771 this.cells.set(cols, rows);
717- this.rt.setSize(cols, rows);
772+ this.rt.setSize(cols * SS, rows * SS);
718773 }
719774
720775 render(dt: number) {
@@ -730,12 +785,13 @@ export class Engine {
730785 c.lookAt(this.centerX, this.centerY, 0);
731786 c.updateProjectionMatrix();
732787
733- const zoom = this.cells.y / this.viewHeight;
788+ const zoom = (this.cells.y * SS) / this.viewHeight;
734789 for (const m of this.pointMats) {
735790 m.uniforms.uZoom.value = zoom;
736791 m.uniforms.uTime.value = this.time;
737792 }
738793 this.fogMat.uniforms.uTime.value = this.time;
794+ this.fogMat.uniforms.uCellWorld.value = this.viewHeight / this.cells.y;
739795
740796 this.renderer.setRenderTarget(this.rt);
741797 this.renderer.clear();