2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2023-01-02 17:36:27 +00:00
|
|
|
* @copyright 2013-2023 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2020-11-06 09:44:29 +00:00
|
|
|
var TransformMatrix = require('../components/TransformMatrix');
|
|
|
|
|
|
|
|
var tempMatrix1 = new TransformMatrix();
|
|
|
|
var tempMatrix2 = new TransformMatrix();
|
|
|
|
var tempMatrix3 = new TransformMatrix();
|
|
|
|
var tempMatrix4 = new TransformMatrix();
|
|
|
|
|
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 {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
|
|
|
*/
|
2020-09-14 14:33:58 +00:00
|
|
|
var ParticleManagerCanvasRenderer = function (renderer, emitterManager, 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;
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:44:29 +00:00
|
|
|
var camMatrix = tempMatrix1.copyFrom(camera.matrix);
|
|
|
|
var calcMatrix = tempMatrix2;
|
|
|
|
var particleMatrix = tempMatrix3;
|
|
|
|
var managerMatrix = tempMatrix4;
|
2018-07-31 22:21:07 +00:00
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
if (parentMatrix)
|
|
|
|
{
|
|
|
|
managerMatrix.loadIdentity();
|
|
|
|
managerMatrix.multiply(parentMatrix);
|
|
|
|
managerMatrix.translate(emitterManager.x, emitterManager.y);
|
|
|
|
managerMatrix.rotate(emitterManager.rotation);
|
|
|
|
managerMatrix.scale(emitterManager.scaleX, emitterManager.scaleY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
managerMatrix.applyITRS(emitterManager.x, emitterManager.y, emitterManager.rotation, emitterManager.scaleX, emitterManager.scaleY);
|
|
|
|
}
|
2018-08-03 00:04:46 +00:00
|
|
|
|
2018-04-04 17:05:59 +00:00
|
|
|
var ctx = renderer.currentContext;
|
2020-09-23 11:06:29 +00:00
|
|
|
var roundPixels = camera.roundPixels;
|
2018-06-25 15:24:08 +00:00
|
|
|
|
2018-08-03 00:04:46 +00:00
|
|
|
for (var e = 0; e < emittersLength; e++)
|
2018-04-04 17:05:59 +00:00
|
|
|
{
|
2018-08-03 00:04:46 +00:00
|
|
|
var emitter = emitters[e];
|
2017-10-17 03:19:03 +00:00
|
|
|
var particles = emitter.alive;
|
2018-08-03 00:04:46 +00:00
|
|
|
var particleCount = particles.length;
|
2017-10-17 03:19:03 +00:00
|
|
|
|
2018-08-03 00:04:46 +00:00
|
|
|
if (!emitter.visible || particleCount === 0)
|
2017-10-19 23:54:28 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:23:17 +00:00
|
|
|
if (emitter.sortCallback)
|
|
|
|
{
|
|
|
|
emitter.depthSort();
|
|
|
|
}
|
|
|
|
|
2021-01-07 14:52:08 +00:00
|
|
|
camera.addToRenderList(emitter);
|
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
var scrollFactorX = emitter.scrollFactorX;
|
|
|
|
var scrollFactorY = emitter.scrollFactorY;
|
2018-08-03 00:04:46 +00:00
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
ctx.save();
|
2017-10-17 03:19:03 +00:00
|
|
|
|
2018-08-03 00:29:18 +00:00
|
|
|
ctx.globalCompositeOperation = renderer.blendModes[emitter.blendMode];
|
2017-10-17 03:19:03 +00:00
|
|
|
|
2018-08-03 00:04:46 +00:00
|
|
|
for (var i = 0; i < particleCount; i++)
|
2017-10-17 03:19:03 +00:00
|
|
|
{
|
2018-08-03 00:04:46 +00:00
|
|
|
var particle = particles[i];
|
2017-10-19 23:54:28 +00:00
|
|
|
|
2018-08-03 00:04:46 +00:00
|
|
|
var alpha = particle.alpha * camera.alpha;
|
2017-11-11 03:51:54 +00:00
|
|
|
|
2018-08-03 00:04:46 +00:00
|
|
|
if (alpha <= 0)
|
2017-11-11 03:51:54 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
particleMatrix.applyITRS(particle.x, particle.y, particle.rotation, particle.scaleX, particle.scaleY);
|
2017-10-19 23:54:28 +00:00
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
camMatrix.copyFrom(camera.matrix);
|
2017-10-17 03:19:03 +00:00
|
|
|
|
2021-02-21 19:30:32 +00:00
|
|
|
camMatrix.multiplyWithOffset(managerMatrix, -camera.scrollX * scrollFactorX, -camera.scrollY * scrollFactorY);
|
2018-02-28 22:40:08 +00:00
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
// Undo the camera scroll
|
|
|
|
particleMatrix.e = particle.x;
|
|
|
|
particleMatrix.f = particle.y;
|
2018-08-03 00:04:46 +00:00
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
// Multiply by the particle matrix, store result in calcMatrix
|
2018-08-03 00:04:46 +00:00
|
|
|
camMatrix.multiply(particleMatrix, calcMatrix);
|
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
var frame = particle.frame;
|
|
|
|
var cd = frame.canvasData;
|
|
|
|
|
2022-02-03 16:44:24 +00:00
|
|
|
if (cd.width > 0 && cd.height > 0)
|
|
|
|
{
|
|
|
|
var x = -(frame.halfWidth);
|
|
|
|
var y = -(frame.halfHeight);
|
2020-09-23 11:06:29 +00:00
|
|
|
|
2022-02-03 16:44:24 +00:00
|
|
|
ctx.globalAlpha = alpha;
|
2020-09-14 14:33:58 +00:00
|
|
|
|
2022-02-03 16:44:24 +00:00
|
|
|
ctx.save();
|
2018-02-28 22:40:08 +00:00
|
|
|
|
2022-02-03 16:44:24 +00:00
|
|
|
calcMatrix.setToContext(ctx);
|
2018-08-03 00:04:46 +00:00
|
|
|
|
2022-02-03 16:44:24 +00:00
|
|
|
if (roundPixels)
|
|
|
|
{
|
|
|
|
x = Math.round(x);
|
|
|
|
y = Math.round(y);
|
|
|
|
}
|
2018-08-03 00:04:46 +00:00
|
|
|
|
2022-06-11 08:07:29 +00:00
|
|
|
ctx.imageSmoothingEnabled = !frame.source.scaleMode;
|
2019-10-01 15:10:46 +00:00
|
|
|
|
2022-02-03 16:44:24 +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
|
|
|
|
2022-02-03 16:44:24 +00:00
|
|
|
ctx.restore();
|
|
|
|
}
|
2017-10-17 03:19:03 +00:00
|
|
|
}
|
2018-04-04 17:05:59 +00:00
|
|
|
|
2020-09-23 11:06:29 +00:00
|
|
|
ctx.restore();
|
|
|
|
}
|
2017-10-17 03:19:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ParticleManagerCanvasRenderer;
|