@petkoneo/phaser3-rex-plugins
Version:
2 lines (1 loc) • 7.24 kB
JavaScript
var e,t;e=void 0,t=function(){var e=function(e,t,s){void 0===s&&(s=[]),s.length=t;for(var i=t;i>0;i--)s[i]=e*(i/t);return s};class t{constructor(e){this.postFXPipeline=e,this.shader=void 0}setShader(e){return this.shader=e,this}getAnotherFrame(e){var t=this.postFXPipeline,s=t.fullFrame1,i=t.fullFrame2;return e===s?i:s}init(e,t){var s=this.postFXPipeline;return void 0===t&&(t=s.fullFrame1),s.copyFrame(e,t),t}draw(e,t){}}class s extends t{draw(e,t){var s,i=this.postFXPipeline,r=this.shader,n=e,o=this.getAnotherFrame(n),a=i.distance/i.renderer.width*Math.cos(i.rotation),l=i.distance/i.renderer.height*Math.sin(i.rotation);return i.set2f("uOffset",a,l,r),i.set3f("color",i._shadowColor.redGL,i._shadowColor.greenGL,i._shadowColor.blueGL,r),i.set1f("alpha",i.alpha,r),t?(i.bindAndDraw(n,o,!0,!0,r),s=o):i.bindAndDraw(n,null,!0,!0,r),s}}class i extends t{draw(e,t){for(var s,i,r,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++)r=(i=o._kernels[p]+.5)*u,n=i*d,o.set2f("uOffset",r,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),s=h):o.bindAndDraw(l,null,!0,!0,a);return s}}const r=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 r{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 s(this),this.kawaseBlurDrawer=new i(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 s=n(e,"blur",4);return"number"==typeof s?(this.setBlur(s),this.setQuality(n(e,"quality",3))):this.setKernela(s),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,s){var i;(i=e,null==i||"object"!=typeof i?null:p(i)?i:p(i.game)?i.game:c(i)?i.sys.game:c(i.scene)?i.scene.sys.game:void 0).renderer.pipelines.addPostPipeline(t,s)};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,s){void 0===s&&(s={}),e.setPostPipeline(t);var i=e.postPipelines[e.postPipelines.length-1];return i.resetFromJSON(s),s.name&&(i.name=s.name),i}(e,this.PostFxPipelineClass,t)}remove(e,t){return function(e,t,s){if(void 0===s)for(var i=(r=e.postPipelines).length-1;i>=0;i--)(o=r[i])instanceof t&&(o.destroy(),g(r,i));else{i=0;for(var r,n=(r=e.postPipelines).length;i<n;i++){var o;(o=r[i])instanceof t&&o.name===s&&(o.destroy(),g(r,i))}}e.hasPostPipeline=e.postPipelines.length>0}(e,this.PostFxPipelineClass,t),this}get(e,t){return function(e,t,s){if(void 0===s){for(var i=[],r=0,n=(o=e.postPipelines).length;r<n;r++)(a=o[r])instanceof t&&i.push(a);return i}var o;for(r=0,n=(o=e.postPipelines).length;r<n;r++){var a;if((a=o[r])instanceof t&&a.name===s)return a}}(e,this.PostFxPipelineClass,t)}}var v=function(e){return null==e||""===e||0===e.length};return function(e,t,s,i){if(void 0===i&&(i="."),"object"==typeof e)if(v(t)){if(null==s)return;"object"==typeof s&&(e=s)}else{"string"==typeof t&&(t=t.split(i));var r=t.pop(),n=function(e,t,s){var i=e;if(v(t));else{var r;"string"==typeof t&&(t=t.split("."));for(var n=0,o=t.length;n<o;n++){var a;null!=i[r=t[n]]&&"object"==typeof i[r]||(a=n===o-1?void 0===s?{}:s:{},i[r]=a),i=i[r]}}return i}(e,t);n[r]=s}}(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();