-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathglsl.min.js
More file actions
34 lines (31 loc) · 9.4 KB
/
glsl.min.js
File metadata and controls
34 lines (31 loc) · 9.4 KB
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
/*!
Copyright 2013 @greweb
http://github.com/gre/glsl.js
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
(function(m){"object"===typeof exports?module.exports=m():"function"===typeof define&&define.amd?define(m):window.Glsl=m()})(function(){function m(a){console.warn?console.warn(p+a):console.log&&console.log(p+("WARN "+a))}function n(a){console.error?console.error(p+a):console.log&&console.log(p+("ERR "+a))}function r(a,b){if(a.getContext)for(var c=["webgl","experimental-webgl"],e=0;e<c.length;++e)try{var d=a.getContext(c[e],b);if(d)return d}catch(h){}}var q=["fragment","canvas","variables"],s={bool:"1i",
"int":"1i","float":"1f",vec2:"2f",ivec2:"2i",bvec2:"2b",vec3:"3f",ivec3:"3i",bvec3:"3b",vec4:"4f",ivec4:"4i",bvec4:"4b",mat2:"Matrix2fv",mat3:"Matrix3fv",mat4:"Matrix4fv"},t=/uniform\s+([a-z]+\s+)?([A-Za-z0-9]+)\s+([a-zA-Z_0-9]+)\s*(\[\s*(.+)\s*\])?/,u=/struct\s+\w+\s*{[^}]+}\s*;/g,v=/struct\s+(\w+)\s*{([^}]+)}\s*;/,w=/[^;]+;/g,x=/\s*([a-z]+\s+)?([A-Za-z0-9]+)\s+([a-zA-Z_0-9]+)\s*(\[\s*(.+)\s*\])?\s*;/,y=/#define\s+([a-zA-Z_0-9]+)\s+(.*)/,p="Glsl: ",z=function(a){return function(){return++a}}(0),
l=function(a){if(!(this instanceof arguments.callee))return new arguments.callee(a);if(!a)throw Error("Glsl: {"+q+"} are required.");for(var b=0;b<q.length;b++)if(!(q[b]in a))throw Error("Glsl: '"+q[b]+"' is required.");this.canvas=a.canvas;this.variables=a.variables;this.init=a.init||function(a){};this.update=a.update||function(a){};this.ready=a.ready||function(a){};this.prog=new l.Program("attribute vec2 position; void main() { gl_Position = vec4(2.0*position-1.0, 0.0, 1.0);}",a.fragment);this.defines=
this.prog.defines;if(!this.prog.uniformTypes.resolution)throw Error("Glsl: You must use a 'vec2 resolution' in your shader.");for(var c in this.prog.uniformTypes)c in this.variables||"resolution"==c||m("variable '"+c+"' not initialized");this.initGL(a.contextArgs);this.load();this.syncAll();this.init();this.update(0,0);this.render();this.ready()};l.supported=function(){return!!r(document.createElement("canvas"))};l.Program=function(a,b){this.gl=null;this.vertex=a;this.fragment=b;var c=a+"\n"+b;this.parseDefines(c);
this.parseStructs(c);this.parseUniforms(c)};l.Program.prototype={defines:null,syncVariable:function(a,b){this.recSyncVariable(a,b,this.uniformTypes[a],a)},parseDefines:function(a){this.defines={};a=a.split("\n");for(var b=0;b<a.length;++b){var c=a[b].match(y);c&&3==c.length&&(this.defines[c[1]]=c[2])}},parseStructs:function(a){this.structTypes={};if(a=a.match(u))for(var b=0;b<a.length;++b){for(var c=a[b].match(v),e=c[1],c=c[2].match(w),d={},h=0;h<c.length;++h){var f=c[h].match(x),k=f[2],g=f[3],f=
f[4],k=s[k]||k;f&&(f in this.defines&&(f=this.defines[f]),k=[k,parseInt(f,10)]);d[g]=k}this.structTypes[e]=d}},parseUniforms:function(a){this.uniformTypes={};a=a.split("\n");for(var b=0;b<a.length;++b){var c=a[b].match(t);if(c){var e=c[2],d=c[3],c=c[5],e=s[e]||e;c&&(c in this.defines&&(c=this.defines[c]),e=[e,parseInt(c,10)]);this.uniformTypes[d]=e}}},recSyncVariable:function(a,b,c,e){var d=this.gl;if(c){a=c instanceof Array;var h;a&&(h=c[1],c=c[0]);var f=this.locations[e];if(c in this.structTypes)if(d=
this.structTypes[c],a)for(a=0;a<h&&a<b.length;++a){var f=e+"["+a+"].",k=b[a],g;for(g in d){if(!(g in k)){m("variable '"+e+"["+a+"]' ("+c+") has no field '"+g+"'");break}var l=d[g];this.recSyncVariable(g,k[g],l,f+g)}}else for(g in f=e+".",d){if(!(g in b)){m("variable '"+e+"' ("+c+") has no field '"+g+"'");break}l=d[g];this.recSyncVariable(g,b[g],l,f+g)}else switch(h=c,a&&(h+="v"),g="uniform"+h,h){case "2f":case "2i":"length"in b?d[g].call(d,f,b[0],b[1]):"x"in b&&"y"in b?d[g].call(d,f,b.x,b.y):"s"in
b&&"t"in b?d[g].call(d,f,b.s,b.t):n("variable '"+e+"' is not valid for binding to vec2(). Use an Array, a {x,y} or a {s,t}.");break;case "3f":case "3i":"length"in b?d[g].call(d,f,b[0],b[1],b[2]):"x"in b&&"y"in b&&"z"in b?d[g].call(d,f,b.x,b.y,b.z):"s"in b&&"t"in b&&"p"in b?d[g].call(d,f,b.s,b.t,b.p):"r"in b&&"g"in b&&"b"in b?d[g].call(d,f,b.r,b.g,b.b):n("variable '"+e+"' is not valid for binding to vec3(). Use an Array, a {x,y,z}, a {r,g,b} or a {s,t,p}.");break;case "4f":case "4i":"length"in b?d[g].call(d,
f,b[0],b[1],b[2],b[3]):"x"in b&&"y"in b&&"z"in b&&"w"in b?d[g].call(d,f,b.x,b.y,b.z,b.w):"s"in b&&"t"in b&&"p"in b&&"q"in b?d[g].call(d,f,b.s,b.t,b.p,b.q):"r"in b&&"g"in b&&"b"in b&&"a"in b?d[g].call(d,f,b.r,b.g,b.b,b.a):n("variable '"+e+"' is not valid for binding to vec4(). Use an Array, a {x,y,z,w}, a {r,g,b,a} or a {s,t,p,q}.");break;case "sampler2D":this.syncTexture(d,f,b,e);break;default:g in d?d[g].call(d,f,b):n("type '"+c+"' not found.")}}else m("variable '"+a+"' not found in your GLSL.")},
syncTexture:function(a,b,c,e){var d=this.textureUnitForNames[e];d||(d=this.allocTexture(e));a.activeTexture(a.TEXTURE0+d);(e=this.textureForTextureUnit[d])?a.texImage2D(a.TEXTURE_2D,0,a.RGBA,a.RGBA,a.UNSIGNED_BYTE,c):(e=a.createTexture(),a.bindTexture(a.TEXTURE_2D,e),a.pixelStorei(a.UNPACK_FLIP_Y_WEBGL,!0),a.texImage2D(a.TEXTURE_2D,0,a.RGBA,a.RGBA,a.UNSIGNED_BYTE,c),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MAG_FILTER,a.LINEAR),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MIN_FILTER,a.LINEAR),a.texParameteri(a.TEXTURE_2D,
a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE),a.uniform1i(b,d),this.textureForTextureUnit[d]=e)},allocTexture:function(a){var b=this.textureUnitCounter;this.textureUnitForNames[a]=b;this.textureUnitCounter++;return b},initUniformLocations:function(){this.locations={};for(var a in this.uniformTypes)this.recBindLocations(a,this.uniformTypes[a],a)},recBindLocations:function(a,b,c){a=b instanceof Array;var e;a&&(e=b[1],b=b[0]);if(b in this.structTypes)if(b=
this.structTypes[b],a)for(a=0;a<e;++a){var d=c+"["+a+"].",h;for(h in b)this.recBindLocations(h,b[h],d+h)}else for(h in d=c+".",b)this.recBindLocations(h,b[h],d+h);else this.locations[c]=this.gl.getUniformLocation(this.program,c)},load:function(){var a=this.gl;this.program&&(a.deleteProgram(this.program),this.program=null);this.program=this.loadProgram([this.loadShader(this.vertex,a.VERTEX_SHADER),this.loadShader(this.fragment,a.FRAGMENT_SHADER)]);a.useProgram(this.program);this.initUniformLocations();
this.textureUnitForNames={};this.textureForTextureUnit={};this.textureUnitCounter=0},loadProgram:function(a){var b=this.gl,c=b.createProgram();a.forEach(function(a){b.attachShader(c,a)});b.linkProgram(c);if(!b.getProgramParameter(c,b.LINK_STATUS))throw b.deleteProgram(c),Error(c+" "+b.getProgramInfoLog(c));return c},loadShader:function(a,b){var c=this.gl,e=c.createShader(b);c.shaderSource(e,a);c.compileShader(e);if(!c.getShaderParameter(e,c.COMPILE_STATUS)){var d=c.getShaderInfoLog(e),h=d.split(":"),
f=parseInt(h[1],10),h=parseInt(h[2],10),k="";if(!isNaN(f)){for(var k="",g=0;g<f;++g)k+=" ";k="\n"+k+"^"}n(d+"\n"+a.split("\n")[h-1]+k);c.deleteShader(e);throw Error(e+" "+d);}return e}};l.prototype={defines:null,start:function(){var a=this;a._stop=!1;if(a._running)return a;var b=a._running=z(),c=Date.now(),e=a._stopTime||0;requestAnimationFrame(function h(){var f=Date.now()-c+(a._stopTime||0);if(a._stop||a._running!==b)a._running=0,a._stopTime=f;else{requestAnimationFrame(h,a.canvas);var k=f-e;e=
f;a.update(f,k);a.render()}},a.canvas);return a},stop:function(){this._stop=!0;return this},sync:function(){for(var a=0;a<arguments.length;++a)this.syncVariable(arguments[a]);return this},syncAll:function(){for(var a in this.variables)this.syncVariable(a);return this},set:function(a,b){this.variables[a]=b;this.sync(a);return this},setSize:function(a,b){this.canvas.width=a;this.canvas.height=b;this.syncResolution()},initGL:function(a){var b=this;this.canvas.addEventListener("webglcontextlost",function(a){a.preventDefault()},
!1);this.canvas.addEventListener("webglcontextrestored",function(){b.running&&b.syncAll();b.load()},!1);this.gl=this.prog.gl=this.getWebGLContext(this.canvas,a)},render:function(){this.gl.drawArrays(this.gl.TRIANGLES,0,6)},getWebGLContext:function(a,b){return r(a,b)},syncVariable:function(a){return this.prog.syncVariable(a,this.variables[a])},load:function(){var a=this.gl;this.prog.load();var b=a.createBuffer();a.bindBuffer(a.ARRAY_BUFFER,b);b=a.getAttribLocation(this.prog.program,"position");a.enableVertexAttribArray(b);
a.vertexAttribPointer(b,2,a.FLOAT,!1,0,0);this.syncResolution()},syncResolution:function(){var a=this.gl,b=this.canvas.width,c=this.canvas.height;a.viewport(0,0,b,c);a.uniform2f(this.prog.locations.resolution,b,c);a.bufferData(a.ARRAY_BUFFER,new Float32Array([0,0,b,0,0,c,0,c,b,0,b,c]),a.STATIC_DRAW)}};(function(){for(var a=0,b=["ms","moz","webkit","o"],c=0;c<b.length&&!window.requestAnimationFrame;++c)window.requestAnimationFrame=window[b[c]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[b[c]+
"CancelAnimationFrame"]||window[b[c]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(b,c){var h=(new Date).getTime(),f=Math.max(0,16-(h-a)),k=window.setTimeout(function(){b(h+f)},f);a=h+f;return k});window.cancelAnimationFrame||(window.cancelAnimationFrame=function(a){clearTimeout(a)})})();return l});