UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 5.92 kB
var t,e;t=void 0,e=function(){const t="rexFilterHslAdjust";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\n\nuniform vec3 hsvAdjust;\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}\n\n#pragma phaserTemplate(fragmentHeader)\n\nvoid main(void) {\n float hueRotate = hsvAdjust.x;\n float satAdjust = hsvAdjust.y;\n float lumAdjust = hsvAdjust.z;\n\n vec4 front = texture2D(uMainSampler, outTexCoord);\n vec3 hsl = RGBToHSL(front.rgb);\n hsl.x -= hueRotate;\n hsl.y *= satAdjust;\n hsl.z += (lumAdjust - 0.5) * front.a;\n vec3 rgb = HSLToRGB(hsl);\n gl_FragColor = vec4(rgb, front.a);\n}\n")}setupUniforms(t,e){this.programManager.setUniform("hsvAdjust",[t.hueRotate%1,t.satAdjust,t.lumAdjust])}}const n=Phaser.Utils.Objects.GetValue;class s extends Phaser.Filters.Controller{static FilterName=t;constructor(e,n){super(e,t),this.hueRotate=0,this.satAdjust=1,this.lumAdjust=.5,this.resetFromJSON(n)}resetFromJSON(t){return this.setHueRotate(n(t,"hueRotate",0)),this.setSatAdjust(n(t,"satAdjust",1)),this.setLumAdjust(n(t,"lumAdjust",.5)),this}setHueRotate(t){return this.hueRotate=t,this}setSatAdjust(t){return this.satAdjust=t,this}setLumAdjust(t){return this.lumAdjust=t,this}}const r=Phaser.Game;var l=function(t){return t instanceof r};const o=Phaser.Scene;var a=function(t){return t instanceof o},i=function(t,e){var n,s=e.FilterName,r=(n=t,null==n||"object"!=typeof n?null:l(n)?n:l(n.game)?n.game:a(n)?n.sys.game:a(n.scene)?n.scene.sys.game:void 0).renderer.renderNodes;return!r.hasNode(s)&&(r.addNodeConstructor(s,e),!0)},f=function(t,e){return void 0===e&&(e=!1),t.filters||t.enableFilters().focusFilters(),e?t.filters.external:t.filters.internal};const u=Phaser.Utils.Array.SpliceOne;class h extends Phaser.Plugins.BasePlugin{setFilterClass(t,e){return this.FilterClass=t,this.ControllerClass=e,this}setFilterListMethod(t,e){return function(t,e){var n=Phaser.GameObjects.Components.FilterList.prototype;n[t]||(n[t]=e)}(t,e),this}start(){var t=this.game.events;t.once("destroy",this.destroy,this),this.game.isRunning?i(this.game,this.FilterClass):t.once("ready",(function(){i(this.game,this.FilterClass)}),this)}add(t,e,n=!1){return function(t,e,n,s){void 0===n&&(n={});var r=f(t,s),l=r.add(new e(r.camera,n));return n.name&&(l.name=n.name),l}(t,this.ControllerClass,e,n)}remove(t,e,n=!1){return function(t,e,n,s){var r=f(t,s).list;if(void 0===n)for(var l=r.length-1;l>=0;l--)(a=r[l])instanceof e&&(a.destroy(),u(a,l));else{l=0;for(var o=r.length;l<o;l++){var a;(a=r[l])instanceof e&&a.name===n&&(a.destroy(),u(a,l))}}}(t,this.ControllerClass,e,n),this}get(t,e,n=!1){return function(t,e,n,s){var r=f(t,s).list;if(void 0===n){for(var l=[],o=0,a=r.length;o<a;o++)(i=r[o])instanceof e&&l.push(i);return l}for(o=0,a=r.length;o<a;o++){var i;if((i=r[o])instanceof e&&i.name===n)return i}}(t,this.ControllerClass,e,n)}}var d=function(t){return null==t||""===t||0===t.length},c=function(t,e,n,s){if(void 0===s&&(s="."),"object"==typeof t){if(d(e)){if(null==n)return;"object"==typeof n&&(t=n)}else{"string"==typeof e&&(e=e.split(s));var r=e.pop(),l=function(t,e,n){var s=t;if(d(e));else{var r;"string"==typeof e&&(e=e.split("."));for(var l=0,o=e.length;l<o;l++){var a;null!=s[r=e[l]]&&"object"==typeof s[r]||(a=l===o-1?void 0===n?{}:n:{},s[r]=a),s=s[r]}}return s}(t,e);l[r]=n}return t}};return c(window,"RexPlugins.Filters.HslAdjustFilter",e),c(window,"RexPlugins.Filters.HslAdjustController",s),class extends h{constructor(t){super(t),this.setFilterClass(e,s),this.setFilterListMethod("addRexHslAdjust",(function(t){return this.add(new s(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).rexhsladjustfilterplugin=e();