mirror of
https://github.com/troisjs/trois.git
synced 2024-11-24 04:12:02 +08:00
wip
This commit is contained in:
parent
a7a4f15734
commit
b37c1b465f
@ -1,37 +0,0 @@
|
||||
import { TextureLoader } from 'three';
|
||||
|
||||
export default function useTextures() {
|
||||
const obj = {
|
||||
loader: new TextureLoader(),
|
||||
count: 0,
|
||||
textures: [],
|
||||
loadProgress: 0,
|
||||
loadTextures,
|
||||
dispose,
|
||||
};
|
||||
return obj;
|
||||
|
||||
function loadTextures(images, cb) {
|
||||
obj.count = images.length;
|
||||
obj.textures.splice(0);
|
||||
obj.loadProgress = 0;
|
||||
Promise.all(images.map(loadTexture)).then(cb);
|
||||
};
|
||||
|
||||
function loadTexture(img, index) {
|
||||
return new Promise(resolve => {
|
||||
obj.loader.load(
|
||||
img.src,
|
||||
texture => {
|
||||
obj.loadProgress += 1 / obj.count;
|
||||
obj.textures[index] = texture;
|
||||
resolve(texture);
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
function dispose() {
|
||||
obj.textures.forEach(t => t.dispose());
|
||||
}
|
||||
};
|
50
src/use/useTextures.ts
Normal file
50
src/use/useTextures.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { Texture, TextureLoader } from 'three'
|
||||
|
||||
interface TextureConfigInterface {
|
||||
src: string
|
||||
}
|
||||
|
||||
interface TexturesInterface {
|
||||
loader: TextureLoader
|
||||
count: number
|
||||
textures: Texture[],
|
||||
loadProgress: number
|
||||
loadTextures(images: TextureConfigInterface[], cb: {() : void}): void
|
||||
dispose(): void
|
||||
}
|
||||
|
||||
export default function useTextures(): TexturesInterface {
|
||||
const obj: TexturesInterface = {
|
||||
loader: new TextureLoader(),
|
||||
count: 0,
|
||||
textures: [],
|
||||
loadProgress: 0,
|
||||
loadTextures,
|
||||
dispose,
|
||||
}
|
||||
return obj
|
||||
|
||||
function loadTextures(images: TextureConfigInterface[], cb: {() : void}) {
|
||||
obj.count = images.length
|
||||
obj.textures.splice(0)
|
||||
obj.loadProgress = 0
|
||||
Promise.all(images.map(loadTexture)).then(cb)
|
||||
}
|
||||
|
||||
function loadTexture(img: TextureConfigInterface, index: number) {
|
||||
return new Promise(resolve => {
|
||||
obj.loader.load(
|
||||
img.src,
|
||||
texture => {
|
||||
obj.loadProgress += 1 / obj.count
|
||||
obj.textures[index] = texture
|
||||
resolve(texture)
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
function dispose() {
|
||||
obj.textures.forEach(t => t.dispose())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user