Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore/threejs/Loader.js
4627 views
1
/*!
2
* @class bkcore.threejs.Loader
3
*
4
* Loads multiple recources, get progress, callback friendly.
5
* Supports textures, texturesCube, geometries, analysers, images.
6
*
7
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
8
*/
9
10
/*!
11
* @package bkcore.threejs
12
*/
13
var bkcore = bkcore || {};
14
bkcore.threejs = bkcore.threejs || {};
15
16
bkcore.NONE = undefined;
17
18
/**
19
* Creates a new loader
20
* @param {Object{onLoad, onError, onProgress}} opts Callbacks
21
*/
22
bkcore.threejs.Loader = function(opts)
23
{
24
var self = this;
25
26
this.jsonLoader = new THREE.JSONLoader();
27
28
this.errorCallback = opts.onError == undefined ? function(s){ console.warn("Error while loading %s.".replace("%s", s)) } : opts.onError;
29
this.loadCallback = opts.onLoad == undefined ? function(){ console.log("Loaded.") } : opts.onLoad;
30
this.progressCallback = opts.onProgress == undefined ? function(progress, type, name){ /**/ } : opts.onProgress;
31
32
this.types = {
33
textures: null,
34
texturesCube: null,
35
geometries: null,
36
analysers: null,
37
images: null,
38
sounds: null
39
};
40
41
this.states = {};
42
this.data = {};
43
44
for(var t in this.types)
45
{
46
this.data[t] = {};
47
this.states[t] = {};
48
}
49
50
this.progress = {
51
total: 0,
52
remaining: 0,
53
loaded: 0,
54
finished: false
55
};
56
}
57
58
/**
59
* Load the given list of resources
60
* @param {textures, texturesCube, geometries, analysers, images} data
61
*/
62
bkcore.threejs.Loader.prototype.load = function(data)
63
{
64
var self = this;
65
66
for(var k in this.types)
67
{
68
if(k in data)
69
{
70
var size = 0;
71
for(var j in data[k])
72
size++;
73
this.progress.total += size;
74
this.progress.remaining += size;
75
}
76
}
77
78
for(var t in data.textures)
79
this.loadTexture(t, data.textures[t]);
80
81
for(var c in data.texturesCube)
82
this.loadTextureCube(c, data.texturesCube[c]);
83
84
for(var g in data.geometries)
85
this.loadGeometry(g, data.geometries[g]);
86
87
for(var a in data.analysers)
88
this.loadAnalyser(a, data.analysers[a]);
89
90
for(var i in data.images)
91
this.loadImage(i, data.images[i]);
92
93
for(var s in data.sounds)
94
this.loadSound(data.sounds[s].src, s, data.sounds[s].loop, data.sounds[s].usePanner);
95
96
this.progressCallback.call(this, this.progress);
97
}
98
99
bkcore.threejs.Loader.prototype.updateState = function(type, name, state)
100
{
101
if(!(type in this.types))
102
{
103
console.warn("Unkown loader type.");
104
return;
105
}
106
107
if(state == true)
108
{
109
this.progress.remaining--;
110
this.progress.loaded++;
111
this.progressCallback.call(this, this.progress, type, name);
112
}
113
114
this.states[type][name] = state;
115
116
117
if(this.progress.loaded == this.progress.total)
118
{
119
this.loadCallback.call(this);
120
}
121
}
122
123
/**
124
* Get loaded resource
125
* @param string type [textures, texturesCube, geometries, analysers, images]
126
* @param string name
127
* @return Mixed
128
*/
129
bkcore.threejs.Loader.prototype.get = function(type, name)
130
{
131
if(!(type in this.types))
132
{
133
console.warn("Unkown loader type.");
134
return null;
135
}
136
if(!(name in this.data[type]))
137
{
138
console.warn("Unkown file.");
139
return null;
140
}
141
142
return this.data[type][name];
143
}
144
145
bkcore.threejs.Loader.prototype.loaded = function(type, name)
146
{
147
if(!(type in this.types))
148
{
149
console.warn("Unkown loader type.");
150
return null;
151
}
152
if(!(name in this.states[type]))
153
{
154
console.warn("Unkown file.");
155
return null;
156
}
157
158
return this.states[type][name];
159
}
160
161
bkcore.threejs.Loader.prototype.loadTexture = function(name, url)
162
{
163
var self = this;
164
this.updateState("textures", name, false);
165
this.data.textures[name] = THREE.ImageUtils.loadTexture(
166
url,
167
bkcore.NONE,
168
function(){
169
self.updateState("textures", name, true);
170
},
171
function(){
172
self.errorCallback.call(self, name);
173
}
174
);
175
}
176
177
bkcore.threejs.Loader.prototype.loadTextureCube = function(name, url)
178
{
179
var self = this;
180
181
var urls = [
182
url.replace("%1", "px"), url.replace("%1", "nx"),
183
url.replace("%1", "py"), url.replace("%1", "ny"),
184
url.replace("%1", "pz"), url.replace("%1", "nz")
185
];
186
187
this.updateState("texturesCube", name, false);
188
this.data.texturesCube[name] = THREE.ImageUtils.loadTextureCube(
189
urls,
190
new THREE.CubeRefractionMapping(),
191
function(){
192
self.updateState("texturesCube", name, true);
193
}
194
);
195
}
196
197
bkcore.threejs.Loader.prototype.loadGeometry = function(name, url)
198
{
199
var self = this;
200
this.data.geometries[name] = null;
201
this.updateState("geometries", name, false);
202
this.jsonLoader.load(
203
url,
204
function(a){
205
self.data.geometries[name] = a;
206
self.updateState("geometries", name, true);
207
}
208
);
209
}
210
211
bkcore.threejs.Loader.prototype.loadAnalyser = function(name, url)
212
{
213
var self = this;
214
this.updateState("analysers", name, false);
215
this.data.analysers[name] = new bkcore.ImageData(
216
url,
217
function(){
218
self.updateState("analysers", name, true);
219
}
220
);
221
}
222
223
bkcore.threejs.Loader.prototype.loadImage = function(name, url)
224
{
225
var self = this;
226
this.updateState("images", name, false);
227
var e = new Image();
228
e.onload = function() {
229
self.updateState("images", name, true) ;
230
};
231
e.crossOrigin = "anonymous";
232
e.src = url;
233
this.data.images[name] = e;
234
}
235
236
bkcore.threejs.Loader.prototype.loadSound = function(src, name, loop){
237
var self = this;
238
this.updateState("sounds", name, false);
239
240
bkcore.Audio.addSound(
241
src,
242
name,
243
loop,
244
function(){
245
self.updateState("sounds", name, true);
246
}
247
);
248
249
this.data.sounds[name] = {
250
play: function(){
251
bkcore.Audio.play(name);
252
},
253
stop: function(){
254
bkcore.Audio.stop(name);
255
},
256
volume: function(vol){
257
bkcore.Audio.volume(name, vol);
258
}
259
};
260
};
261