Skip to content

Instantly share code, notes, and snippets.

@mio-U-M
Created June 4, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mio-U-M/13c06e7c4c60f91dfe62d3f4d1ce7f4a to your computer and use it in GitHub Desktop.
Save mio-U-M/13c06e7c4c60f91dfe62d3f4d1ce7f4a to your computer and use it in GitHub Desktop.
// 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