Skip to content

Commit 3a61fa3

Browse files
committed
RenderTexture.render now takes a Matrix as its second parameter, not a Point object. This brings it in line with Pixi and allows you to perform much more complex transformations on the object being rendered. If you need to replicate the old behavior please use RenderTexture.renderXY(sprite, point.x, point.y) instead.
RenderTexture.render and `renderXY` would ignore the Sprites rotation or scale. The full Sprite transform is now used correctly when the Sprite is drawn to the texture. If you wish to replicate the old behavior please use `RenderTexture.renderRawXY` instead. RenderTexture.matrix has been removed as it's no longer used. Fixed bug in Pixi where RenderTexture.render would ignore the given matrix. Fixed a bug in Pixi where drawing a Sprite to a RenderTexture would reset the Sprites transform to an identity Matrix.
1 parent cdfc4ff commit 3a61fa3

2 files changed

Lines changed: 99 additions & 48 deletions

File tree

src/gameobjects/RenderTexture.js

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ Phaser.RenderTexture = function (game, width, height, key, scaleMode, resolution
4141
this.type = Phaser.RENDERTEXTURE;
4242

4343
/**
44-
* @property {PIXI.Matrix} matrix - The matrix that is applied when display objects are rendered to this RenderTexture.
44+
* @property {PIXI.Matrix} _tempMatrix - The matrix that is applied when display objects are rendered to this RenderTexture.
45+
* @private
4546
*/
46-
this.matrix = new PIXI.Matrix();
47+
this._tempMatrix = new PIXI.Matrix();
4748

4849
PIXI.RenderTexture.call(this, width, height, this.game.renderer, scaleMode, resolution);
4950

@@ -55,50 +56,98 @@ Phaser.RenderTexture.prototype = Object.create(PIXI.RenderTexture.prototype);
5556
Phaser.RenderTexture.prototype.constructor = Phaser.RenderTexture;
5657

5758
/**
58-
* This function will draw the display object to the texture.
59+
* This function will draw the display object to the RenderTexture at the given coordinates.
60+
*
61+
* When the display object is drawn it takes into account scale and rotation.
62+
*
63+
* If you don't want those then use RenderTexture.renderRawXY instead.
5964
*
6065
* @method Phaser.RenderTexture.prototype.renderXY
61-
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Group} displayObject The display object to render to this texture.
66+
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Group} displayObject - The display object to render to this texture.
6267
* @param {number} x - The x position to render the object at.
6368
* @param {number} y - The y position to render the object at.
64-
* @param {boolean} clear - If true the texture will be cleared before the display object is drawn.
69+
* @param {boolean} [clear=false] - If true the texture will be cleared before the display object is drawn.
6570
*/
6671
Phaser.RenderTexture.prototype.renderXY = function (displayObject, x, y, clear) {
6772

68-
this.matrix.tx = x;
69-
this.matrix.ty = y;
73+
displayObject.updateTransform();
74+
75+
this._tempMatrix.copyFrom(displayObject.worldTransform);
76+
this._tempMatrix.tx = x;
77+
this._tempMatrix.ty = y;
78+
79+
if (this.renderer.type === PIXI.WEBGL_RENDERER)
80+
{
81+
this.renderWebGL(displayObject, this._tempMatrix, clear);
82+
}
83+
else
84+
{
85+
this.renderCanvas(displayObject, this._tempMatrix, clear);
86+
}
87+
88+
};
89+
90+
/**
91+
* This function will draw the display object to the RenderTexture at the given coordinates.
92+
*
93+
* When the display object is drawn it doesn't take into account scale, rotation or translation.
94+
*
95+
* If you need those then use RenderTexture.renderXY instead.
96+
*
97+
* @method Phaser.RenderTexture.prototype.renderRawXY
98+
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Group} displayObject - The display object to render to this texture.
99+
* @param {number} x - The x position to render the object at.
100+
* @param {number} y - The y position to render the object at.
101+
* @param {boolean} [clear=false] - If true the texture will be cleared before the display object is drawn.
102+
*/
103+
Phaser.RenderTexture.prototype.renderRawXY = function (displayObject, x, y, clear) {
104+
105+
this._tempMatrix.identity().translate(x, y);
70106

71107
if (this.renderer.type === PIXI.WEBGL_RENDERER)
72108
{
73-
this.renderWebGL(displayObject, this.matrix, clear);
109+
this.renderWebGL(displayObject, this._tempMatrix, clear);
74110
}
75111
else
76112
{
77-
this.renderCanvas(displayObject, this.matrix, clear);
113+
this.renderCanvas(displayObject, this._tempMatrix, clear);
78114
}
79115

80116
};
81117

82118
/**
83-
* This function will draw the display object to the texture.
119+
* This function will draw the display object to the RenderTexture.
120+
*
121+
* In versions of Phaser prior to 2.4.0 the second parameter was a Phaser.Point object.
122+
* This is now a Matrix allowing you much more control over how the Display Object is rendered.
123+
* If you need to replicate the earlier behavior please use Phaser.RenderTexture.renderXY instead.
124+
*
125+
* If you wish for the displayObject to be rendered taking its current scale, rotation and translation into account then either
126+
* pass `null`, leave it undefined or pass `displayObject.worldTransform` as the matrix value.
84127
*
85128
* @method Phaser.RenderTexture.prototype.render
86-
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Group} displayObject The display object to render to this texture.
87-
* @param {Phaser.Point} position - A Point object containing the position to render the display object at.
88-
* @param {boolean} clear - If true the texture will be cleared before the display object is drawn.
129+
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Group} displayObject - The display object to render to this texture.
130+
* @param {Phaser.Matrix} [matrix] - Optional matrix to apply to the display object before rendering. If null or undefined it will use the worldTransform matrix of the given display object.
131+
* @param {boolean} [clear=false] - If true the texture will be cleared before the display object is drawn.
89132
*/
90-
Phaser.RenderTexture.prototype.render = function (displayObject, position, clear) {
133+
Phaser.RenderTexture.prototype.render = function (displayObject, matrix, clear) {
91134

92-
this.matrix.tx = position.x;
93-
this.matrix.ty = position.y;
135+
if (typeof matrix === 'undefined' || matrix === null)
136+
{
137+
this._tempMatrix.copyFrom(displayObject.worldTransform);
138+
}
139+
else
140+
{
141+
this._tempMatrix.copyFrom(matrix);
142+
}
94143

95144
if (this.renderer.type === PIXI.WEBGL_RENDERER)
96145
{
97-
this.renderWebGL(displayObject, this.matrix, clear);
146+
this.renderWebGL(displayObject, this._tempMatrix, clear);
98147
}
99148
else
100149
{
101-
this.renderCanvas(displayObject, this.matrix, clear);
150+
this.renderCanvas(displayObject, this._tempMatrix, clear);
102151
}
103152

104153
};

src/pixi/textures/RenderTexture.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution)
128128
*/
129129
this.valid = true;
130130

131+
this.tempMatrix = new Phaser.Matrix();
132+
131133
this._updateUvs();
132134
};
133135

