Skip to content

Commit c237832

Browse files
authored
ImageOverlayPlugin: Simplify plugin, use normalized ranges (#1414)
* Switch to use normalized ranges * Cleanup further
1 parent a5b621b commit c237832

2 files changed

Lines changed: 19 additions & 29 deletions

File tree

src/three/plugins/images/ImageOverlayPlugin.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function markOverlayImages( range, level, overlay, doRelease ) {
5858

5959
const promises = [];
6060
const { imageSource, tiling } = overlay;
61-
forEachTileInBounds( range, level, tiling, overlay.isPlanarProjection, ( tx, ty, tl ) => {
61+
forEachTileInBounds( range, level, tiling, ( tx, ty, tl ) => {
6262

6363
if ( doRelease ) {
6464

@@ -91,7 +91,7 @@ function markOverlayImages( range, level, overlay, doRelease ) {
9191
function countTilesInRange( range, level, overlay ) {
9292

9393
let total = 0;
94-
forEachTileInBounds( range, level, overlay.tiling, overlay.isPlanarProjection, ( x, y, l ) => {
94+
forEachTileInBounds( range, level, overlay.tiling, ( x, y, l ) => {
9595

9696
total ++;
9797

@@ -973,15 +973,14 @@ export class ImageOverlayPlugin {
973973
}
974974

975975
// internal
976-
_calculateLevelFromOverlay( overlay, range, tile, normalized = false ) {
976+
_calculateLevelFromOverlay( overlay, range, tile ) {
977977

978978
if ( overlay.isPlanarProjection ) {
979979

980980
const { resolution } = this;
981981
const { tiling } = overlay;
982982

983-
const normalizedRange = normalized ? range : tiling.toNormalizedRange( range );
984-
const [ minX, minY, maxX, maxY ] = normalizedRange;
983+
const [ minX, minY, maxX, maxY ] = range;
985984
const w = maxX - minX;
986985
const h = maxY - minY;
987986

@@ -1117,17 +1116,18 @@ export class ImageOverlayPlugin {
11171116
.tileInfo
11181117
.set( tile, info );
11191118

1120-
if ( overlay.isPlanarProjection ) {
1119+
// if the overlay isn't ready then we can't convert the range correctly, yet
1120+
if ( overlay.isReady ) {
11211121

1122-
// TODO: we could project the shape into the frame, compute 2d bounds, and then mark tiles
1122+
if ( overlay.isPlanarProjection ) {
11231123

1124-
} else {
1124+
// TODO: we could project the shape into the frame, compute 2d bounds, and then mark tiles
11251125

1126-
// If the tile has a region bounding volume then mark the tiles to preload
1127-
if ( tile.boundingVolume.region ) {
1126+
} else if ( tile.boundingVolume.region ) {
11281127

1128+
// If the tile has a region bounding volume then mark the tiles to preload
11291129
const [ minLon, minLat, maxLon, maxLat ] = tile.boundingVolume.region;
1130-
const range = [ minLon, minLat, maxLon, maxLat ];
1130+
const range = overlay.tiling.toNormalizedRange( [ minLon, minLat, maxLon, maxLat ] );
11311131
info.range = range;
11321132
info.level = this._calculateLevelFromOverlay( overlay, range, tile );
11331133

@@ -1224,25 +1224,15 @@ export class ImageOverlayPlugin {
12241224
}
12251225

12261226
( { range, uvs } = getMeshesCartographicRange( meshes, ellipsoid, _matrix, tiling ) );
1227+
range = tiling.toNormalizedRange( range );
12271228
heightInRange = true;
12281229

12291230
}
12301231

1231-
let normalizedRange;
1232-
if ( ! overlay.isPlanarProjection ) {
1233-
1234-
normalizedRange = tiling.toNormalizedRange( range );
1235-
1236-
} else {
1237-
1238-
normalizedRange = range;
1239-
1240-
}
1241-
12421232
// calculate the tiling level here if not already created
12431233
if ( info.level === null ) {
12441234

1245-
info.level = this._calculateLevelFromOverlay( overlay, normalizedRange, tile, true );
1235+
info.level = this._calculateLevelFromOverlay( overlay, range, tile );
12461236

12471237
}
12481238

@@ -1283,10 +1273,10 @@ export class ImageOverlayPlugin {
12831273

12841274
// if the previous layer is present then draw it as an overlay to fill in any gaps while we wait for
12851275
// the next set of textures
1286-
tileComposer.setRenderTarget( target, normalizedRange );
1276+
tileComposer.setRenderTarget( target, range );
12871277
tileComposer.clear( 0xffffff, 0 );
12881278

1289-
forEachTileInBounds( range, info.level - 1, tiling, overlay.isPlanarProjection, ( tx, ty, tl ) => {
1279+
forEachTileInBounds( range, info.level - 1, tiling, ( tx, ty, tl ) => {
12901280

12911281
// draw using normalized bounds since the mercator bounds are non-linear
12921282
const span = tiling.getTileBounds( tx, ty, tl, true, false );
@@ -1322,10 +1312,10 @@ export class ImageOverlayPlugin {
13221312
}
13231313

13241314
// draw the textures
1325-
tileComposer.setRenderTarget( target, normalizedRange );
1315+
tileComposer.setRenderTarget( target, range );
13261316
tileComposer.clear( 0xffffff, 0 );
13271317

1328-
forEachTileInBounds( range, info.level, tiling, overlay.isPlanarProjection, ( tx, ty, tl ) => {
1318+
forEachTileInBounds( range, info.level, tiling, ( tx, ty, tl ) => {
13291319

13301320
// draw using normalized bounds since the mercator bounds are non-linear
13311321
const span = tiling.getTileBounds( tx, ty, tl, true, false );

src/three/plugins/images/overlays/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Vector3, Matrix4, MathUtils } from 'three';
22

33
// iterates over all present tiles in the given tileset at the given level in the given range
4-
export function forEachTileInBounds( range, level, tiling, normalized, callback ) {
4+
export function forEachTileInBounds( range, level, tiling, callback ) {
55

66
// pull the bounds in a bit to avoid loading unnecessary tiles. 1e-8 was chosen since smaller values
77
// are not larger enough and cause extra tiles to load in cases where 1-to-1 tile-to-image should occur
@@ -12,7 +12,7 @@ export function forEachTileInBounds( range, level, tiling, normalized, callback
1212
maxLon -= 1e-8;
1313

1414
const clampedLevel = Math.max( Math.min( level, tiling.maxLevel ), tiling.minLevel );
15-
const [ minX, minY, maxX, maxY ] = tiling.getTilesInRange( minLon, minLat, maxLon, maxLat, clampedLevel, normalized );
15+
const [ minX, minY, maxX, maxY ] = tiling.getTilesInRange( minLon, minLat, maxLon, maxLat, clampedLevel, true );
1616
for ( let x = minX; x <= maxX; x ++ ) {
1717

1818
for ( let y = minY; y <= maxY; y ++ ) {

0 commit comments

Comments
 (0)