UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 4.56 kB
var e,t;e=void 0,t=function(){const e="rexFilterSwirl";class t extends Phaser.Renderer.WebGL.RenderNodes.BaseFilterShader{static FilterName=e;constructor(t){super(e,t,null,"#pragma phaserTemplate(shaderName)\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 vec2 center;\nuniform vec2 config;\n\n#pragma phaserTemplate(fragmentHeader)\n\nvoid main (void) {\n float radius = config.x;\n float angle = config.y;\n\n vec2 tc = outTexCoord * texSize;\n tc -= center;\n float dist = length(tc);\n if (dist < radius) {\n float percent = (radius - dist) / radius;\n float theta = percent * percent * angle * 8.0;\n float s = sin(theta);\n float c = cos(theta);\n tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c)));\n }\n tc += center;\n gl_FragColor = texture2D(uMainSampler, tc / texSize);\n}\n")}setupUniforms(e,t){const n=this.programManager;n.setUniform("config",[e.radius,e.rotation]),n.setUniform("center",[e.centerX,t.height-e.centerY]),n.setUniform("texSize",[t.width,t.height])}}const n=Phaser.Utils.Objects.GetValue,r=Phaser.Math.DegToRad,i=Phaser.Math.RadToDeg;class s extends Phaser.Filters.Controller{static FilterName=e;constructor(t,n){super(t,e),this.centerX=0,this.centerY=0,this.radius=0,this.rotation=0,this.resetFromJSON(n)}resetFromJSON(e){this.radius=n(e,"radius",0);var t=n(e,"rotation",void 0);return void 0===t?this.setAngle(n(e,"angle",0)):this.setRotation(t),this.setCenter(n(e,"center.x",void 0),n(e,"center.y",void 0)),this}setRadius(e){return this.radius=e,this}setRotation(e){return this.rotation=e,this}get angle(){return i(this.rotation)}set angle(e){this.rotation=r(e)}setAngle(e){return this.angle=e,this}setCenter(e,t){return void 0===e&&(e=this.camera.centerX,t=this.camera.centerY),this.centerX=e,this.centerY=t,this}}const o=Phaser.Game;var a=function(e){return e instanceof o};const l=Phaser.Scene;var c=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:c(n)?n.sys.game:c(n.scene)?n.scene.sys.game:void 0).renderer.renderNodes;return!i.hasNode(r)&&(i.addNodeConstructor(r,t),!0)},d=function(e,t){return void 0===t&&(t=!1),e.filters||e.enableFilters().focusFilters(),t?e.filters.external:e.filters.internal};const u=Phaser.Utils.Array.SpliceOne;class f 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=d(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=d(e,r).list;if(void 0===n)for(var s=i.length-1;s>=0;s--)(a=i[s])instanceof t&&(a.destroy(),u(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(),u(a,s))}}}(e,this.ControllerClass,t,n),this}get(e,t,n=!1){return function(e,t,n,r){var i=d(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 g=function(e){return null==e||""===e||0===e.length},m=function(e,t,n,r){if(void 0===r&&(r="."),"object"==typeof e){if(g(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(g(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 m(window,"RexPlugins.Filters.SwirlFilter",t),m(window,"RexPlugins.Filters.SwirlController",s),class extends f{constructor(e){super(e),this.setFilterClass(t,s),this.setFilterListMethod("addRexSwirl",(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).rexswirlfilterplugin=t();