@@ -198,43 +200,43 @@ PIXI.RenderTexture.prototype.clear = function()
198200
*/
199201
PIXI.RenderTexture.prototype.renderWebGL = function(displayObject, matrix, clear)
200202
{
201-
if(!this.valid)return;
202-
//TOOD replace position with matrix..
203+
if (!this.valid)
204+
{
205+
return;
206+
}
203207

204-
//Lets create a nice matrix to apply to our display object. Frame buffers come in upside down so we need to flip the matrix
205-
var wt = displayObject.worldTransform;
206-
wt.identity();
207-
wt.translate(0, this.projection.y * 2);
208-
if(matrix)wt.append(matrix);
209-
wt.scale(1,-1);
208+
// Frame buffers come in upside down so we need to flip the matrix
209+
matrix.translate(0, this.projection.y * 2);
210+
matrix.scale(1, -1);
210211

211-
// setWorld Alpha to ensure that the object is renderer at full opacity
212+
// Set worldAlpha to ensure that the object is renderer at full opacity
212213
displayObject.worldAlpha = 1;
213214

214-
// Time to update all the children of the displayObject with the new matrix..
215-
var children = displayObject.children;
216-
217-
for(var i=0,j=children.length; i<j; i++)
215+
// Time to update all the children of the displayObject with the new matrix.
216+
for (var i = 0; i < displayObject.children.length; i++)
218217
{
219-
children[i].updateTransform();
218+
displayObject.children[i].updateTransform();
220219
}
221220

222-
// time for the webGL fun stuff!
221+
// Time for the webGL fun stuff!
223222
var gl = this.renderer.gl;
224223

225224
gl.viewport(0, 0, this.width * this.resolution, this.height * this.resolution);
226225

227226
gl.bindFramebuffer(gl.FRAMEBUFFER, this.textureBuffer.frameBuffer );
228227

229-
if(clear)this.textureBuffer.clear();
228+
if (clear)
229+
{
230+
this.textureBuffer.clear();
231+
}
230232

231233
this.renderer.spriteBatch.dirty = true;
232234

233-
this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer);
235+
this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer, matrix);
234236

235237
this.renderer.spriteBatch.dirty = true;
236-
};
237238

239+
};
238240

239241
/**
240242
* This function will draw the display object to the texture.
@@ -247,32 +249,32 @@ PIXI.RenderTexture.prototype.renderWebGL = function(displayObject, matrix, clear
247249
*/
248250
PIXI.RenderTexture.prototype.renderCanvas = function(displayObject, matrix, clear)
249251
{
250-
if(!this.valid)return;
252+
if (!this.valid)
253+
{
254+
return;
255+
}
251256

252-
var wt = displayObject.worldTransform;
253-
wt.identity();
254-
if(matrix)wt.append(matrix);
255-
256257
// setWorld Alpha to ensure that the object is renderer at full opacity
257258
displayObject.worldAlpha = 1;
258259

259-
// Time to update all the children of the displayObject with the new matrix..
260-
var children = displayObject.children;
261-
262-
for(var i = 0, j = children.length; i < j; i++)
260+
// Time to update all the children of the displayObject with the new matrix (what new matrix? there isn't one!)
261+
for (var i = 0; i < displayObject.children.length; i++)
263262
{
264-
children[i].updateTransform();
263+
displayObject.children[i].updateTransform();
265264
}
266265

267-
if(clear)this.textureBuffer.clear();
266+
if (clear)
267+
{
268+
this.textureBuffer.clear();
269+
}
268270

269271
var context = this.textureBuffer.context;
270272

271273
var realResolution = this.renderer.resolution;
272274

273275
this.renderer.resolution = this.resolution;
274276

275-
this.renderer.renderDisplayObject(displayObject, context);
277+
this.renderer.renderDisplayObject(displayObject, context, matrix);
276278

277279
this.renderer.resolution = realResolution;
278280
};

0 commit comments

Comments
 (0)