babylon.asciiArtPostProcess.min.js 4.8 KB

1
  1. var __extends=this&&this.__extends||(function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])};return function(e,o){function r(){this.constructor=e}t(e,o),e.prototype=null===o?Object.create(o):(r.prototype=o.prototype,new r)}})(),__decorate=this&&this.__decorate||function(t,e,o,r){var n,i=arguments.length,a=i<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,o,r);else for(var c=t.length-1;c>=0;c--)(n=t[c])&&(a=(i<3?n(a):i>3?n(e,o,a):n(e,o))||a);return i>3&&a&&Object.defineProperty(e,o,a),a},BABYLON;!(function(t){var e=(function(e){function o(o,r,n,i){var a=e.call(this,i)||this;a.name=o,a._text,a._font,a.wrapU=t.Texture.CLAMP_ADDRESSMODE,a.wrapV=t.Texture.CLAMP_ADDRESSMODE;var c=a.getFontHeight(r),l=a.getFontWidth(r);a._charSize=Math.max(c.height,l);var f=Math.ceil(a._charSize*n.length),s=a._charSize;a._texture=i.getEngine().createDynamicTexture(f,s,!1,t.Texture.NEAREST_SAMPLINGMODE);var h=a.getSize(),u=document.createElement("canvas");u.width=h.width,u.height=h.height;var g=u.getContext("2d");g.textBaseline="top",g.font=r,g.fillStyle="white",g.imageSmoothingEnabled=!1;for(var x=0;x<n.length;x++)g.fillText(n[x],x*a._charSize,-c.offset);return a.getScene().getEngine().updateDynamicTexture(a._texture,u,!1,!0),a}return __extends(o,e),Object.defineProperty(o.prototype,"charSize",{get:function(){return this._charSize},enumerable:!0,configurable:!0}),o.prototype.getFontWidth=function(t){var e=document.createElement("canvas"),o=e.getContext("2d");return o.fillStyle="white",o.font=t,o.measureText("W").width},o.prototype.getFontHeight=function(t){var e=document.createElement("canvas"),o=e.getContext("2d");o.fillRect(0,0,e.width,e.height),o.textBaseline="top",o.fillStyle="white",o.font=t,o.fillText("jH|",0,0);for(var r=o.getImageData(0,0,e.width,e.height).data,n=-1,i=-1,a=0;a<e.height;a++)for(var c=0;c<e.width;c++){var l=4*(a*e.width+c);{if(0!==r[l]){-1===n&&(n=a);break}if(c===e.width-1&&-1!==n){i=a,a=e.height;break}}}return{height:i-n+1,offset:n-1}},o.prototype.clone=function(){return new o(this.name,this._font,this._text,this.getScene())},o.Parse=function(e,r){return t.SerializationHelper.Parse((function(){return new o(e.name,e.font,e.text,r)}),e,r,null)},__decorate([t.serialize("font")],o.prototype,"_font",void 0),__decorate([t.serialize("text")],o.prototype,"_text",void 0),o})(t.BaseTexture);t.AsciiArtFontTexture=e;var o=(function(o){function r(r,n,i){var a=o.call(this,r,"asciiart",["asciiArtFontInfos","asciiArtOptions"],["asciiArtFont"],{width:n.getEngine().getRenderWidth(),height:n.getEngine().getRenderHeight()},n,t.Texture.TRILINEAR_SAMPLINGMODE,n.getEngine(),!0)||this;a.mixToTile=0,a.mixToNormal=0;var c="40px Monospace",l=" `-.'_:,\"=^;<+!*?/cL\\zrs7TivJtC{3F)Il(xZfY5S2eajo14[nuyE]P6V9kXpKwGhqAUbOd8#HRDB0$mgMW&Q%N@";i&&("string"==typeof i?c=i:(c=i.font||c,l=i.characterSet||l,a.mixToTile=i.mixToTile||a.mixToTile,a.mixToNormal=i.mixToNormal||a.mixToNormal)),a._asciiArtFontTexture=new e(r,c,l,n.getScene());var f=a._asciiArtFontTexture.getSize();return a.onApply=function(t){t.setTexture("asciiArtFont",a._asciiArtFontTexture),t.setFloat4("asciiArtFontInfos",a._asciiArtFontTexture.charSize,l.length,f.width,f.height),t.setFloat4("asciiArtOptions",a.width,a.height,a.mixToNormal,a.mixToTile)},a}return __extends(r,o),r})(t.PostProcess);t.AsciiArtPostProcess=o})(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}";