Skip to content

Commit fb849ae

Browse files
committed
The ParticleManagerWebGLRenderer now calculates its transform matrix differently, splitting out the parent matrix and factoring in follow offsets separately. This fixes numerous issues with particle emitters being incorrectly offset when added to Containers. Fix phaserjs#5319 phaserjs#5195 phaserjs#4739 phaserjs#4691
1 parent b350ebd commit fb849ae

2 files changed

Lines changed: 32 additions & 35 deletions

File tree

src/gameobjects/particles/Particle.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,6 @@ var Particle = new Class({
301301

302302
if (x === undefined)
303303
{
304-
if (emitter.follow)
305-
{
306-
this.x += emitter.follow.x + emitter.followOffset.x;
307-
}
308-
309304
this.x += emitter.x.onEmit(this, 'x');
310305
}
311306
else
@@ -315,11 +310,6 @@ var Particle = new Class({
315310

316311
if (y === undefined)
317312
{
318-
if (emitter.follow)
319-
{
320-
this.y += emitter.follow.y + emitter.followOffset.y;
321-
}
322-
323313
this.y += emitter.y.onEmit(this, 'y');
324314
}
325315
else

src/gameobjects/particles/ParticleManagerWebGLRenderer.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@ var ParticleManagerWebGLRenderer = function (renderer, emitterManager, camera, p
3535
var camMatrix = pipeline._tempMatrix1.copyFrom(camera.matrix);
3636
var calcMatrix = pipeline._tempMatrix2;
3737
var particleMatrix = pipeline._tempMatrix3;
38-
var managerMatrix = pipeline._tempMatrix4.applyITRS(emitterManager.x, emitterManager.y, emitterManager.rotation, emitterManager.scaleX, emitterManager.scaleY);
38+
var managerMatrix = pipeline._tempMatrix4;
3939

40-
camMatrix.multiply(managerMatrix);
40+
if (parentMatrix)
41+
{
42+
managerMatrix.loadIdentity();
43+
managerMatrix.multiply(parentMatrix);
44+
managerMatrix.translate(emitterManager.x, emitterManager.y);
45+
managerMatrix.rotate(emitterManager.rotation);
46+
managerMatrix.scale(emitterManager.scaleX, emitterManager.scaleY);
47+
}
48+
else
49+
{
50+
managerMatrix.applyITRS(emitterManager.x, emitterManager.y, emitterManager.rotation, emitterManager.scaleX, emitterManager.scaleY);
51+
}
4152

4253
var roundPixels = camera.roundPixels;
4354
var texture = emitterManager.defaultFrame.glTexture;
@@ -56,23 +67,13 @@ var ParticleManagerWebGLRenderer = function (renderer, emitterManager, camera, p
5667
continue;
5768
}
5869

59-
var scrollX = camera.scrollX * emitter.scrollFactorX;
60-
var scrollY = camera.scrollY * emitter.scrollFactorY;
70+
var followX = (emitter.follow) ? emitter.follow.x + emitter.followOffset.x : 0;
71+
var followY = (emitter.follow) ? emitter.follow.y + emitter.followOffset.y : 0;
6172

62-
if (parentMatrix)
63-
{
64-
// Multiply the camera by the parent matrix
65-
camMatrix.multiplyWithOffset(parentMatrix, -scrollX, -scrollY);
73+
var scrollFactorX = emitter.scrollFactorX;
74+
var scrollFactorY = emitter.scrollFactorY;
6675

67-
scrollX = 0;
68-
scrollY = 0;
69-
}
70-
71-
if (renderer.setBlendMode(emitter.blendMode))
72-
{
73-
// Rebind the texture if we've flushed
74-
// pipeline.setTexture2D(texture, 0);
75-
}
76+
renderer.setBlendMode(emitter.blendMode);
7677

7778
if (emitter.mask)
7879
{
@@ -94,20 +95,26 @@ var ParticleManagerWebGLRenderer = function (renderer, emitterManager, camera, p
9495
continue;
9596
}
9697

97-
var frame = particle.frame;
98+
particleMatrix.applyITRS(particle.x, particle.y, particle.rotation, particle.scaleX, particle.scaleY);
9899

99-
var x = -(frame.halfWidth);
100-
var y = -(frame.halfHeight);
101-
var xw = x + frame.width;
102-
var yh = y + frame.height;
100+
camMatrix.copyFrom(camera.matrix);
103101

104-
particleMatrix.applyITRS(0, 0, particle.rotation, particle.scaleX, particle.scaleY);
102+
camMatrix.multiplyWithOffset(managerMatrix, followX + -camera.scrollX * scrollFactorX, followY + -camera.scrollY * scrollFactorY);
105103

106-
particleMatrix.e = particle.x - scrollX;
107-
particleMatrix.f = particle.y - scrollY;
104+
// Undo the camera scroll
105+
particleMatrix.e = particle.x;
106+
particleMatrix.f = particle.y;
108107

108+
// Multiply by the particle matrix, store result in calcMatrix
109109
camMatrix.multiply(particleMatrix, calcMatrix);
110110

111+
var frame = particle.frame;
112+
113+
var x = -frame.halfWidth;
114+
var y = -frame.halfHeight;
115+
var xw = x + frame.width;
116+
var yh = y + frame.height;
117+
111118
var tx0 = calcMatrix.getX(x, y);
112119
var ty0 = calcMatrix.getY(x, y);
113120

0 commit comments

Comments
 (0)