Skip to content

Commit

Permalink
Close #761 tiles: use file loader if HTTP headers are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Sep 23, 2022
1 parent 364fa0c commit 6762f28
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
37 changes: 25 additions & 12 deletions src/adapters/cubemap-tiles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,17 @@ export class CubemapTilesAdapter extends CubemapAdapter {
* @member {external:THREE.ImageLoader}
* @private
*/
this.loader = new ImageLoader();
if (this.psv.config.withCredentials) {
this.loader.setWithCredentials(true);
this.loader = null;

if (this.psv.config.requestHeaders) {
utils.logWarn('CubemapTilesAdapter fallbacks to file loader because "requestHeaders" where provided. '
+ 'Consider removing "requestHeaders" if you experience performances issues.');
}
if (this.psv.config.requestHeaders && typeof this.psv.config.requestHeaders === 'object') {
this.loader.setRequestHeader(this.psv.config.requestHeaders);
else {
this.loader = new ImageLoader();
if (this.psv.config.withCredentials) {
this.loader.setWithCredentials(true);
}
}

this.psv.on(CONSTANTS.EVENTS.POSITION_UPDATED, this);
Expand Down Expand Up @@ -447,13 +452,7 @@ export class CubemapTilesAdapter extends CubemapAdapter {
}
const url = panorama.tileUrl(CUBE_HASHMAP[tile.face], col, row);

if (this.psv.config.requestHeaders && typeof this.psv.config.requestHeaders === 'function') {
this.loader.setRequestHeader(this.psv.config.requestHeaders(url));
}

return new Promise((resolve, reject) => {
this.loader.load(url, resolve, undefined, reject);
})
return this.__loadImage(url)
.then((image) => {
if (!task.isCancelled()) {
const material = new MeshBasicMaterial({ map: utils.createTexture(image) });
Expand All @@ -472,6 +471,20 @@ export class CubemapTilesAdapter extends CubemapAdapter {
});
}

/**
* @private
*/
__loadImage(url) {
if (this.loader) {
return new Promise((resolve, reject) => {
this.loader.load(url, resolve, undefined, reject);
});
}
else {
return this.psv.textureLoader.loadImage(url);
}
}

/**
* @summary Applies a new texture to the faces
* @param {int} face
Expand Down
37 changes: 25 additions & 12 deletions src/adapters/equirectangular-tiles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,17 @@ export class EquirectangularTilesAdapter extends EquirectangularAdapter {
* @member {external:THREE.ImageLoader}
* @private
*/
this.loader = new ImageLoader();
if (this.psv.config.withCredentials) {
this.loader.setWithCredentials(true);
this.loader = null;

if (this.psv.config.requestHeaders) {
utils.logWarn('EquirectangularTilesAdapter fallbacks to file loader because "requestHeaders" where provided. '
+ 'Consider removing "requestHeaders" if you experience performances issues.');
}
if (this.psv.config.requestHeaders && typeof this.psv.config.requestHeaders === 'object') {
this.loader.setRequestHeader(this.psv.config.requestHeaders);
else {
this.loader = new ImageLoader();
if (this.psv.config.withCredentials) {
this.loader.setWithCredentials(true);
}
}

this.psv.on(CONSTANTS.EVENTS.POSITION_UPDATED, this);
Expand Down Expand Up @@ -579,13 +584,7 @@ export class EquirectangularTilesAdapter extends EquirectangularAdapter {
const panorama = this.psv.config.panorama;
const url = panorama.tileUrl(tile.col, tile.row);

if (this.psv.config.requestHeaders && typeof this.psv.config.requestHeaders === 'function') {
this.loader.setRequestHeader(this.psv.config.requestHeaders(url));
}

return new Promise((resolve, reject) => {
this.loader.load(url, resolve, undefined, reject);
})
return this.__loadImage(url)
.then((image) => {
if (!task.isCancelled()) {
const material = new MeshBasicMaterial({ map: utils.createTexture(image) });
Expand All @@ -604,6 +603,20 @@ export class EquirectangularTilesAdapter extends EquirectangularAdapter {
});
}

/**
* @private
*/
__loadImage(url) {
if (this.loader) {
return new Promise((resolve, reject) => {
this.loader.load(url, resolve, undefined, reject);
});
}
else {
return this.psv.textureLoader.loadImage(url);
}
}

/**
* @summary Applies a new texture to the faces
* @param {int} col
Expand Down
6 changes: 3 additions & 3 deletions src/services/TextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ export class TextureLoader extends AbstractService {

return new Promise((resolve, reject) => {
let progress = 0;
onProgress && onProgress(progress);
onProgress?.(progress);

this.loader.load(
url,
(result) => {
progress = 100;
onProgress && onProgress(progress);
onProgress?.(progress);
resolve(result);
},
(e) => {
if (e.lengthComputable) {
const newProgress = e.loaded / e.total * 100;
if (newProgress > progress) {
progress = newProgress;
onProgress && onProgress(progress);
onProgress?.(progress);
}
}
},
Expand Down

0 comments on commit 6762f28

Please sign in to comment.