phaser3-rex-plugins
Version:
2 lines (1 loc) • 7.28 kB
JavaScript
var e,t;e=void 0,t=function(){var e=function(e,t,i){void 0===i&&(i=[]),i.length=t;for(var r=t;r>0;r--)i[r]=e*(r/t);return i};class t{constructor(e){this.postFXPipeline=e,this.shader=void 0}setShader(e){return this.shader=e,this}getAnotherFrame(e){var t=this.postFXPipeline,i=t.fullFrame1,r=t.fullFrame2;return e===i?r:i}init(e,t){var i=this.postFXPipeline;return void 0===t&&(t=i.fullFrame1),i.copyFrame(e,t),t}draw(e,t){}}class i extends t{draw(e,t){var i,r=this.postFXPipeline,s=this.shader,n=e,o=this.getAnotherFrame(n),a=r.distance/r.renderer.width*Math.cos(r.rotation),l=r.distance/r.renderer.height*Math.sin(r.rotation);return r.set2f("uOffset",a,l,s),r.set3f("color",r._shadowColor.redGL,r._shadowColor.greenGL,r._shadowColor.blueGL,s),r.set1f("alpha",r.alpha,s),t?(r.bindAndDraw(n,o,!0,!0,s),i=o):r.bindAndDraw(n,null,!0,!0,s),i}}class r extends t{draw(e,t){for(var i,r,s,n,o=this.postFXPipeline,a=this.shader,l=e,h=this.getAnotherFrame(l),u=o.pixelWidth/o.renderer.width,d=o.pixelHeight/o.renderer.height,p=0,f=o._quality-1;p<=f;p++)s=(r=o._kernels[p]+.5)*u,n=r*d,o.set2f("uOffset",s,n,a),p<f?(o.bindAndDraw(l,h,!0,!0,a),l=h,h=this.getAnotherFrame(l)):t?(o.bindAndDraw(l,h,!0,!0,a),i=h):o.bindAndDraw(l,null,!0,!0,a);return i}}const s=Phaser.Renderer.WebGL.Pipelines.PostFXPipeline,n=Phaser.Utils.Objects.GetValue,o=Phaser.Math.DegToRad,a=Phaser.Math.RadToDeg,l=Phaser.Display.Color.IntegerToRGB,h=Phaser.Display.Color;class u extends s{constructor(e){super({name:"rexDropShadowPostFx",game:e,shaders:[{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 alpha;\nuniform vec3 color;\nuniform vec2 uOffset;\n\nvoid main (void) {\n vec4 sample = texture2D(uMainSampler, outTexCoord - uOffset);\n\n // Premultiply alpha\n sample.rgb = color.rgb * sample.a;\n\n // alpha user alpha\n sample *= alpha;\n\n gl_FragColor = sample;\n}\n"},{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 vec2 uOffset;\n\nvoid main (void) {\n vec4 color = vec4(0.0);\n\n // Sample top left pixel\n color += texture2D(uMainSampler, vec2(outTexCoord.x - uOffset.x, outTexCoord.y + uOffset.y));\n\n // Sample top right pixel\n color += texture2D(uMainSampler, vec2(outTexCoord.x + uOffset.x, outTexCoord.y + uOffset.y));\n\n // Sample bottom right pixel\n color += texture2D(uMainSampler, vec2(outTexCoord.x + uOffset.x, outTexCoord.y - uOffset.y));\n\n // Sample bottom left pixel\n color += texture2D(uMainSampler, vec2(outTexCoord.x - uOffset.x, outTexCoord.y - uOffset.y));\n\n // Average\n color *= 0.25;\n\n gl_FragColor = color;\n}\n"}]}),this.shadowDrawer=new i(this),this.kawaseBlurDrawer=new r(this),this.rotation=0,this.distance=0,this._shadowColor=new h,this.alpha=.5,this.shadowOnly=!1,this._kernels=[0],this._blur=0,this._quality=1,this.pixelWidth=1,this.pixelHeight=1}resetFromJSON(e){var t=n(e,"rotation",void 0);void 0===t?this.setAngle(n(e,"angle",45)):this.setRotation(t),this.setDistance(n(e,"distance",5)),this.setShadowColor(n(e,"shadowColor",16777215)),this.setAlpha(n(e,"alpha",.5)),this.setShadowOnly(n(e,"shadowOnly",!1));var i=n(e,"blur",4);return"number"==typeof i?(this.setBlur(i),this.setQuality(n(e,"quality",3))):this.setKernela(i),this.setPixelSize(n(e,"pixelWidth",1),n(e,"pixelHeight",1)),this}onPreRender(){}onDraw(e){var t;t=this.shadowDrawer.setShader(this.shaders[0]).draw(this.shadowDrawer.init(e),!0),t=this.kawaseBlurDrawer.setShader(this.shaders[1]).draw(t,!0),this.shadowOnly||this.copyFrame(e,t,1,!1),this.copyToGame(t)}setRotation(e){return this.rotation=e,this}get angle(){return a(this.rotation)}set angle(e){this.rotation=o(e)}setAngle(e){return this.angle=e,this}setDistance(e){return this.distance=e,this}get shadowColor(){return this._shadowColor}set shadowColor(e){"number"==typeof e&&(e=l(e)),this._shadowColor.setFromRGB(e)}setShadowColor(e){return this.shadowColor=e,this}setAlpha(e){return this.alpha=e,this}setShadowOnly(e){return void 0===e&&(e=!0),this.shadowOnly=e,this}get blur(){return this._blur}set blur(t){this._blur!==t&&(this._blur=t,e(this._blur,this._quality,this._kernels))}setBlur(e){return this.blur=e,this}get quality(){return this._quality}set quality(t){this._quality!==t&&(this._quality=t,e(this._blur,this._quality,this._kernels))}setQuality(e){return this.quality=e,this}get kernels(){return this._kernels}set kernels(e){void 0===e&&(e=[0]),this._kernels=e,this._quality=e.length,this._blur=Math.max(...e)}setKernela(e){return this.kernels=e,this}setPixelWidth(e){return this.pixelWidth=e,this}setPixelHeight(e){return this.pixelHeight=e,this}setPixelSize(e,t){return void 0===t&&(t=e),this.pixelWidth=e,this.pixelHeight=t,this}}const d=Phaser.Game;var p=function(e){return e instanceof d};const f=Phaser.Scene;var c=function(e){return e instanceof f},m=function(e,t,i){var r;(r=e,null==r||"object"!=typeof r?null:p(r)?r:p(r.game)?r.game:c(r)?r.sys.game:c(r.scene)?r.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(t,i)};const g=Phaser.Utils.Array.SpliceOne;class x extends Phaser.Plugins.BasePlugin{setPostPipelineClass(e,t){return this.PostFxPipelineClass=e,this.postFxPipelineName=t,this}start(){this.game.events.once("destroy",this.destroy,this),m(this.game,this.postFxPipelineName,this.PostFxPipelineClass)}add(e,t){return function(e,t,i){void 0===i&&(i={}),e.setPostPipeline(t);var r=e.postPipelines[e.postPipelines.length-1];return r.resetFromJSON(i),i.name&&(r.name=i.name),r}(e,this.PostFxPipelineClass,t)}remove(e,t){return function(e,t,i){if(void 0===i)for(var r=(s=e.postPipelines).length-1;r>=0;r--)(o=s[r])instanceof t&&(o.destroy(),g(s,r));else{r=0;for(var s,n=(s=e.postPipelines).length;r<n;r++){var o;(o=s[r])instanceof t&&o.name===i&&(o.destroy(),g(s,r))}}e.hasPostPipeline=e.postPipelines.length>0}(e,this.PostFxPipelineClass,t),this}get(e,t){return function(e,t,i){if(void 0===i){for(var r=[],s=0,n=(o=e.postPipelines).length;s<n;s++)(a=o[s])instanceof t&&r.push(a);return r}var o;for(s=0,n=(o=e.postPipelines).length;s<n;s++){var a;if((a=o[s])instanceof t&&a.name===i)return a}}(e,this.PostFxPipelineClass,t)}}var P=function(e){return null!==e&&"object"==typeof e};return function(e,t,i,r="."){if(!P(e))return e;if(function(e){return null==e}(t)||""===t||Array.isArray(t)&&0===t.length)return e;if("string"==typeof t&&-1===t.indexOf(r)||"number"==typeof t)return e[t]=i,e;var s=function(e,t){return Array.isArray(e)||(e="string"!=typeof e||""===e.trim()?[]:e.split(t).filter(Boolean)),e}(t,r);if(0===s.length)return e;for(var n=e,o=s.length,a=0;a<o-1;a++){var l=s[a],h=n[l];P(h)||(n[l]={}),n=n[l]}n[s[o-1]]=i}(window,"RexPlugins.Pipelines.DropShadowPostFx",u),class extends x{constructor(e){super(e),this.setPostPipelineClass(u,"rexDropShadowPostFx")}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexdropshadowpipelineplugin=t();