-
Notifications
You must be signed in to change notification settings - Fork 5
/
ShaderLoader.js
198 lines (127 loc) · 4.24 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Shader Loader will Load any shader you want,
// And be able to add in large functions ( such as noise )
// with regex inside the shader
function ShaderLoader( pathToShaders , pathToChunks ){
this.fragmentShaders = {};
this.vertexShaders = {};
this.simulationShaders = {};
this.fs = this.fragmentShaders;
this.vs = this.vertexShaders;
this.ss = this.simulationShaders;
this.pathToShaders = pathToShaders || "/" ;
this.pathToChunks = pathToChunks || pathToShaders;
this.shaderChunks = {};
this.shadersLoaded = 0;
this.shadersToLoad = 0;
}
/*
Loads in a shader chunk when told to by
onShaderLoaded.
it is important to know that the title of the
chunk needs to be the same as the reference in the shader
AKA, if I use:
$simplexNoise
I will need to create a file in the pathToChunks directory
called
simplexNoise.js
*/
ShaderLoader.prototype.loadShaderChunk = function( type ){
var path = this.pathToChunks + "/" + type + ".glsl";
var self = this;
$.ajax({
url:path,
dataType:'text',
context:{
title:type,
path: path
},
complete: function( r ){
self.onChunkLoaded( r.responseText , this.title );
},
error:function( r ){
console.log( 'ERROR: Unable to Load Shader' + this.path );
self.onChunkLoaded( " NO SHADER LOADED " , this.title );
}
});
}
ShaderLoader.prototype.onChunkLoaded = function( chunk , title ){
this.shaderChunks[title] = chunk;
}
/*
This function Loads a shader with whatever title/
type we prefer.
*/
ShaderLoader.prototype.load = function( shader , title , type ){
var self = this;
this._beginLoad( shader , title , type );
// request the file over AJAX
$.ajax({
url: self.pathToShaders +"/" + shader + ".glsl" ,
dataType: 'text',
context: {
type: type
},
complete: function(r){
self.onShaderLoaded( r.responseText , title , this.type );
}
});
}
/*
Once a Shader is loaded, check to see if there are any extra chunks
we need to find and pull in.
Will recall itself, until the chunk has been loaded in
*/
ShaderLoader.prototype.onShaderLoaded = function( shaderText , title , type ){
var finalShader = shaderText;
var readyToLoad = true;
var array = finalShader.split( "$" );
for( var i = 1; i < array.length; i++ ){
var chunkName = array[i].split("\n")[0];
if( this.shaderChunks[chunkName] ){
var tmpShader = finalShader.split( "$" + chunkName );
finalShader = tmpShader.join( this.shaderChunks[chunkName] );
}else{
readyToLoad = false;
this.loadShaderChunk( chunkName );
}
}
if( readyToLoad ){
if( type == 'vertex' ){
this.vertexShaders[ title ] = finalShader;
}else if( type == 'fragment' ){
this.fragmentShaders[ title ] = finalShader;
}else if( type == 'simulation' ){
this.simulationShaders[ title ] = finalShader;
}
this._endLoad( finalShader , title , type );
}else{
var self = this;
setTimeout( function(){
self.onShaderLoaded( finalShader , title , type )
}, 300 );
}
}
// might add something later...
ShaderLoader.prototype._beginLoad = function( shader , title , type ){
this.shadersToLoad ++;
this.beginLoad( shader , title , type );
}
ShaderLoader.prototype._endLoad = function( shaderText , title , type ){
this.shadersLoaded ++;
if( this.shadersLoaded == this.shadersToLoad ){
this.shaderSetLoaded();
}
this.endLoad( shaderText , title , type );
}
ShaderLoader.prototype.setValue = function( shader , name , value ){
//console.log( name , value );
var a = '@'+name;
//console.log( a );
var replaced = false;
var newStr = shader.replace( a , function(token){replaced = true; return value;});
console.log( 'replaced' , replaced );
return newStr;
}
ShaderLoader.prototype.shaderSetLoaded = function(){}
ShaderLoader.prototype.endLoad = function(){}
ShaderLoader.prototype.beginLoad = function(){}