Repository navigation
feat: add zRange to RasterTileLayer for draping COGs over terrain - #720
Conversation
RasterTileLayer culled tiles as if the ground were at sea level, so a COG draped over high terrain was missing tiles near the camera. It now accepts zRange (meters) and passes it to its inner TileLayer, compared by value. The traversal's bounding-volume cache compared elevation in common-space units, which change with the viewport's latitude, so with a zRange every north-south pan reprojected every visited tile. Cache entries now keep the tile's reprojected reference points, and a new elevation only refits the box. Co-Authored-By: Claude Opus 5.5 <[email protected]>
An experimental example that drapes a COG over AWS Terrain Tiles with deck.gl's _TerrainExtension: swisstopo's 1:1 million map (EPSG:2056) and a 1962 USGS topographic quad in a NAD27 polyconic projection, neither of which shares the terrain's Web Mercator tile grid. Adds a card for it on the docs examples page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| const refPointPositions: [number, number, number][] = []; | ||
| for (const p of commonSpacePositions) { | ||
| refPointPositions.push([p[0], p[1], minZ]); | ||
|
|
||
| if (minZ !== maxZ) { | ||
| // Also sample at maximum elevation to capture the full 3D volume | ||
| refPointPositions.push([p[0], p[1], maxZ]); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
This looks like the same code that we refactored below into fitBoundingVolume... what changed?
There was a problem hiding this comment.
Note
This comment was written by Claude (Claude Code), not by @kylebarron.
Nothing in the logic. The loop moved into fitBoundingVolume unchanged, so both this full computation and the new cache-refit path in getBoundingVolume can call it.
🤖 Written by Claude Code
| const entry = { | ||
| ...cacheHit, | ||
| zRange, | ||
| boundingVolume: fitBoundingVolume( |
There was a problem hiding this comment.
Oh we also fit the bounding volume here
There was a problem hiding this comment.
Note
This comment was written by Claude (Claude Code), not by @kylebarron.
Yes. On a cache hit where only zRange differs, this refits the box from the cached reference points instead of reprojecting them. In common-space units zRange changes with the viewport's latitude, so before this, every north-south pan reprojected every visited tile.
🤖 Written by Claude Code
| /** | ||
| * Lowest and highest elevation, in meters, of the surface the raster is | ||
| * drawn on, such as the terrain under a COG draped with deck.gl's | ||
| * `TerrainExtension`. | ||
| * | ||
| * Tiles are selected for the volume between these heights instead of for | ||
| * the ground at sea level, so tiles the terrain lifts into view still | ||
| * load. This only changes which tiles load; it doesn't move the raster. | ||
| * Ignored in `GlobeView`. | ||
| * | ||
| * @default null | ||
| */ | ||
| zRange?: [number, number] | null; |
There was a problem hiding this comment.
again I'm a little surprised that the user would need to / be able to reliably know this. Shouldn't this be passed through from the other tile layer somehow?
There was a problem hiding this comment.
Note
This comment was written by Claude (Claude Code), not by @kylebarron.
deck.gl layers don't share state, so there's no built-in way to pass it through. TileLayer's own zRange is also a user prop: TerrainLayer computes it from its loaded tiles and passes it to its inner TileLayer. A draped COGLayer has no handle on the terrain layer.
So the prop stays, matching TileLayer's, and the JSDoc now says how to get it from a TerrainLayer: each tile mesh's header.boundingBox, in onTileLoad. The example does exactly that (67e7d1e), so nobody needs to know the elevations ahead of time.
Doing it automatically would mean RasterTileLayer finding terrain layers through deck.gl's layer manager and reading TerrainLayer's internal state. That's possible as a follow-up, but brittle.
🤖 Written by Claude Code
Reworks the cog-terrain example after review: - Adds nine 30 cm NAIP COGs around Pikes Peak, in true color and as false-color infrared, and makes them the default at 1.5x vertical exaggeration. NAIP's fourth band is near-infrared, so its tiles use their own getTileData and renderTile instead of COGLayer's default pipeline, which would draw that band as alpha. They use linear filtering and a 64 MB tile cache per COG. - Measures zRange from the terrain tiles as they load (TerrainLayer's onTileLoad and each mesh's header.boundingBox) instead of hard-coding it per dataset. - Adds a vertical exaggeration slider that scales the Terrarium decoder. - Uses TerrainController, so the camera follows the ground, with a new view per scene. - Limits the terrain to the COGs' WGS84 bounds: TerrainLayer's extent skips tiles outside them, and a ClipExtension stops it drawing there. - Caps the NAIP view at zoom 14 and pitch 60, because COGLayer picks every tile's resolution for the zoom at the center of the view. Also documents how to get RasterTileLayer's zRange from a TerrainLayer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replaces the two placeholder screenshots with a false-color infrared view of the NAIP imagery draped over Pikes Peak. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| * A deck.gl `TerrainLayer`'s tiles report their elevations: in its | ||
| * `onTileLoad`, each tile's mesh has a `header.boundingBox`, which is | ||
| * also how `TerrainLayer` sets its own `zRange`. The `cog-terrain` | ||
| * example widens this range as terrain tiles load. | ||
| * |
There was a problem hiding this comment.
So do we need this at all or can we remove it? Seems like we should be able to automatically expand the zRange on the fly without adding a public prop to RasterTileLayer...
There was a problem hiding this comment.
The deck.gl TileLayer accepts a zRange prop, and so I think it's ok to also accept a zRange prop here.
…xample Address the third static review of the cog-terrain example: - Memoize the Terrarium decoder and the clip box. TerrainLayer compares the decoder by reference in its tiles' getTileData update triggers, so every zRange widening reloaded every terrain tile, and switching between the two NAIP options reloaded the terrain too. - Rescale the measured zRange when the exaggeration changes, instead of selecting tiles for sea level until the terrain reloads. - Scale the camera's height with the exaggeration and give the view a new TerrainController. The controller moves only during interactions, so at 3x the camera ended up inside the terrain. - Start the NAIP view where TerrainController's first pick stays under maxZoom, so the view no longer jumps back once the terrain loads. - Don't create a NAIP texture for a tile cancelled while it decodes. - Correct the README and comments that the review found wrong. Co-Authored-By: Claude Opus 5.5 <[email protected]>
kylebarron
left a comment
There was a problem hiding this comment.
lgtm, just waiting for the merge conflict to be resolved
main removed RasterTileLayer's `_errorSpace` prop (#721), so the conflict in raster-tile-layer.test.ts keeps this branch's zRange tests and drops the `_errorSpace` test that main deleted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…changes (#731) > [!NOTE] > This PR was written by Claude (Claude Code), not by @kylebarron. Follow-ups to #720's terrain example, from a final review of its camera changes: - **Scene changes reset the exaggeration** to the default 1.5x. Every start view is set for it. At 3x, NAIP's start put the camera inside the terrain in short windows, and jumped back about 1.6 zoom levels in 750 px ones. - **Lowering the exaggeration leaves the camera where it is.** Lowered with the terrain, the camera ended up nearer the ground, and the new `TerrainController`'s first pick could zoom past `maxZoom` and jump back. Raising it still raises the camera with the terrain. - **The camera math uses the window's size.** The view states the controller emits keep a stale size after a resize, until you next move the map. - **Wording:** - The NAIP start view's thresholds are 630 px, and 1760x1100 at the default exaggeration. - The pitch limit of 70 raises deck.gl's default of 60 rather than capping it. - The `naip.ts` comment on cancelled tiles no longer depends on #728. ### Not fixed: the first pick after an exaggeration change The review found that the new `TerrainController`'s first pick can land on the wrong ground, and suggested a new `TerrainLayer` per exaggeration. I tried that, and also waiting for the terrain to load before handing the camera to a `TerrainController`. Neither changed the result. - Raising NAIP from 1.5x to 2x put the target at 6,005 m on terrain beyond the summit. The slope in view is at 8,360 m. - This happened even with every terrain tile loaded. - The pick reports the correct terrain tile, but a point farther along the view ray than the visible surface. - Picks from a view whose target is on the ground come back correct, so this looks like a deck.gl picking issue. The README now describes it: the target eases to the ground as you move, so zooming straight in after raising the exaggeration can take the camera into the slope. Checked in headed Chrome at 1200x750: - 1.5x to 3x one step at a time, and back down to 1x. The zoom never passed 12.2, and the terrain stayed in view. - Scene switches reset the slider, and NAIP's start view lands where it does on first load (zoom 13.58). - No console errors. 🤖 Written by [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
Note
This PR was written by Claude (Claude Code), not by @kylebarron.
Adds an experimental example that drapes COGs over 3D terrain with deck.gl's
_TerrainExtension. It also adds azRangeprop toRasterTileLayer, and so toCOGLayerandZarrLayer, so that tiles are selected for the terrain's heights.Draping
Draping itself needs no library change:
TerrainLayerwithoperation: "terrain+draw"is the terrain source.COGLayerwithextensions: [new _TerrainExtension()]andterrainDrawMode: "drape"is rendered onto it.terrainDrawMode: "drape"has to be set explicitly. Left unset, the extension picks"offset"for our mesh layer, because it inheritsSimpleMeshLayer'sinstancePositions. Offset mode lifts only the reprojection mesh's vertices onto the terrain.The example
examples/experimental/cog-terraindrapes three kinds of COG over AWS Terrain Tiles, none of them in the terrain's grid:getTileDataandrenderTile;COGLayer's default pipeline would draw that band as alpha.It also:
zRangeas terrain tiles load, from each tile mesh'sheader.boundingBoxinTerrainLayer'sonTileLoad.TerrainLayersets its ownzRangefrom the same field;TerrainController, so the camera rises and falls with the ground as you move;TerrainLayer'sextentloads only tiles that overlap them, and aClipExtensionstops it drawing outside them;zRange, sinceTerrainControllerdoesn't lift the camera when the terrain rises;TerrainController's first pick stays undermaxZoom, so the camera doesn't jump back once the terrain loads.zRange
RasterTileLayerculled tiles as if the ground were at sea level. Over the Grand Canyon, about 1.9 km up, that left tile-shaped holes near the camera.zRange(meters) is now passed to the innerTileLayer, whose traversal already builds bounding volumes over that height range. It is compared by value, so an inline array doesn't re-render the layer.The traversal caches each tile's bounding volume, keyed on the elevation in common-space units. Those units change with the viewport's latitude, so with a
zRange, every north-south pan missed the cache and reprojected every visited tile. Cache entries now keep the tile's reprojected reference points, so a new elevation only refits the box.Not handled
COGLayerpicks each tile's resolution for the zoom at the center of the view, however far away the tile is. Tilted views therefore load tiles at the center's resolution out to the horizon, so the example caps the NAIP view at zoom 14 and pitch 60.COGLayerdoesn't destroy its tile textures, because deck.gl'sTileset2D.finalizedoesn't callonTileUnload. This also affectsCOGLayer's default pipeline, so it's left for a follow-up inRasterTileset2D.zRangeapplies.zRangeis ignored inGlobeView, whose bounding volumes don't model elevation yet.terrain+drawlayer that isn't pickable logs "Picked non-existent layer". The example's terrain is pickable, whichTerrainControllerneeds anyway.Closes #57
🤖 Written by Claude Code