UNPKG

@petkoneo/phaser3-rex-plugins

Version:
2 lines (1 loc) 8.16 kB
var e,t;e=void 0,t=function(){const e=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,t=Phaser.Utils.Objects.GetValue,n=Phaser.Display.Color.IntegerToRGB,s=Phaser.Display.Color;class r extends e{constructor(e){super({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"}),this.edgeThreshold=0,this.hueLevels=0,this._satLevels=0,this._valLevels=0,this._edgeColor=new s}resetFromJSON(e){return this.setEdgeThreshold(t(e,"edgeThreshold",.2)),this.setHueLevels(t(e,"hueLevels",0)),this.setSatLevels(t(e,"satLevels",0)),this.setValLevels(t(e,"valLevels",0)),this.setEdgeColor(t(e,"edgeColor",0)),this}onPreRender(){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)}setEdgeThreshold(e){return this.edgeThreshold=e,this}setHueLevels(e){return this.hueLevels=e,this}get hueStep(){return this.hueLevels>0?360/this.hueLevels:0}get satLevels(){return this._satLevels}set satLevels(e){this._satLevels=e}setSatLevels(e){return this.satLevels=e,this}get satStep(){return this._satLevels>0?1/this._satLevels:0}get valLevels(){return this._valLevels}set valLevels(e){this._valLevels=e}setValLevels(e){return this.valLevels=e,this}get valStep(){return this._valLevels>0?1/this._valLevels:0}get edgeColor(){return this._edgeColor}set edgeColor(e){"number"==typeof e&&(e=n(e)),this._edgeColor.setFromRGB(e)}setEdgeColor(e){return this.edgeColor=e,this}}const o=Phaser.Game;var l=function(e){return e instanceof o};const i=Phaser.Scene;var a=function(e){return e instanceof i},v=function(e,t,n){var s;(s=e,null==s||"object"!=typeof s?null:l(s)?s:l(s.game)?s.game:a(s)?s.sys.game:a(s.scene)?s.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(t,n)};const f=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),v(this.game,this.postFxPipelineName,this.PostFxPipelineClass)}add(e,t){return function(e,t,n){void 0===n&&(n={}),e.setPostPipeline(t);var s=e.postPipelines[e.postPipelines.length-1];return s.resetFromJSON(n),n.name&&(s.name=n.name),s}(e,this.PostFxPipelineClass,t)}remove(e,t){return function(e,t,n){if(void 0===n)for(var s=(r=e.postPipelines).length-1;s>=0;s--)(l=r[s])instanceof t&&(l.destroy(),f(r,s));else{s=0;for(var r,o=(r=e.postPipelines).length;s<o;s++){var l;(l=r[s])instanceof t&&l.name===n&&(l.destroy(),f(r,s))}}e.hasPostPipeline=e.postPipelines.length>0}(e,this.PostFxPipelineClass,t),this}get(e,t){return function(e,t,n){if(void 0===n){for(var s=[],r=0,o=(l=e.postPipelines).length;r<o;r++)(i=l[r])instanceof t&&s.push(i);return s}var l;for(r=0,o=(l=e.postPipelines).length;r<o;r++){var i;if((i=l[r])instanceof t&&i.name===n)return i}}(e,this.PostFxPipelineClass,t)}}var p=function(e){return null==e||""===e||0===e.length};return function(e,t,n,s){if(void 0===s&&(s="."),"object"==typeof e)if(p(t)){if(null==n)return;"object"==typeof n&&(e=n)}else{"string"==typeof t&&(t=t.split(s));var r=t.pop(),o=function(e,t,n){var s=e;if(p(t));else{var r;"string"==typeof t&&(t=t.split("."));for(var o=0,l=t.length;o<l;o++){var i;null!=s[r=t[o]]&&"object"==typeof s[r]||(i=o===l-1?void 0===n?{}:n:{},s[r]=i),s=s[r]}}return s}(e,t);o[r]=n}}(window,"RexPlugins.Pipelines.ToonifyPostFx",r),class extends h{constructor(e){super(e),this.setPostPipelineClass(r,"rexToonifyPostFx")}}},"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();