forked from spite/Wagner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderLoader.js
executable file
·74 lines (54 loc) · 1.29 KB
/
ShaderLoader.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
var ShaderLoader = function() {
this.loaded = 0;
this.toLoad = 0;
this.shaders = {};
this.queue = [];
this.onLoadedCallback = function(){};
}
ShaderLoader.prototype.add = function( id, name ) {
this.toLoad++;
this.shaders[ id ] = {
id: id,
name: name,
content: '',
loaded: false
}
this.queue.push( this.shaders[ id ] );
}
ShaderLoader.prototype.processQueue = function() {
var shader = this.queue.pop();
var oReq = new XMLHttpRequest();
oReq.onload = function() {
this.loaded++;
shader.content = oReq.responseText;
if( this.loaded != this.toLoad ) {
this.processQueue();
} else {
this.onLoadedCallback();
}
}.bind( this );
oReq.open( 'get', shader.name, true );
oReq.send();
}
ShaderLoader.prototype.load = function() {
this.processQueue();
}
ShaderLoader.prototype.onLoaded = function( callback ) {
if( this.loaded == this.toLoad ) callback();
else this.onLoadedCallback = callback;
}
ShaderLoader.prototype.get = function( id ) {
function ShaderLoaderGetException( message ) {
this.message = 'Cannot find shader "' + id + '".';
this.name = "ShaderLoaderGetException";
this.toString = function() {
return this.message
};
}
var s = this.shaders[ id ];
if( !s ) {
throw new ShaderLoaderGetException( id );
return;
}
return s.content;
}