-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshaderboy.min.js
22 lines (22 loc) · 6.15 KB
/
shaderboy.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Shaderboy
* =========
*
* @author Christian Engel <[email protected]>
* @url https://github.com/Paratron/shaderboy
* @license MIT
* @version 1.1
*/
(function(){function v(a,b){function c(){g--;0>=g&&b(w,k)}var d=a.getAttribute("data-shader"),e,g=0,w={fragment:d,vertex:null},k=[];g++;if(l[d])n(d,l[scriptIdentifier].innerHTML);else if("#"===d.substr(0,1)){e=document.getElementById(d.substr(1));if(null===e){console.error("Unable to locate the script tag with id "+d);f.onerror();return}l[d]=e;n(d,e.innerHTML)}else e=new XMLHttpRequest,e.onload=function(a){l[d].innerHTML=a.target.responseText;n(d,a.target.responseText);c()},g++,e.open("GET",d),e.send(),
l[d]=document.createElement("script");e=[];var h,m;e.push(a.getAttribute("src"));k.push(null);for(h=0;5>=h&&a.hasAttribute("data-texture"+h);h++)e.push(a.getAttribute("data-texture"+h)),k.push(null);for(h=0;h<e.length;h++)if(m=e[h],l[m])k[h]=l[m];else if("#"===m.substr(0,1)){k[h]=document.getElementById(m.substr(1));if(null===k[h]){console.error("Unable to locate image tag with id "+m);f.onerror();return}l[m]=k[h]}else k[h]=l[m]=new Image,k[h].onload=function(){c()},g++,k[h].src=m;c()}function n(a,
b){f.fragmentShaders[a]&&console.log('Shader source with the id "'+a+'" already exists and has been replaced.');f.fragmentShaders[a]=b}function p(a,b,c){c=a.createShader(c);a.shaderSource(c,b);a.compileShader(c);return a.getShaderParameter(c,a.COMPILE_STATUS)?c:(console.log("Shader compilation failed"),a=a.getShaderInfoLog(c),console.error(a),f.onerror(),null)}function x(a,b,c){var d=a.createProgram(),e;b||(e=p(a,f.defaultVertexShader,a.VERTEX_SHADER));b=p(a,f.fragmentShaders[c],a.FRAGMENT_SHADER);
a.attachShader(d,e);a.attachShader(d,b);a.deleteShader(e);a.deleteShader(b);a.linkProgram(d);if(a.getProgramParameter(d,a.LINK_STATUS))return a.useProgram(d),d;console.error(a.getProgramInfoLog(d))}function y(a,b,c,d){var e=Math.random().toString().split(".")[1];a.sbId=e;(b=a.getContext("experimental-webgl",{antialias:!0}))||(b=a.getContext("webgl"));b.viewportWidth=a.width;b.viewportHeight=a.height;if(c=x(b,null,c)){var f=b.getAttribLocation(c,"position");g[e]={canvas:a,gl:b,program:c,renderStart:Date.now(),
textures:[],mouseX:-1,mouseY:-1,mouseL:0,mouseR:0,uniforms:{viewport:b.getUniformLocation(c,"viewport"),mouseCoords:b.getUniformLocation(c,"mouseCoords"),mouseDownLeft:b.getUniformLocation(c,"mouseDownLeft"),mouseDownRight:b.getUniformLocation(c,"mouseDownRight"),time:b.getUniformLocation(c,"time"),image:b.getUniformLocation(c,"image"),texture0:b.getUniformLocation(c,"texture0"),texture1:b.getUniformLocation(c,"texture1"),texture2:b.getUniformLocation(c,"texture2"),texture3:b.getUniformLocation(c,
"texture3"),texture4:b.getUniformLocation(c,"texture4"),texture5:b.getUniformLocation(c,"texture5")},attributes:{position:f,vertexes:z(b)}};a.addEventListener("mousedown",function(a){g[e].mouseL=0===a.button?1:0;g[e].mouseR=2===a.button?1:0});a.addEventListener("mouseup",function(a){0===a.button&&(g[e].mouseL=0);2===a.button&&(g[e].mouseR=0)});a.addEventListener("mousemove",function(a){var b=g[e],c=a.clientX,d=a.target,f=0;do isNaN(d.offsetLeft)||(f+=d.offsetLeft);while(d=d.offsetParent);b.mouseX=
c-f+document.body.scrollLeft;b=g[e];c=a.clientY;a=a.target;d=0;do isNaN(a.offsetTop)||(d+=a.offsetTop);while(a=a.offsetParent);b.mouseY=c-d+document.body.scrollTop});a.addEventListener("mouseout",function(a){g[e].mouseX=g[e].mouseY=-1;g[e].mouseL=g[e].mouseR=0});b.enableVertexAttribArray(f);for(a=0;a<d.length;a++)g[e].textures.push(!0),A(b,d[a],a);b.clearColor(0,0,0,0);b.disable(b.DEPTH_TEST)}}function z(a){var b=a.createBuffer();a.bindBuffer(a.ARRAY_BUFFER,b);a.bufferData(a.ARRAY_BUFFER,new Float32Array(f.defaultVertex),
a.STATIC_DRAW);b.itemSize=3;b.numItems=4;return b}function A(a,b,c){var d=a.createTexture();a.activeTexture(a.TEXTURE0+c);a.bindTexture(a.TEXTURE_2D,d);a.pixelStorei(a.UNPACK_FLIP_Y_WEBGL,!0);a.texImage2D(a.TEXTURE_2D,0,a.RGBA,a.RGBA,a.UNSIGNED_BYTE,b);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)}function q(){window.requestAnimationFrame(q);for(var a in g){var b=
g[a],c=b.gl,d=b.canvas,e=b.attributes.vertexes,f=b.attributes.position;c.viewport(0,0,d.width,d.height);c.clear(c.COLOR_BUFFER_BIT);c.uniform2i(b.uniforms.viewport,d.width,d.height);-1!==b.mouseX?c.uniform2f(b.uniforms.mouseCoords,b.mouseX/d.width,1-b.mouseY/d.height):c.uniform2f(b.uniforms.mouseCoords,-1,-1);c.uniform1i(b.uniforms.mouseDownLeft,b.mouseL);c.uniform1i(b.uniforms.mouseDownRight,b.mouseR);c.uniform1i(b.uniforms.image,0);c.uniform1f(b.uniforms.time,(Date.now()-b.renderStart)/1E3);for(d=
0;d<b.textures.length;d++)b.textures[d]&&c.uniform1i(b.uniforms["texture"+d],d+1);c.bindBuffer(c.ARRAY_BUFFER,e);c.vertexAttribPointer(f,e.itemSize,c.FLOAT,!1,0,0);c.drawArrays(c.TRIANGLE_STRIP,0,e.numItems)}}function r(a,b,c){var d=document.createElement("canvas");f.canvases.push(d);d.className=a.className+" shaderboy";t(d,a);v(a,function(e,f){b?(a.className+=" shaderboy-replaced",a.parentNode.insertBefore(d,a)):a.parentNode.replaceChild(d,a);y(d,a,e.fragment,f);setTimeout(function(){q(d)},1);c&&
c(d)})}function t(a,b){var c=b.offsetHeight;a.width=b.offsetWidth;a.height=c}var f;window.shaderboy=f={};f.canvases=[];f.fragmentShaders={};f.defaultVertexShader="attribute vec2 position;varying vec2 pixelCoords;\nvoid main() {\ngl_Position = vec4(vec2(position * vec2(2,2)) - vec2(1,1), 0., 1.);\npixelCoords=position;}";f.defaultVertex=[1,1,0,-1,1,0,1,-1,0,-1,-1,0];f.onerror=function(){};var l={},g={},u=null;f.webglSupported=!1;(function(){var a=document.createElement("canvas"),b;try{if((b=a.getContext("experimental-webgl"))||
(b=a.getContext("webgl")),!b){console.log("Webgl is not supported on this platform.");return}}catch(c){console.log("Webgl is not supported on this platform.");return}f.webglSupported=!0})();f.autoReplace=function(){if(f.webglSupported)for(var a=document.querySelectorAll("[data-shader]"),b=0;b<a.length;b++)r(a[b])};f.replace=function(a,b,c){f.webglSupported&&r(a,b,c)};window.addEventListener("resize",function(){clearTimeout(u);u=setTimeout(function(){for(var a=f.canvases,b=0;b<a.length;b++)t(a[b],
a[b])},100)})})();