From 53dccc5408b66c93c6616ddae79964f7cdbdfe03 Mon Sep 17 00:00:00 2001 From: NiHoel Date: Wed, 14 Nov 2018 21:56:37 +0100 Subject: [PATCH] external loader for tiles multiRes.loader: function(node, image) => Promise --- src/js/libpannellum.js | 28 ++++++++++++++++++++++------ src/js/pannellum.js | 1 + 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/js/libpannellum.js b/src/js/libpannellum.js index 92644b3..0ec18ab 100644 --- a/src/js/libpannellum.js +++ b/src/js/libpannellum.js @@ -845,7 +845,12 @@ function Renderer(container) { this.level = level; this.x = x; this.y = y; - this.path = path.replace('%s',side).replace('%l',level).replace('%x',x).replace('%y',y); + if (!path) { + this.path = side + '_' + level + '_' + x + '_' + y; + } else { + this.path = path.replace('%s', side).replace('%l', level).replace('%x', x).replace('%y', y); + } + this.uri = encodeURI(this.path + '.' + image.extension); } /** @@ -1146,10 +1151,21 @@ function Renderer(container) { this.image.addEventListener('error', loadFn); // ignore missing tile file to support partial image, otherwise retry loop causes high CPU load }; - TextureImageLoader.prototype.loadTexture = function(src, texture, callback) { + TextureImageLoader.prototype.loadTexture = function(node, src, texture, callback) { this.texture = texture; this.callback = callback; - this.image.src = src; + if (src instanceof Function) { + src(node, this.image, this.texture).then(img => { + if (!image) + this.callback(this.texture, false); + else if (img != this.image) { + processLoadedTexture(img, this.texture); + this.callback(img, true); + } + }) + } else { + this.image.src = src; + } }; function PendingTextureRequest(node, src, texture, callback) { @@ -1162,7 +1178,7 @@ function Renderer(container) { function releaseTextureImageLoader(til) { if (pendingTextureRequests.length) { var req = pendingTextureRequests.shift(); - til.loadTexture(req.src, req.texture, req.callback); + til.loadTexture(req.node, req.src, req.texture, req.callback); } else textureImageCache[cacheTop++] = til; } @@ -1174,7 +1190,7 @@ function Renderer(container) { crossOrigin = _crossOrigin; var texture = gl.createTexture(); if (cacheTop) - textureImageCache[--cacheTop].loadTexture(src, texture, callback); + textureImageCache[--cacheTop].loadTexture(node, src, texture, callback); else pendingTextureRequests.push(new PendingTextureRequest(node, src, texture, callback)); return texture; @@ -1187,7 +1203,7 @@ function Renderer(container) { * @param {MultiresNode} node - Input node. */ function processNextTile(node) { - loadTexture(node, encodeURI(node.path + '.' + image.extension), function(texture, loaded) { + loadTexture(node, image.loader || node.uri, function (texture, loaded) { node.texture = texture; node.textureLoaded = loaded ? 2 : 1; }, globalParams.crossOrigin); diff --git a/src/js/pannellum.js b/src/js/pannellum.js index bf927c7..7eae2ad 100644 --- a/src/js/pannellum.js +++ b/src/js/pannellum.js @@ -322,6 +322,7 @@ function init() { infoDisplay.load.lbar.style.display = 'none'; } else if (config.type == 'multires') { var c = JSON.parse(JSON.stringify(config.multiRes)); // Deep copy + c.loader = config.multiRes.loader; // Avoid "undefined" in path, check (optional) multiRes.basePath, too // Use only multiRes.basePath if it's an absolute URL if (config.basePath && config.multiRes.basePath &&