babylon.asciiArtPostProcess.min.js 4.2 KB

1
  1. var BABYLON;!(function(t){var e=(function(e){function i(i,o,n,r){e.call(this,r),this.name=i,this._text==n,this._font==o,this.wrapU=t.Texture.CLAMP_ADDRESSMODE,this.wrapV=t.Texture.CLAMP_ADDRESSMODE;var a=this.getFontHeight(o),l=this.getFontWidth(o);this._charSize=Math.max(a.height,l);var c=Math.ceil(this._charSize*n.length),s=this._charSize;this._texture=r.getEngine().createDynamicTexture(c,s,!1,t.Texture.NEAREST_SAMPLINGMODE);var f=this.getSize(),h=document.createElement("canvas");h.width=f.width,h.height=f.height;var g=h.getContext("2d");g.textBaseline="top",g.font=o,g.fillStyle="white",g.imageSmoothingEnabled=!1;for(var x=0;x<n.length;x++)g.fillText(n[x],x*this._charSize,-a.offset);this.getScene().getEngine().updateDynamicTexture(this._texture,h,!1,!0)}return __extends(i,e),Object.defineProperty(i.prototype,"charSize",{get:function(){return this._charSize},enumerable:!0,configurable:!0}),i.prototype.getFontWidth=function(t){var e=document.createElement("canvas"),i=e.getContext("2d");return i.fillStyle="white",i.font=t,i.measureText("W").width},i.prototype.getFontHeight=function(t){var e=document.createElement("canvas"),i=e.getContext("2d");i.fillRect(0,0,e.width,e.height),i.textBaseline="top",i.fillStyle="white",i.font=t,i.fillText("jH|",0,0);for(var o=i.getImageData(0,0,e.width,e.height).data,n=-1,r=-1,a=0;a<e.height;a++)for(var l=0;l<e.width;l++){var c=4*(a*e.width+l);{if(0!==o[c]){n===-1&&(n=a);break}if(l===e.width-1&&n!==-1){r=a,a=e.height;break}}}return{height:r-n+1,offset:n-1}},i.prototype.clone=function(){return new i(this.name,this._font,this._text,this.getScene())},i.Parse=function(e,o){var n=t.SerializationHelper.Parse((function(){return new i(e.name,e.font,e.text,o)}),e,o,null);return n},__decorate([t.serialize("font")],i.prototype,"_font",void 0),__decorate([t.serialize("text")],i.prototype,"_text",void 0),i})(t.BaseTexture);t.AsciiArtFontTexture=e;var i=(function(i){function o(o,n,r){var a=this;i.call(this,o,"asciiart",["asciiArtFontInfos","asciiArtOptions"],["asciiArtFont"],{width:n.getEngine().getRenderWidth(),height:n.getEngine().getRenderHeight()},n,t.Texture.TRILINEAR_SAMPLINGMODE,n.getEngine(),!0),this.mixToTile=0,this.mixToNormal=0;var l="40px Monospace",c=" `-.'_:,\"=^;<+!*?/cL\\zrs7TivJtC{3F)Il(xZfY5S2eajo14[nuyE]P6V9kXpKwGhqAUbOd8#HRDB0$mgMW&Q%N@";r&&("string"==typeof r?l=r:(l=r.font||l,c=r.characterSet||c,this.mixToTile=r.mixToTile||this.mixToTile,this.mixToNormal=r.mixToNormal||this.mixToNormal)),this._asciiArtFontTexture=new e(o,l,c,n.getScene());var s=this._asciiArtFontTexture.getSize();this.onApply=function(t){t.setTexture("asciiArtFont",a._asciiArtFontTexture),t.setFloat4("asciiArtFontInfos",a._asciiArtFontTexture.charSize,c.length,s.width,s.height),t.setFloat4("asciiArtOptions",a.width,a.height,a.mixToNormal,a.mixToTile)}}return __extends(o,i),o})(t.PostProcess);t.AsciiArtPostProcess=i})(BABYLON||(BABYLON={})),BABYLON.Effect.ShadersStore.asciiartPixelShader="\nvarying vec2 vUV;\nuniform sampler2D textureSampler;\nuniform sampler2D asciiArtFont;\n\nuniform vec4 asciiArtFontInfos;\nuniform vec4 asciiArtOptions;\n\nfloat getLuminance(vec3 color)\n{\nreturn clamp(dot(color,vec3(0.2126,0.7152,0.0722)),0.,1.);\n}\n\nvoid main(void) \n{\nfloat caracterSize=asciiArtFontInfos.x;\nfloat numChar=asciiArtFontInfos.y-1.0;\nfloat fontx=asciiArtFontInfos.z;\nfloat fonty=asciiArtFontInfos.w;\nfloat screenx=asciiArtOptions.x;\nfloat screeny=asciiArtOptions.y;\nfloat tileX=float(floor((gl_FragCoord.x)/caracterSize))*caracterSize/screenx;\nfloat tileY=float(floor((gl_FragCoord.y)/caracterSize))*caracterSize/screeny;\nvec2 tileUV=vec2(tileX,tileY);\nvec4 tileColor=texture2D(textureSampler,tileUV);\nvec4 baseColor=texture2D(textureSampler,vUV);\nfloat tileLuminance=getLuminance(tileColor.rgb);\nfloat offsetx=(float(floor(tileLuminance*numChar)))*caracterSize/fontx;\nfloat offsety=0.0;\nfloat x=float(mod(gl_FragCoord.x,caracterSize))/fontx;\nfloat y=float(mod(gl_FragCoord.y,caracterSize))/fonty;\nvec4 finalColor=texture2D(asciiArtFont,vec2(offsetx+x,offsety+(caracterSize/fonty-y)));\nfinalColor.rgb*=tileColor.rgb;\nfinalColor.a=1.0;\nfinalColor=mix(finalColor,tileColor,asciiArtOptions.w);\nfinalColor=mix(finalColor,baseColor,asciiArtOptions.z);\ngl_FragColor=finalColor;\n}";