@petkoneo/phaser3-rex-plugins
Version:
2 lines (1 loc) • 8.51 kB
JavaScript
var e,t;e=void 0,t=function(){const e=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,t=Phaser.Utils.Objects.GetValue,o=Phaser.Math.Clamp;class i extends e{constructor(e){super({name:"rexDissolvePostFx",game:e,renderTarget:!0,fragShader:"#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\nvec3 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"}),this._progress=0,this.toFrame=null,this.targetTexture=null,this.resizeMode=1,this.toRatio=1,this.noiseX=0,this.noiseY=0,this.noiseZ=0,this.fromEdgeStart=.01,this.fromEdgeWidth=.05,this.toEdgeStart=.01,this.toEdgeWidth=.05}resetFromJSON(e){return this.setProgress(t(e,"progress",0)),this.setTransitionTargetTexture(t(e,"toTexture","__DEFAULT"),t(e,"toFrame",void 0),t(e,"resizeMode",1)),this.setNoise(t(e,"noiseX",void 0),t(e,"noiseY",void 0),t(e,"noiseZ",void 0)),this.setFromEdge(t(e,"fromEdgeStart",.01),t(e,"fromEdgeWidth",.05)),this.setToEdge(t(e,"toEdgeStart",.01),t(e,"toEdgeWidth",.05)),this}onBoot(){}onPreRender(){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)}onDraw(e){this.set1f("fromRatio",e.width/e.height),this.set1f("toRatio",this.toRatio),this.set1i("uMainSampler2",1),this.bindTexture(this.targetTexture,1),this.bindAndDraw(e)}get progress(){return this._progress}set progress(e){this._progress=o(e,0,1)}setProgress(e){return this.progress=e,this}setTransitionTargetTexture(e,t,o){void 0===e&&(e="__DEFAULT");var i=this.game.textures.getFrame(e,t);return i||(i=this.game.textures.getFrame("__DEFAULT")),this.toRatio=i.width/i.height,this.toFrame=i,this.targetTexture=i.glTexture,void 0!==o&&(this.resizeMode=o),this}setResizeMode(e){return"string"==typeof e&&(e=r[e]),this.resizeMode=e,this}setNoise(e,t,o){return void 0===e&&(e=4+6*Math.random()),void 0===t&&(t=4+6*Math.random()),void 0===o&&(o=10*Math.random()),this.noiseX=e,this.noiseY=t,this.noiseZ=o,this}setFromEdge(e,t){return this.fromEdgeStart=e,this.fromEdgeWidth=t,this}setToEdge(e,t){return this.toEdgeStart=e,this.toEdgeWidth=t,this}}var r={stretch:0,contain:1,cover:2};const n=Phaser.Game;var s=function(e){return e instanceof n};const g=Phaser.Scene;var a=function(e){return e instanceof g},f=function(e,t,o){var i;(i=e,null==i||"object"!=typeof i?null:s(i)?i:s(i.game)?i.game:a(i)?i.sys.game:a(i.scene)?i.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(t,o)};const d=Phaser.Utils.Array.SpliceOne;class h extends Phaser.Plugins.BasePlugin{setPostPipelineClass(e,t){return this.PostFxPipelineClass=e,this.postFxPipelineName=t,this}start(){this.game.events.once("destroy",this.destroy,this),f(this.game,this.postFxPipelineName,this.PostFxPipelineClass)}add(e,t){return function(e,t,o){void 0===o&&(o={}),e.setPostPipeline(t);var i=e.postPipelines[e.postPipelines.length-1];return i.resetFromJSON(o),o.name&&(i.name=o.name),i}(e,this.PostFxPipelineClass,t)}remove(e,t){return function(e,t,o){if(void 0===o)for(var i=(r=e.postPipelines).length-1;i>=0;i--)(s=r[i])instanceof t&&(s.destroy(),d(r,i));else{i=0;for(var r,n=(r=e.postPipelines).length;i<n;i++){var s;(s=r[i])instanceof t&&s.name===o&&(s.destroy(),d(r,i))}}e.hasPostPipeline=e.postPipelines.length>0}(e,this.PostFxPipelineClass,t),this}get(e,t){return function(e,t,o){if(void 0===o){for(var i=[],r=0,n=(s=e.postPipelines).length;r<n;r++)(g=s[r])instanceof t&&i.push(g);return i}var s;for(r=0,n=(s=e.postPipelines).length;r<n;r++){var g;if((g=s[r])instanceof t&&g.name===o)return g}}(e,this.PostFxPipelineClass,t)}}var v=function(e){return null==e||""===e||0===e.length};return function(e,t,o,i){if(void 0===i&&(i="."),"object"==typeof e)if(v(t)){if(null==o)return;"object"==typeof o&&(e=o)}else{"string"==typeof t&&(t=t.split(i));var r=t.pop(),n=function(e,t,o){var i=e;if(v(t));else{var r;"string"==typeof t&&(t=t.split("."));for(var n=0,s=t.length;n<s;n++){var g;null!=i[r=t[n]]&&"object"==typeof i[r]||(g=n===s-1?void 0===o?{}:o:{},i[r]=g),i=i[r]}}return i}(e,t);n[r]=o}}(window,"RexPlugins.Pipelines.DissolvePostFx",i),class extends h{constructor(e){super(e),this.setPostPipelineClass(i,"rexDissolvePostFx")}}},"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();