-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.js
72 lines (59 loc) · 2.61 KB
/
shader.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
/* eslint-disable require-jsdoc*/
(function libraryWrapper(window) {
function defineLibrary() {
const ShaderLib = {};
ShaderLib.makeProgram = function makeProgram(gl, vertexshadernode, fragmentshadernode) {
const vb = document.getElementById(vertexshadernode) // eslint-disable-line no-undef
.firstChild.nodeValue;
const fb = document.getElementById(fragmentshadernode) // eslint-disable-line no-undef
.firstChild.nodeValue;
const vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, vb);
gl.compileShader(vs);
const fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, fb);
gl.compileShader(fs);
const program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) {
console.log(gl.getShaderInfoLog(vs)); // eslint-disable-line no-console
}
if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) {
console.log(gl.getShaderInfoLog(fs)); // eslint-disable-line no-console
}
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.log(gl.getProgramInfoLog(program)); // eslint-disable-line no-console
}
return program;
};
ShaderLib.makeProgram2 = function makeProgram(gl, vertexshader, fragmentshader) {
const vb = vertexshader;
const fb = fragmentshader;
const vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, vb);
gl.compileShader(vs);
const fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, fb);
gl.compileShader(fs);
const program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) {
console.log(gl.getShaderInfoLog(vs)); // eslint-disable-line no-console
}
if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) {
console.log(gl.getShaderInfoLog(fs)); // eslint-disable-line no-console
}
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.log(gl.getProgramInfoLog(program)); // eslint-disable-line no-console
}
return program;
};
return ShaderLib;
}
if (typeof (ShaderLib) === 'undefined') window.ShaderLib = defineLibrary(); // eslint-disable-line no-param-reassign, no-undef
else console.log('Library already defined.'); // eslint-disable-line no-console
}(window)); // eslint-disable-line no-undef