fix(client): fix fog system — blocky edges and zero explored visibility (#569)

Root cause: update_from_state() used visible_tiles (always empty in live
server mode) instead of visible_positions for bounds calculation. Bounds
never grew beyond 64x64, so tiles outside that area rendered as solid
unexplored black.

Fix: new _grow_bounds_from_positions() method reads visible_positions
(always populated from server snapshots). Shader fix: removed the
(explored < 0.01 && vis_raw < 0.01) guard that cut off the Gaussian
gradient at unexplored tile boundaries. Doubled blur step size for
D-066 compliant 6-8 tile soft gradient. Added debug_exploration mode
for diagnostic rendering of the exploration texture.

19 acceptance tests covering exploration persistence, bounds grow-only
invariant, gradient margin, Forward-only visibility writes, and
exploration data surviving texture resize.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-28 14:26:13 +01:00
co-authored by Claude Opus 4.6
parent 073c5a317c
commit ebc87d8e74
4 changed files with 613 additions and 46 deletions
+58 -40
View File
@@ -2,11 +2,11 @@ shader_type canvas_item;
// D-059: 3-layer fog shader. Composites over world content.
// Layer 1: Clear (forward cone) — transparent, soft gradient edge
// Layer 2: Explored fog — light overlay indicating "not fresh", art preserved
// Layer 2: Explored fog — light overlay (~25-30%), art fully preserved
// Layer 3: Unexplored — solid near-black #12141a
uniform sampler2D visibility_tex : filter_linear, repeat_disable;
uniform sampler2D exploration_tex : filter_linear, repeat_disable;
uniform sampler2D exploration_tex : filter_nearest, repeat_disable;
uniform sampler2D zone_tint_tex : filter_nearest, repeat_disable;
uniform sampler2D noise_tex : filter_linear, repeat_enable;
uniform vec2 rect_pos; // World-space position of the ColorRect (pixels)
@@ -15,14 +15,17 @@ uniform vec2 map_offset; // map_bounds.position (tiles)
uniform vec2 map_size; // map_bounds.size (tiles)
uniform float tile_size; // Pixels per sim tile
uniform float time; // Seconds since start
uniform bool debug_exploration = false; // When true, render raw exploration texture
const vec3 UNEXPLORED_COLOR = vec3(0.071, 0.078, 0.102); // #12141a
const vec3 DARK_OVERLAY = vec3(0.02, 0.02, 0.05);
const vec3 FOG_OVERLAY_COLOR = vec3(0.06, 0.07, 0.12); // Dark blue-grey fog tint
// Soft gradient via 7x7 Gaussian blur on visibility (sigma 2.0).
// Spreads the cone boundary into a 3-4 tile radius gradient for soft edges.
// D-066: 6-8 sim tile soft gradient at cone boundary.
// 7x7 Gaussian kernel sampling at 2-texel intervals spreads across ±6 tiles.
// Effective sigma = 4 tiles in world space (sigma_kernel=2.0 * step=2.0).
// At 2-sigma (8 tiles): weight drops to 0.14, giving ~6-8 tile visible gradient.
float sample_visibility(vec2 uv) {
vec2 t = 1.0 / map_size;
vec2 t = 2.0 / map_size;
float sum = 0.0;
float weight = 0.0;
for (float dy = -3.0; dy <= 3.0; dy += 1.0) {
@@ -44,39 +47,54 @@ void fragment() {
// Outside known map -> unexplored
if (tex_uv.x < 0.0 || tex_uv.x > 1.0 || tex_uv.y < 0.0 || tex_uv.y > 1.0) {
COLOR = vec4(UNEXPLORED_COLOR, 1.0);
} else {
float vis_raw = texture(visibility_tex, tex_uv).r;
float vis = sample_visibility(tex_uv);
float explored = texture(exploration_tex, tex_uv).r;
// Don't bleed gradient into never-explored tiles
if (explored < 0.01 && vis_raw < 0.01) {
vis = 0.0;
}
if (explored < 0.01 && vis < 0.01) {
// Unexplored: solid near-black
COLOR = vec4(UNEXPLORED_COLOR, 1.0);
} else {
// Continuous blend: clear vision (vis=1) -> light fog (vis=0).
// The Gaussian blur creates a smooth 3-4 tile soft gradient
// at the cone edge — no hard boundary.
// Light fog: subtle animated overlay, preserves all art/info
float noise_val = texture(noise_tex, tile * 0.03 + vec2(time * 0.11, time * 0.07)).r;
float fog_alpha = 0.28 + noise_val * 0.04;
// Clarity ramp from the blurred visibility
float clarity = smoothstep(0.0, 0.85, vis);
float alpha = mix(fog_alpha, 0.0, clarity);
vec3 color = mix(DARK_OVERLAY, vec3(0.0), clarity);
// Soft edge between explored and unexplored
float exp_fade = smoothstep(0.0, 0.3, explored);
alpha = mix(1.0, alpha, exp_fade);
color = mix(UNEXPLORED_COLOR, color, exp_fade);
COLOR = vec4(color, alpha);
}
return;
}
float vis = sample_visibility(tex_uv);
float explored = texture(exploration_tex, tex_uv).r;
// Debug mode: render raw exploration texture (bypass fog rendering).
// Green = EXP_VISIBLE (255), blue = EXP_EXPLORED (128), red = EXP_UNEXPLORED (0).
if (debug_exploration) {
if (explored > 0.9) {
COLOR = vec4(0.0, explored, 0.0, 0.8); // Green: currently visible
} else if (explored > 0.1) {
COLOR = vec4(0.0, 0.0, explored * 2.0, 0.8); // Blue: explored
} else {
COLOR = vec4(0.5, 0.0, 0.0, 0.8); // Red: unexplored
}
return;
}
// D-066: 6-8 sim tile gradient at the cone boundary.
// smoothstep(0.02, 0.95, vis) maps the Gaussian-blurred visibility into a
// smooth clarity ramp spanning the full blur radius — no tile-stepping.
float clarity = smoothstep(0.02, 0.95, vis);
if (explored < 0.01) {
// Unexplored: smooth fade from transparent (inside cone) to solid black (beyond).
// The Gaussian vis gradient drives alpha — same ramp as explored fog — so the
// cone edge looks seamless regardless of whether adjacent tiles are explored.
// World art is NOT preserved in this region: alpha approaches 1.0 outside the cone.
COLOR = vec4(UNEXPLORED_COLOR, 1.0 - clarity);
return;
}
// Explored fog: light overlay preserving all art and information (D-059).
// fog_alpha ~0.27-0.31 -> world appears at ~70% brightness with blue-grey tint.
// 8-10s Perlin noise adds atmosphere without obscuring content.
float noise_val = texture(noise_tex, tile * 0.03 + vec2(time * 0.11, time * 0.07)).r;
float fog_alpha = 0.27 + noise_val * 0.04;
// Blend from fog (clarity=0, out of cone) to clear (clarity=1, in cone)
float alpha = mix(fog_alpha, 0.0, clarity);
vec3 color = mix(FOG_OVERLAY_COLOR, vec3(0.0), clarity);
// Soft transition at the explored/unexplored tile boundary.
// exploration_tex uses filter_nearest: explored is exactly 0.0, ~0.5, or 1.0.
float exp_fade = smoothstep(0.0, 0.2, explored);
alpha = mix(1.0, alpha, exp_fade);
color = mix(UNEXPLORED_COLOR, color, exp_fade);
COLOR = vec4(color, alpha);
}