-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoader.js
executable file
·154 lines (136 loc) · 3.59 KB
/
Loader.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// interface to load assets and provide progress reports to frontend
var AssetFactory = function (onFinish, onProgress) {
var that = {};
var map = {};
var loader = THREE.JSONLoader(false);
var filesLoaded;
var filesToLoad;
var loadAudio = function (key, uri) {
var audio = new Audio();
audio.AddEventListener('canplaythrough', finishLoading, false);
audio.src = uri
map[key] = audio;
};
var loadMesh = function (key, uri) {
loader.load(uri, function (geo, mats) {
map[key] = THREE.Mesh(geo, mats);
finishLoading();
});
};
var finishLoading = function () {
filesLoaded += 1;
if (filesToLoad <= filesLoaded) {
// finish
if(typeof onLoad === "function") {
onFinish(map);
}
}
else
{
onProgress(filesLoaded, filesToLoad);
}
}
var loadAssets = function (assetMap, callback, progressCallback) {
filesToLoad = Object.keys(assetMap).length;
filesLoaded = 0;
onProgress = progressCallback;
onFinish = callback;
if(filesToLoad === 0)
{
onFinish({});
}
for (var prop in assetMap) {
if(typeof assetMap[prop] === "string") {
var uri = assetMap[prop];
if(uri.indexOf(".ogg") >= 0 || uri.indexOf(".mp3") >= 0)
{
// audio
loadAudio(prop, uri);
}
else if(uri.indexOf(".js") >= 0 || uri.indexOf(".json") >= 0)
{
// mesh
loadMesh(prop, uri);
}
else
{
console.error("file " + uri + " not supported");
}
}
}
};
// for testing draw
var dummyLoad = function(callback, progressCallback) {
onProgress = progressCallback;
onFinish = callback;
var dummyTime = 0;
var timerInterval = setInterval(function() {
dummyTime += 1;
if(dummyTime < 10) {
onProgress(dummyTime, 10);
}
else {
clearInterval(timerInterval);
onFinish();
}
}, 1000);
};
that.loadAssets = loadAssets;
that.dummyLoad = dummyLoad;
return that;
};
var LoadingScreen = function (width, height, assetMap, callback) {
var factory = new AssetFactory();
var map = assetMap || {};
var that = {};
var loading = true;
var done = false;
var barBorder = document.getElementById("barBorder");
var elemHeight = height / 10;
var elemWidth = width * 0.8;
barBorder.style.height = elemHeight.toString();
barBorder.style.width = elemWidth.toString();
barBorder.style.top = (height / 2 - elemHeight).toString();
barBorder.style.left = (elemWidth - width / 2).toString();
var loadingBar = document.getElementById("loadingBar");
loadingBar.style.height = elemHeight.toString();
var finished = function(map) {
done = true;
loading = false;
if (typeof callback === "function") {
callback(map);
}
hideLoadingScreen();
};
var progress = function(loaded, total) {
// update graphic
var pcent = (loaded / total) * 100;
loadingBar.style.width = pcent.toString() + "%";
};
// call from the main program's render loop
// first call starts the load requests
var animate = function() {
if(loading === true) {
if(typeof assetMap !== "object") {
//factory.loadAssets(assetMap, finished, progress);
factory.dummyLoad(finished, progress);
}
else {
factory.loadAssets(assetMap, finished, progress);
}
}
};
var hideLoadingScreen = function () {
barBorder.style.display = "none";
loadingBar.style.display = "none";
};
that.animate = animate;
that.done = done;
return that;
};
/*var lScreen = new LoadingScreen(640, 480);
var testAnim = function() {
if(lScreen.done === false)
requestAnimationFrame(testAnim);
lScreen.animate();
};*/