-
Notifications
You must be signed in to change notification settings - Fork 1
/
AssetManager.js
48 lines (46 loc) · 1.52 KB
/
AssetManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import * as THREE from 'https://cdn.skypack.dev/[email protected]';
import {
GLTFLoader
} from 'https://cdn.skypack.dev/[email protected]/examples/jsm/loaders/GLTFLoader.js';
const AssetManager = {};
AssetManager.gltfLoader = new GLTFLoader();
AssetManager.audioLoader = new THREE.AudioLoader();
AssetManager.loadGLTFAsync = (url) => {
return new Promise((resolve, reject) => {
AssetManager.gltfLoader.load(url, obj => {
resolve(obj);
})
});
}
AssetManager.loadAudioAsync = (url) => {
return new Promise((resolve, reject) => {
AssetManager.audioLoader.load(url, (buffer) => {
resolve(buffer);
});
})
}
AssetManager.loadTextureAsync = (url) => {
return new Promise((resolve, reject) => {
THREE.ImageUtils.loadTexture(url, null, (tex) => {
resolve(tex);
})
})
}
AssetManager.loadAll = (promiseArr, element, message) => {
let count = promiseArr.length;
let results = [];
element.innerHTML = `${message} (${promiseArr.length - count} / ${promiseArr.length})...`
return new Promise((resolve, reject) => {
promiseArr.forEach((promise, i) => {
promise.then(result => {
results[i] = result;
count--;
element.innerHTML = `${message} (${promiseArr.length - count} / ${promiseArr.length})...`
if (count === 0) {
resolve(results);
}
})
})
});
}
export { AssetManager };