phaser4-rex-plugins
Version:
2 lines (1 loc) • 5.43 kB
JavaScript
var e,t;e=void 0,t=function(){const e="rexFilterOutline";class t extends Phaser.Renderer.WebGL.RenderNodes.BaseFilterShader{static FilterName=e;constructor(t){super(e,t,null,"#pragma phaserTemplate(shaderName)\n\n#define ANGLESTEP 0.314\n\n#pragma phaserTemplate(fragmentDefine)\n\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n#define highmedp highp\n#else\n#define highmedp mediump\n#endif\nprecision highmedp float;\n\n// Scene buffer\nuniform sampler2D uMainSampler; \nvarying vec2 outTexCoord;\n\n// Effect parameters\nuniform vec2 texSize;\nuniform float thickness;\nuniform vec3 outlineColor;\n\nconst float DOUBLE_PI = 3.14159265358979323846264 * 2.;\n\n#pragma phaserTemplate(fragmentHeader)\n\nvoid main() {\n vec4 front = boundedSampler(uMainSampler, outTexCoord);\n\n if (thickness > 0.0) {\n vec2 mag = vec2(thickness/texSize.x, thickness/texSize.y);\n vec4 curColor;\n float maxAlpha = front.a;\n vec2 offset;\n for (float angle = 0.; angle < DOUBLE_PI; angle += ANGLESTEP) {\n offset = vec2(mag.x * cos(angle), mag.y * sin(angle)); \n curColor = boundedSampler(uMainSampler, outTexCoord + offset);\n maxAlpha = max(maxAlpha, curColor.a);\n }\n vec3 resultColor = front.rgb + (outlineColor.rgb * (1. - front.a)) * maxAlpha;\n gl_FragColor = vec4(resultColor, maxAlpha);\n } else {\n gl_FragColor = front;\n }\n}\n",[{name:"anglestep_0.314",additions:{fragmentDefine:"#define ANGLESTEP 0.314"},tags:["anglestep"]}])}updateShaderConfig(e,t){var n=this.programManager,r=Math.max(100*e.quality,1),i=(2*Math.PI/r).toFixed(7),s=n.getAdditionsByTag("anglestep")[0];s.name="anglestep_"+i,s.additions.fragmentDefine="#undef ANGLESTEP\n#define ANGLESTEP "+i}setupUniforms(e,t){const n=this.programManager;n.setUniform("thickness",e.thickness),n.setUniform("outlineColor",[e._outlineColor.redGL,e._outlineColor.greenGL,e._outlineColor.blueGL]),n.setUniform("texSize",[t.width,t.height])}}const n=Phaser.Utils.Objects.GetValue,r=Phaser.Display.Color.IntegerToRGB,i=Phaser.Display.Color;class s extends Phaser.Filters.Controller{static FilterName=e;constructor(t,n){super(t,e),this.thickness=0,this._outlineColor=new i,this.quality=.1,this.setPaddingOverride(null),this.resetFromJSON(n)}resetFromJSON(e){return this.setThickness(n(e,"thickness",3)),this.setOutlineColor(n(e,"outlineColor",16777215)),this.setQuality(n(e,"quality",.1)),this}setThickness(e){return this.thickness=e,this}get outlineColor(){return this._outlineColor}set outlineColor(e){"number"==typeof e&&(e=r(e)),this._outlineColor.setFromRGB(e)}setOutlineColor(e){return this.outlineColor=e,this}setQuality(e){return this.quality=e,this}getPadding(){var e=this.paddingOverride;if(e)return this.currentPadding.setTo(e.x,e.y,e.width,e.height),e;var t=this.currentPadding,n=this.thickness;return t.left=-n,t.top=-n,t.right=n,t.bottom=n,t}}const o=Phaser.Game;var a=function(e){return e instanceof o};const l=Phaser.Scene;var u=function(e){return e instanceof l},h=function(e,t){var n,r=t.FilterName,i=(n=e,null==n||"object"!=typeof n?null:a(n)?n:a(n.game)?n.game:u(n)?n.sys.game:u(n.scene)?n.scene.sys.game:void 0).renderer.renderNodes;return!i.hasNode(r)&&(i.addNodeConstructor(r,t),!0)},f=function(e,t){return void 0===t&&(t=!1),e.filters||e.enableFilters().focusFilters(),t?e.filters.external:e.filters.internal};const d=Phaser.Utils.Array.SpliceOne;class c extends Phaser.Plugins.BasePlugin{setFilterClass(e,t){return this.FilterClass=e,this.ControllerClass=t,this}setFilterListMethod(e,t){return function(e,t){var n=Phaser.GameObjects.Components.FilterList.prototype;n[e]||(n[e]=t)}(e,t),this}start(){var e=this.game.events;e.once("destroy",this.destroy,this),this.game.isRunning?h(this.game,this.FilterClass):e.once("ready",(function(){h(this.game,this.FilterClass)}),this)}add(e,t,n=!1){return function(e,t,n,r){void 0===n&&(n={});var i=f(e,r),s=i.add(new t(i.camera,n));return n.name&&(s.name=n.name),s}(e,this.ControllerClass,t,n)}remove(e,t,n=!1){return function(e,t,n,r){var i=f(e,r).list;if(void 0===n)for(var s=i.length-1;s>=0;s--)(a=i[s])instanceof t&&(a.destroy(),d(a,s));else{s=0;for(var o=i.length;s<o;s++){var a;(a=i[s])instanceof t&&a.name===n&&(a.destroy(),d(a,s))}}}(e,this.ControllerClass,t,n),this}get(e,t,n=!1){return function(e,t,n,r){var i=f(e,r).list;if(void 0===n){for(var s=[],o=0,a=i.length;o<a;o++)(l=i[o])instanceof t&&s.push(l);return s}for(o=0,a=i.length;o<a;o++){var l;if((l=i[o])instanceof t&&l.name===n)return l}}(e,this.ControllerClass,t,n)}}var m=function(e){return null==e||""===e||0===e.length},g=function(e,t,n,r){if(void 0===r&&(r="."),"object"==typeof e){if(m(t)){if(null==n)return;"object"==typeof n&&(e=n)}else{"string"==typeof t&&(t=t.split(r));var i=t.pop(),s=function(e,t,n){var r=e;if(m(t));else{var i;"string"==typeof t&&(t=t.split("."));for(var s=0,o=t.length;s<o;s++){var a;null!=r[i=t[s]]&&"object"==typeof r[i]||(a=s===o-1?void 0===n?{}:n:{},r[i]=a),r=r[i]}}return r}(e,t);s[i]=n}return e}};return g(window,"RexPlugins.Filters.OutlineFilter",t),g(window,"RexPlugins.Filters.OutlineController",s),class extends c{constructor(e){super(e),this.setFilterClass(t,s),this.setFilterListMethod("addRexOutline",(function(e){return this.add(new s(this.camera,e))}))}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexoutlinefilterplugin=t();