forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetLoader.js
More file actions
80 lines (74 loc) · 2.35 KB
/
AssetLoader.js
File metadata and controls
80 lines (74 loc) · 2.35 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import * as THREE from "three";
import {FBXLoader} from "three/examples/jsm/loaders/FBXLoader.js";
class AssetLoader {
constructor() {
this.assets = {};
}
async init() {
return new Promise(async (resolve) => {
let assets = await $.get("assets/assetLoader.json");
await this.load(assets, () => {
console.log(this.assets)
console.log("AssetLoader: done loading!");
resolve()
});
})
}
async load(assets, callback) {
var textureLoader = new THREE.TextureLoader();
var fbxl = new FBXLoader();
var assetsNumber = 0;
var assetsLoaded = 0;
Object.keys(assets).forEach(function () {
return assetsNumber++;
});
Object.keys(assets).forEach((p) => {
var img, path, type;
type = assets[p].type;
path = assets[p].path;
if (type === "texture") {
textureLoader.load(path, (texture) => {
this.assets[p] = texture;
assetsLoaded++;
if (assetsLoaded === assetsNumber) {
return callback();
}
});
}
if (type === "text") {
$.get(path, (data) => {
this.assets[p] = data;
assetsLoaded++;
if (assetsLoaded === assetsNumber) {
return callback();
}
});
}
if (type === "image") {
img = new Image();
img.onload = () => {
this.assets[p] = img;
assetsLoaded++;
if (assetsLoaded === assetsNumber) {
return callback();
}
};
img.src = path;
}
if (type === "fbx") {
return fbxl.load(path, (fbx) => {
this.assets[p] = fbx;
assetsLoaded++;
if (assetsLoaded === assetsNumber) {
return callback();
}
});
}
});
return this;
}
get(assetName) {
return this.assets[assetName];
}
}
export {AssetLoader};