-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
115 lines (95 loc) · 2.59 KB
/
lib.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
class WebGL {
gl;
program;
vs;
fs;
buffer;
textures = [];
constructor(el) {
if (el instanceof HTMLCanvasElement) {
this.gl = el.getContext("webgl");
this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, true);
return this;
} else {
throw new Error("please pass canvas element");
}
}
init(vsSource, fsSource) {
const vs = (this.vs = this.gl.createShader(this.gl.VERTEX_SHADER));
this.gl.shaderSource(vs, vsSource);
this.gl.compileShader(vs);
const fs = (this.fs = this.gl.createShader(this.gl.FRAGMENT_SHADER));
this.gl.shaderSource(fs, fsSource);
this.gl.compileShader(fs);
const program = (this.program = this.gl.createProgram());
this.gl.attachShader(program, vs);
this.gl.attachShader(program, fs);
this.gl.linkProgram(program);
this.gl.useProgram(program);
return this;
}
loadBuffer(typedArray) {
const buffer = (this.buffer = this.gl.createBuffer());
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);
this.gl.bufferData(this.gl.ARRAY_BUFFER, typedArray, this.gl.STATIC_DRAW);
return this;
}
loadTexture(image) {
const texture = this.gl.createTexture();
this.gl.activeTexture(this.gl['TEXTURE' + this.textures.length]);
this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
this.gl.texImage2D(
this.gl.TEXTURE_2D,
0,
this.gl.RGBA,
this.gl.RGBA,
this.gl.UNSIGNED_BYTE,
image
);
this.gl.texParameteri(
this.gl.TEXTURE_2D,
this.gl.TEXTURE_MIN_FILTER,
this.gl.LINEAR
);
this.gl.texParameteri(
this.gl.TEXTURE_2D,
this.gl.TEXTURE_WRAP_S,
this.gl.CLAMP_TO_EDGE
);
this.gl.texParameteri(
this.gl.TEXTURE_2D,
this.gl.TEXTURE_WRAP_T,
this.gl.CLAMP_TO_EDGE
);
this.textures.push(texture);
return this;
}
setAttrib(name, size, type, normalized, stride, offset) {
const location1 = this.gl.getAttribLocation(this.program, name);
this.gl.vertexAttribPointer(
location1,
size,
type,
normalized,
stride,
offset
);
this.gl.enableVertexAttribArray(location1);
return this;
}
setUniform(name, type, value) {
const location = this.gl.getUniformLocation(this.program, name);
this.gl[type](location, value)
return this;
}
draw(type, count) {
this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height);
this.gl.clearColor(0, 0, 0, 0);
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
this.gl.drawArrays(type, 0, count);
}
then(callback) {
callback.call(this)
return this;
}
}