2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-02-06 19:22:20 +00:00
|
|
|
/**
|
|
|
|
* Renders this Game Object with the Canvas Renderer to the given Camera.
|
|
|
|
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
|
|
|
|
* This method should not be called directly. It is a utility function of the Render module.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Particles.EmitterManager#renderCanvas
|
|
|
|
* @since 3.0.0
|
|
|
|
* @private
|
|
|
|
*
|
2018-03-28 14:04:09 +00:00
|
|
|
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.
|
2018-03-29 12:12:07 +00:00
|
|
|
* @param {Phaser.GameObjects.Particles.ParticleEmitterManager} emitterManager - The Game Object being rendered in this call.
|
2018-02-06 19:22:20 +00:00
|
|
|
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
|
|
|
|
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
|
2018-04-04 16:14:55 +00:00
|
|
|
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
|
2018-02-06 19:22:20 +00:00
|
|
|
*/
|
2018-04-04 16:14:55 +00:00
|
|
|
var ParticleManagerCanvasRenderer = function (renderer, emitterManager, interpolationPercentage, camera, parentMatrix)
|
2017-10-17 03:19:03 +00:00
|
|
|
{
|
2017-10-20 13:14:37 +00:00
|
|
|
var emitters = emitterManager.emitters.list;
|
2018-07-19 12:19:02 +00:00
|
|
|
var emittersLength = emitters.length;
|
2017-10-17 03:19:03 +00:00
|
|
|
|
2018-07-19 12:19:02 +00:00
|
|
|
if (emittersLength === 0)
|
2017-10-17 03:19:03 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-31 22:21:07 +00:00
|
|
|
var camMatrix = renderer._tempMatrix1.copyFrom(camera.matrix);
|
|
|
|
var managerMatrix = renderer._tempMatrix2.applyITRS(emitterManager.x, emitterManager.y, emitterManager.rotation, emitterManager.scaleX, emitterManager.scaleY);
|
|
|
|
|
|
|
|
camMatrix.multiply(managerMatrix);
|
|
|
|
|
2018-04-04 17:05:59 +00:00
|
|
|
var ctx = renderer.currentContext;
|
2018-07-31 22:21:07 +00:00
|
|
|
var matrix = camMatrix.matrix;
|
2018-04-04 17:05:59 +00:00
|
|
|
|
|
|
|
ctx.save();
|
2018-06-25 15:24:08 +00:00
|
|
|
|
2018-07-31 22:21:07 +00:00
|
|
|
ctx.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
|
|
|
|
|
2018-04-04 17:05:59 +00:00
|
|
|
if (parentMatrix !== undefined)
|
|
|
|
{
|
2018-07-31 22:21:07 +00:00
|
|
|
matrix = parentMatrix.matrix;
|
2018-06-25 15:24:08 +00:00
|
|
|
|
2018-04-04 17:05:59 +00:00
|
|
|
ctx.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:19:02 +00:00
|
|
|
for (var i = 0; i < emittersLength; i++)
|
2017-10-17 03:19:03 +00:00
|
|
|
{
|
|
|
|
var emitter = emitters[i];
|
|
|
|
|
|
|
|
var particles = emitter.alive;
|
|
|
|
var length = particles.length;
|
|
|
|
|
2017-10-19 23:54:28 +00:00
|
|
|
if (!emitter.visible || length === 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-17 03:19:03 +00:00
|
|
|
var lastAlpha = ctx.globalAlpha;
|
2018-06-25 15:24:08 +00:00
|
|
|
|
2017-10-17 03:19:03 +00:00
|
|
|
var cameraScrollX = camera.scrollX * emitter.scrollFactorX;
|
|
|
|
var cameraScrollY = camera.scrollY * emitter.scrollFactorY;
|
|
|
|
|
|
|
|
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];
|
2017-10-19 23:54:28 +00:00
|
|
|
|
2018-06-25 15:24:08 +00:00
|
|
|
var particleAlpha = camera.alpha * ((particle.color >> 24) & 0xFF) / 255;
|
2017-11-11 03:51:54 +00:00
|
|
|
|
2018-06-25 15:24:08 +00:00
|
|
|
if (particleAlpha <= 0)
|
2017-11-11 03:51:54 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-19 23:54:28 +00:00
|
|
|
var frame = particle.frame;
|
|
|
|
var width = frame.width;
|
|
|
|
var height = frame.height;
|
|
|
|
var ox = width * 0.5;
|
|
|
|
var oy = height * 0.5;
|
|
|
|
var cd = frame.canvasData;
|
|
|
|
|
2017-10-17 03:19:03 +00:00
|
|
|
var x = -ox;
|
|
|
|
var y = -oy;
|
|
|
|
|
2018-04-18 21:44:09 +00:00
|
|
|
var tx = particle.x - cameraScrollX;
|
|
|
|
var ty = particle.y - cameraScrollY;
|
2018-02-28 22:40:08 +00:00
|
|
|
|
2018-06-27 14:15:00 +00:00
|
|
|
if (camera.roundPixels)
|
2018-02-28 22:40:08 +00:00
|
|
|
{
|
|
|
|
tx |= 0;
|
|
|
|
ty |= 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:24:08 +00:00
|
|
|
ctx.globalAlpha = particleAlpha;
|
2017-10-17 03:19:03 +00:00
|
|
|
|
|
|
|
ctx.save();
|
2018-02-28 22:40:08 +00:00
|
|
|
|
|
|
|
ctx.translate(tx, ty);
|
|
|
|
|
2017-10-17 03:19:03 +00:00
|
|
|
ctx.rotate(particle.rotation);
|
2018-02-28 22:40:08 +00:00
|
|
|
|
2017-10-17 03:19:03 +00:00
|
|
|
ctx.scale(particle.scaleX, particle.scaleY);
|
2018-02-28 22:40:08 +00:00
|
|
|
|
2018-07-29 11:16:42 +00:00
|
|
|
ctx.drawImage(frame.source.image, cd.x, cd.y, cd.width, cd.height, x, y, cd.width, cd.height);
|
2018-02-28 22:40:08 +00:00
|
|
|
|
2017-10-17 03:19:03 +00:00
|
|
|
ctx.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.globalAlpha = lastAlpha;
|
|
|
|
}
|
2018-04-04 17:05:59 +00:00
|
|
|
|
|
|
|
ctx.restore();
|
2017-10-17 03:19:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ParticleManagerCanvasRenderer;
|