-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebGL.js
306 lines (241 loc) · 8.27 KB
/
WebGL.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
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
var VS =
"attribute vec2 av;" +
"uniform sampler2D ut;" +
"attribute vec2 a_texCoord;" +
"varying vec2 v_texCoord;" +
"void main ()" +
"{" +
"gl_Position = vec4(av*2.-1.,0,1);" +
"v_texCoord = a_texCoord;" +
"}"
var FS1 =
"precision mediump float;" +
"uniform vec2 resolution;" +
"uniform sampler2D ut;" +
"uniform sampler2D u_image;" +
"varying vec2 v_texCoord;" +
"varying vec4 Channel0;" +
"uniform float time;" +
"uniform vec2 mouse;" +
"void main ()" +
"{" +
"gl_FragColor = texture2D (ut, gl_FragCoord.xy/resolution);" +
"}"
var CHAN0 =
"precision mediump float;" +
"uniform vec2 resolution;" +
"uniform sampler2D u_image;" +
"varying vec2 v_texCoord;" +
"varying vec4 Channel0;" +
"void main ()" +
"{" +
"Channel0 = texture2D(u_image, v_texCoord);" +
"gl_FragColor = texture2D(u_image, v_texCoord);" +
"}"
//"gl_FragColor = texture2D (ut, gl_FragCoord.xy/resolution) ;" +
function Framebuffer (gl, n, type, w, h=w)
{
this.gl = gl;
this.type = type;
this.n = n;
this.w = w;
this.h = h;
this.width = w;
this.height = h;
this.fbo = gl.createFramebuffer();
this.renderbuffer = gl.createRenderbuffer();
this.texture = gl.createTexture();
gl.activeTexture(gl['TEXTURE' + this.n]);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.fbo);
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, this.type, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderbuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, w, h);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture, 0);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.renderbuffer);
this.write = function (typedArray) {
var gl = this.gl;
gl.activeTexture(gl["TEXTURE" + this.n]);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, this.type, typedArray);}
this.source = function (element) {
var gl = this.gl;
gl.activeTexture(gl["TEXTURE" + this.n]);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, element);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);}
this.route = function () {
gl.activeTexture(gl["TEXTURE"+this.n]);
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.fbo);
gl.viewport(0,0,this.w,this.h);}}
function createProgram (gl, vstr, fstr)
{
if (vstr.length < 50) vstr = document.getElementById(vstr).textContent;
if (fstr.length < 50) fstr = document.getElementById(fstr).textContent;
var program = gl.createProgram(),
vshader = createShader(gl, vstr, gl.VERTEX_SHADER),
fshader = createShader(gl, fstr, gl.FRAGMENT_SHADER);
gl.attachShader(program, vshader);
gl.attachShader(program, fshader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw gl.getProgramInfoLog(program);
}
return program;
}
function createShader (gl, str, type)
{
var shader = gl.createShader(type);
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
{
alert(gl.getShaderInfoLog(shader)+str);
throw gl.getShaderInfoLog(shader);
}
return shader;
}
function initAttrib (gl, program)
{
gl.useProgram(program);
var attrib = gl.getAttribLocation(program, 'av');
var texcoordLocation = gl.getAttribLocation(program, "a_texCoord");
// position
gl.enableVertexAttribArray(attrib);
gl.vertexAttribPointer(attrib, 2, gl.FLOAT, gl.FALSE, 0, 0);
// texture
gl.enableVertexAttribArray(texcoordLocation);
gl.vertexAttribPointer(texcoordLocation, 2, gl.FLOAT, gl.FALSE, 0, 0);
return initAttrib;
}
function setUni (gl, program, name, args, int = false)
{
gl.useProgram(program);
if (!program[name]) program[name] = gl.getUniformLocation (program, name);
if (int || typeof(args) == "boolean")
{
gl.uniform1i(program[name], args);
}
else if (args.constructor == Array)
{
console.log(args);
gl["uniform" + args.length + "fv"](program[name], args);
}
else if (typeof(args) == "number")
{
gl.uniform1f(program[name], args);
}
else if (args.constructor == Framebuffer)
{
gl.uniform1i(program[name], args.n);
}
else
{
//gl.uniform1i(program[name], 7);
console.log("its nothing");
}
return setUni;
}
function draw (gl, program, dest=false, type=gl.TRIANGLES, a=0, b=6, clear = true)
{
gl.useProgram (program);
if (dest) dest.route(); else
{
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, canvas.width, canvas.height);
}
if (clear) gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawArrays(type,a,b);
}
function initVerts (gl, w=0, h=w)
{
var arr = new Float32Array(w*h*2+12),sqr = [0,0,1,0,0,1, 1,1,1,0,0,1], i = 0;
for (j = 0; j < 12; j++) arr[i++] = sqr[j];
for (var y = 0; y < h; y ++)
{
for (var x = 0; x < w; x ++)
{
arr[i++] = x/w;
arr[i++] = y/h;
}
}
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW);
return arr;
}
var recmpl = false;
var gfb1 = true;
var gfb2 = false;
function fb1()
{
gfb1 = true;
gfb2 = false;
}
function fb2()
{
gfb1 = false;
gfb2 = true;
}
function Recompile(){
recmpl = true;
}
window.onload = function ()
{
var image = new Image();
image.src = "tex0.jpg";
var shdrsrc0 = document.getElementById("fsSource0").value;
var shdrsrc1 = document.getElementById("fsSource1").value;
var canvas = document.getElementById("canvas"),
gl = canvas.getContext("webgl"),
verts = initVerts (gl),
program = createProgram (gl, VS, shdrsrc0),
program1 = createProgram (gl, VS, FS1),
program2 = createProgram (gl, VS, shdrsrc1),
//program3 = createProgram (gl, VS, CHAN0),
ext = gl.getExtension('OES_texture_float'),
lin = gl.getExtension('OES_texture_float_linear'),
fbo = new Framebuffer (gl, 0, gl.FLOAT, canvas.width, canvas.height),
fbo2 = new Framebuffer (gl, 0, gl.FLOAT, canvas.width, canvas.height);
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
image.onload = function(){
//gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
}
initAttrib (gl, program);
setUni (gl, program, "resolution", [canvas.width, canvas.height]);
setUni (gl, program1, "resolution", [canvas.width, canvas.height]);
setUni (gl, program2, "resolution", [canvas.width, canvas.height]);
var t = 0;
function animate ()
{
requestAnimationFrame (animate);
setUni (gl, program, "time", t+=0.015);
if(gfb1)
draw (gl, program, fbo);
//setUni (gl, program1, "ut", fbo);
draw (gl, program1);
setUni (gl, program2, "time", t+=0.015);
if(gfb2)
draw (gl, program2, fbo2);
if(recmpl)
{
var newscrc0 = document.getElementById("fsSource0").value;
var newscrc1 = document.getElementById("fsSource1").value;
program = createProgram (gl, VS, newscrc0);
setUni (gl, program, "resolution", [canvas.width, canvas.height]);
program2 = createProgram (gl, VS, newscrc1);
setUni (gl, program2, "resolution", [canvas.width, canvas.height]);
recmpl = false;
}
}
animate ();
}