phaser3-rex-plugins
Version:
2 lines (1 loc) • 11.2 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).rexdropshadowpipelineplugin=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,(i=r.key,o=void 0,"symbol"==typeof(o=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)}(i,"string"))?o:String(o)),r);}var i,o;}function r(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function i(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&&s(e,t);}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function s(e,t){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},s(e,t)}function a(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function u(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=o(e);if(t){var i=o(this).constructor;n=Reflect.construct(r,arguments,i);}else n=r.apply(this,arguments);return function(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 a(e)}(this,n)}}function l(e){return function(e){if(Array.isArray(e))return f(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(!e)return;if("string"==typeof e)return f(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return f(e,t)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function f(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}var h=function(e,t,n){void 0===n&&(n=[]),n.length=t;for(var r=t;r>0;r--)n[r]=e*(r/t);return n},c=function(){function e(n,r){t(this,e),this.postFXPipeline=n,this.shader=r;}return r(e,[{key:"getAnotherFrame",value:function(e){var t=this.postFXPipeline,n=t.fullFrame1,r=t.fullFrame2;return e===n?r:n}},{key:"init",value:function(e,t){var n=this.postFXPipeline;return void 0===t&&(t=n.fullFrame1),n.copyFrame(e,t),t}},{key:"draw",value:function(e,t){}}]),e}(),p=function(e){i(o,e);var n=u(o);function o(){return t(this,o),n.apply(this,arguments)}return r(o,[{key:"draw",value:function(e,t){var n,r=this.postFXPipeline,i=this.shader,o=e,s=this.getAnotherFrame(o),a=r.distance/r.renderer.width*Math.cos(r.rotation),u=r.distance/r.renderer.height*Math.sin(r.rotation);return r.set2f("offset",a,u,i),r.set3f("color",r._shadowColor.redGL,r._shadowColor.greenGL,r._shadowColor.blueGL,i),r.set1f("alpha",r.alpha,i),t?(r.bindAndDraw(o,s,!0,!0,i),n=s):r.bindAndDraw(o,null,!0,!0,i),n}}]),o}(c),d=function(e){i(o,e);var n=u(o);function o(){return t(this,o),n.apply(this,arguments)}return r(o,[{key:"draw",value:function(e,t){for(var n,r,i,o,s=this.postFXPipeline,a=this.shader,u=e,l=this.getAnotherFrame(u),f=s.pixelWidth/s.renderer.width,h=s.pixelHeight/s.renderer.height,c=0,p=s._quality-1;c<=p;c++)i=(r=s._kernels[c]+.5)*f,o=r*h,s.set2f("uOffset",i,o,a),c<p?(s.bindAndDraw(u,l,!0,!0,a),u=l,l=this.getAnotherFrame(u)):t?(s.bindAndDraw(u,l,!0,!0,a),n=l):s.bindAndDraw(u,null,!0,!0,a);return n}}]),o}(c),y=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,v=Phaser.Utils.Objects.GetValue,m=Phaser.Math.DegToRad,g=Phaser.Math.RadToDeg,b=Phaser.Display.Color.IntegerToRGB,P=Phaser.Display.Color,w=function(e){i(o,e);var n=u(o);function o(e){var r;return t(this,o),(r=n.call(this,{name:"rexDropShadowPostFx",game:e,shaders:[{fragShader:"#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 float alpha;\nuniform vec3 color;\nuniform vec2 offset;\n\nvoid main (void) {\n vec4 sample = texture2D(uMainSampler, outTexCoord - offset);\n\n // Premultiply alpha\n sample.rgb = color.rgb * sample.a;\n\n // alpha user alpha\n sample *= alpha;\n\n gl_FragColor = sample;\n}\n"},{fragShader:"#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 uOffset;\n\nvoid main (void) {\n vec4 color = vec4(0.0);\n\n // Sample top left pixel\n color += texture2D(uMainSampler, vec2(outTexCoord.x - uOffset.x, outTexCoord.y + uOffset.y));\n\n // Sample top right pixel\n color += texture2D(uMainSampler, vec2(outTexCoord.x + uOffset.x, outTexCoord.y + uOffset.y));\n\n // Sample bottom right pixel\n color += texture2D(uMainSampler, vec2(outTexCoord.x + uOffset.x, outTexCoord.y - uOffset.y));\n\n // Sample bottom left pixel\n color += texture2D(uMainSampler, vec2(outTexCoord.x - uOffset.x, outTexCoord.y - uOffset.y));\n\n // Average\n color *= 0.25;\n\n gl_FragColor = color;\n}\n"}]})).shadowDrawer=new p(a(r),r.shaders[0]),r.kawaseBlurDrawer=new d(a(r),r.shaders[1]),r.rotation=0,r.distance=0,r._shadowColor=new P,r.alpha=.5,r.shadowOnly=!1,r._kernels=[0],r._blur=0,r._quality=1,r.pixelWidth=1,r.pixelHeight=1,r}return r(o,[{key:"resetFromJSON",value:function(e){var t=v(e,"rotation",void 0);void 0===t?this.setAngle(v(e,"angle",45)):this.setRotation(t),this.setDistance(v(e,"distance",5)),this.setShadowColor(v(e,"shadowColor",16777215)),this.setAlpha(v(e,"alpha",.5)),this.setShadowOnly(v(e,"shadowOnly",!1));var n=v(e,"blur",4);return "number"==typeof n?(this.setBlur(n),this.setQuality(v(e,"quality",3))):this.setKernela(n),this.setPixelSize(v(e,"pixelWidth",1),v(e,"pixelHeight",1)),this}},{key:"onPreRender",value:function(){}},{key:"onDraw",value:function(e){var t;t=this.shadowDrawer.draw(this.shadowDrawer.init(e),!0),t=this.kawaseBlurDrawer.draw(t,!0),this.shadowOnly||this.copyFrame(e,t,1,!1),this.copyToGame(t);}},{key:"setRotation",value:function(e){return this.rotation=e,this}},{key:"angle",get:function(){return g(this.rotation)},set:function(e){this.rotation=m(e);}},{key:"setAngle",value:function(e){return this.angle=e,this}},{key:"setDistance",value:function(e){return this.distance=e,this}},{key:"shadowColor",get:function(){return this._shadowColor},set:function(e){"number"==typeof e&&(e=b(e)),this._shadowColor.setFromRGB(e);}},{key:"setShadowColor",value:function(e){return this.shadowColor=e,this}},{key:"setAlpha",value:function(e){return this.alpha=e,this}},{key:"setShadowOnly",value:function(e){return void 0===e&&(e=!0),this.shadowOnly=e,this}},{key:"blur",get:function(){return this._blur},set:function(e){this._blur!==e&&(this._blur=e,h(this._blur,this._quality,this._kernels));}},{key:"setBlur",value:function(e){return this.blur=e,this}},{key:"quality",get:function(){return this._quality},set:function(e){this._quality!==e&&(this._quality=e,h(this._blur,this._quality,this._kernels));}},{key:"setQuality",value:function(e){return this.quality=e,this}},{key:"kernels",get:function(){return this._kernels},set:function(e){void 0===e&&(e=[0]),this._kernels=e,this._quality=e.length,this._blur=Math.max.apply(Math,l(e));}},{key:"setKernela",value:function(e){return this.kernels=e,this}},{key:"setPixelWidth",value:function(e){return this.pixelWidth=e,this}},{key:"setPixelHeight",value:function(e){return this.pixelHeight=e,this}},{key:"setPixelSize",value:function(e,t){return void 0===t&&(t=e),this.pixelWidth=e,this.pixelHeight=t,this}}]),o}(y),x=Phaser.Game,S=function(e){return e instanceof x},_=Phaser.Scene,k=function(e){return e instanceof _},O=function(t,n,r){var i;(i=t,null==i||"object"!==e(i)?null:S(i)?i:S(i.game)?i.game:k(i)?i.sys.game:k(i.scene)?i.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(n,r);},C=Phaser.Utils.Array.SpliceOne,F=function(e){i(o,Phaser.Plugins.BasePlugin);var n=u(o);function o(){return t(this,o),n.apply(this,arguments)}return r(o,[{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),O(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=(i=e.postPipelines).length-1;r>=0;r--)(s=i[r])instanceof t&&(s.destroy(),C(i,r));else {r=0;for(var i,o=(i=e.postPipelines).length;r<o;r++){var s;(s=i[r])instanceof t&&s.name===n&&(s.destroy(),C(i,r));}}}(e,this.PostFxPipelineClass,t),this}},{key:"get",value:function(e,t){return function(e,t,n){if(void 0===n){for(var r=[],i=0,o=(s=e.postPipelines).length;i<o;i++)(a=s[i])instanceof t&&r.push(a);return r}var s;for(i=0,o=(s=e.postPipelines).length;i<o;i++){var a;if((a=s[i])instanceof t&&a.name===n)return a}}(e,this.PostFxPipelineClass,t)}}]),o}(),D=function(e){return null==e||""===e||0===e.length},A=function(e){i(o,e);var n=u(o);function o(e){var r;return t(this,o),(r=n.call(this,e)).setPostPipelineClass(w,"rexDropShadowPostFx"),r}return r(o)}(F);return function(t,n,r,i){if(void 0===i&&(i="."),"object"===e(t)){if(D(n)){if(null==r)return;"object"===e(r)&&(t=r);}else {"string"==typeof n&&(n=n.split(i));var o=n.pop(),s=function(t,n,r){var i=t;if(D(n));else {var o;"string"==typeof n&&(n=n.split("."));for(var s=0,a=n.length;s<a;s++){var u;null!=i[o=n[s]]&&"object"===e(i[o])||(u=s===a-1?void 0===r?{}:r:{},i[o]=u),i=i[o];}}return i}(t,n);s[o]=r;}}}(window,"RexPlugins.Pipelines.DropShadowPostFx",w),A}));