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).rexdissolvepipelineplugin=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 o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,(r=o.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 o=n.call(e,t||"default");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===t?String:Number)(e)}(r,"string"))?i:String(i)),o);}var r,i;}function o(e,t,o){return t&&n(e.prototype,t),o&&n(e,o),Object.defineProperty(e,"prototype",{writable:!1}),e}function r(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 i(e){return i=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},i(e)}function s(e,t){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},s(e,t)}function f(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 a(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,o=i(e);if(t){var r=i(this).constructor;n=Reflect.construct(o,arguments,r);}else n=o.apply(this,arguments);return f(this,n)}}var u="#ifdef GL_FRAGMENT_PRECISION_HIGH\n#define highmedp highp\n#else\n#define highmedp mediump\n#endif\nprecision highmedp float;\n// Scene buffer\nuniform sampler2D uMainSampler;\nuniform sampler2D uMainSampler2;\n\nuniform int resizeMode;\nuniform float progress;\nuniform float fromRatio;\nuniform float toRatio;\nvarying vec2 outFragCoord;\n// Effect parameters\nuniform float noiseX;\nuniform float noiseY;\nuniform float noiseZ;\nuniform float fromEdgeStart;\nuniform float fromEdgeWidth;\nuniform float toEdgeStart;\nuniform float toEdgeWidth;\n\n".concat("vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }\nvec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }\nvec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }\nvec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }\nvec3 fade(vec3 t) { return t*t*t*(t*(t*6.0-15.0)+10.0); }\nfloat Perlin(vec3 P) {\n vec3 i0 = mod289(floor(P)), i1 = mod289(i0 + vec3(1.0));\n vec3 f0 = fract(P), f1 = f0 - vec3(1.0), f = fade(f0);\n vec4 ix = vec4(i0.x, i1.x, i0.x, i1.x), iy = vec4(i0.yy, i1.yy);\n vec4 iz0 = i0.zzzz, iz1 = i1.zzzz;\n vec4 ixy = permute(permute(ix) + iy), ixy0 = permute(ixy + iz0), ixy1 = permute(ixy + iz1);\n vec4 gx0 = ixy0 * (1.0 / 7.0), gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;\n vec4 gx1 = ixy1 * (1.0 / 7.0), gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;\n gx0 = fract(gx0); gx1 = fract(gx1);\n vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0), sz0 = step(gz0, vec4(0.0));\n vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1), sz1 = step(gz1, vec4(0.0));\n gx0 -= sz0 * (step(0.0, gx0) - 0.5); gy0 -= sz0 * (step(0.0, gy0) - 0.5);\n gx1 -= sz1 * (step(0.0, gx1) - 0.5); gy1 -= sz1 * (step(0.0, gy1) - 0.5);\n vec3 g0 = vec3(gx0.x,gy0.x,gz0.x), g1 = vec3(gx0.y,gy0.y,gz0.y),\n g2 = vec3(gx0.z,gy0.z,gz0.z), g3 = vec3(gx0.w,gy0.w,gz0.w),\n g4 = vec3(gx1.x,gy1.x,gz1.x), g5 = vec3(gx1.y,gy1.y,gz1.y),\n g6 = vec3(gx1.z,gy1.z,gz1.z), g7 = vec3(gx1.w,gy1.w,gz1.w);\n vec4 norm0 = taylorInvSqrt(vec4(dot(g0,g0), dot(g2,g2), dot(g1,g1), dot(g3,g3)));\n vec4 norm1 = taylorInvSqrt(vec4(dot(g4,g4), dot(g6,g6), dot(g5,g5), dot(g7,g7)));\n g0 *= norm0.x; g2 *= norm0.y; g1 *= norm0.z; g3 *= norm0.w;\n g4 *= norm1.x; g6 *= norm1.y; g5 *= norm1.z; g7 *= norm1.w;\n vec4 nz = mix(vec4(dot(g0, vec3(f0.x, f0.y, f0.z)), dot(g1, vec3(f1.x, f0.y, f0.z)),\n dot(g2, vec3(f0.x, f1.y, f0.z)), dot(g3, vec3(f1.x, f1.y, f0.z))),\n vec4(dot(g4, vec3(f0.x, f0.y, f1.z)), dot(g5, vec3(f1.x, f0.y, f1.z)),\n dot(g6, vec3(f0.x, f1.y, f1.z)), dot(g7, vec3(f1.x, f1.y, f1.z))), f.z);\n return 2.2 * mix(mix(nz.x,nz.z,f.y), mix(nz.y,nz.w,f.y), f.x);\n}\nfloat Perlin(vec2 P) { return Perlin(vec3(P, 0.0)); }\n","\n\nvec4 getFromColor (vec2 uv) {\n return texture2D(uMainSampler, uv);\n}\n\nvec4 getToColor (vec2 uv) {\n if (resizeMode == 2) {\n // cover\n return texture2D(uMainSampler2, 0.5 + (vec2(uv.x, 1.0 - uv.y) - 0.5) * vec2(min(fromRatio / toRatio, 1.0), min((toRatio / fromRatio), 1.0)));\n } else if (resizeMode == 1) {\n // contain\n return texture2D(uMainSampler2, 0.5 + (vec2(uv.x, 1.0 - uv.y) - 0.5) * vec2(max(fromRatio / toRatio, 1.0), max((toRatio / fromRatio), 1.0)));\n } else {\n // stretch\n return texture2D(uMainSampler2, vec2(uv.x, 1.0 - uv.y));\n }\n}\n\nvec4 transition (vec2 uv) { \n vec4 colorFront = getFromColor(uv);\n vec4 colorTo = getToColor(uv);\n\n float noise = (Perlin(vec3(uv.x * noiseX, uv.y * noiseY, noiseZ)) + 1.0) / 2.0\n * (1.0 - (fromEdgeStart + fromEdgeWidth + toEdgeStart + toEdgeWidth))\n + (fromEdgeStart + fromEdgeWidth + toEdgeStart + toEdgeWidth) * 0.5;\n vec4 colorResult = colorFront * smoothstep(progress - (fromEdgeStart + fromEdgeWidth), progress - fromEdgeStart, noise)\n + colorTo * smoothstep((1.0 - progress) - (toEdgeStart + toEdgeWidth), (1.0 - progress) - toEdgeStart, (1.0 - noise));\n return colorResult;\n}\n\nvoid main () {\n vec2 uv = outFragCoord;\n gl_FragColor = transition(uv);\n}\n"),c=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,g=Phaser.Utils.Objects.GetValue,l=Phaser.Math.Clamp,v=function(e){r(i,e);var n=a(i);function i(e){var o;return t(this,i),(o=n.call(this,{name:"rexDissolvePostFx",game:e,renderTarget:!0,fragShader:u}))._progress=0,o.toFrame=null,o.targetTexture=null,o.resizeMode=1,o.toRatio=1,o.noiseX=0,o.noiseY=0,o.noiseZ=0,o.fromEdgeStart=.01,o.fromEdgeWidth=.05,o.toEdgeStart=.01,o.toEdgeWidth=.05,o}return o(i,[{key:"resetFromJSON",value:function(e){return this.setProgress(g(e,"progress",0)),this.setTransitionTargetTexture(g(e,"toTexture","__DEFAULT"),g(e,"toFrame",void 0),g(e,"resizeMode",1)),this.setNoise(g(e,"noiseX",void 0),g(e,"noiseY",void 0),g(e,"noiseZ",void 0)),this.setFromEdge(g(e,"fromEdgeStart",.01),g(e,"fromEdgeWidth",.05)),this.setToEdge(g(e,"toEdgeStart",.01),g(e,"toEdgeWidth",.05)),this}},{key:"onBoot",value:function(){this.setTransitionTargetTexture();}},{key:"onPreRender",value:function(){this.set1f("progress",this.progress),this.set1i("resizeMode",this.resizeMode),this.set1f("noiseX",this.noiseX),this.set1f("noiseY",this.noiseY),this.set1f("noiseZ",this.noiseZ),this.set1f("fromEdgeStart",this.fromEdgeStart),this.set1f("fromEdgeWidth",this.fromEdgeWidth),this.set1f("toEdgeStart",this.toEdgeStart),this.set1f("toEdgeWidth",this.toEdgeWidth);}},{key:"onDraw",value:function(e){this.set1f("fromRatio",e.width/e.height),this.bindTexture(this.targetTexture,1),this.bindAndDraw(e);}},{key:"progress",get:function(){return this._progress},set:function(e){this._progress=l(e,0,1);}},{key:"setProgress",value:function(e){return this.progress=e,this}},{key:"setTransitionTargetTexture",value:function(e,t,n){void 0===e&&(e="__DEFAULT");var o=this.game.textures.getFrame(e,t);return o||(o=this.game.textures.getFrame("__DEFAULT")),this.toRatio=o.width/o.height,this.toFrame=o,this.targetTexture=o.glTexture,void 0!==n&&(this.resizeMode=n),this.set1i("uMainSampler2",1),this.set1f("toRatio",this.toRatio),this}},{key:"setResizeMode",value:function(e){return "string"==typeof e&&(e=d[e]),this.resizeMode=e,this}},{key:"setNoise",value:function(e,t,n){return void 0===e&&(e=4+6*Math.random()),void 0===t&&(t=4+6*Math.random()),void 0===n&&(n=10*Math.random()),this.noiseX=e,this.noiseY=t,this.noiseZ=n,this}},{key:"setFromEdge",value:function(e,t){return this.fromEdgeStart=e,this.fromEdgeWidth=t,this}},{key:"setToEdge",value:function(e,t){return this.toEdgeStart=e,this.toEdgeWidth=t,this}}]),i}(c),d={stretch:0,contain:1,cover:2},h=Phaser.Game,m=function(e){return e instanceof h},p=Phaser.Scene,y=function(e){return e instanceof p},x=function(t,n,o){var r;(r=t,null==r||"object"!==e(r)?null:m(r)?r:m(r.game)?r.game:y(r)?r.sys.game:y(r.scene)?r.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(n,o);},P=Phaser.Utils.Array.SpliceOne,z=function(e){r(i,Phaser.Plugins.BasePlugin);var n=a(i);function i(){return t(this,i),n.apply(this,arguments)}return o(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),x(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 o=e.postPipelines[e.postPipelines.length-1];return o.resetFromJSON(n),n.name&&(o.name=n.name),o}(e,this.PostFxPipelineClass,t)}},{key:"remove",value:function(e,t){return function(e,t,n){if(void 0===n)for(var o=(r=e.postPipelines).length-1;o>=0;o--)(s=r[o])instanceof t&&(s.destroy(),P(r,o));else {o=0;for(var r,i=(r=e.postPipelines).length;o<i;o++){var s;(s=r[o])instanceof t&&s.name===n&&(s.destroy(),P(r,o));}}}(e,this.PostFxPipelineClass,t),this}},{key:"get",value:function(e,t){return function(e,t,n){if(void 0===n){for(var o=[],r=0,i=(s=e.postPipelines).length;r<i;r++)(f=s[r])instanceof t&&o.push(f);return o}var s;for(r=0,i=(s=e.postPipelines).length;r<i;r++){var f;if((f=s[r])instanceof t&&f.name===n)return f}}(e,this.PostFxPipelineClass,t)}}]),i}(),E=function(e){return null==e||""===e||0===e.length},b=function(e){r(i,e);var n=a(i);function i(e){var o;return t(this,i),(o=n.call(this,e)).setPostPipelineClass(v,"rexDissolvePostFx"),o}return o(i)}(z);return function(t,n,o,r){if(void 0===r&&(r="."),"object"===e(t)){if(E(n)){if(null==o)return;"object"===e(o)&&(t=o);}else {"string"==typeof n&&(n=n.split(r));var i=n.pop(),s=function(t,n,o){var r=t;if(E(n));else {var i;"string"==typeof n&&(n=n.split("."));for(var s=0,f=n.length;s<f;s++){var a;null!=r[i=n[s]]&&"object"===e(r[i])||(a=s===f-1?void 0===o?{}:o:{},r[i]=a),r=r[i];}}return r}(t,n);s[i]=o;}}}(window,"RexPlugins.Pipelines.DissolvePostFx",v),b}));