From b37c1b465f7cb3e1b828136161c249d14b6a4828 Mon Sep 17 00:00:00 2001 From: Kevin Levron Date: Sun, 18 Apr 2021 23:06:50 +0200 Subject: [PATCH] wip --- src/use/useTextures.js | 37 ------------------------------- src/use/useTextures.ts | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 37 deletions(-) delete mode 100644 src/use/useTextures.js create mode 100644 src/use/useTextures.ts diff --git a/src/use/useTextures.js b/src/use/useTextures.js deleted file mode 100644 index d9924d8..0000000 --- a/src/use/useTextures.js +++ /dev/null @@ -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()); - } -}; diff --git a/src/use/useTextures.ts b/src/use/useTextures.ts new file mode 100644 index 0000000..3318e56 --- /dev/null +++ b/src/use/useTextures.ts @@ -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()) + } +}