forked from scgruber/MachineSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaders.js
60 lines (52 loc) · 1.25 KB
/
shaders.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
// Machine System
// Sam Gruber
//
// Global shader list
var shaderScripts = {};
shaderScripts['frag'] = {
type: 'x-shader/x-fragment',
source: [
'precision mediump float;',
'',
'varying vec4 vColor;',
'',
'void main(void) {',
' gl_FragColor = vColor;',
'}'
].join('\n')
};
shaderScripts['vert'] = {
type: 'x-shader/x-vertex',
source: [
'attribute vec3 aVertexPosition;',
'attribute vec4 aVertexColor;',
'',
'uniform mat4 uMVMatrix;',
'uniform mat4 uPMatrix;',
'',
'varying vec4 vColor;',
'',
'void main(void) {',
' gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);',
' vColor = aVertexColor;',
'}'
].join('\n')
};
function getShader(name) {
if (!shaderScripts[name]) return null;
var shader;
if(shaderScripts[name].type == 'x-shader/x-fragment') {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScripts[name].type == 'x-shader/x-vertex') {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, shaderScripts[name].source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}