demolishedtexture
Version:
Procedual texture generator for Demolished or what ever you want.Renders base64 strings and/or binary.
1 lines • 4.05 kB
JavaScript
"use strict";var __extends=this&&this.__extends||function(){var extendStatics=function(d,b){return(extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)b.hasOwnProperty(p)&&(d[p]=b[p])})(d,b)};return function(d,b){function __(){this.constructor=d}extendStatics(d,b),d.prototype=null===b?Object.create(b):(__.prototype=b.prototype,new __)}}();Object.defineProperty(exports,"__esModule",{value:!0});var TextureBase=function(){function TextureBase(){this.perm=this.seed(255)}return TextureBase.prototype.vec=function(x,y,z,a){return[x,0|y,0|z,0|a]},TextureBase.prototype.normalize=function(a){var l=this.length(a);return a=0!=l?this.func(a,(function(v,i){return v/l})):a},TextureBase.prototype.R=function(a,b){return Math.abs(a*b*255)},TextureBase.prototype.abs=function(a){return a.map((function(v,i){return Math.abs(v)}))},TextureBase.prototype.func=function(a,exp){return a.map((function(v,i){return exp(v,i)}))},TextureBase.prototype.toScale=function(v,w){var a=0,b,c=-1,d=1;return(v-0)/(w-0)*2+c},TextureBase.prototype.dot=function(a,b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]},TextureBase.prototype.length=function(a){return Math.sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2])},TextureBase.prototype.fade=function(t){return t*t*t*(t*(6*t-15)+10)},TextureBase.prototype.clamp=function(n,a,b){return n<=a?a:n>=b?b:n},TextureBase.prototype.lerp=function(t,a,b){return a+t*(b-a)},TextureBase.prototype.grad=function(hash,x,y,z){var h=15&hash,u=h<8?x:y,v=h<4?y:12==h||14==h?x:z;return(0==(1&h)?u:-u)+(0==(2&h)?v:-v)},TextureBase.prototype.scale=function(n){return(1+n)/2},TextureBase.prototype.seed=function(n){for(var p=[],a=[],b=0;n>=b;b++)a.push(b);for(b=0;n>=b;b++){var c=n*Math.random(),d=a[~~c];a.splice(c,1,a[b]),a.splice(b,1,d)}for(var i=0;i<n;i++)p[n+i]=p[i]=a[i];return p},TextureBase.prototype.noise=function(x,y,z){var t=this,p=this.perm,X=255&~~x,Y=255&~~y,Z=255&~~z;x-=~~x,y-=~~y,z-=~~z;var u=t.fade(x),v=t.fade(y),w=t.fade(z),A=p[X]+Y,AA=p[A]+Z,AB=p[A+1]+Z,B=p[X+1]+Y,BA=p[B]+Z,BB=p[B+1]+Z;return t.scale(t.lerp(w,t.lerp(v,t.lerp(u,t.grad(p[AA],x,y,z),t.grad(p[BA],x-1,y,z)),t.lerp(u,t.grad(p[AB],x,y-1,z),t.grad(p[BB],x-1,y-1,z))),t.lerp(v,t.lerp(u,t.grad(p[AA+1],x,y,z-1),t.grad(p[BA+1],x-1,y,z-1)),t.lerp(u,t.grad(p[AB+1],x,y-1,z-1),t.grad(p[BB+1],x-1,y-1,z-1)))))},TextureBase}();exports.TextureBase=TextureBase;var TextureGen=function(){function TextureGen(width,height){var _this=this;this.width=width,this.height=height,this.frag=function(pixel,x,y,w,h,v,fn){var r=pixel[0],g=pixel[1],b=pixel[2],t=_this.helpers,res;return fn.apply(t,[[r,b,g],x,y,w,h,v])};var c=document.createElement("canvas");c.width=width,c.height=height,this.ctx=c.getContext("2d"),this.ctx.fillStyle="#0",this.ctx.fillRect(0,0,this.width,this.height),this.buffer=this.ctx.getImageData(0,0,this.width,this.height),this.helpers=new TextureBase}return TextureGen.createTexture=function(width,height,fn){var instance=new TextureGen(width,height);return instance.render(fn),instance},TextureGen.prototype.render=function(fn){for(var bf=this.buffer,f=bf.data,w=this.width,h=this.height,s=this.helpers.toScale,i,x=0;x<w;x++)for(var y=0;y<h;y++){var r=f[(i=4*(x+y*w))+0],g=f[i+1],b=f[i+2],v=[s(x,w),s(y,w),0],p=this.frag([r,g,b],x,y,w,h,v,fn);f[i+0]=p[0],f[i+1]=p[1],f[i+2]=p[2]}this.ctx.putImageData(bf,0,0)},TextureGen.prototype.toBase64=function(){return this.ctx.canvas.toDataURL("image/png")},TextureGen.prototype.toBlob=function(cb){this.ctx.canvas.toBlob(cb,"image/png")},TextureGen}();exports.TextureGen=TextureGen;var CanvasTextureGen=function(_super){function CanvasTextureGen(x,y,w,h){return _super.call(this,w,h)||this}return __extends(CanvasTextureGen,_super),CanvasTextureGen.prototype.D=function(fn){var res;return fn.apply(this.helpers,[this.ctx,0,0,this.width,this,this.height])},CanvasTextureGen.createTexture=function(width,height,fn){var instance=new CanvasTextureGen(0,0,width,height);return instance.D(fn),instance},CanvasTextureGen}(TextureGen);exports.CanvasTextureGen=CanvasTextureGen;