phaser4-rex-plugins
Version:
2 lines (1 loc) • 6.18 kB
JavaScript
var t,e;t=void 0,e=function(){const t="rexFilterSplit";class e extends Phaser.Renderer.WebGL.RenderNodes.BaseFilterShader{static FilterName=t;constructor(e){super(t,e,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 split;\nuniform vec4 spaceConfig;\nuniform float angle;\nuniform float shiftEnable;\n\nvec2 rotate(vec2 uv, float angle) {\n float s = sin(angle);\n float c = cos(angle);\n return vec2(\n uv.x * c + uv.y * s,\n uv.y * c - uv.x * s\n );\n}\n\n#pragma phaserTemplate(fragmentHeader)\n\nvoid main (void) {\n float spaceLeft = spaceConfig.x;\n float spaceRight = spaceConfig.y;\n float spaceTop = spaceConfig.z;\n float spaceBottom = spaceConfig.w;\n\n vec2 tc = outTexCoord * texSize; \n tc -= split;\n tc = rotate(tc, -angle);\n\n if (\n ((tc.x > -spaceLeft) && (tc.x < spaceRight)) ||\n ((tc.y > -spaceTop) && (tc.y < spaceBottom))\n ) {\n gl_FragColor = vec4(0,0,0,0);\n } else {\n if (shiftEnable > 0.0) {\n tc.x += (tc.x < 0.0)? spaceLeft: -spaceRight;\n tc.y += (tc.y < 0.0)? spaceTop: -spaceBottom;\n }\n\n tc = rotate(tc, angle);\n tc += split;\n gl_FragColor = texture2D(uMainSampler, tc / texSize);\n }\n\n}\n")}setupUniforms(t,e){const s=this.programManager;s.setUniform("split",[t.splitX,e.height-t.splitY]),s.setUniform("angle",t.rotation),s.setUniform("texSize",[e.width,e.height]),s.setUniform("spaceConfig",[t.spaceLeft,t.spaceRight,t.spaceTop,t.spaceBottom]),s.setUniform("shiftEnable",t.shiftEnable?1:0)}}const s=Phaser.Utils.Objects.GetValue,i=Phaser.Math.DegToRad,n=Phaser.Math.RadToDeg;class r extends Phaser.Filters.Controller{static FilterName=t;constructor(e,s){super(e,t),this.splitX=0,this.splitY=0,this.spaceLeft=0,this.spaceRight=0,this.spaceTop=0,this.spaceBottom=0,this.rotation=0,this.shiftEnable=!0,this.resetFromJSON(s)}resetFromJSON(t){var e=s(t,"width",void 0);void 0===e?(this.spaceLeft=s(t,"left",0),this.spaceRight=s(t,"right",0)):this.splittedWidth=e;var i=s(t,"height",void 0);void 0===i?(this.spaceTop=s(t,"top",0),this.spaceBottom=s(t,"bottom",0)):this.splittedHeight=i,this.splitX=s(t,"x",this.camera.centerX),this.splitY=s(t,"Y",this.camera.centerY);var n=s(t,"rotation",void 0);return void 0===n?this.setAngle(s(t,"angle",0)):this.setRotation(n),this.shiftEnable=s(t,"shiftEnable",!0),this}setSplit(t,e){return void 0===t&&(t=0),void 0===e&&(e=0),this.splitX=t,this.splitY=e,this}splitAtCenter(t,e){return this.setSplit(this.camera.centerX,this.camera.centerY),void 0!==t&&this.setSplittedWidth(t),void 0!==e&&this.setSplittedHeight(e),this}setRotation(t){return this.rotation=t,this}get angle(){return n(this.rotation)}set angle(t){this.rotation=i(t)}setAngle(t){return this.angle=t,this}setSpace(t,e,s,i){return void 0===t&&(t=0),void 0===e&&(e=0),void 0===s&&(s=0),void 0===i&&(i=0),this.spaceLeft=t,this.spaceRight=e,this.spaceTop=s,this.spaceBottom=i,this}get splittedWidth(){return this.spaceLeft+this.spaceRight}set splittedWidth(t){this.spaceLeft=t/2,this.spaceRight=this.spaceLeft}setSplittedWidth(t){return void 0===t&&(t=0),this.splittedWidth=t,this}get splittedHeight(){return this.spaceTop+this.spaceBottom}set splittedHeight(t){this.spaceTop=t/2,this.spaceBottom=this.spaceTop}setSplittedHeight(t){return void 0===t&&(t=0),this.splittedHeight=t,this}setShiftEnable(t){return void 0===t&&(t=!0),this.shiftEnable=t,!0}}const a=Phaser.Game;var o=function(t){return t instanceof a};const l=Phaser.Scene;var h=function(t){return t instanceof l},c=function(t,e){var s,i=e.FilterName,n=(s=t,null==s||"object"!=typeof s?null:o(s)?s:o(s.game)?s.game:h(s)?s.sys.game:h(s.scene)?s.scene.sys.game:void 0).renderer.renderNodes;return!n.hasNode(i)&&(n.addNodeConstructor(i,e),!0)},p=function(t,e){return void 0===e&&(e=!1),t.filters||t.enableFilters().focusFilters(),e?t.filters.external:t.filters.internal};const f=Phaser.Utils.Array.SpliceOne;class d extends Phaser.Plugins.BasePlugin{setFilterClass(t,e){return this.FilterClass=t,this.ControllerClass=e,this}setFilterListMethod(t,e){return function(t,e){var s=Phaser.GameObjects.Components.FilterList.prototype;s[t]||(s[t]=e)}(t,e),this}start(){var t=this.game.events;t.once("destroy",this.destroy,this),this.game.isRunning?c(this.game,this.FilterClass):t.once("ready",(function(){c(this.game,this.FilterClass)}),this)}add(t,e,s=!1){return function(t,e,s,i){void 0===s&&(s={});var n=p(t,i),r=n.add(new e(n.camera,s));return s.name&&(r.name=s.name),r}(t,this.ControllerClass,e,s)}remove(t,e,s=!1){return function(t,e,s,i){var n=p(t,i).list;if(void 0===s)for(var r=n.length-1;r>=0;r--)(o=n[r])instanceof e&&(o.destroy(),f(o,r));else{r=0;for(var a=n.length;r<a;r++){var o;(o=n[r])instanceof e&&o.name===s&&(o.destroy(),f(o,r))}}}(t,this.ControllerClass,e,s),this}get(t,e,s=!1){return function(t,e,s,i){var n=p(t,i).list;if(void 0===s){for(var r=[],a=0,o=n.length;a<o;a++)(l=n[a])instanceof e&&r.push(l);return r}for(a=0,o=n.length;a<o;a++){var l;if((l=n[a])instanceof e&&l.name===s)return l}}(t,this.ControllerClass,e,s)}}var u=function(t){return null==t||""===t||0===t.length},g=function(t,e,s,i){if(void 0===i&&(i="."),"object"==typeof t){if(u(e)){if(null==s)return;"object"==typeof s&&(t=s)}else{"string"==typeof e&&(e=e.split(i));var n=e.pop(),r=function(t,e,s){var i=t;if(u(e));else{var n;"string"==typeof e&&(e=e.split("."));for(var r=0,a=e.length;r<a;r++){var o;null!=i[n=e[r]]&&"object"==typeof i[n]||(o=r===a-1?void 0===s?{}:s:{},i[n]=o),i=i[n]}}return i}(t,e);r[n]=s}return t}};return g(window,"RexPlugins.Filters.SplitFilter",e),g(window,"RexPlugins.Filters.SplitController",r),class extends d{constructor(t){super(t),this.setFilterClass(e,r),this.setFilterListMethod("addRexSplit",(function(t){return this.add(new r(this.camera,t))}))}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexsplitfilterplugin=e();