phaser3-rex-plugins
Version:
2 lines (1 loc) • 9.37 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexglowfilter2pipelineplugin=t();}(undefined,(function(){function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,(o=r.key,i=void 0,"symbol"==typeof(i=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===t?String:Number)(e)}(o,"string"))?i:String(i)),r);}var o,i;}function r(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function o(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&l(e,t);}function i(e){return i=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},i(e)}function l(e,t){return l=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},l(e,t)}function u(e,t){if(t&&("object"==typeof t||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function s(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return !1}}();return function(){var n,r=i(e);if(t){var o=i(this).constructor;n=Reflect.construct(r,arguments,o);}else n=r.apply(this,arguments);return u(this,n)}}function a(){return a="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(e,t,n){var r=function(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=i(e)););return e}(e,t);if(r){var o=Object.getOwnPropertyDescriptor(r,t);return o.get?o.get.call(arguments.length<3?e:n):o.value}},a.apply(this,arguments)}var c=Phaser.Utils.Objects.GetValue,f=function(e){var t=c(e,"quality",0,1),n=c(e,"distance",10);return "#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 outerStrength;\nuniform float innerStrength;\nuniform vec4 glowColor; // (0, 0, 0);\nuniform float knockout;\n\n// const\nconst float PI = 3.14159265358979323846264;\n\nconst float DIST = __DIST__;\nconst float ANGLE_STEP_SIZE = min(__ANGLE_STEP_SIZE__, PI * 2.0);\nconst float ANGLE_STEP_NUM = ceil(PI * 2.0 / ANGLE_STEP_SIZE);\n\nconst float MAX_TOTAL_ALPHA = ANGLE_STEP_NUM * DIST * (DIST + 1.0) / 2.0;\n\n\nvoid main(void) {\n vec2 px = vec2(1./texSize.x, 1./texSize.y);\n\n float totalAlpha = 0.0;\n\n vec2 direction;\n vec2 offset;\n vec4 curColor;\n\n for (float angle = 0.; angle < PI * 2.; angle += ANGLE_STEP_SIZE) {\n direction = vec2(cos(angle), sin(angle)) * px;\n\n for (float curDistance = 0.0; curDistance < DIST; curDistance++) {\n offset = direction * (curDistance + 1.0);\n curColor = texture2D(uMainSampler, outTexCoord + offset);\n totalAlpha += (DIST - curDistance) * curColor.a;\n }\n }\n \n curColor = texture2D(uMainSampler, outTexCoord);\n\n float alphaRatio = (totalAlpha / MAX_TOTAL_ALPHA);\n\n float innerGlowAlpha = (1.0 - alphaRatio) * innerStrength * curColor.a;\n float innerGlowStrength = min(1.0, innerGlowAlpha);\n \n vec4 innerColor = mix(curColor, glowColor, innerGlowStrength);\n\n float outerGlowAlpha = alphaRatio * outerStrength * (1. - curColor.a);\n float outerGlowStrength = min(1.0 - innerColor.a, outerGlowAlpha);\n\n vec4 outerGlowColor = outerGlowStrength * glowColor.rgba;\n \n if (knockout > 0.) {\n float resultAlpha = outerGlowAlpha + innerGlowAlpha;\n gl_FragColor = vec4(glowColor.rgb * resultAlpha, resultAlpha);\n }\n else {\n gl_FragColor = innerColor + outerGlowColor;\n }\n}\n".replace(/__ANGLE_STEP_SIZE__/gi,"".concat((1/t/n).toFixed(7))).replace(/__DIST__/gi,"".concat(Math.round(n).toFixed(0),".0"))},h=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,p=Phaser.Utils.Objects.GetValue,y=Phaser.Display.Color.IntegerToRGB,g=Phaser.Display.Color,v=.1,d=10,P=f({quality:v,distance:d}),m=function(e){o(i,e);var n=s(i);function i(e){var r;return t(this,i),(r=n.call(this,{name:"rexGlowFilterPostFx",game:e,renderTarget:!0,fragShader:P})).outerStrength=0,r.innerStrength=0,r._glowColor=new g,r.knockout=!1,r}return r(i,[{key:"resetFromJSON",value:function(e){return this.setOuterStrength(p(e,"outerStrength",4)),this.setInnerStrength(p(e,"innerStrength",0)),this.setGlowColor(p(e,"glowColor",16777215)),this.setKnockout(p(e,"knockout",!1)),this}},{key:"onPreRender",value:function(){this.set1f("outerStrength",this.outerStrength),this.set1f("innerStrength",this.innerStrength);var e=this._glowColor;this.set4f("glowColor",e.redGL,e.greenGL,e.blueGL,e.alphaGL),this.set1f("knockout",this.knockout?1:0),this.set2f("texSize",this.renderer.width,this.renderer.height);}},{key:"setOuterStrength",value:function(e){return this.outerStrength=e,this}},{key:"setInnerStrength",value:function(e){return this.innerStrength=e,this}},{key:"glowColor",get:function(){return this._glowColor},set:function(e){"number"==typeof e&&(e=y(e)),this._glowColor.setFromRGB(e);}},{key:"setGlowColor",value:function(e){return this.glowColor=e,this}},{key:"setKnockout",value:function(e){return this.knockout=e,this}}],[{key:"setQuality",value:function(e){v!==e&&(P=f({quality:v=e,distance:d}));}},{key:"getQuality",value:function(){return v}},{key:"setDistance",value:function(e){d!==e&&(P=f({quality:v,distance:d=e}));}},{key:"getDistance",value:function(){return d}}]),i}(h),S=Phaser.Game,b=function(e){return e instanceof S},_=Phaser.Scene,w=function(e){return e instanceof _},C=function(t,n,r){var o;(o=t,null==o||"object"!==e(o)?null:b(o)?o:b(o.game)?o.game:w(o)?o.sys.game:w(o.scene)?o.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(n,r);},k=Phaser.Utils.Array.SpliceOne,G=function(e){o(i,Phaser.Plugins.BasePlugin);var n=s(i);function i(){return t(this,i),n.apply(this,arguments)}return r(i,[{key:"setPostPipelineClass",value:function(e,t){return this.PostFxPipelineClass=e,this.postFxPipelineName=t,this}},{key:"start",value:function(){this.game.events.once("destroy",this.destroy,this),C(this.game,this.postFxPipelineName,this.PostFxPipelineClass);}},{key:"add",value:function(e,t){return function(e,t,n){void 0===n&&(n={}),e.setPostPipeline(t);var r=e.postPipelines[e.postPipelines.length-1];return r.resetFromJSON(n),n.name&&(r.name=n.name),r}(e,this.PostFxPipelineClass,t)}},{key:"remove",value:function(e,t){return function(e,t,n){if(void 0===n)for(var r=(o=e.postPipelines).length-1;r>=0;r--)(l=o[r])instanceof t&&(l.destroy(),k(o,r));else {r=0;for(var o,i=(o=e.postPipelines).length;r<i;r++){var l;(l=o[r])instanceof t&&l.name===n&&(l.destroy(),k(o,r));}}}(e,this.PostFxPipelineClass,t),this}},{key:"get",value:function(e,t){return function(e,t,n){if(void 0===n){for(var r=[],o=0,i=(l=e.postPipelines).length;o<i;o++)(u=l[o])instanceof t&&r.push(u);return r}var l;for(o=0,i=(l=e.postPipelines).length;o<i;o++){var u;if((u=l[o])instanceof t&&u.name===n)return u}}(e,this.PostFxPipelineClass,t)}}]),i}(),x=function(e){return null==e||""===e||0===e.length},O=Phaser.Utils.Objects.GetValue,T=function(e){o(l,e);var n=s(l);function l(e){var r;return t(this,l),(r=n.call(this,e)).setPostPipelineClass(m,"rexGlowFilter2PostFx"),r}return r(l,[{key:"add",value:function(e,t){return this.setQuality(O(t,"quality",this.quality)),this.setDistance(O(t,"distance",this.distance)),a(i(l.prototype),"add",this).call(this,e,t)}},{key:"setQuality",value:function(e){return m.setQuality(e),this}},{key:"quality",get:function(){return m.getQuality()},set:function(e){this.setQuality(e);}},{key:"setDistance",value:function(e){return m.setDistance(e),this}},{key:"distance",get:function(){return m.getDistance()},set:function(e){this.setDistance(e);}}]),l}(G);return function(t,n,r,o){if(void 0===o&&(o="."),"object"===e(t)){if(x(n)){if(null==r)return;"object"===e(r)&&(t=r);}else {"string"==typeof n&&(n=n.split(o));var i=n.pop(),l=function(t,n,r){var o=t;if(x(n));else {var i;"string"==typeof n&&(n=n.split("."));for(var l=0,u=n.length;l<u;l++){var s;null!=o[i=n[l]]&&"object"===e(o[i])||(s=l===u-1?void 0===r?{}:r:{},o[i]=s),o=o[i];}}return o}(t,n);l[i]=r;}}}(window,"RexPlugins.Pipelines.GlowFilter2PostFx",m),T}));