Skip to content

fix(examples): correct NDVI range filter in naip-mosaic - #522

Merged
kylebarron merged 2 commits into
mainfrom
kyle/naip-ndvi-rescale-fix
May 11, 2026
Merged

kylebarron merged 2 commits into
mainfrom
kyle/naip-ndvi-rescale-fix

Conversation

@kylebarron

@kylebarron kylebarron commented May 11, 2026 •

Copy link
Copy Markdown
Member

Before:

See how the filter and colormap isn't lining up with what's displayed? The colormap implies that no blue pixels should be rendered, but a bunch of blue pixels are still being rendered...
image

This is because we were accidentally rescaling the NDVI output with range [-1, 1] to range [0, 1] before applying the filtering.

After:

Now, we ensure that we apply the filter before doing any filtering:

image

Bug

In the naip-mosaic example, the NDVI render pipeline was:

CreateTexture → ndvi → ndviFilter → Colormap → SetAlpha1

The ndvi shader module pre-normalized raw NDVI from [-1, 1] to [0, 1] (color.r = (ndvi + 1.0) / 2.0) so the Colormap module — which samples color.r as a [0, 1] index — read it correctly. But ndviFilter runs after ndvi and compared the already-normalized color.r ∈ [0, 1] against ndviMin/ndviMax that come straight from the NDVI-range slider, which is in raw NDVI units [-1, 1]. So a slider value of 0.5 actually filtered at raw NDVI 0.0, not 0.5 — the filter was effectively (ndviMin + 1) / 2 … (ndviMax + 1) / 2.

Fix

  • The ndvi module now writes the raw value (color.r = (nir - red) / (nir + red), range [-1, 1]) so ndviFilter compares like-for-like.
  • Inserted { module: LinearRescale, props: { rescaleMin: -1, rescaleMax: 1 } } into the renderNDVI pipeline right before Colormap, remapping [-1, 1] → [0, 1] for the colormap-texture lookup. Pipeline is now CreateTexture → ndvi → ndviFilter → LinearRescale → Colormap → SetAlpha1.
  • Also switched ndviFilter.getUniforms from || to ?? so a slider min/max of exactly 0 isn't silently replaced by the -1/1 defaults.

The slider stays [-1, 1] — that's the correct user-facing range.

Verification

  • pnpm --filter naip-mosaic-example typecheck ✓
  • pnpm --filter naip-mosaic-example build ✓
  • pnpm check ✓

🤖 Generated with Claude Code

The `ndvi` shader module pre-normalized raw NDVI from [-1, 1] to [0, 1]
(`color.r = (ndvi + 1.0) / 2.0`) so the `Colormap` module — which samples
`color.r` as a [0, 1] index — read it correctly. But `ndviFilter` runs after
`ndvi` and compared the already-normalized `color.r` against `ndviMin`/`ndviMax`
that come straight from the slider, which is in raw NDVI units [-1, 1]. A slider
value of 0.5 therefore filtered at raw NDVI 0.0, not 0.5.

Fix: have the `ndvi` module write the raw value (`color.r = (nir - red) /
(nir + red)`, range [-1, 1]) so `ndviFilter` compares like-for-like, then insert
`{ module: LinearRescale, props: { rescaleMin: -1, rescaleMax: 1 } }` into the
`renderNDVI` pipeline right before `Colormap` to remap to [0, 1] for the lookup.
Also switches `ndviFilter.getUniforms` from `||` to `??` so a slider min/max of
exactly `0` isn't replaced by the `-1`/`1` defaults.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@github-actions github-actions Bot added the fix label May 11, 2026
@kylebarron
kylebarron merged commit c262534 into main May 11, 2026
4 checks passed
@kylebarron
kylebarron deleted the kyle/naip-ndvi-rescale-fix branch May 11, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant