Skip to content

Commit 7f7da99

Browse files
pre-cache materials and apply maps when loaded
1 parent bbd3c55 commit 7f7da99

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/model/index.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -856,25 +856,26 @@ let createCube = function (width, height, depth, name, faces, fallbackFaces, tex
856856
return;
857857
}
858858

859-
let textureLoaded = function (texture) {
860-
let n = textureNames[textureRef];
861-
if (n.startsWith("#")) {
862-
n = textureNames[name.substr(1)];
863-
}
864-
865-
let material = new THREE.MeshBasicMaterial({
866-
map: texture,
867-
transparent: hasTransparency,
868-
side: hasTransparency ? THREE.DoubleSide : THREE.FrontSide,
869-
alphaTest: 0.5,
870-
name: f + "_" + textureRef + "_" + n
871-
});
859+
let n = textureNames[textureRef];
860+
if (n.startsWith("#")) {
861+
n = textureNames[name.substr(1)];
862+
}
863+
console.debug("Pre-Caching Material " + hash + ", without meta");
864+
materialCache[hash] = new THREE.MeshBasicMaterial({
865+
map: null,
866+
transparent: hasTransparency,
867+
side: hasTransparency ? THREE.DoubleSide : THREE.FrontSide,
868+
alphaTest: 0.5,
869+
name: f + "_" + textureRef + "_" + n
870+
});
872871

872+
let textureLoaded = function (texture) {
873873
// Add material to cache
874-
console.debug("Caching Material " + hash);
875-
materialCache[hash] = material;
874+
console.debug("Finalizing Cached Material " + hash + ", without meta");
875+
materialCache[hash].map = texture;
876+
materialCache[hash].needsUpdate = true;
876877

877-
resolve(material);
878+
resolve(materialCache[hash]);
878879
};
879880

880881
if (textureCache.hasOwnProperty(hash)) {// Use texture from cache
@@ -914,6 +915,14 @@ let createCube = function (width, height, depth, name, faces, fallbackFaces, tex
914915
return;
915916
}
916917

918+
console.debug("Pre-Caching Material " + hash + ", with meta");
919+
materialCache[hash] = new THREE.MeshBasicMaterial({
920+
map: null,
921+
transparent: hasTransparency,
922+
side: hasTransparency ? THREE.DoubleSide : THREE.FrontSide,
923+
alphaTest: 0.5
924+
});
925+
917926
let frametime = 1;
918927
if (meta.hasOwnProperty("animation")) {
919928
if (meta.animation.hasOwnProperty("frametime")) {
@@ -961,22 +970,14 @@ let createCube = function (width, height, depth, name, faces, fallbackFaces, tex
961970

962971
Promise.all(promises1).then((textures) => {
963972

964-
// Don't cache this material, since it's animated
965-
let material = new THREE.MeshBasicMaterial({
966-
map: textures[0],
967-
transparent: hasTransparency,
968-
side: hasTransparency ? THREE.DoubleSide : THREE.FrontSide,
969-
alphaTest: 0.5
970-
});
971-
972973
let frameCounter = 0;
973974
let textureIndex = 0;
974975
animatedTextures.push(() => {// called on render
975976
if (frameCounter >= frametime) {
976977
frameCounter = 0;
977978

978979
// Set new texture
979-
material.map = textures[textureIndex];
980+
materialCache[hash].map = textures[textureIndex];
980981

981982
textureIndex++;
982983
}
@@ -987,10 +988,11 @@ let createCube = function (width, height, depth, name, faces, fallbackFaces, tex
987988
})
988989

989990
// Add material to cache
990-
console.debug("Caching Material " + hash + ", with meta");
991-
materialCache[hash] = material;
991+
console.debug("Finalizing Cached Material " + hash + ", with meta");
992+
materialCache[hash].map = textures[0];
993+
materialCache[hash].needsUpdate = true;
992994

993-
resolve(material);
995+
resolve(materialCache[hash]);
994996
});
995997
};
996998

0 commit comments

Comments
 (0)