-
Notifications
You must be signed in to change notification settings - Fork 4
/
AssetManager.js
52 lines (50 loc) · 1.76 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
49
50
51
52
import * as THREE from 'https://cdn.skypack.dev/three@0.142.0';
import {
GLTFLoader
} from 'https://unpkg.com/three@0.142.0/examples/jsm/loaders/GLTFLoader.js';
import { DRACOLoader } from 'https://unpkg.com/three@0.142.0/examples/jsm/loaders/DRACOLoader.js';
const AssetManager = {};
AssetManager.gltfLoader = new GLTFLoader();
AssetManager.dracoLoader = new DRACOLoader();
AssetManager.dracoLoader.setDecoderPath("./");
AssetManager.gltfLoader.setDRACOLoader(AssetManager.dracoLoader);
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 };