UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 7.91 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).rexhsladjustpipelineplugin=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 r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(o=r.key,i=void 0,"symbol"==typeof(i=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(o,"string"))?i:String(i)),r);}var o,i;}function r(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),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 i(t){return i=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},i(t)}function s(t,e){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},s(t,e)}function l(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 f(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,r=i(t);if(e){var o=i(this).constructor;n=Reflect.construct(r,arguments,o);}else n=r.apply(this,arguments);return l(this,n)}}var u=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,a=Phaser.Utils.Objects.GetValue,c=function(t){o(i,t);var n=f(i);function i(t){var r;return e(this,i),(r=n.call(this,{name:"rexHslAdjustPostFx",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\n// Scene buffer\nuniform sampler2D uMainSampler; \nvarying vec2 outTexCoord;\n\n// Effect parameters\nuniform float hueRotate;\nuniform float satAdjust;\nuniform float lumAdjust;\nvec3 RGBToHSL(vec3 color) {\n vec3 hsl = vec3(0.0, 0.0, 0.0);\n\t\n float fmin = min(min(color.r, color.g), color.b);\n float fmax = max(max(color.r, color.g), color.b);\n float delta = fmax - fmin;\n\n hsl.z = (fmax + fmin) / 2.0;\n\n if (delta == 0.0) {\n\t\thsl.x = 0.0;\n\t\thsl.y = 0.0;\n\t} else {\n\t\tif (hsl.z < 0.5) {\n\t\t\thsl.y = delta / (fmax + fmin);\n } else {\n hsl.y = delta / (2.0 - fmax - fmin);\n }\n\t\t\n\t\tfloat dR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;\n\t\tfloat dG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;\n\t\tfloat dB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;\n\n\t\tif (color.r == fmax) {\n\t\t\thsl.x = dB - dG;\n } else if (color.g == fmax) {\n\t\t\thsl.x = (1.0 / 3.0) + dR - dB;\n\t\t} else if (color.b == fmax) {\n hsl.x = (2.0 / 3.0) + dG - dR;\n }\n\n\t\tif (hsl.x < 0.0) {\n\t\t\thsl.x += 1.0;\n } else if (hsl.x > 1.0) {\n hsl.x -= 1.0;\n }\n\t}\n\n\treturn hsl;\n}\nfloat HUEToRGB(float f1, float f2, float hue) {\n if (hue < 0.0) {\n hue += 1.0;\n } else if (hue > 1.0) {\n hue -= 1.0;\n }\n \t\n float ret;\n\t\n\tif ((6.0 * hue) < 1.0) {\n\t\tret = f1 + (f2 - f1) * 6.0 * hue;\n } else if ((2.0 * hue) < 1.0) {\n\t\tret = f2;\n\t} else if ((3.0 * hue) < 2.0) {\n\t\tret = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;\n } else {\n ret = f1;\n }\n\t\n return ret;\n}\nvec3 HSLToRGB(vec3 hsl) {\n\tvec3 rgb = vec3(hsl.z);\n\t\n\tif (hsl.y != 0.0) {\n\t\tfloat f2;\n\t\t\n\t\tif (hsl.z < 0.5) {\n\t\t f2 = hsl.z * (1.0 + hsl.y);\n } else {\n f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);\n }\n\t\t\t\n\t\tfloat f1 = 2.0 * hsl.z - f2;\n\t\t\n\t\trgb.r = HUEToRGB(f1, f2, hsl.x + (1.0 / 3.0));\n\t\trgb.g = HUEToRGB(f1, f2, hsl.x);\n\t\trgb.b = HUEToRGB(f1, f2, hsl.x - (1.0 / 3.0));\n }\n \n return rgb;\n}\nvoid main(void) {\n\tvec4 front = texture2D(uMainSampler, outTexCoord);\n\tvec3 hsl = RGBToHSL(front.rgb);\n\thsl.x -= hueRotate;\n\thsl.y *= satAdjust;\n\thsl.z += (lumAdjust - 0.5) * front.a;\n\tvec3 rgb = HSLToRGB(hsl);\n\tgl_FragColor = vec4(rgb, front.a);\n}\n"})).hueRotate=0,r.satAdjust=1,r.lumAdjust=.5,r}return r(i,[{key:"resetFromJSON",value:function(t){return this.setHueRotate(a(t,"hueRotate",0)),this.setSatAdjust(a(t,"satAdjust",1)),this.setLumAdjust(a(t,"lumAdjust",.5)),this}},{key:"onPreRender",value:function(){this.set1f("hueRotate",this.hueRotate%1),this.set1f("satAdjust",this.satAdjust),this.set1f("lumAdjust",this.lumAdjust);}},{key:"setHueRotate",value:function(t){return this.hueRotate=t,this}},{key:"setSatAdjust",value:function(t){return this.satAdjust=t,this}},{key:"setLumAdjust",value:function(t){return this.lumAdjust=t,this}}]),i}(u),h=Phaser.Game,p=function(t){return t instanceof h},d=Phaser.Scene,m=function(t){return t instanceof d},y=function(e,n,r){var o;(o=e,null==o||"object"!==t(o)?null:p(o)?o:p(o.game)?o.game:m(o)?o.sys.game:m(o.scene)?o.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(n,r);},v=Phaser.Utils.Array.SpliceOne,b=function(t){o(i,Phaser.Plugins.BasePlugin);var n=f(i);function i(){return e(this,i),n.apply(this,arguments)}return r(i,[{key:"setPostPipelineClass",value:function(t,e){return this.PostFxPipelineClass=t,this.postFxPipelineName=e,this}},{key:"start",value:function(){this.game.events.once("destroy",this.destroy,this),y(this.game,this.postFxPipelineName,this.PostFxPipelineClass);}},{key:"add",value:function(t,e){return function(t,e,n){void 0===n&&(n={}),t.setPostPipeline(e);var r=t.postPipelines[t.postPipelines.length-1];return r.resetFromJSON(n),n.name&&(r.name=n.name),r}(t,this.PostFxPipelineClass,e)}},{key:"remove",value:function(t,e){return function(t,e,n){if(void 0===n)for(var r=(o=t.postPipelines).length-1;r>=0;r--)(s=o[r])instanceof e&&(s.destroy(),v(o,r));else {r=0;for(var o,i=(o=t.postPipelines).length;r<i;r++){var s;(s=o[r])instanceof e&&s.name===n&&(s.destroy(),v(o,r));}}}(t,this.PostFxPipelineClass,e),this}},{key:"get",value:function(t,e){return function(t,e,n){if(void 0===n){for(var r=[],o=0,i=(s=t.postPipelines).length;o<i;o++)(l=s[o])instanceof e&&r.push(l);return r}var s;for(o=0,i=(s=t.postPipelines).length;o<i;o++){var l;if((l=s[o])instanceof e&&l.name===n)return l}}(t,this.PostFxPipelineClass,e)}}]),i}(),P=function(t){return null==t||""===t||0===t.length},g=function(t){o(i,t);var n=f(i);function i(t){var r;return e(this,i),(r=n.call(this,t)).setPostPipelineClass(c,"rexHslAdjustPostFx"),r}return r(i)}(b);return function(e,n,r,o){if(void 0===o&&(o="."),"object"===t(e)){if(P(n)){if(null==r)return;"object"===t(r)&&(e=r);}else {"string"==typeof n&&(n=n.split(o));var i=n.pop(),s=function(e,n,r){var o=e;if(P(n));else {var i;"string"==typeof n&&(n=n.split("."));for(var s=0,l=n.length;s<l;s++){var f;null!=o[i=n[s]]&&"object"===t(o[i])||(f=s===l-1?void 0===r?{}:r:{},o[i]=f),o=o[i];}}return o}(e,n);s[i]=r;}}}(window,"RexPlugins.Pipelines.HslAdjustPostFx",c),g}));