@@ -101,7 +101,7 @@ function getGeometryCartographicChannel( geometry, geomToEllipsoidMatrix, ellips
101101
102102}
103103
104- export function getMeshesCartographicRange ( meshes , ellipsoid , meshToEllipsoidMatrix = null , projection = null ) {
104+ export function getMeshesCartographicRange ( meshes , ellipsoid , meshToEllipsoidMatrix = null , projection = null , normalizedRange = null ) {
105105
106106 // find the lat / lon ranges
107107 let minLat = Infinity ;
@@ -138,18 +138,21 @@ export function getMeshesCartographicRange( meshes, ellipsoid, meshToEllipsoidMa
138138
139139 } ) ;
140140
141- let clampedRange = [ minLon , minLat , maxLon , maxLat ] ;
142141 if ( projection !== null ) {
143142
144- // Clamp the lat lon range to the bounds of the projection scheme. Note that clamping the data
145- // allows for "stretching" the texture look at the edges of the projection which leads to a nicer
146- // looking overlay. Eg at the poles of a web-mercator projection - otherwise there will be gaps
147- // that show the underlying tile data. It's arguable which one is better but in all supported
148- // ellipsoid projections (Web mercator, equirect) the projection ranges always span the entire
149- // globe range.
150- // const clampedRange = [ minLon, minLat, maxLon, maxLat ];
151- clampedRange = projection . clampToBounds ( [ minLon , minLat , maxLon , maxLat ] ) ;
152- const [ minU , minV , maxU , maxV ] = projection . toNormalizedRange ( clampedRange ) ;
143+ // Clamp the mesh vertex range to the projection's valid bounds (e.g. ~±85° for Mercator) to
144+ // avoid NaN UV values for vertices that fall outside the projection range. This also stretches
145+ // the texture at the projection boundary rather than leaving gaps.
146+
147+ // takes generate a normalized range if not already provided
148+ if ( normalizedRange === null ) {
149+
150+ normalizedRange = projection . clampToBounds ( [ minLon , minLat , maxLon , maxLat ] ) ;
151+ normalizedRange = projection . toNormalizedRange ( normalizedRange ) ;
152+
153+ }
154+
155+ const [ minU , minV , maxU , maxV ] = normalizedRange ;
153156 uvs . forEach ( uv => {
154157
155158 for ( let i = 0 , l = uv . length ; i < l ; i += 3 ) {
@@ -158,7 +161,9 @@ export function getMeshesCartographicRange( meshes, ellipsoid, meshToEllipsoidMa
158161 const lat = uv [ i + 1 ] ;
159162 const h = uv [ i + 2 ] ;
160163
161- const [ u , v ] = projection . toNormalizedPoint ( lon , lat ) ;
164+ let [ u , v ] = projection . toNormalizedPoint ( lon , lat ) ;
165+ u = MathUtils . clamp ( u , 0 , 1 ) ;
166+ v = MathUtils . clamp ( v , 0 , 1 ) ;
162167 uv [ i + 0 ] = MathUtils . mapLinear ( u , minU , maxU , 0 , 1 ) ;
163168 uv [ i + 1 ] = MathUtils . mapLinear ( v , minV , maxV , 0 , 1 ) ;
164169 uv [ i + 2 ] = MathUtils . mapLinear ( h , minHeight , maxHeight , 0 , 1 ) ;
@@ -171,7 +176,7 @@ export function getMeshesCartographicRange( meshes, ellipsoid, meshToEllipsoidMa
171176
172177 return {
173178 uvs,
174- range : clampedRange ,
179+ range : normalizedRange ,
175180 region : [ minLon , minLat , maxLon , maxLat , minHeight , maxHeight ] ,
176181 } ;
177182
0 commit comments