From 3fb1fddd1aa0443439ad12f3300931c057a8bdba Mon Sep 17 00:00:00 2001 From: Marshall Quander <marshall@quander.me> Date: Sun, 16 Sep 2018 16:57:28 -0700 Subject: [PATCH] Don't clone GLTF environment scenes for no reason --- src/components/gltf-bundle.js | 2 +- src/components/gltf-model-plus.js | 18 +++++++++++++----- src/hub.js | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/components/gltf-bundle.js b/src/components/gltf-bundle.js index 72163ec54..7ad784082 100644 --- a/src/components/gltf-bundle.js +++ b/src/components/gltf-bundle.js @@ -28,7 +28,7 @@ AFRAME.registerComponent("gltf-bundle", { const src = new URL(asset.src, this.baseURL).href; const gltfEl = document.createElement("a-entity"); - gltfEl.setAttribute("gltf-model-plus", { src, inflate: true }); + gltfEl.setAttribute("gltf-model-plus", { src, useCache: false, inflate: true }); loaded.push(new Promise(resolve => gltfEl.addEventListener("model-loaded", resolve))); this.el.appendChild(gltfEl); } diff --git a/src/components/gltf-model-plus.js b/src/components/gltf-model-plus.js index a5080fe24..625fc4c35 100644 --- a/src/components/gltf-model-plus.js +++ b/src/components/gltf-model-plus.js @@ -279,6 +279,7 @@ AFRAME.registerComponent("gltf-model-plus", { schema: { src: { type: "string" }, contentType: { type: "string" }, + useCache: { default: true }, inflate: { default: false } }, @@ -300,6 +301,17 @@ AFRAME.registerComponent("gltf-model-plus", { }); }, + async loadModel(src, contentType, technique, useCache) { + if (useCache) { + if (!GLTFCache[src]) { + GLTFCache[src] = await loadGLTF(src, contentType, technique); + } + return cloneGltf(GLTFCache[src]); + } else { + return await loadGLTF(src, contentType, technique); + } + }, + async applySrc(src, contentType) { try { // If the src attribute is a selector, get the url from the asset item. @@ -319,11 +331,7 @@ AFRAME.registerComponent("gltf-model-plus", { return; } - if (!GLTFCache[src]) { - GLTFCache[src] = loadGLTF(src, contentType, this.preferredTechnique); - } - - const model = cloneGltf(await GLTFCache[src]); + const model = await this.loadModel(src, contentType, this.preferredTechnique, this.data.useCache); // If we started loading something else already // TODO: there should be a way to cancel loading instead diff --git a/src/hub.js b/src/hub.js index 798b41235..6bc7382c8 100644 --- a/src/hub.js +++ b/src/hub.js @@ -561,7 +561,7 @@ const onReady = async () => { if (/\.gltf/i.test(sceneUrl) || /\.glb/i.test(sceneUrl)) { const resolved = await resolveMedia(sceneUrl, false, 0); const gltfEl = document.createElement("a-entity"); - gltfEl.setAttribute("gltf-model-plus", { src: resolved.raw, inflate: true }); + gltfEl.setAttribute("gltf-model-plus", { src: resolved.raw, useCache: false, inflate: true }); gltfEl.addEventListener("model-loaded", () => initialEnvironmentEl.emit("bundleloaded")); initialEnvironmentEl.appendChild(gltfEl); } else { -- GitLab