-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathShaderRuntime.js
More file actions
326 lines (222 loc) · 8.45 KB
/
ShaderRuntime.js
File metadata and controls
326 lines (222 loc) · 8.45 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import * as THREE from 'three';
let defaultThreeUniforms = [
'normalMatrix', 'viewMatrix', 'projectionMatrix', 'position', 'normal',
'modelViewMatrix', 'uv', 'uv2', 'modelMatrix'
];
function ShaderRuntime() {}
ShaderRuntime.prototype = {
mainCamera: null,
cubeCameras: {},
reserved: { time: null, cameraPosition: null },
umap: {
float: { type: 'f', value: 0 },
int: { type: 'i', value: 0 },
vec2: { type: 'v2', value() { return new THREE.Vector2(); } },
vec3: { type: 'v3', value() { return new THREE.Vector3(); } },
vec4: { type: 'v4', value() { return new THREE.Vector4(); } },
samplerCube: { type: 't' },
sampler2D: { type: 't' }
},
getUmap( type ) {
let value = this.umap[ type ].value;
return typeof value === 'function' ? value() : value;
},
load( sourceOrSources, callback ) {
let sources = sourceOrSources,
onlyOneSource = typeof sourceOrSources === 'string';
if( onlyOneSource ) {
sources = [ sourceOrSources ];
}
let loadedShaders = new Array( sources.length ),
itemsLoaded = 0;
let loadSource = ( index, source ) => {
let loader = new THREE.FileLoader();
loader.load( source, ( json ) => {
let parsed;
try {
parsed = JSON.parse( json );
delete parsed.id; // Errors if passed to rawshadermaterial :(
} catch( e ) {
throw new Error( 'Could not parse shader' + source + '! Please verify the URL is correct.' );
}
this.add( parsed.name, parsed );
loadedShaders[ index ] = parsed;
if( ++itemsLoaded === sources.length ) {
callback( onlyOneSource ? loadedShaders[ 0 ] : loadedShaders );
}
});
};
for( let x = 0; x < sources.length; x++ ) {
loadSource( x, sources[ x ] );
}
},
registerCamera( camera ) {
if( !( camera.isCamera ) ) {
throw new Error( 'Cannot register a non-camera as a camera!' );
}
this.mainCamera = camera;
},
registerCubeCamera( name, camera ) {
if( !camera.renderTarget ) {
throw new Error( 'Cannot register a non-camera as a camera!' );
}
this.cubeCameras[ name ] = camera;
},
unregisterCamera( name ) {
if( name in this.cubeCameras ) {
delete this.cubeCameras[ name ];
} else if( name === this.mainCamera ) {
delete this.mainCamera;
} else {
throw new Error( 'You never registered camera ' + name );
}
},
updateSource( identifier, config, findBy ) {
findBy = findBy || 'name';
if( !this.shaderTypes[ identifier ] ) {
throw new Error( 'Runtime Error: Cannot update shader ' + identifier + ' because it has not been added.' );
}
let newShaderData = this.add( identifier, config ),
shader, x;
for( x = 0; shader = this.runningShaders[ x++ ]; ) {
if( shader[ findBy ] === identifier ) {
extend( shader.material, omit( newShaderData, 'id' ) );
shader.material.needsUpdate = true;
}
}
},
renameShader( oldName, newName ) {
let x, shader;
if( !( oldName in this.shaderTypes ) ) {
throw new Error('Could not rename shader ' + oldName + ' to ' + newName + '. It does not exist.');
}
this.shaderTypes[ newName ] = this.shaderTypes[ oldName ];
delete this.shaderTypes[ oldName ];
for( x = 0; shader = this.runningShaders[ x++ ]; ) {
if( shader.name === oldName ) {
shader.name = newName;
}
}
},
get( identifier ) {
let shaderType = this.shaderTypes[ identifier ];
if( !shaderType.initted ) {
this.create( identifier );
}
return shaderType.material;
},
add( shaderName, config ) {
let newData = clone( config ),
uniform;
newData.fragmentShader = config.fragment;
newData.vertexShader = config.vertex;
delete newData.fragment;
delete newData.vertex;
for( var uniformName in newData.uniforms ) {
uniform = newData.uniforms[ uniformName ];
if( uniform.value === null ) {
newData.uniforms[ uniformName ].value = this.getUmap( uniform.glslType );
}
}
if( shaderName in this.shaderTypes ) {
// maybe not needed? too sleepy, need document
extend( this.shaderTypes[ shaderName ], newData );
} else {
this.shaderTypes[ shaderName ] = newData;
}
return newData;
},
create( identifier ) {
let shaderType = this.shaderTypes[ identifier ];
let keys = Object.keys( shaderType );
// Three's shadermaterial id is not assignable, so filter it out
let withoutId = {};
for( let i = 0; i < keys.length; i++ ) {
if( keys[ i ] !== 'id' ) {
withoutId[ keys[ i ] ] = shaderType[ keys[ i ] ];
}
}
shaderType.material = new THREE.RawShaderMaterial( withoutId );
this.runningShaders.push( shaderType );
shaderType.init && shaderType.init( shaderType.material );
shaderType.material.needsUpdate = true;
shaderType.initted = true;
return shaderType.material;
},
updateRuntime( identifier, data, findBy ) {
findBy = findBy || 'name';
let shader, x, uniformName, uniform;
// This loop does not appear to be a slowdown culprit
for( x = 0; shader = this.runningShaders[ x++ ]; ) {
if( shader[ findBy ] === identifier ) {
for( uniformName in data.uniforms ) {
if( uniformName in this.reserved ) {
continue;
}
if( uniformName in shader.material.uniforms ) {
uniform = data.uniforms[ uniformName ];
// this is nasty, since the shader serializes
// CubeCamera model to string. Maybe not update it at
// all?
if( uniform.type === 't' && typeof uniform.value === 'string' ) {
uniform.value = this.cubeCameras[ uniform.value ].renderTarget;
}
shader.material.uniforms[ uniformName ].value = data.uniforms[ uniformName ].value;
}
}
}
}
},
// Update global shader uniform values
updateShaders( time, obj ) {
let shader, x;
obj = obj || {};
for( x = 0; shader = this.runningShaders[ x++ ]; ) {
for( let uniform in obj.uniforms ) {
if( uniform in shader.material.uniforms ) {
shader.material.uniforms[ uniform ].value = obj.uniforms[ uniform ];
}
}
if( 'cameraPosition' in shader.material.uniforms && this.mainCamera ) {
shader.material.uniforms.cameraPosition.value = this.mainCamera.position.clone();
}
if( 'viewMatrix' in shader.material.uniforms && this.mainCamera ) {
shader.material.uniforms.viewMatrix.value = this.mainCamera.matrixWorldInverse;
}
if( 'time' in shader.material.uniforms ) {
shader.material.uniforms.time.value = time;
}
}
},
shaderTypes: {},
runningShaders: []
};
// Convenience methods so we don't have to include underscore
function extend() {
let length = arguments.length,
obj = arguments[ 0 ];
if( length < 2 ) {
return obj;
}
for( let index = 1; index < length; index++ ) {
let source = arguments[ index ],
keys = Object.keys( source || {} ),
l = keys.length;
for( let i = 0; i < l; i++ ) {
let key = keys[i];
obj[ key ] = source[ key ];
}
}
return obj;
}
function clone( obj ) {
return extend( {}, obj );
}
function omit( obj, ...keys ) {
let cloned = clone( obj ), x, key;
for( x = 0; key = keys[ x++ ]; ) {
delete cloned[ key ];
}
return cloned;
}
export default ShaderRuntime;