phaser3-rex-plugins
Version:
2 lines (1 loc) • 6.66 kB
JavaScript
!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).rexcolorreplacepipelineplugin=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 o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,(r=o.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 o=n.call(e,t||"default");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===t?String:Number)(e)}(r,"string"))?i:String(i)),o);}var r,i;}function o(e,t,o){return t&&n(e.prototype,t),o&&n(e,o),Object.defineProperty(e,"prototype",{writable:!1}),e}function r(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&&l(e,t);}function i(e){return i=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},i(e)}function l(e,t){return l=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},l(e,t)}function s(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 u(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,o=i(e);if(t){var r=i(this).constructor;n=Reflect.construct(o,arguments,r);}else n=o.apply(this,arguments);return s(this,n)}}var a=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,f=Phaser.Utils.Objects.GetValue,c=Phaser.Display.Color.IntegerToRGB,p=Phaser.Display.Color,h=function(e){r(i,e);var n=u(i);function i(e){var o;return t(this,i),(o=n.call(this,{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"})).epsilon=.4,o._originalColor=new p,o._newColor=new p,o}return o(i,[{key:"resetFromJSON",value:function(e){return this.setEpsilon(f(e,"epsilon",.4)),this.setOriginalColor(f(e,"originalColor",16711680)),this.setNewColor(f(e,"newColor",0)),this}},{key:"onPreRender",value:function(){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);}},{key:"setEpsilon",value:function(e){return this.epsilon=e,this}},{key:"originalColor",get:function(){return this._originalColor},set:function(e){"number"==typeof e&&(e=c(e)),this._originalColor.setFromRGB(e);}},{key:"setOriginalColor",value:function(e){return this.originalColor=e,this}},{key:"newColor",get:function(){return this._newColor},set:function(e){"number"==typeof e&&(e=c(e)),this._newColor.setFromRGB(e);}},{key:"setNewColor",value:function(e){return this.newColor=e,this}}]),i}(a),y=Phaser.Game,v=function(e){return e instanceof y},d=Phaser.Scene,g=function(e){return e instanceof d},m=function(t,n,o){var r;(r=t,null==r||"object"!==e(r)?null:v(r)?r:v(r.game)?r.game:g(r)?r.sys.game:g(r.scene)?r.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(n,o);},P=Phaser.Utils.Array.SpliceOne,b=function(e){r(i,Phaser.Plugins.BasePlugin);var n=u(i);function i(){return t(this,i),n.apply(this,arguments)}return o(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),m(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 o=e.postPipelines[e.postPipelines.length-1];return o.resetFromJSON(n),n.name&&(o.name=n.name),o}(e,this.PostFxPipelineClass,t)}},{key:"remove",value:function(e,t){return function(e,t,n){if(void 0===n)for(var o=(r=e.postPipelines).length-1;o>=0;o--)(l=r[o])instanceof t&&(l.destroy(),P(r,o));else {o=0;for(var r,i=(r=e.postPipelines).length;o<i;o++){var l;(l=r[o])instanceof t&&l.name===n&&(l.destroy(),P(r,o));}}}(e,this.PostFxPipelineClass,t),this}},{key:"get",value:function(e,t){return function(e,t,n){if(void 0===n){for(var o=[],r=0,i=(l=e.postPipelines).length;r<i;r++)(s=l[r])instanceof t&&o.push(s);return o}var l;for(r=0,i=(l=e.postPipelines).length;r<i;r++){var s;if((s=l[r])instanceof t&&s.name===n)return s}}(e,this.PostFxPipelineClass,t)}}]),i}(),C=function(e){return null==e||""===e||0===e.length},w=function(e){r(i,e);var n=u(i);function i(e){var o;return t(this,i),(o=n.call(this,e)).setPostPipelineClass(h,"rexColorReplacePostFx"),o}return o(i)}(b);return function(t,n,o,r){if(void 0===r&&(r="."),"object"===e(t)){if(C(n)){if(null==o)return;"object"===e(o)&&(t=o);}else {"string"==typeof n&&(n=n.split(r));var i=n.pop(),l=function(t,n,o){var r=t;if(C(n));else {var i;"string"==typeof n&&(n=n.split("."));for(var l=0,s=n.length;l<s;l++){var u;null!=r[i=n[l]]&&"object"===e(r[i])||(u=l===s-1?void 0===o?{}:o:{},r[i]=u),r=r[i];}}return r}(t,n);l[i]=o;}}}(window,"RexPlugins.Pipelines.ColorReplacePostFx",h),w}));