Projecting Raster Meshes
Applications can compose math.gl's existing geometry and projection interfaces to prepare raster meshes with UVs and feature identity. The runnable example uses native projections; the same preparation function accepts an external engine's synchronous transform after its asynchronous initialization. No projection engine is imported by the recipe's geometry helper. The general projection interface is projection-engine agnostic. For an async-only engine, use the adaptive asynchronous inverse recipe: refinement requests new sample batches as needed. A one-time preprocessing pass is insufficient when refinement introduces new vertices. The real proj-wasm integration reuses an operation across those frontiers without changing synchronous converter semantics.
Explore the four-engine raster lab and external-engine adapters. Changing projection parameters creates a new operation and can invalidate domains and meshes; the current rebuild example does not promise continuous frame-rate or automatic geometry reuse.
Preparation order
- Define the source raster grid and its UVs in its own CRS. Resolve source coverage, singularities and cuts into continuous source patches before refinement. Duplicate vertices at attribute or projection seams. Do not interpolate longitude through a pole or across a wrapped seam.
- Call
subdivideTriangleMeshfor each patch with its selected chart transform. Convert a pixel tolerance into target units using the intended view scale. Null/nonfinite samples and exhausted limits reject the patch; never filter failed vertices out of existing triangles. - Convert the explicit target domain's outer rings and holes into disjoint triangles using
earcut. Pass those convex regions toclipTriangleMesh, with periodic metadata when coordinates have already been unwrapped into a continuous target interval. - Apply
interpolatePackedAttributesfirst using subdivision provenance, then clipping provenance. The example keeps source coordinates and UVs in float64 through both stages and retains a flat source-row attribute. Compose triangle identity through both stages too. Quantize only at upload. - Render the resulting indexed positions and UVs. Do not weld coincident vertices: periodic copies and different source patches can occupy the same position with different attributes.
prepare.mjs is deliberately example code. Its domain topology and source-patch assumptions are
explicit; it is not a general topology repair or reprojection API. For native discontinuous maps,
use the existing MapEngine/map chart boundaries and clipping rather than introducing a competing
spherical clipper. For arbitrary external projections, the backend must supply cuts and domain geometry.
Fixtures and what they verify
| Fixture | Source preparation | Checks |
|---|---|---|
| Pole-containing raster | Stereographic XY square containing the north pole, projected to polar orthographic | Finite geometry, sampled error and source UVs through the pole |
| Rotated raster | The same raster's affine grid rotated in source XY | Source interpolation and UV provenance independent of grid orientation |
| UTM zone 60 | UTM XY grid crossing longitude 180°, target longitudes explicitly unwrapped around that meridian | Two periodic copies, preserved row IDs and UVs |
| Full world | Four source longitude strips, separate seam vertices | Full 360×180 target area, no duplicate seam area |
| Interrupted sinusoidal | Native chart meridian cuts, per-chart transform and sampled target outline | No cross-chart triangles; six chart patches retain original world-raster UVs |
| Domain hole | Planar raster clipped to an outer ring with an interior hole | Hole remains empty and total area is conserved |
The interrupted fixture stops at ±80°; pole behavior is tested separately with the projected polar source grid. Its meridian cuts are specific to the native sinusoidal charts. Spherical great-circle clipping and straight raster-grid edges have different semantics; do not substitute one silently. Domain curves are sampled polygons, not exact boundaries. Rendering bounds are never treated as a validity domain. Unknown domains must remain unknown rather than becoming a bounding rectangle.
Source-grid versus target-grid meshing
This example refines a source grid: UVs are affine in source coordinates and target positions are approximated by triangles. That preserves the source texture mapping, but requires source cuts and finite transform samples. Target clipping after refinement cannot repair a source triangle that crossed an invalid projection region before refinement.
An alternative is to build a target grid, clip it to a target domain, then inverse-transform each vertex to the source raster to obtain UVs. This can simplify pole-containing coverage, but requires a well-defined inverse, per-chart choices for interrupted maps, source-coverage checks, and refinement against inverse/texture error. Forward geometric tolerance alone does not bound inverse UV error.
The library subdivision tolerance is sampled target error, not a universal mathematical bound. This example additionally checks triangle centroids after clipping for these fixtures; it does not certify all possible transforms. Sampling scale is explicitly supplied and the canvas fits results separately. For a real camera, include camera/view policy and texture-error criteria in the sampling decision. Rebuild on changed projection parameters, domain sampling, data, UVs or view tolerance. A backend execution key alone is insufficient to identify a derived raster mesh.
Qualified inverse-mesh recipe
The projection-domain preparation recipe adds actual EPSG:3031 pole-containing and rotated source grids, Fiji/UTM seam crossings, native interrupted charts, and orthographic/tilted-perspective horizons. Target triangles refine against source-texel interpolation error before image-footprint clipping. Source IDs and chart ownership survive preparation. Independent PROJ reference points test coverage; different post-clipping probes test texture error. Invalid samples and exhausted budgets reject the operation rather than silently producing a partial mesh. This remains a qualified example recipe with explicit sampled coverage and boundary assumptions, not generic raster reprojection support.
Keep array and CRS frames separate
A CRS descriptor does not locate array samples. Supply each raster level's explicit column/row-to-coordinate affine transform, stored component order, native units and sample registration. Area-registered sample centers occur at index + 0.5; point registration describes nodes and does not by itself establish cell coverage. Rotated/skewed affine grids must not be approximated as axis-aligned TileMatrix geometry. OME array transforms and CF/GDAL georeferencing can describe different frames; their interpretation and precedence belong to loaders.gl, not this meshing recipe.
The Raster coordinate frames example composes existing math.gl Matrix3 and SpatialReference APIs, with hand-computed native coordinate fixtures, stored-axis permutations, unequal spacing, rotated grids and explicit per-level transforms. It preserves native units and leaves CRS conversion to a separately prepared projection-engine agnostic converter.