Created
June 4, 2020 23:19
-
-
Save mio-U-M/13c06e7c4c60f91dfe62d3f4d1ce7f4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// textures should be object | |
export function threeTextureLoad(textures) { | |
const texturePromises = []; | |
const loadedTextures = {}; | |
const loader = new THREE.TextureLoader(); | |
Object.keys(textures).forEach(key => { | |
texturePromises.push( | |
new Promise((resolve, reject) => { | |
const entry = textures[key]; | |
// change path depends on your enviroment | |
const url = `/img/${entry}`; | |
loader.load( | |
url, | |
texture => { | |
loadedTextures[key] = texture; | |
if (texture instanceof THREE.Texture) resolve(); | |
}, | |
xhr => { | |
reject( | |
new Error( | |
xhr + | |
"An error occurred loading while loading: " + | |
entry.url | |
) | |
); | |
} | |
); | |
}) | |
); | |
}); | |
return Promise.all(texturePromises).then(() => { | |
return loadedTextures; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment