Files
settled-reach/client/shaders/fog.gdshader
T
jpmschweitzerandClaude Opus 4.6 ebc87d8e74 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>
2026-02-28 14:26:13 +01:00

101 lines
4.4 KiB
Plaintext

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 (~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_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)
uniform vec2 rect_sz; // World-space size of the ColorRect (pixels)
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 FOG_OVERLAY_COLOR = vec3(0.06, 0.07, 0.12); // Dark blue-grey fog tint
// 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 = 2.0 / map_size;
float sum = 0.0;
float weight = 0.0;
for (float dy = -3.0; dy <= 3.0; dy += 1.0) {
for (float dx = -3.0; dx <= 3.0; dx += 1.0) {
float w = exp(-(dx * dx + dy * dy) / 8.0);
vec2 sample_uv = clamp(uv + vec2(dx, dy) * t, vec2(0.0), vec2(1.0));
sum += texture(visibility_tex, sample_uv).r * w;
weight += w;
}
}
return sum / weight;
}
void fragment() {
vec2 world_px = rect_pos + UV * rect_sz;
vec2 tile = world_px / tile_size;
vec2 tex_uv = (tile - map_offset) / map_size;
// 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);
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);
}