forked from mrdoob/texgen.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexgen.min.js
14 lines (14 loc) · 4.78 KB
/
texgen.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// texgen.js - http://github.com/mrdoob/texgen.js
'use strict';var TG={Texture:function(a,b){this.width=a;this.height=b;this.array=new Float32Array(a*b*4);this.arrayCopy=new Float32Array(a*b*4);return this}};
TG.Texture.prototype={constructor:TG.Texture,pass:function(a,b){void 0===b&&(b="");var c=a.getColor(),d=a.getSource();this.arrayCopy.set(this.array);c=["var x = 0, y = 0;\nfor ( var i = 0, il = dst.length; i < il; i += 4 ) {","\t"+d,0!==c[0]?"\tdst[ i + 0 ] "+b+"= color * "+c[0]+";":"",0!==c[1]?"\tdst[ i + 1 ] "+b+"= color * "+c[1]+";":"",0!==c[2]?"\tdst[ i + 2 ] "+b+"= color * "+c[2]+";":"","\tif ( ++x === width ) { x = 0; y ++; }\n}"].join("\n");(new Function("dst, src, width, height",c))(this.array,
this.arrayCopy,this.width,this.height);return this},add:function(a){return this.pass(a,"+")},sub:function(a){return this.pass(a,"-")},mul:function(a){return this.pass(a,"*")},div:function(a){return this.pass(a,"/")},toImageData:function(a){var b=this.array;a=a.createImageData(this.width,this.height);for(var c=a.data,d=0,e=b.length;d<e;d+=4)c[d]=255*b[d],c[d+1]=255*b[d+1],c[d+2]=255*b[d+2],c[d+3]=255;return a},toCanvas:function(){var a=document.createElement("canvas");a.width=this.width;a.height=this.height;
var b=a.getContext("2d"),c=this.toImageData(b);b.putImageData(c,0,0);return a}};TG.Program=function(a){var b=[1,1,1];a.color=function(a,d,e){b=[a,d,e];return this};a.getColor=function(){return b};return a};TG.Number=function(){return new TG.Program({getSource:function(){return"var color = 1;"}})};
TG.SinX=function(){var a=1,b=0;return new TG.Program({frequency:function(b){a=b*Math.PI;return this},offset:function(a){b=a;return this},getSource:function(){return"var color = Math.sin( ( x + "+b+" ) * "+a+" );"}})};TG.SinY=function(){var a=1,b=0;return new TG.Program({frequency:function(b){a=b*Math.PI;return this},offset:function(a){b=a;return this},getSource:function(){return"var color = Math.sin( ( y + "+b+" ) * "+a+" );"}})};TG.OR=function(){return new TG.Program({getSource:function(){return"var color = ( x | y ) / width;"}})};
TG.XOR=function(){return new TG.Program({getSource:function(){return"var color = ( x ^ y ) / width;"}})};TG.Noise=function(){return new TG.Program({getSource:function(){return"var color = Math.random();"}})};
TG.CheckerBoard=function(){var a=[32,32],b=[0,0],c=0;return new TG.Program({size:function(b,c){a=[b,c];return this},offset:function(a,c){b=[a,c];return this},rowShift:function(a){c=a;return this},getSource:function(){return"var color = ( ( ( y + "+b[1]+" ) / "+a[1]+" ) & 1 ) ^ ( ( ( x + "+b[0]+" + parseInt( y / "+a[1]+" ) * "+c+" ) / "+a[0]+" ) & 1 ) ? 0 : 1"}})};
TG.Rect=function(){var a=[0,0],b=[32,32];return new TG.Program({position:function(b,d){a=[b,d];return this},size:function(a,d){b=[a,d];return this},getSource:function(){return"var color = ( x >= "+a[0]+" && x <= "+(a[0]+b[0])+" && y <= "+(a[1]+b[1])+" && y >= "+a[1]+" ) ? 1 : 0;"}})};
TG.Circle=function(){var a=[0,0],b=50,c=1;return new TG.Program({delta:function(a){c=a;return this},position:function(b,c){a=[b,c];return this},radius:function(a){b=a;return this},getSource:function(){return["var dist = TG.Utils.distance( x, y, "+a[0]+","+a[1]+");","var color = TG.Utils.smoothStep( "+b+" - "+c+", "+b+", dist );"].join("\n")}})};
TG.SineDistort=function(){var a=[4,4],b=[0,0],c=[16,16];return new TG.Program({sines:function(b,c){a=[b,c];return this},offset:function(a,c){b=[a,c];return this},amplitude:function(a,b){c=[a,b];return this},getSource:function(){return["var sx = Math.sin("+a[0]/100+" * y + "+b[0]+") * "+c[0]+" + x;","var sy = Math.sin("+a[1]/100+" * x + "+b[1]+") * "+c[1]+" + y;","var color = TG.Utils.getPixelBilinear(src, sx, sy, 0, width);"].join("\n")}})};
TG.Twirl=function(){var a=0,b=120,c=[128,128];return new TG.Program({strength:function(b){a=b/100;return this},radius:function(a){b=a;return this},position:function(a,b){c=[a,b];return this},getSource:function(){return["var dist = TG.Utils.distance( x, y, "+c[0]+","+c[1]+");","dist = dist > "+b+" ? 0 : ("+b+" - dist) * ("+b+" - dist) / "+b+";","var angle = 2.0 * Math.PI * (dist / ("+b+" / "+a+"));","var xpos = (((x - "+c[0]+") * Math.cos(angle)) - ((y - "+c[0]+") * Math.sin(angle)) + "+c[0]+" + 0.5);",
"var ypos = (((y - "+c[1]+") * Math.cos(angle)) + ((x - "+c[1]+") * Math.sin(angle)) + "+c[1]+" + 0.5);","var color = TG.Utils.getPixelBilinear(src, xpos, ypos, 0, width);"].join("\n")}})};
TG.Utils={smoothStep:function(a,b,c){c=TG.Utils.clamp((c-a)/(b-a),0,1);return c*c*(3-2*c)},distance:function(a,b,c,d){a=c-a;b=d-b;return Math.sqrt(a*a+b*b)},clamp:function(a,b,c){return Math.min(Math.max(a,b),c)},getPixelNearest:function(a,b,c,d,e){return a[d+Math.round(c)*e*4+4*Math.round(b)]},getPixelBilinear:function(a,b,c,d,e){var f=b-(b^0),g=1-f,h=c-(c^0);b=4*(b^0);var k=b+4,l=4*(c^0);c=(l+4)*e+d;d=l*e+d;return(a[c+b]*g+a[c+k]*f)*h+(a[d+b]*g+a[d+k]*f)*(1-h)}};