UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 12.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexhorrifipipelineplugin=e();}(undefined,(function(){function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,(o=i.key,r=void 0,"symbol"==typeof(r=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,e||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(o,"string"))?r:String(r)),i);}var o,r;}function i(t,e,i){return e&&n(t.prototype,e),i&&n(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function o(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&s(t,e);}function r(t){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},r(t)}function s(t,e){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},s(t,e)}function a(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function l(t){var e=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(t){return !1}}();return function(){var n,i=r(t);if(e){var o=r(this).constructor;n=Reflect.construct(i,arguments,o);}else n=i.apply(this,arguments);return a(this,n)}}var u={setBloomEnable:function(t){return void 0===t&&(t=!0),this.bloomEnable=t,this},setBloomRadius:function(t){return this.bloomRadius=t,this},setBloomIntensity:function(t){return this.bloomIntensity=t,this},setBloomThreshold:function(t){return this.bloomThreshold=t,this},setBloomTexelSize:function(t,e){return void 0===e&&(e=t),this.bloomTexelWidth=t,this.bloomTexelHeight=e,this}},h={setChromaticEnable:function(t){return void 0===t&&(t=!0),this.chromaticEnable=t,this},setChabIntensity:function(t){return this.chabIntensity=t,this}},c={setVignetteEnable:function(t){return void 0===t&&(t=!0),this.vignetteEnable=t,this},setVignetteStrength:function(t){return this.vignetteStrength=t,this},setVignetteIntensity:function(t){return this.vignetteIntensity=t,this}},f={setNoiseEnable:function(t){return void 0===t&&(t=!0),this.noiseEnable=t,this},setNoiseStrength:function(t){return this.noiseStrength=t,this},setNoiseSeed:function(t){return this.noiseSeed=t,this}},b={setVHSEnable:function(t){return void 0===t&&(t=!0),this.vhsEnable=t,this},setVhsStrength:function(t){return this.vhsStrength=t,this}},v={setScanlinesEnable:function(t){return void 0===t&&(t=!0),this.scanlinesEnable=t,this},setScanStrength:function(t){return this.scanStrength=t,this}},m={setCRTEnable:function(t){return void 0===t&&(t=!0),this.crtEnable=t,this},setCrtSize:function(t,e){return void 0===e&&(e=t),this.crtWidth=t,this.crtHeight=e,this}},g={setEnable:function(t){return void 0===t&&(t=!0),this.bloomEnable=t,this.chromaticEnable=t,this.vignetteEnable=t,this.noiseEnable=t,this.vhsEnable=t,this.scanlinesEnable=t,this.crtEnable=t,this}};Object.assign(g,u,h,c,f,b,v,m);var p=Phaser.Game,d=function(t){return t instanceof p},y=Phaser.Scene,S=function(t){return t instanceof y},E=function(e){return null==e||"object"!==t(e)?null:d(e)?e:d(e.game)?e.game:S(e)?e.sys.game:S(e.scene)?e.scene.sys.game:void 0},P=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,x=Phaser.Utils.Objects.GetValue,T=function(t){o(r,t);var n=l(r);function r(t){var i;return e(this,r),(i=n.call(this,{name:"rexHorrifiPostFx",game:t,renderTarget:!0,fragShader:"#ifdef GL_FRAGMENT_PRECISION_HIGH\n#define highmedp highp\n#else\n#define highmedp mediump\n#endif\nprecision highmedp float;\n\nuniform float time;\n\n// Scene buffer\nuniform sampler2D uMainSampler; \nvarying vec2 outTexCoord;\n\n// Effect parameters\n#define SAMPLES 32.\n\n// Bloom\nuniform float bloomEnable;\nuniform vec3 bloom;\nuniform vec2 bloomTexel;\n\n// Chromatic abberation\nuniform float chromaticEnable;\nuniform float chabIntensity;\n\n// Vignette\nuniform float vignetteEnable;\nuniform vec2 vignette;\n\n// Noise\nuniform float noiseEnable;\nuniform float noiseStrength;\nuniform float noiseSeed;\n\n// VHS\nuniform float vhsEnable;\nuniform float vhsStrength;\n\n// Scanlines\nuniform float scanlinesEnable;\nuniform float scanStrength;\n\n// CRT\nuniform float crtEnable;\nuniform vec2 crtSize;\n\n\n// Noise\nfloat noise(vec2 uv) {\n return fract(sin(uv.x*12.9898+uv.y*78.233)*437.585453*noiseSeed);\n}\n\n// VHS\nvec4 vhs(vec2 uv) {\n vec2 tcoord = uv;\n tcoord.x += sin(time*noise(uv));\n return texture2D( uMainSampler, tcoord)*vhsStrength;\t\n}\n\n// Vignette\nfloat vig(vec2 uv) {\n uv *= 1. - uv;\n return ( pow(uv.x*uv.y*vignette.x*10.,vignette.y) );\n}\n\n// Chromatic abberation\nvec3 chromatic(vec2 uv, float offset) {\n float r = texture2D( uMainSampler, vec2(uv.x+offset, uv.y)).r;\n float g = texture2D( uMainSampler, uv).g;\n float b = texture2D( uMainSampler, vec2(uv.x-offset, uv.y)).b;\n return vec3(r,g,b);\n}\n\n// Bloom\nvec4 blur(vec2 uv) {\n float total = 0.;\n float rad = 1.;\n mat2 ang = mat2(.73736882209777832,-.67549037933349609,.67549037933349609,.73736882209777832);\n vec2 point = normalize(fract(cos(uv*mat2(195,174,286,183))*742.)-.5)*(bloom.x/sqrt(SAMPLES));\n vec4 amount = vec4(0);\n\t\n for ( float i=0.; i<SAMPLES; i++ ) {\n point*=ang;\n rad+=1./rad;\n vec4 samp = texture2D(uMainSampler, uv + point * (rad-1.) * bloomTexel);\n \n float mul = 1.;\n float lum = ( samp.r+samp.g+samp.b )/3.;\n if ( lum < bloom.z ){ mul = 0.; }\n \n amount += samp*(1./rad)*mul;\n total+=(1./rad);\n }\n amount /= total;\n return amount*bloom.y;\n}\n\n// TV Curve\nvec2 crtCurve( vec2 uv ) {\n uv = uv*2.-1.;\n vec2 uvoff = abs(uv.xy) / crtSize;\n uv = uv + uv * uvoff * uvoff;\n uv = uv * .5 + .5;\n return uv;\n}\n\nvoid main() {\t\n vec2 mainUv = outTexCoord;\n\n // CRT\n if ( crtEnable > .5 ) {\n mainUv = crtCurve(outTexCoord);\n }\n\t\n // Base coloring\n vec4 color = texture2D( uMainSampler, mainUv);\n\t\n // Chromatic abberation\n if ( chromaticEnable > .5 ) {\n color.rgb *= chromatic(mainUv, chabIntensity * 0.01);\n }\n\t\n // Scanlines\n if ( scanlinesEnable > .5 ) {\n color.rgb *= (1.-scanStrength)+(sin(mainUv.y*1024.)*scanStrength);\n }\n\n // Bloom\n if ( bloomEnable > .5 ) {\n color.rgb += blur(mainUv).rgb;\n }\n\t\n // Noise\n if ( noiseEnable > .5 ) {\n color.rgb += noise(mainUv)*noiseStrength;\n }\n\t\n // VHS\n if ( vhsEnable > .5 ) {\n color += vhs(mainUv);\n }\n\t\n // Vignette\n if ( vignetteEnable > .5) {\n color.rgb *= vig(mainUv);\n }\n\t\n // Cutoff edges\n if ( crtEnable > .5) {\n if ( (mainUv.x < 0.)|| (mainUv.y < 0.) || (mainUv.x > 1.)|| (mainUv.y > 1.) ) {\n color.rgb *= 0.;\n }\n }\n\t\n gl_FragColor = color;\n}\n"})).now=0,i.bloomEnable=!1,i.bloomRadius=0,i.bloomIntensity=0,i.bloomThreshold=0,i.bloomTexelWidth=0,i.bloomTexelHeight=0,i.chromaticEnable=!1,i.chabIntensity=0,i.vignetteEnable=!1,i.vignetteStrength=0,i.vignetteIntensity=0,i.noiseEnable=!1,i.noiseStrength=0,i.noiseSeed=Math.random(),i.vhsEnable=!1,i.vhsStrength=0,i.scanlinesEnable=!1,i.scanStrength=0,i.crtEnable=!1,i.crtWidth=0,i.crtHeight=0,i}return i(r,[{key:"resetFromJSON",value:function(t){var e=x(t,"enable",!1);return this.setBloomEnable(x(t,"bloomEnable",e)),this.setBloomRadius(x(t,"bloomRadius",0)),this.setBloomIntensity(x(t,"bloomIntensity",0)),this.setBloomThreshold(x(t,"bloomThreshold",0)),this.setBloomTexelSize(x(t,"bloomTexelWidth",0),x(t,"bloomTexelHeight")),this.setChromaticEnable(x(t,"chromaticEnable",e)),this.setChabIntensity(x(t,"chabIntensity",0)),this.setVignetteEnable(x(t,"vignetteEnable",e)),this.setVignetteStrength(x(t,"vignetteStrength",0)),this.setVignetteIntensity(x(t,"vignetteIntensity",0)),this.setNoiseEnable(x(t,"noiseEnable",e)),this.setNoiseStrength(x(t,"noiseStrength",0)),this.setNoiseSeed(x(0,"noiseSeed",Math.random())),this.setVHSEnable(x(t,"vhsEnable",e)),this.setVhsStrength(x(t,"vhsStrength",0)),this.setScanlinesEnable(x(t,"scanlinesEnable",e)),this.setScanStrength(x(t,"scanStrength",0)),this.setCRTEnable(x(t,"crtEnable",e)),this.setCrtSize(x(t,"crtWidth",0),x(t,"crtHeight",void 0)),this}},{key:"onPreRender",value:function(){var t;this.set1f("noiseSeed",this.noiseSeed),this.set1f("bloomEnable",this.bloomEnable?1:0),this.set3f("bloom",this.bloomRadius,this.bloomIntensity,this.bloomThreshold),this.set2f("bloomTexel",this.bloomTexelWidth,this.bloomTexelHeight),this.set1f("chromaticEnable",this.chromaticEnable?1:0),this.set1f("chabIntensity",this.chabIntensity),this.set1f("vignetteEnable",this.vignetteEnable?1:0),this.set2f("vignette",this.vignetteStrength,this.vignetteIntensity),this.set1f("noiseEnable",this.noiseEnable?1:0),this.set1f("noiseStrength",this.noiseStrength),this.set1f("vhsEnable",this.vhsEnable?1:0),this.set1f("vhsStrength",this.vhsStrength),this.set1f("scanlinesEnable",this.scanlinesEnable?1:0),this.set1f("scanStrength",this.scanStrength),this.set1f("crtEnable",this.crtEnable?1:0),this.set2f("crtSize",this.crtWidth,this.crtHeight),this.vhsEnable&&(this.now+=(t=this.game,E(t).loop.delta)),this.set1f("time",this.now);}}]),r}(P);Object.assign(T.prototype,g);var C=Phaser.Utils.Array.SpliceOne,I=function(t){o(r,Phaser.Plugins.BasePlugin);var n=l(r);function r(){return e(this,r),n.apply(this,arguments)}return i(r,[{key:"setPostPipelineClass",value:function(t,e){return this.PostFxPipelineClass=t,this.postFxPipelineName=e,this}},{key:"start",value:function(){var t,e,n;this.game.events.once("destroy",this.destroy,this),t=this.game,e=this.postFxPipelineName,n=this.PostFxPipelineClass,E(t).renderer.pipelines.addPostPipeline(e,n);}},{key:"add",value:function(t,e){return function(t,e,n){void 0===n&&(n={}),t.setPostPipeline(e);var i=t.postPipelines[t.postPipelines.length-1];return i.resetFromJSON(n),n.name&&(i.name=n.name),i}(t,this.PostFxPipelineClass,e)}},{key:"remove",value:function(t,e){return function(t,e,n){if(void 0===n)for(var i=(o=t.postPipelines).length-1;i>=0;i--)(s=o[i])instanceof e&&(s.destroy(),C(o,i));else {i=0;for(var o,r=(o=t.postPipelines).length;i<r;i++){var s;(s=o[i])instanceof e&&s.name===n&&(s.destroy(),C(o,i));}}}(t,this.PostFxPipelineClass,e),this}},{key:"get",value:function(t,e){return function(t,e,n){if(void 0===n){for(var i=[],o=0,r=(s=t.postPipelines).length;o<r;o++)(a=s[o])instanceof e&&i.push(a);return i}var s;for(o=0,r=(s=t.postPipelines).length;o<r;o++){var a;if((a=s[o])instanceof e&&a.name===n)return a}}(t,this.PostFxPipelineClass,e)}}]),r}(),O=function(t){return null==t||""===t||0===t.length},j=function(t){o(r,t);var n=l(r);function r(t){var i;return e(this,r),(i=n.call(this,t)).setPostPipelineClass(T,"rexHorrifiPostFx"),i}return i(r)}(I);return function(e,n,i,o){if(void 0===o&&(o="."),"object"===t(e)){if(O(n)){if(null==i)return;"object"===t(i)&&(e=i);}else {"string"==typeof n&&(n=n.split(o));var r=n.pop(),s=function(e,n,i){var o=e;if(O(n));else {var r;"string"==typeof n&&(n=n.split("."));for(var s=0,a=n.length;s<a;s++){var l;null!=o[r=n[s]]&&"object"===t(o[r])||(l=s===a-1?void 0===i?{}:i:{},o[r]=l),o=o[r];}}return o}(e,n);s[r]=i;}}}(window,"RexPlugins.Pipelines.HorrifiPostFx",T),j}));