-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.js
79 lines (64 loc) · 2.29 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
73
74
75
76
77
78
79
export default class Shader {
static createShader(gl, type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
var info = gl.getShaderInfoLog(shader);
console.log('Could not compile WebGL program:' + info);
}
return shader;
}
static createProgram(gl, vertexShader, fragmentShader) {
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
var info = gl.getProgramInfoLog(program);
console.log('Could not compile WebGL program:' + info);
}
return program;
}
static isArrayBuffer(value) {
return value && value.buffer instanceof ArrayBuffer && value.byteLength !== undefined;
}
static createBuffer(gl, type, data) {
if (data.length == 0)
return null;
if (!Shader.isArrayBuffer(data)) {
console.warn('Data is not an instance of ArrayBuffer');
return null;
}
var buffer = gl.createBuffer();
gl.bindBuffer(type, buffer);
gl.bufferData(type, data, gl.STATIC_DRAW);
return buffer;
}
static createVAO(gl, posAttribLoc, posBuffer, colorAttribLoc = null, colorBuffer = null, normAttribLoc = null, normBuffer = null) {
var vao = gl.createVertexArray();
gl.bindVertexArray(vao);
if (posAttribLoc != null && posAttribLoc != undefined) {
gl.enableVertexAttribArray(posAttribLoc);
var size = 4;
var type = gl.FLOAT;
gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer);
gl.vertexAttribPointer(posAttribLoc, size, type, false, 0, 0);
}
if (colorAttribLoc != null && colorAttribLoc != undefined) {
gl.enableVertexAttribArray(colorAttribLoc);
size = 4;
type = gl.FLOAT;
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.vertexAttribPointer(colorAttribLoc, size, type, false, 0, 0);
}
if (normAttribLoc != null && normAttribLoc != undefined) {
gl.enableVertexAttribArray(normAttribLoc);
size = 4;
type = gl.FLOAT;
gl.bindBuffer(gl.ARRAY_BUFFER, normBuffer);
gl.vertexAttribPointer(normAttribLoc, size, type, false, 0, 0);
}
return vao;
}
}