From 38605c83de86b5edfdcd2e52e631c5c820315815 Mon Sep 17 00:00:00 2001 From: Travis Curtsinger Date: Wed, 5 Aug 2026 13:35:14 -0500 Subject: [PATCH] Make the National echo edge actually fade to nothing The previous edge fix was live but invisible: coverage only reached 0.5 at the outer boundary, so the fragment still painted at ~71% opacity and then cliffed to zero across a half-cell ramp. A 30% dimming in front of a 70% cliff reads as a hard edge. Coverage is now the opacity directly, and Smooth no longer stops at the measured footprint: opacity ramps from full at the last measured cell center to zero at the first unmeasured cell center, so an echo boundary fades out over a full cell instead of ending in a square, and a lone measured cell becomes a soft dot rather than a tile. Native keeps the hard-edged exact footprint at full opacity. The tradeoff is explicit: Smooth feathers up to half a cell (~500 m) past the measured footprint at low opacity. No measured value is ever blended with a sentinel - only opacity varies there - and interrogation still answers missing/no-coverage truthfully inside the margin. Co-Authored-By: Claude Fable 5 --- docs/25_RADAR_RENDERING_QUALITY.md | 12 +++-- src/national-radar/NationalGridLayer.ts | 20 ++++---- src/national-radar/sampling.test.ts | 68 ++++++++++++++++++------- src/national-radar/sampling.ts | 44 +++++++--------- 4 files changed, 84 insertions(+), 60 deletions(-) diff --git a/docs/25_RADAR_RENDERING_QUALITY.md b/docs/25_RADAR_RENDERING_QUALITY.md index b12373b..5565c6a 100644 --- a/docs/25_RADAR_RENDERING_QUALITY.md +++ b/docs/25_RADAR_RENDERING_QUALITY.md @@ -88,12 +88,14 @@ The above-radar set is deliberately small. Its lines remain recognizable when so The National grid renderer weights only the **valid** neighbors of a sample footprint and renormalizes them. A missing or no-coverage cell contributes no value at any weight, so `Smooth` still never bridges an invalid status; but a partially invalid footprint no longer collapses to a hard nearest-cell block, which previously drew every echo boundary as a row of 1 km squares. -The renormalized weight that survives is the fragment's **coverage**, and it scales palette opacity by its square root. A fully surrounded cell is unchanged at coverage 1; the outermost half-cell ring fades rather than terminating in a square; and a lone valid corner still renders at half palette opacity, so a measured cell never becomes invisible. This is display-only, `Smooth`-only, and applied after palette lookup: +The renormalized weight that survives is the fragment's **coverage**, and it is the fragment's opacity. Coverage is 1 wherever every contributing neighbor is valid, then ramps continuously to 0 across an echo boundary: full opacity at the last measured cell center, nothing at the first unmeasured cell center. A lone measured cell is a soft dot at full opacity on its own center rather than a hard square. -- the painted footprint is identical in both modes, because an invalid nearest cell already paints nothing; -- no coverage is invented beyond the measured cells; -- `Native` remains exact nearest-cell sampling at full opacity; -- interrogation continues to report the exact backend value in both modes. +This means `Smooth` feathers up to **half a cell (~500 m) past the measured footprint** at low opacity. That translucent margin is the thing that replaces the hard square edge, and it is an accepted display-only cost of the mode: + +- no measured value is ever blended with a missing or no-coverage sentinel — only opacity varies there; +- the feather is `Smooth`-only and applied after palette lookup on a premultiplied palette; +- `Native` keeps the exact measured footprint: hard cell boundaries, full opacity, no feather; +- interrogation reports the exact backend value and status in both modes, so a click inside the feathered margin still answers `missing` or `no coverage` truthfully. `src/national-radar/sampling.ts` mirrors this contract outside WebGL so it stays unit-tested; the shader in `src/national-radar/NationalGridLayer.ts` must match it. diff --git a/src/national-radar/NationalGridLayer.ts b/src/national-radar/NationalGridLayer.ts index 4a571a0..1496206 100644 --- a/src/national-radar/NationalGridLayer.ts +++ b/src/national-radar/NationalGridLayer.ts @@ -96,8 +96,9 @@ void main() { vec2 local = grid - u_halo_origin; ivec2 nearest_cell = ivec2(floor(local + 0.5)); uint nearest_raw = texelFetch(u_raw_codes, nearest_cell, 0).r; - if (!valid_code(nearest_raw)) discard; if (!u_smooth) { + // Native paints exact measured cells only: hard boundaries, full opacity. + if (!valid_code(nearest_raw)) discard; frag_color = palette_color(float(nearest_raw)); return; } @@ -110,11 +111,11 @@ void main() { uint c01 = texelFetch(u_raw_codes, ivec2(lower.x, upper.y), 0).r; uint c11 = texelFetch(u_raw_codes, upper, 0).r; vec4 codes = vec4(float(c00), float(c10), float(c01), float(c11)); - // Weight only the valid neighbors and renormalize. A missing or no-coverage - // cell never contributes its value, but it no longer collapses the fragment - // to a hard nearest-cell block either, so echo edges stop rendering as - // squares. Coverage then fades the outermost half-cell ring; the painted - // footprint is unchanged because an invalid nearest cell already discarded. + // Weight only the valid neighbors and renormalize: a missing or no-coverage + // cell never contributes its value at any weight. The surviving weight is + // the fragment's coverage and drives opacity directly, so an echo boundary + // ramps from full opacity at the last measured cell center to nothing at + // the first unmeasured cell center instead of ending in a hard square. vec2 fraction = clamp(fract(local), 0.0, 1.0); vec4 weights = vec4( (1.0 - fraction.x) * (1.0 - fraction.y), @@ -128,12 +129,9 @@ void main() { valid_code(c11) ? 1.0 : 0.0 ); float coverage = dot(weights, vec4(1.0)); - if (coverage <= 0.0) { - frag_color = palette_color(float(nearest_raw)); - return; - } + if (coverage < 0.0001) discard; // The palette is premultiplied, so scaling the whole color scales opacity. - frag_color = palette_color(dot(weights, codes) / coverage) * sqrt(coverage); + frag_color = palette_color(dot(weights, codes) / coverage) * coverage; }`; export interface NationalPaintReceipt { diff --git a/src/national-radar/sampling.test.ts b/src/national-radar/sampling.test.ts index 58a657c..332eb14 100644 --- a/src/national-radar/sampling.test.ts +++ b/src/national-radar/sampling.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { decodeNationalRaw, nationalEdgeAlphaScale, sampleNationalGrid } from "./sampling"; +import { decodeNationalRaw, sampleNationalGrid } from "./sampling"; describe("National numeric sampling", () => { it("decodes the complete structural u16 domain by the GRIB formula", () => { @@ -50,28 +50,60 @@ describe("National numeric sampling", () => { expect(sampleNationalGrid(uncovered, 2, 2, 0.25, 0.25, "smooth")).toEqual(smoothed); }); - it("softens an echo edge instead of collapsing it to a nearest-cell block", () => { - // Two valid cells above two no-coverage cells. Sampling just inside the - // measured row still blends both valid neighbors and reports partial - // coverage, where the old contract snapped to one flat nearest square. + it("ramps an echo boundary from full opacity to nothing across one cell", () => { + // Valid top row, no-coverage bottom row. Opacity must fall continuously + // from the last measured cell center to the first unmeasured one, where + // the old contract held full opacity and then cut to a hard square. const edge = new Uint16Array([10_200, 10_400, 0, 0]); - const sample = sampleNationalGrid(edge, 2, 2, 0.5, 0.25, "smooth"); - expect(sample.rawCode).toBeCloseTo(10_300, 8); - expect(sample.coverage).toBeCloseTo(0.75, 10); - expect(nationalEdgeAlphaScale(sample.coverage)).toBeCloseTo(Math.sqrt(0.75), 10); + const centers = sampleNationalGrid(edge, 2, 2, 0.5, 0, "smooth"); + expect(centers.rawCode).toBeCloseTo(10_300, 8); + expect(centers.coverage).toBe(1); + + const boundary = sampleNationalGrid(edge, 2, 2, 0.5, 0.5, "smooth"); + expect(boundary.rawCode).toBeCloseTo(10_300, 8); + expect(boundary.coverage).toBeCloseTo(0.5, 10); + + const beyond = sampleNationalGrid(edge, 2, 2, 0.5, 0.9, "smooth"); + expect(beyond.coverage).toBeCloseTo(0.1, 10); + // The measured value is still the mean of valid cells only, never a blend + // toward the sentinel, no matter how faint the fragment becomes. + expect(beyond.rawCode).toBeCloseTo(10_300, 8); + }); + + it("paints nothing where no valid cell contributes", () => { + const edge = new Uint16Array([10_200, 10_400, 0, 0]); + expect(sampleNationalGrid(edge, 2, 2, 0.5, 1, "smooth")).toEqual({ + status: "no_coverage", + rawCode: 0, + valueDbz: null, + coverage: 0, + }); }); - it("keeps a lone measured corner visible at half palette opacity", () => { + it("renders a lone measured cell at full opacity on its own center", () => { const isolated = new Uint16Array([0, 0, 9_000, 10_600]); - const sample = sampleNationalGrid(isolated, 2, 2, 0.5, 0.5, "smooth"); - expect(sample.rawCode).toBeCloseTo(10_600, 8); - expect(sample.coverage).toBeCloseTo(0.25, 10); - expect(nationalEdgeAlphaScale(sample.coverage)).toBeCloseTo(0.5, 10); + const center = sampleNationalGrid(isolated, 2, 2, 1, 1, "smooth"); + expect(center.rawCode).toBe(10_600); + expect(center.coverage).toBe(1); + + const corner = sampleNationalGrid(isolated, 2, 2, 0.5, 0.5, "smooth"); + expect(corner.rawCode).toBeCloseTo(10_600, 8); + expect(corner.coverage).toBeCloseTo(0.25, 10); }); - it("bounds the display-only edge fade", () => { - expect(nationalEdgeAlphaScale(1)).toBe(1); - expect(nationalEdgeAlphaScale(0)).toBe(0); - expect(() => nationalEdgeAlphaScale(1.5)).toThrow("between 0 and 1"); + it("keeps Native hard-edged with no feather", () => { + const edge = new Uint16Array([10_200, 10_400, 0, 0]); + expect(sampleNationalGrid(edge, 2, 2, 0.5, 0.4, "native")).toEqual({ + status: "valid", + rawCode: 10_400, + valueDbz: 41, + coverage: 1, + }); + expect(sampleNationalGrid(edge, 2, 2, 0.5, 0.6, "native")).toEqual({ + status: "no_coverage", + rawCode: 0, + valueDbz: null, + coverage: 0, + }); }); }); diff --git a/src/national-radar/sampling.ts b/src/national-radar/sampling.ts index 6f0a252..ae1551d 100644 --- a/src/national-radar/sampling.ts +++ b/src/national-radar/sampling.ts @@ -10,27 +10,15 @@ export interface NationalDisplaySample { export interface NationalDisplayFragment extends NationalDisplaySample { /** - * Fraction of the sample footprint backed by valid measured cells: 1 when - * every contributing neighbor is valid (and always 1 in `Native`), lower - * near a missing or no-coverage boundary. + * Fraction of the sample footprint backed by valid measured cells, and the + * display-only opacity scale applied to the palette color. It is 1 wherever + * every contributing neighbor is valid, ramps to 0 across an echo boundary, + * and is exactly 0 where nothing is painted. `Native` is always 1 on a + * measured cell and 0 elsewhere — it never fades. */ coverage: number; } -/** - * Display-only opacity applied to a partially covered `Smooth` fragment. The - * square root keeps a measured cell clearly visible at its weakest footprint - * (a lone valid corner renders at half palette opacity) while the outermost - * half-cell ring fades instead of ending in a hard square. `Native` never - * applies it, and interrogation is unaffected in either mode. - */ -export function nationalEdgeAlphaScale(coverage: number): number { - if (!Number.isFinite(coverage) || coverage < 0 || coverage > 1) { - throw new RangeError("National coverage must be between 0 and 1"); - } - return Math.sqrt(coverage); -} - export function decodeNationalRaw( rawCode: number, missingRaw = 9000, @@ -46,10 +34,16 @@ export function decodeNationalRaw( /** * Mirrors the National fragment shader's sampling contract so it stays - * testable outside WebGL. `Smooth` weights only the valid neighbors of the - * footprint and renormalizes: a missing or no-coverage cell never contributes - * its value, but it no longer collapses the fragment to a hard nearest-cell - * block either. The returned `coverage` drives the display-only edge fade. + * testable outside WebGL. + * + * `Smooth` weights only the valid neighbors of the footprint and + * renormalizes: a missing or no-coverage cell never contributes its value at + * any weight. The surviving weight is the fragment's coverage and becomes its + * opacity, so an echo boundary ramps from full opacity at the last measured + * cell center to nothing at the first unmeasured cell center. That feather + * reaches up to half a cell past the measured footprint at low opacity, which + * is what replaces the former hard square edge; `Native` keeps the exact + * measured footprint at full opacity, and interrogation is exact in both. */ export function sampleNationalGrid( rawCodes: Uint16Array, @@ -76,10 +70,8 @@ export function sampleNationalGrid( const nearestY = clamp(Math.floor(y + 0.5), 0, height - 1); const nearest = rawCodes[nearestY * width + nearestX]; const nearestSample = decodeNationalRaw(nearest, missingRaw, noCoverageRaw); - // An invalid nearest cell paints nothing at all, so the measured footprint - // is identical in both modes; only opacity inside it can soften. - if (mode === "native" || nearestSample.status !== "valid") { - return { ...nearestSample, coverage: 1 }; + if (mode === "native") { + return { ...nearestSample, coverage: nearestSample.status === "valid" ? 1 : 0 }; } const lowerX = clamp(Math.floor(x), 0, width - 1); const lowerY = clamp(Math.floor(y), 0, height - 1); @@ -100,7 +92,7 @@ export function sampleNationalGrid( coverage += corner.weight; weighted += corner.weight * corner.raw; } - if (coverage <= 0) return { ...nearestSample, coverage: 1 }; + if (coverage <= 0) return { ...nearestSample, coverage: 0 }; const rawCode = weighted / coverage; return { status: "valid",