UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 10.9 kB
!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).rextoonifypipelineplugin=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 r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,(o=r.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 r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===t?String:Number)(e)}(o,"string"))?i:String(i)),r);}var o,i;}function r(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function o(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 l(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,r=i(e);if(t){var o=i(this).constructor;n=Reflect.construct(r,arguments,o);}else n=r.apply(this,arguments);return l(this,n)}}var f=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,u=Phaser.Utils.Objects.GetValue,c=Phaser.Display.Color.IntegerToRGB,v=Phaser.Display.Color,p=function(e){o(i,e);var n=a(i);function i(e){var r;return t(this,i),(r=n.call(this,{name:"rexToonifyPostFx",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\n// Scene buffer\nuniform sampler2D uMainSampler; \nvarying vec2 outTexCoord;\nuniform vec2 texSize;\n\n// Effect parameters\nuniform float edgeThreshold; // 0.2;\nuniform float hStep; // 60\nuniform float sStep; // 0.15\nuniform float vStep; // 0.33\nuniform vec3 edgeColor; // (0, 0, 0);\nvec3 RGBToHSV(vec3 color) {\n float minv, maxv, delta;\n vec3 res;\n minv = min(min(color.r, color.g), color.b);\n maxv = max(max(color.r, color.g), color.b);\n res.z = maxv; // v\n\n delta = maxv - minv;\n if( maxv != 0.0 ) {\n res.y = delta / maxv; // s\n } else {\n // s = 0, v is undefined\n res.y = 0.0;\n res.x = -1.0;\n return res;\n }\n\n if( color.r == maxv ) {\n res.x = ( color.g - color.b ) / delta; // between yellow & magenta\n } else if( color.g == maxv ) {\n res.x = 2.0 + ( color.b - color.r ) / delta; // between cyan & yellow\n } else {\n res.x = 4.0 + ( color.r - color.g ) / delta; // between magenta & cyan\n }\n\n res.x = res.x * 60.0; // degrees\n if( res.x < 0.0 ) {\n res.x = res.x + 360.0;\n }\n \n return res;\n}\nfloat AvgRGB(vec4 color) {\n return (color.r + color.g + color.b)/3.0;\n}\n#define EDGEGAIN 5.0\nbool IsEdge(vec2 coords, vec2 texSize, float threshold) {\n if (threshold > 1.0) {\n return false;\n }\n\n vec2 tc = coords * texSize;\n \n float pixel[9];\n int k = 0;\n float delta;\n\n // read neighboring pixel intensities\n pixel[0] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float(-1), float(-1))) / texSize ) );\n pixel[1] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float(-1), float( 0))) / texSize ) );\n pixel[2] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float(-1), float( 1))) / texSize ) );\n pixel[3] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float( 0), float(-1))) / texSize ) );\n pixel[4] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float( 0), float( 0))) / texSize ) );\n pixel[5] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float( 0), float( 1))) / texSize ) );\n pixel[6] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float( 1), float(-1))) / texSize ) );\n pixel[7] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float( 1), float( 0))) / texSize ) );\n pixel[8] = AvgRGB( texture2D( uMainSampler, (tc + vec2(float( 1), float( 1))) / texSize ) );\n\n // average color differences around neighboring pixels\n delta = (abs(pixel[1]-pixel[7])+\n abs(pixel[5]-pixel[3]) +\n abs(pixel[0]-pixel[8])+\n abs(pixel[2]-pixel[6])\n )/4.0;\n\n return (clamp(delta*EDGEGAIN, 0.0, 1.0) >= threshold);\n}\nvec3 HSVToRGB(float h, float s, float v) {\n int i;\n float f, p, q, t;\n vec3 res;\n if( s == 0.0 ) {\n // achromatic (grey)\n res.x = v;\n res.y = v;\n res.z = v;\n return res;\n }\n\n h /= 60.0; // sector 0 to 5\n i = int(floor( h ));\n f = h - float(i); // factorial part of h\n p = v * ( 1.0 - s );\n q = v * ( 1.0 - s * f );\n t = v * ( 1.0 - s * ( 1.0 - f ) ); \n if (i == 0) {\n res.x = v;\n res.y = t;\n res.z = p;\n } else if (i == 1) {\n res.x = q;\n res.y = v;\n res.z = p;\n } else if (i == 2) {\n res.x = p;\n res.y = v;\n res.z = t;\n } else if (i == 3) {\n res.x = p;\n res.y = q;\n res.z = v;\n } else if (i == 4) {\n res.x = t;\n res.y = p;\n res.z = v;\n } else { // i == 5\n res.x = v;\n res.y = p;\n res.z = q;\n }\n return res;\n}\n\nvoid main() {\n vec4 front = texture2D(uMainSampler, outTexCoord); \n vec3 colorLevel;\n if ((hStep > 0.0) || (sStep > 0.0) || (vStep > 0.0)) {\n vec3 colorHsv = RGBToHSV(front.rgb); \n if (hStep > 0.0) {\n colorHsv.x = min(floor((colorHsv.x / hStep) + 0.5) * hStep, 360.0);\n }\n if (sStep > 0.0) {\n colorHsv.y = min(floor((colorHsv.y / sStep) + 0.5) * sStep, 1.0);\n }\n if (vStep > 0.0) {\n colorHsv.z = min(floor((colorHsv.z / vStep) + 0.5) * vStep, 1.0);\n }\n colorLevel = HSVToRGB(colorHsv.x, colorHsv.y, colorHsv.z);\n } else {\n colorLevel = front.rgb;\n }\n\n vec3 outColor = (IsEdge(outTexCoord, texSize, edgeThreshold))? edgeColor : colorLevel;\n gl_FragColor = vec4(outColor, front.a);\n}\n"})).edgeThreshold=0,r.hueLevels=0,r._satLevels=0,r._valLevels=0,r._edgeColor=new v,r}return r(i,[{key:"resetFromJSON",value:function(e){return this.setEdgeThreshold(u(e,"edgeThreshold",.2)),this.setHueLevels(u(e,"hueLevels",0)),this.setSatLevels(u(e,"satLevels",0)),this.setValLevels(u(e,"valLevels",0)),this.setEdgeColor(u(e,"edgeColor",0)),this}},{key:"onPreRender",value:function(){this.set1f("edgeThreshold",this.edgeThreshold),this.set1f("hStep",this.hueStep),this.set1f("sStep",this.satStep),this.set1f("vStep",this.valStep),this.set3f("edgeColor",this._edgeColor.redGL,this._edgeColor.greenGL,this._edgeColor.blueGL),this.set2f("texSize",this.renderer.width,this.renderer.height);}},{key:"setEdgeThreshold",value:function(e){return this.edgeThreshold=e,this}},{key:"setHueLevels",value:function(e){return this.hueLevels=e,this}},{key:"hueStep",get:function(){return this.hueLevels>0?360/this.hueLevels:0}},{key:"satLevels",get:function(){return this._satLevels},set:function(e){this._satLevels=e;}},{key:"setSatLevels",value:function(e){return this.satLevels=e,this}},{key:"satStep",get:function(){return this._satLevels>0?1/this._satLevels:0}},{key:"valLevels",get:function(){return this._valLevels},set:function(e){this._valLevels=e;}},{key:"setValLevels",value:function(e){return this.valLevels=e,this}},{key:"valStep",get:function(){return this._valLevels>0?1/this._valLevels:0}},{key:"edgeColor",get:function(){return this._edgeColor},set:function(e){"number"==typeof e&&(e=c(e)),this._edgeColor.setFromRGB(e);}},{key:"setEdgeColor",value:function(e){return this.edgeColor=e,this}}]),i}(f),h=Phaser.Game,d=function(e){return e instanceof h},y=Phaser.Scene,g=function(e){return e instanceof y},x=function(t,n,r){var o;(o=t,null==o||"object"!==e(o)?null:d(o)?o:d(o.game)?o.game:g(o)?o.sys.game:g(o.scene)?o.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(n,r);},m=Phaser.Utils.Array.SpliceOne,S=function(e){o(i,Phaser.Plugins.BasePlugin);var n=a(i);function i(){return t(this,i),n.apply(this,arguments)}return r(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 r=e.postPipelines[e.postPipelines.length-1];return r.resetFromJSON(n),n.name&&(r.name=n.name),r}(e,this.PostFxPipelineClass,t)}},{key:"remove",value:function(e,t){return function(e,t,n){if(void 0===n)for(var r=(o=e.postPipelines).length-1;r>=0;r--)(s=o[r])instanceof t&&(s.destroy(),m(o,r));else {r=0;for(var o,i=(o=e.postPipelines).length;r<i;r++){var s;(s=o[r])instanceof t&&s.name===n&&(s.destroy(),m(o,r));}}}(e,this.PostFxPipelineClass,t),this}},{key:"get",value:function(e,t){return function(e,t,n){if(void 0===n){for(var r=[],o=0,i=(s=e.postPipelines).length;o<i;o++)(l=s[o])instanceof t&&r.push(l);return r}var s;for(o=0,i=(s=e.postPipelines).length;o<i;o++){var l;if((l=s[o])instanceof t&&l.name===n)return l}}(e,this.PostFxPipelineClass,t)}}]),i}(),b=function(e){return null==e||""===e||0===e.length},P=function(e){o(i,e);var n=a(i);function i(e){var r;return t(this,i),(r=n.call(this,e)).setPostPipelineClass(p,"rexToonifyPostFx"),r}return r(i)}(S);return function(t,n,r,o){if(void 0===o&&(o="."),"object"===e(t)){if(b(n)){if(null==r)return;"object"===e(r)&&(t=r);}else {"string"==typeof n&&(n=n.split(o));var i=n.pop(),s=function(t,n,r){var o=t;if(b(n));else {var i;"string"==typeof n&&(n=n.split("."));for(var s=0,l=n.length;s<l;s++){var a;null!=o[i=n[s]]&&"object"===e(o[i])||(a=s===l-1?void 0===r?{}:r:{},o[i]=a),o=o[i];}}return o}(t,n);s[i]=r;}}}(window,"RexPlugins.Pipelines.ToonifyPostFx",p),P}));