From bc281871ac0a9141c49d3711eec6ad51512d9f3a Mon Sep 17 00:00:00 2001 From: Felipe Alfonso Date: Thu, 28 Sep 2017 22:16:10 -0300 Subject: [PATCH] - Particle Emitter canvas renderer - Camera Effects resets blend mode to avoid bug when dealing with custom blended game objects --- .../emitter/ParticleEmitterCanvasRenderer.js | 53 ++++++++++++++++++- v3/src/renderer/canvas/CanvasRenderer.js | 2 + v3/src/renderer/webgl/WebGLRenderer.js | 3 +- .../particlerenderer/ParticleRenderer.js | 4 +- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/v3/src/gameobjects/emitter/ParticleEmitterCanvasRenderer.js b/v3/src/gameobjects/emitter/ParticleEmitterCanvasRenderer.js index e047146a7..d602cf327 100644 --- a/v3/src/gameobjects/emitter/ParticleEmitterCanvasRenderer.js +++ b/v3/src/gameobjects/emitter/ParticleEmitterCanvasRenderer.js @@ -1,6 +1,55 @@ -var ParticleEmitterCanvasRenderer = function (renderer, src, interpolationPercentage, camera) -{ +var GameObject = require('../GameObject'); +var ParticleEmitterCanvasRenderer = function (renderer, emitter, interpolationPercentage, camera) +{ + var particles = emitter.alive; + var length = particles.length; + var ctx = renderer.currentContext; + var frame = emitter.frame; + var dx = frame.x; + var dy = frame.y; + var width = frame.width; + var height = frame.height; + var ox = width * 0.5; + var oy = height * 0.5; + var lastAlpha = ctx.globalAlpha; + var cd = frame.canvasData; + var cameraScrollX = camera.scrollX * emitter.scrollFactorX; + var cameraScrollY = camera.scrollY * emitter.scrollFactorY; + + if (length === 0 || GameObject.RENDER_MASK !== emitter.renderFlags || (emitter.cameraFilter > 0 && (emitter.cameraFilter & camera._id))) + { + return; + } + + if (renderer.currentBlendMode !== emitter.blendMode) + { + renderer.currentBlendMode = emitter.blendMode; + ctx.globalCompositeOperation = renderer.blendModes[emitter.blendMode]; + } + + for (var index = 0; index < length; ++index) + { + var particle = particles[index]; + var x = -ox; + var y = -oy; + var scaleX = particle.scaleX; + var scaleY = particle.scaleY; + var rotation = particle.rotation; + var color = particle.color; + var alpha = ((color >> 24) & 0xFF) / 255.0; + + ctx.globalAlpha = alpha; + + ctx.save(); + ctx.translate(particle.x - cameraScrollX, particle.y - cameraScrollY); + ctx.rotate(rotation); + ctx.scale(scaleX, scaleY); + ctx.drawImage(frame.source.image, cd.sx, cd.sy, cd.sWidth, cd.sHeight, x, y, cd.dWidth, cd.dHeight); + ctx.restore(); + } + + ctx.globalAlpha = lastAlpha; }; module.exports = ParticleEmitterCanvasRenderer; diff --git a/v3/src/renderer/canvas/CanvasRenderer.js b/v3/src/renderer/canvas/CanvasRenderer.js index 4e1f7ae80..6bd78c70c 100644 --- a/v3/src/renderer/canvas/CanvasRenderer.js +++ b/v3/src/renderer/canvas/CanvasRenderer.js @@ -227,6 +227,8 @@ var CanvasRenderer = new Class({ if (camera._fadeAlpha > 0 || camera._flashAlpha > 0) { + ctx.globalCompositeOperation = 'source-over'; + // fade rendering ctx.fillStyle = 'rgb(' + (camera._fadeRed * 255) + ',' + (camera._fadeGreen * 255) + ',' + (camera._fadeBlue * 255) + ')'; ctx.globalAlpha = camera._fadeAlpha; diff --git a/v3/src/renderer/webgl/WebGLRenderer.js b/v3/src/renderer/webgl/WebGLRenderer.js index c0745ee18..f18222205 100644 --- a/v3/src/renderer/webgl/WebGLRenderer.js +++ b/v3/src/renderer/webgl/WebGLRenderer.js @@ -500,8 +500,7 @@ var WebGLRenderer = new Class({ if (camera._fadeAlpha > 0 || camera._flashAlpha > 0) { this.setRenderTarget(null); - - quadBatch.bind(); + this.setBlendMode(BlendModes.NORMAL); // fade rendering quadBatch.add( diff --git a/v3/src/renderer/webgl/renderers/particlerenderer/ParticleRenderer.js b/v3/src/renderer/webgl/renderers/particlerenderer/ParticleRenderer.js index 3b026ab40..65e605d6f 100644 --- a/v3/src/renderer/webgl/renderers/particlerenderer/ParticleRenderer.js +++ b/v3/src/renderer/webgl/renderers/particlerenderer/ParticleRenderer.js @@ -215,6 +215,8 @@ var ParticleRenderer = new Class({ var tempMatrix = this.tempMatrix; var tempMatrixMatrix = tempMatrix.matrix; var particleOffset = 0; + var cameraScrollX = camera.scrollX * emitter.scrollFactorX; + var cameraScrollY = camera.scrollY * emitter.scrollFactorY; if (length === 0) return; @@ -235,7 +237,7 @@ var ParticleRenderer = new Class({ var xw = x + width; var yh = y + height; - tempMatrix.applyITRS(particle.x, particle.y, rotation, scaleX, scaleY); + tempMatrix.applyITRS(particle.x - cameraScrollX, particle.y - cameraScrollY, rotation, scaleX, scaleY); var sra = tempMatrixMatrix[0]; var srb = tempMatrixMatrix[1];