@petkoneo/phaser3-rex-plugins
Version:
2 lines (1 loc) • 4.05 kB
JavaScript
var e,o;e=void 0,o=function(){const e=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,o=Phaser.Utils.Objects.GetValue,n=Phaser.Display.Color.IntegerToRGB,r=Phaser.Display.Color;class i extends e{constructor(e){super({name:"rexColorReplacePostFx",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;\n\n// Effect parameters\nuniform float epsilon;\nuniform vec3 originalColor;\nuniform vec3 newColor;\n\nvoid main (void) {\n vec4 currentColor = texture2D(uMainSampler, outTexCoord);\n vec3 colorDiff = originalColor - (currentColor.rgb / max(currentColor.a, 0.0000000001));\n float colorDistance = length(colorDiff);\n float doReplace = step(colorDistance, epsilon);\n gl_FragColor = vec4(mix(currentColor.rgb, (newColor + colorDiff) * currentColor.a, doReplace), currentColor.a);\n}\n"}),this.epsilon=.4,this._originalColor=new r,this._newColor=new r}resetFromJSON(e){return this.setEpsilon(o(e,"epsilon",.4)),this.setOriginalColor(o(e,"originalColor",16711680)),this.setNewColor(o(e,"newColor",0)),this}onPreRender(){this.set1f("epsilon",this.epsilon),this.set3f("originalColor",this._originalColor.redGL,this._originalColor.greenGL,this._originalColor.blueGL),this.set3f("newColor",this._newColor.redGL,this._newColor.greenGL,this._newColor.blueGL)}setEpsilon(e){return this.epsilon=e,this}get originalColor(){return this._originalColor}set originalColor(e){"number"==typeof e&&(e=n(e)),this._originalColor.setFromRGB(e)}setOriginalColor(e){return this.originalColor=e,this}get newColor(){return this._newColor}set newColor(e){"number"==typeof e&&(e=n(e)),this._newColor.setFromRGB(e)}setNewColor(e){return this.newColor=e,this}}const t=Phaser.Game;var s=function(e){return e instanceof t};const l=Phaser.Scene;var a=function(e){return e instanceof l},p=function(e,o,n){var r;(r=e,null==r||"object"!=typeof r?null:s(r)?r:s(r.game)?r.game:a(r)?r.sys.game:a(r.scene)?r.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(o,n)};const f=Phaser.Utils.Array.SpliceOne;class h extends Phaser.Plugins.BasePlugin{setPostPipelineClass(e,o){return this.PostFxPipelineClass=e,this.postFxPipelineName=o,this}start(){this.game.events.once("destroy",this.destroy,this),p(this.game,this.postFxPipelineName,this.PostFxPipelineClass)}add(e,o){return function(e,o,n){void 0===n&&(n={}),e.setPostPipeline(o);var r=e.postPipelines[e.postPipelines.length-1];return r.resetFromJSON(n),n.name&&(r.name=n.name),r}(e,this.PostFxPipelineClass,o)}remove(e,o){return function(e,o,n){if(void 0===n)for(var r=(i=e.postPipelines).length-1;r>=0;r--)(s=i[r])instanceof o&&(s.destroy(),f(i,r));else{r=0;for(var i,t=(i=e.postPipelines).length;r<t;r++){var s;(s=i[r])instanceof o&&s.name===n&&(s.destroy(),f(i,r))}}e.hasPostPipeline=e.postPipelines.length>0}(e,this.PostFxPipelineClass,o),this}get(e,o){return function(e,o,n){if(void 0===n){for(var r=[],i=0,t=(s=e.postPipelines).length;i<t;i++)(l=s[i])instanceof o&&r.push(l);return r}var s;for(i=0,t=(s=e.postPipelines).length;i<t;i++){var l;if((l=s[i])instanceof o&&l.name===n)return l}}(e,this.PostFxPipelineClass,o)}}var u=function(e){return null==e||""===e||0===e.length};return function(e,o,n,r){if(void 0===r&&(r="."),"object"==typeof e)if(u(o)){if(null==n)return;"object"==typeof n&&(e=n)}else{"string"==typeof o&&(o=o.split(r));var i=o.pop(),t=function(e,o,n){var r=e;if(u(o));else{var i;"string"==typeof o&&(o=o.split("."));for(var t=0,s=o.length;t<s;t++){var l;null!=r[i=o[t]]&&"object"==typeof r[i]||(l=t===s-1?void 0===n?{}:n:{},r[i]=l),r=r[i]}}return r}(e,o);t[i]=n}}(window,"RexPlugins.Pipelines.ColorReplacePostFx",i),class extends h{constructor(e){super(e),this.setPostPipelineClass(i,"rexColorReplacePostFx")}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e="undefined"!=typeof globalThis?globalThis:e||self).rexcolorreplacepipelineplugin=o();