UNPKG

phaser4-rex-plugins

Version:
1,666 lines (1,312 loc) 99.1 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.rexeffectpropertiesplugin = factory()); })(this, (function () { 'use strict'; var HasProperty = function (obj, prop) { if (!obj) { return false; } if (obj.hasOwnProperty(prop)) { return true; } while (obj) { if (Object.getOwnPropertyDescriptor(obj, prop)) { return true; } obj = obj.__proto__; } return false; }; var GetFilterList = function (gameObject, external) { if (external === undefined) { external = false; } if (!gameObject.filters) { gameObject.enableFilters().focusFilters(); } var filterList = (!external) ? gameObject.filters.internal : gameObject.filters.external; return filterList; }; var AddClearEffectCallback = function (gameObject, effectSwitchName) { if (!gameObject._effectSwitchNames) { gameObject._effectSwitchNames = []; gameObject.clearAllEffects = function () { var effectSwitchNames = gameObject._effectSwitchNames; for (var i = 0, cnt = effectSwitchNames.length; i < cnt; i++) { gameObject[effectSwitchNames[i]] = null; } return gameObject; }; gameObject.on('destroy', gameObject.clearAllEffects, gameObject); } gameObject._effectSwitchNames.push(effectSwitchName); }; var AddBarrelProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameObject, 'barrel')) { return gameObject; } var filterList = GetFilterList(gameObject); var barrel; Object.defineProperty(gameObject, 'barrel', { get: function () { return barrel; }, set: function (value) { if (barrel === value) { return; } barrel = value; if ((barrel === null) || (barrel === false)) { if (gameObject._barrelEffect) { filterList.remove(gameObject._barrelEffect); gameObject._barrelEffect = undefined; } } else { if (!gameObject._barrelEffect) { gameObject._barrelEffect = filterList.addBarrel(); } gameObject._barrelEffect.amount = barrel; } }, }); gameObject.barrel = null; AddClearEffectCallback(gameObject, 'barrel'); return gameObject; }; var AddColorMatrixEffectPropertiesBase = function (gameObject, effectName, inputMode) { // Don't attach properties again if (HasProperty(gameObject, effectName)) { return gameObject; } var filterList = GetFilterList(gameObject); var EffectInstancePropertyName = `_${effectName}Effect`; var currentValue; Object.defineProperty(gameObject, effectName, { get: function () { return currentValue; }, set: function (value) { if (currentValue === value) { return; } currentValue = value; if ((currentValue === null) || (currentValue === false)) { if (gameObject[EffectInstancePropertyName]) { filterList.remove(gameObject[EffectInstancePropertyName]); gameObject[EffectInstancePropertyName] = undefined; } } else { if (!gameObject[EffectInstancePropertyName]) { gameObject[EffectInstancePropertyName] = filterList.addColorMatrix(); } var effectInstance = gameObject[EffectInstancePropertyName]; effectInstance.colorMatrix[effectName]((inputMode === 1) ? value : undefined); } }, }); gameObject[effectName] = null; AddClearEffectCallback(gameObject, effectName); return gameObject; }; var AddBlackWhiteProperties = function (gameObject) { AddColorMatrixEffectPropertiesBase(gameObject, 'blackWhite'); return gameObject; }; var AddBlockyProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameObject, 'blockySize')) { return gameObject; } var filterList = GetFilterList(gameObject); var blockySize, blockyOffsetX = 0, blockyOffsetY = 0; Object.defineProperty(gameObject, 'blockySize', { get: function () { return blockySize; }, set: function (value) { if (blockySize === value) { return; } blockySize = value; if ((blockySize === null) || (blockySize === false)) { if (gameObject._blockyEffect) { filterList.remove(gameObject._blockyEffect); gameObject._blockyEffect = undefined; } } else { if (!gameObject._blockyEffect) { gameObject._blockyEffect = filterList.addBlocky({ size: blockySize, offset: { x: blockyOffsetX, y: blockyOffsetY }, }); } gameObject._blockyEffect.size.x = blockySize; gameObject._blockyEffect.size.y = blockySize; } }, }); Object.defineProperty(gameObject, 'blockyOffsetX', { get: function () { return blockyOffsetX; }, set: function (value) { if (blockyOffsetX === value) { return; } blockyOffsetX = value; if (gameObject._blockyEffect) { gameObject._blockyEffect.offset.x = blockyOffsetX; } }, }); Object.defineProperty(gameObject, 'blockyOffsetY', { get: function () { return blockyOffsetY; }, set: function (value) { if (blockyOffsetY === value) { return; } blockyOffsetY = value; if (gameObject._blockyEffect) { gameObject._blockyEffect.offset.y = blockyOffsetY; } }, }); AddClearEffectCallback(gameObject, 'blockySize'); return gameObject; }; const GameClass = Phaser.Game; var IsGame = function (object) { return (object instanceof GameClass); }; const SceneClass = Phaser.Scene; var IsSceneObject = function (object) { return (object instanceof SceneClass); }; var GetGame = function (object) { if ((object == null) || (typeof (object) !== 'object')) { return null; } else if (IsGame(object)) { return object; } else if (IsGame(object.game)) { return object.game; } else if (IsSceneObject(object)) { // object = scene object return object.sys.game; } else if (IsSceneObject(object.scene)) { // object = game object return object.scene.sys.game; } }; var RegisterFilter = function (game, FilterClass) { var filterName = FilterClass.FilterName; var renderNodes = GetGame(game).renderer.renderNodes; if (renderNodes.hasNode(filterName)) { return false; } renderNodes.addNodeConstructor(filterName, FilterClass); return true; }; var AddFilterListMethod = function (name, callback) { var FilterListComponent = Phaser.GameObjects.Components.FilterList.prototype; if (FilterListComponent[name]) { return; } FilterListComponent[name] = callback; }; const StepFilterName = 'FilterP3BloomStep'; // Built-in fx in phaser3 const frag$5 = `\ #pragma phaserTemplate(shaderName) #ifdef GL_FRAGMENT_PRECISION_HIGH #define highmedp highp #else #define highmedp mediump #endif precision highmedp float; // Scene buffer uniform sampler2D uMainSampler; varying vec2 outTexCoord; // Effect parameters uniform vec2 offset; uniform float strength; uniform vec3 color; #pragma phaserTemplate(fragmentHeader) void main (void) { vec4 sum = texture2D(uMainSampler, outTexCoord) * 0.204164 * strength; sum = sum + texture2D(uMainSampler, outTexCoord + offset * 1.407333) * 0.304005; sum = sum + texture2D(uMainSampler, outTexCoord - offset * 1.407333) * 0.304005; sum = sum + texture2D(uMainSampler, outTexCoord + offset * 3.294215) * 0.093913; sum = sum + texture2D(uMainSampler, outTexCoord - offset * 3.294215) * 0.093913; gl_FragColor = sum * vec4(color, 1); } `; class BloomStepFilter extends Phaser.Renderer.WebGL.RenderNodes.BaseFilterShader { static FilterName = StepFilterName; constructor(manager) { super(StepFilterName, manager, null, frag$5); } // This method sets up the uniforms for the shader. setupUniforms(controller, drawingContext) { const programManager = this.programManager; var x = (2 / drawingContext.width) * controller.offsetX; var y = (2 / drawingContext.height) * controller.offsetY; programManager.setUniform('offset', [x, y]); programManager.setUniform('strength', controller.strength); programManager.setUniform('color', controller.glcolor); } } const GetValue$6 = Phaser.Utils.Objects.GetValue; class BloomStepController extends Phaser.Filters.Controller { static FilterName = StepFilterName; constructor(camera, config) { super(camera, StepFilterName); this.offsetX = 1; this.offsetY = 1; this.strength = 1; this.glcolor = [1, 1, 1]; this.resetFromJSON(config); } resetFromJSON(o) { this.setOffset(GetValue$6(o, 'offsetX', 1), GetValue$6(o, 'offsetY', 1)); this.setStrength(GetValue$6(o, 'strength', 1)); this.setColor(GetValue$6(o, 'color', 0xFFFFFF)); return this; } get color() { var color = this.glcolor; return (((color[0] * 255) << 16) + ((color[1] * 255) << 8) + (color[2] * 255 | 0)); } set color(value) { var color = this.glcolor; color[0] = ((value >> 16) & 0xFF) / 255; color[1] = ((value >> 8) & 0xFF) / 255; color[2] = (value & 0xFF) / 255; } setOffset(x, y) { this.offsetX = x; this.offsetY = y; return this; } setStrength(strength) { this.strength = strength; return this; } setColor(color) { this.color = color; return this; } } const GetValue$5 = Phaser.Utils.Objects.GetValue; let BloomController$1 = class BloomController extends Phaser.Filters.ParallelFilters { constructor(camera, config) { super(camera); this.steps = 0; this.offsetX = 1; this.offsetY = 1; this.blurStrength = 1; this.color = 0xffffff; this.strength = 1; this.resetFromJSON(config); } resetFromJSON(o) { this.setOffset(GetValue$5(o, 'offsetX', 1), GetValue$5(o, 'offsetY', 1)); this.setBlurStrength(GetValue$5(o, 'blurStrength', 1)); this.setColor(GetValue$5(o, 'color', 0xFFFFFF)); this.setStrength(GetValue$5(o, 'strength', 1)); this.setSteps(GetValue$5(o, 'steps', 4)); return this; } forEachController(callback, scope) { this.top.list.forEach(callback, scope); } get steps() { return this._steps; } set steps(value) { if (this._steps === value) { return; } var camera = this.camera; if (this.steps < value) { var filters = this.top; var startIndex = this.steps * 2; var stopIndex = value * 2; for (var i = startIndex; i < stopIndex; i++) { filters.add(new BloomStepController(camera)); } } else { // this.steps > value var filtersList = this.top.list; var startIndex = this.steps * 2; var stopIndex = value * 2; for (var i = startIndex - 1; i >= stopIndex; i--) { filtersList[i].destroy(); } filtersList.length = stopIndex; } this._steps = value; this.setOffset(this.offsetX, this.offsetY); this.setBlurStrength(this.strength); this.setColor(this.color); } setSteps(steps) { this.steps = steps; return this; } get offsetX() { return this._offsetX; } set offsetX(value) { this._offsetX = value; this.forEachController(function (bloomStepController, i) { bloomStepController.offsetX = (i % 2 === 0) ? value : 0; }); } get offsetY() { return this._offsetY; } set offsetY(value) { this._offsetY = value; this.forEachController(function (bloomStepController, i) { bloomStepController.offsetY = (i % 2 === 1) ? value : 0; }); } setOffset(x, y) { this.offsetX = x; this.offsetY = y; return this; } get blurStrength() { return this._blurStrength; } set blurStrength(value) { this._blurStrength = value; this.forEachController(function (bloomStepController) { bloomStepController.strength = value; }); } setBlurStrength(blurStrength) { this.blurStrength = blurStrength; return this; } get color() { return this._color; } set color(value) { this._color = value; this.forEachController(function (bloomStepController) { bloomStepController.color = value; }); } setColor(color) { this.color = color; return this; } get strength() { return this._strength; } set strength(value) { this._strength = value; this.blend.amount = value; } setStrength(strength) { this.strength = strength; return this; } }; var InstallBloomFX = function (game) { game = GetGame(game); var success = RegisterFilter(game, BloomStepFilter); if (!success) { return false; } AddFilterListMethod( 'addP3Bloom', function (color, offsetX, offsetY, blurStrength, strength, steps) { if (color === undefined) { color = 0xFFFFFF; } if (offsetX === undefined) { offsetX = 1; } if (offsetY === undefined) { offsetY = 1; } if (blurStrength === undefined) { blurStrength = 1; } if (strength === undefined) { strength = 1; } if (steps === undefined) { steps = 4; } return this.add(new BloomController$1( this.camera, { color, offsetX, offsetY, blurStrength, strength, steps } )); } ); return true; }; var AddBloomProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameObject, 'bloomColor')) { return gameObject; } InstallBloomFX(gameObject); var filterList = GetFilterList(gameObject); var bloomColor, bloomOffsetX = 1, bloomOffsetY = 1, bloomBlurStrength = 1, bloomStrength = 1, bloomSteps = 4; Object.defineProperty(gameObject, 'bloomColor', { get: function () { return bloomColor; }, set: function (value) { if (bloomColor === value) { return; } bloomColor = value; if ((bloomColor === null) || (bloomColor === false)) { if (gameObject._bloom) { filterList.remove(gameObject._bloom); gameObject._bloom = undefined; } } else { if (!gameObject._bloom) { gameObject._bloom = filterList.addBloom(bloomColor, bloomOffsetX, bloomOffsetY, bloomBlurStrength, bloomStrength, bloomSteps); } gameObject._bloom.color = bloomColor; } }, }); Object.defineProperty(gameObject, 'bloomOffsetX', { get: function () { return bloomOffsetX; }, set: function (value) { if (bloomOffsetX === value) { return; } bloomOffsetX = value; if (gameObject._bloom) { gameObject._bloom.offsetX = bloomOffsetX; } }, }); Object.defineProperty(gameObject, 'bloomOffsetY', { get: function () { return bloomOffsetY; }, set: function (value) { if (bloomOffsetY === value) { return; } bloomOffsetY = value; if (gameObject._bloom) { gameObject._bloom.offsetY = bloomOffsetY; } }, }); Object.defineProperty(gameObject, 'bloomBlurStrength', { get: function () { return bloomBlurStrength; }, set: function (value) { if (bloomBlurStrength === value) { return; } bloomBlurStrength = value; if (gameObject._bloom) { gameObject._bloom.blurStrength = bloomBlurStrength; } }, }); Object.defineProperty(gameObject, 'bloomStrength', { get: function () { return bloomStrength; }, set: function (value) { if (bloomStrength === value) { return; } bloomStrength = value; if (gameObject._bloom) { gameObject._bloom.strength = bloomStrength; } }, }); Object.defineProperty(gameObject, 'bloomSteps', { get: function () { return bloomSteps; }, set: function (value) { if (bloomSteps === value) { return; } bloomSteps = value; if (gameObject._bloom) { gameObject._bloom.steps = bloomSteps; } }, }); gameObject.bloomColor = null; AddClearEffectCallback(gameObject, 'bloomColor'); return gameObject; }; var AddBlurProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameObject, 'blurColor')) { return gameObject; } var filterList = GetFilterList(gameObject); var blurColor, blurQuality = 0, blurX = 1, blurY = 1, blurStrength = 1, blurSteps = 4; Object.defineProperty(gameObject, 'blurColor', { get: function () { return blurColor; }, set: function (value) { if (blurColor === value) { return; } blurColor = value; if ((blurColor === null) || (blurColor === false)) { if (gameObject._blur) { filterList.remove(gameObject._blur); gameObject._blur = undefined; } } else { if (!gameObject._blur) { gameObject._blur = filterList.addBlur(blurQuality, blurX, blurY, blurStrength, blurColor, blurSteps); } gameObject._blur.color = blurColor; } }, }); Object.defineProperty(gameObject, 'blurQuality', { get: function () { return blurQuality; }, set: function (value) { if (blurQuality === value) { return; } blurQuality = value; if (gameObject._blur) { gameObject._blur.quality = blurQuality; } }, }); Object.defineProperty(gameObject, 'blurX', { get: function () { return blurX; }, set: function (value) { if (blurX === value) { return; } blurX = value; if (gameObject._blur) { gameObject._blur.x = blurX; } }, }); Object.defineProperty(gameObject, 'blurY', { get: function () { return blurY; }, set: function (value) { if (blurY === value) { return; } blurY = value; if (gameObject._blur) { gameObject._blur.y = blurY; } }, }); Object.defineProperty(gameObject, 'blurStrength', { get: function () { return blurStrength; }, set: function (value) { if (blurStrength === value) { return; } blurStrength = value; if (gameObject._blur) { gameObject._blur.strength = blurStrength; } }, }); Object.defineProperty(gameObject, 'blurSteps', { get: function () { return blurSteps; }, set: function (value) { if (blurSteps === value) { return; } blurSteps = value; if (gameObject._blur) { gameObject._blur.steps = blurSteps; } }, }); gameObject.blurColor = null; AddClearEffectCallback(gameObject, 'blurColor'); return gameObject; }; var AddBokehProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameObject, 'bokehRadius')) { return gameObject; } var filterList = GetFilterList(gameObject); var bokehRadius, bokehAmount = 1, bokehContrast = 0.2; Object.defineProperty(gameObject, 'bokehRadius', { get: function () { return bokehRadius; }, set: function (value) { if (bokehRadius === value) { return; } bokehRadius = value; if ((bokehRadius === null) || (bokehRadius === false)) { if (gameObject._bokeh) { filterList.remove(gameObject._bokeh); gameObject._bokeh = undefined; } } else { if (!gameObject._bokeh) { gameObject._bokeh = filterList.addBokeh(bokehRadius, bokehAmount, bokehContrast); } gameObject._bokeh.radius = bokehRadius; } }, }); Object.defineProperty(gameObject, 'bokehAmount', { get: function () { return bokehAmount; }, set: function (value) { if (bokehAmount === value) { return; } bokehAmount = value; if (gameObject._bokeh) { gameObject._bokeh.amount = bokehAmount; } }, }); Object.defineProperty(gameObject, 'bokehContrast', { get: function () { return bokehContrast; }, set: function (value) { if (bokehContrast === value) { return; } bokehContrast = value; if (gameObject._bokeh) { gameObject._bokeh.contrast = bokehContrast; } }, }); gameObject.bokehRadius = null; AddClearEffectCallback(gameObject, 'bokehRadius'); return gameObject; }; var AddBrightnessProperties = function (gameObject) { AddColorMatrixEffectPropertiesBase(gameObject, 'brightness', 1); return gameObject; }; var AddBrownProperties = function (gameObject) { AddColorMatrixEffectPropertiesBase(gameObject, 'brown'); return gameObject; }; const FilterName$4 = 'FilterP3Circle'; // Built-in fx in phaser3 const frag$4 = `\ #pragma phaserTemplate(shaderName) #ifdef GL_FRAGMENT_PRECISION_HIGH #define highmedp highp #else #define highmedp mediump #endif precision highmedp float; // Scene buffer uniform sampler2D uMainSampler; varying vec2 outTexCoord; // Effect parameters uniform vec2 texSize; uniform vec3 color; uniform vec4 backgroundColor; uniform vec3 config; #pragma phaserTemplate(fragmentHeader) void main (void) { float thickness = config.x; float scale = config.y; float feather = config.z; vec4 texture = texture2D(uMainSampler, outTexCoord); vec2 position = (gl_FragCoord.xy / texSize.xy) * 2.0 - 1.0; float aspectRatio = texSize.x / texSize.y; position.x *= aspectRatio; float grad = length(position); // height > width float outer = aspectRatio; float inner = outer - (thickness * 2.0 / texSize.y); // width > height if (aspectRatio >= 1.0) { float f = 2.0 + (texSize.y / texSize.x); outer = 1.0; inner = 1.0 - (thickness * f / texSize.x); } outer *= scale; inner *= scale; float circle = smoothstep(outer, outer - 0.01, grad); float ring = circle - smoothstep(inner, inner - feather, grad); texture = mix(backgroundColor * backgroundColor.a, texture, texture.a); texture = (texture * (circle - ring)); gl_FragColor = vec4(texture.rgb + (ring * color), texture.a); } `; class CircleFilter extends Phaser.Renderer.WebGL.RenderNodes.BaseFilterShader { static FilterName = FilterName$4; constructor(manager) { super(FilterName$4, manager, null, frag$4); } // This method sets up the uniforms for the shader. setupUniforms(controller, drawingContext) { const programManager = this.programManager; programManager.setUniform('texSize', [drawingContext.width, drawingContext.height]); programManager.setUniform('color', controller.glcolor); programManager.setUniform('backgroundColor', controller.glcolor2); programManager.setUniform('config', [controller.thickness, controller.scale, controller.feather]); } } const GetValue$4 = Phaser.Utils.Objects.GetValue; class CircleController extends Phaser.Filters.Controller { static FilterName = FilterName$4; constructor(camera, config) { super(camera, FilterName$4); this.thickness = 8; this.scale = 1; this.feather = 0.005; this.glcolor = [1, 0.2, 0.7]; this.glcolor2 = [1, 0, 0, 0.4]; this.resetFromJSON(config); } resetFromJSON(o) { this.setThickness(GetValue$4(o, 'thickness', 8)); this.setScale(GetValue$4(o, 'scale', 1)); this.setFeather(GetValue$4(o, 'feather', 0.005)); this.setColor(GetValue$4(o, 'color', 0xFF33B2)); this.setBackgroundColor(GetValue$4(o, 'backgroundColor', 0xFF0000)); this.setBackgroundAlpha(GetValue$4(o, 'backgroundAlpha', 0.4)); return this; } get color() { var color = this.glcolor; return (((color[0] * 255) << 16) + ((color[1] * 255) << 8) + (color[2] * 255 | 0)); } set color(value) { var color = this.glcolor; color[0] = ((value >> 16) & 0xFF) / 255; color[1] = ((value >> 8) & 0xFF) / 255; color[2] = (value & 0xFF) / 255; } get backgroundColor() { var color = this.glcolor2; return (((color[0] * 255) << 16) + ((color[1] * 255) << 8) + (color[2] * 255 | 0)); } set backgroundColor(value) { var color = this.glcolor2; color[0] = ((value >> 16) & 0xFF) / 255; color[1] = ((value >> 8) & 0xFF) / 255; color[2] = (value & 0xFF) / 255; } get backgroundAlpha() { var color = this.glcolor2; return color[3]; } set backgroundAlpha(value) { var color = this.glcolor2; color[3] = value; } setThickness(thickness) { this.thickness = thickness; return this; } setScale(scale) { this.scale = scale; return this; } setFeather(feather) { this.feather = feather; return this; } setColor(color) { this.color = color; return this; } setBackgroundColor(color) { this.backgroundColor = color; return this; } setBackgroundAlpha(alpha) { this.backgroundAlpha = alpha; return this; } } var InstallCircleFX = function (game) { game = GetGame(game); var success = RegisterFilter(game, CircleFilter); if (!success) { return false; } AddFilterListMethod( 'addP3Bloom', function (color, offsetX, offsetY, blurStrength, strength, steps) { if (color === undefined) { color = 0xFFFFFF; } if (offsetX === undefined) { offsetX = 1; } if (offsetY === undefined) { offsetY = 1; } if (blurStrength === undefined) { blurStrength = 1; } if (strength === undefined) { strength = 1; } if (steps === undefined) { steps = 4; } return this.add(new BloomController( this.camera, { color, offsetX, offsetY, blurStrength, strength, steps } )); } ); AddFilterListMethod( 'addP3Circle', function (thickness, color, backgroundColor, scale, feather) { if (thickness === undefined) { thickness = 8; } if (color === undefined) { color = 0xFF33B2; } if (backgroundColor === undefined) { backgroundColor = 0xFF0000; } if (scale === undefined) { scale = 1; } if (feather === undefined) { feather = 0.005; } return this.add(new CircleController( this.camera, { thickness, color, backgroundColor, scale, feather } )); } ); return true; }; var AddCircleProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameObject, 'circleColor')) { return gameObject; } InstallCircleFX(gameObject); var filterList = GetFilterList(gameObject); var circleColor, circleThickness = 8, circleBackgroundColor = 0x000000, circleBackgroundAlpha = 0.4, circleScale = 1, circleFeather = 0.005; Object.defineProperty(gameObject, 'circleColor', { get: function () { return circleColor; }, set: function (value) { if (circleColor === value) { return; } circleColor = value; if ((circleColor === null) || (circleColor === false)) { if (gameObject._circle) { filterList.remove(gameObject._circle); gameObject._circle = undefined; } } else { if (!gameObject._circle) { gameObject._circle = filterList.addP3Circle(circleThickness, circleColor, circleBackgroundColor, circleScale, circleFeather); gameObject.circleBackgroundAlpha = circleBackgroundAlpha; } gameObject._circle.color = circleColor; } }, }); Object.defineProperty(gameObject, 'circleThickness', { get: function () { return circleThickness; }, set: function (value) { if (circleThickness === value) { return; } circleThickness = value; if (gameObject._circle) { gameObject._circle.thickness = circleThickness; } }, }); Object.defineProperty(gameObject, 'circleBackgroundColor', { get: function () { return circleBackgroundColor; }, set: function (value) { if (circleBackgroundColor === value) { return; } circleBackgroundColor = value; if (gameObject._circle) { gameObject._circle.backgroundColor = circleBackgroundColor; } }, }); Object.defineProperty(gameObject, 'circleBackgroundAlpha', { get: function () { return circleBackgroundAlpha; }, set: function (value) { if (circleBackgroundAlpha === value) { return; } circleBackgroundAlpha = value; if (gameObject._circle) { gameObject._circle.glcolor2[3] = circleBackgroundAlpha; } }, }); Object.defineProperty(gameObject, 'circleScale', { get: function () { return circleScale; }, set: function (value) { if (circleScale === value) { return; } circleScale = value; if (gameObject._circle) { gameObject._circle.scale = circleScale; } }, }); Object.defineProperty(gameObject, 'circleFeather', { get: function () { return circleFeather; }, set: function (value) { if (circleFeather === value) { return; } circleFeather = value; if (gameObject._circle) { gameObject._circle.feather = circleFeather; } }, }); gameObject.circleColor = null; AddClearEffectCallback(gameObject, 'circleColor'); return gameObject; }; var AddContrastProperties = function (gameObject) { AddColorMatrixEffectPropertiesBase(gameObject, 'contrast', 1); return gameObject; }; var AddDesaturateProperties = function (gameObject) { AddColorMatrixEffectPropertiesBase(gameObject, 'desaturate', 1); return gameObject; }; var AddDesaturateLuminanceProperties = function (gameObject) { AddColorMatrixEffectPropertiesBase(gameObject, 'desaturateLuminance'); return gameObject; }; var AddDisplacementProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameObject, 'displacementKey')) { return gameObject; } var filterList = GetFilterList(gameObject); var displacementKey, displacementX = 0.005, displacementY = 0.005; Object.defineProperty(gameObject, 'displacementKey', { get: function () { return displacementKey; }, set: function (value) { if (displacementKey === value) { return; } displacementKey = value; if ((displacementKey === null) || (displacementKey === false)) { if (gameObject._displacement) { filterList.remove(gameObject._displacement); gameObject._displacement = undefined; } } else { if (!gameObject._displacement) { gameObject._displacement = filterList.addDisplacement(displacementKey, displacementX, displacementY); } gameObject._displacement.setTexture(displacementKey); } }, }); Object.defineProperty(gameObject, 'displacementX', { get: function () { return displacementX; }, set: function (value) { if (displacementX === value) { return; } displacementX = value; if (gameObject._displacement) { gameObject._displacement.x = displacementX; } }, }); Object.defineProperty(gameObject, 'displacementY', { get: function () { return displacementY; }, set: function (value) { if (displacementY === value) { return; } displacementY = value; if (gameObject._displacement) { gameObject._displacement.y = displacementY; } }, }); gameObject.displacementKey = null; AddClearEffectCallback(gameObject, 'displacementKey'); return gameObject; }; var AddGlowProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameObject, 'glowColor')) { return gameObject; } var filterList = GetFilterList(gameObject); var glowColor, glowOuterStrength = 4, glowInnerStrength = 0; Object.defineProperty(gameObject, 'glowColor', { get: function () { return glowColor; }, set: function (value) { if (glowColor === value) { return; } glowColor = value; if ((glowColor === null) || (glowColor === false)) { if (gameObject._glow) { filterList.remove(gameObject._glow); gameObject._glow = undefined; } } else { if (!gameObject._glow) { gameObject._glow = filterList.addGlow(glowColor, glowOuterStrength, glowInnerStrength); } gameObject._glow.color = glowColor; } }, }); Object.defineProperty(gameObject, 'glowOuterStrength', { get: function () { return glowOuterStrength; }, set: function (value) { if (glowOuterStrength === value) { return; } glowOuterStrength = value; if (gameObject._glow) { gameObject._glow.outerStrength = glowOuterStrength; } }, }); Object.defineProperty(gameObject, 'glowInnerStrength', { get: function () { return glowInnerStrength; }, set: function (value) { if (glowInnerStrength === value) { return; } glowInnerStrength = value; if (gameObject._glow) { gameObject._glow.innerStrength = glowInnerStrength; } }, }); gameObject.glowColor = null; AddClearEffectCallback(gameObject, 'glowColor'); return gameObject; }; const FilterName$3 = 'FilterP3Gradient'; // Built-in fx in phaser3 const frag$3 = `\ #pragma phaserTemplate(shaderName) #ifdef GL_FRAGMENT_PRECISION_HIGH #define highmedp highp #else #define highmedp mediump #endif precision highmedp float; #define SRGB_TO_LINEAR(c) pow((c), vec3(2.2)) #define LINEAR_TO_SRGB(c) pow((c), vec3(1.0 / 2.2)) #define SRGB(r, g, b) SRGB_TO_LINEAR(vec3(float(r), float(g), float(b)) / 255.0) // Scene buffer uniform sampler2D uMainSampler; varying vec2 outTexCoord; // Effect parameters uniform float alpha; uniform vec2 positionFrom; uniform vec2 positionTo; uniform vec3 color1; uniform vec3 color2; uniform int size; #pragma phaserTemplate(fragmentHeader) float gradientNoise(in vec2 uv) { const vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189); return fract(magic.z * fract(dot(uv, magic.xy))); } float stepped (in float s, in float scale, in int steps) { return steps > 0 ? floor( s / ((1.0 * scale) / float(steps))) * 1.0 / float(steps - 1) : s; } void main () { vec2 a = positionFrom; vec2 b = positionTo; vec2 ba = b - a; float d = dot(outTexCoord - a, ba) / dot(ba, ba); float t = size > 0 ? stepped(d, 1.0, size) : d; t = smoothstep(0.0, 1.0, clamp(t, 0.0, 1.0)); vec3 color = mix(SRGB(color1.r, color1.g, color1.b), SRGB(color2.r, color2.g, color2.b), t); color = LINEAR_TO_SRGB(color); color += (1.0 / 255.0) * gradientNoise(outTexCoord) - (0.5 / 255.0); vec4 texture = texture2D(uMainSampler, outTexCoord); gl_FragColor = vec4(mix(color.rgb, texture.rgb, alpha), 1.0) * texture.a; } `; class GradientFilter extends Phaser.Renderer.WebGL.RenderNodes.BaseFilterShader { static FilterName = FilterName$3; constructor(manager) { super(FilterName$3, manager, null, frag$3); } // This method sets up the uniforms for the shader. setupUniforms(controller, drawingContext) { const programManager = this.programManager; programManager.setUniform('alpha', controller.alpha); programManager.setUniform('positionFrom', [controller.fromX, controller.fromY]); programManager.setUniform('positionTo', [controller.toX, controller.toY]); programManager.setUniform('color1', controller.glcolor1); programManager.setUniform('color2', controller.glcolor2); programManager.setUniform('size', controller.size); } } const GetValue$3 = Phaser.Utils.Objects.GetValue; class GradientController extends Phaser.Filters.Controller { static FilterName = FilterName$3; constructor(camera, config) { super(camera, FilterName$3); this.alpha = 0.2; this.fromX = 0; this.fromY = 0; this.toX = 0; this.toY = 1; this.glcolor1 = [255, 0, 0]; this.glcolor2 = [0, 255, 0]; this.size = 0; this.resetFromJSON(config); } resetFromJSON(o) { this.setAlpha(GetValue$3(o, 'alpha', 0.2)); this.setFromPosition(GetValue$3(o, 'fromX', 0), GetValue$3(o, 'fromY', 0)); this.setToPosition(GetValue$3(o, 'toX', 0), GetValue$3(o, 'toY', 1)); this.setColor1(GetValue$3(o, 'color1', 0xff0000)); this.setColor2(GetValue$3(o, 'color2', 0x00ff00)); this.setSize(GetValue$3(o, 'size', 0)); return this; } get color1() { var color = this.glcolor1; return (((color[0]) << 16) + ((color[1]) << 8) + (color[2] | 0)); } set color1(value) { var color = this.glcolor1; color[0] = ((value >> 16) & 0xFF); color[1] = ((value >> 8) & 0xFF); color[2] = (value & 0xFF); } get color2() { var color = this.glcolor2; return (((color[0]) << 16) + ((color[1]) << 8) + (color[2] | 0)); } set color2(value) { var color = this.glcolor2; color[0] = ((value >> 16) & 0xFF); color[1] = ((value >> 8) & 0xFF); color[2] = (value & 0xFF); } setAlpha(alpha) { this.alpha = alpha; return this; } setFromPosition(x, y) { this.fromX = x; this.fromY = y; return this; } setToPosition(x, y) { this.toX = x; this.toY = y; return this; } setColor1(color1) { this.color1 = color1; return this; } setColor2(color2) { this.color2 = color2; return this; } setSize(size) { this.size = size; return this; } } var InstallGradientFX = function (game) { game = GetGame(game); var success = RegisterFilter(game, GradientFilter); if (!success) { return false; } AddFilterListMethod( 'addP3Gradient', function (color1, color2, alpha, fromX, fromY, toX, toY, size) { if (color1 === undefined) { color1 = 0xff0000; } if (color2 === undefined) { color2 = 0x00ff00; } if (alpha === undefined) { alpha = 0.2; } if (fromX === undefined) { fromX = 0; } if (fromY === undefined) { fromY = 0; } if (toX === undefined) { toX = 0; } if (toY === undefined) { toY = 1; } if (size === undefined) { size = 0; } return this.add(new GradientController( this.camera, { color1, color2, alpha, fromX, fromY, toX, toY, size } )); } ); return true; }; var AddGradientProperties = function (gameObject) { // Don't attach properties again if (HasProperty(gameOb