Skip to content

Commit cdfc4ff

Browse files
committed
Pixi.Sprite.renderCanvas and renderWebGL now has a new optional matrix parameter. You can use this to render the Sprite with an alternative transform matrix without actually adjusting the Sprite matrix at all.
1 parent 1d8f881 commit cdfc4ff

4 files changed

Lines changed: 93 additions & 68 deletions

File tree

src/pixi/display/Sprite.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,23 @@ PIXI.Sprite.prototype.getBounds = function(matrix)
260260
*
261261
* @method _renderWebGL
262262
* @param renderSession {RenderSession}
263+
* @param {Matrix} [matrix] - Optional matrix. If provided the Display Object will be rendered using this matrix, otherwise it will use its worldTransform.
263264
* @private
264265
*/
265-
PIXI.Sprite.prototype._renderWebGL = function(renderSession)
266+
PIXI.Sprite.prototype._renderWebGL = function(renderSession, matrix)
266267
{
267268
// if the sprite is not visible or the alpha is 0 then no need to render this element
268269
if (!this.visible || this.alpha <= 0 || !this.renderable) return;
269270

270-
var i, j;
271+
// They provided an alternative rendering matrix, so use it
272+
var wt = this.worldTransform;
271273

272-
// do a quick check to see if this element has a mask or a filter.
274+
if (matrix)
275+
{
276+
wt = matrix;
277+
}
278+
279+
// A quick check to see if this element has a mask or a filter.
273280
if (this._mask || this._filters)
274281
{
275282
var spriteBatch = renderSession.spriteBatch;
@@ -292,7 +299,7 @@ PIXI.Sprite.prototype._renderWebGL = function(renderSession)
292299
spriteBatch.render(this);
293300

294301
// now loop through the children and make sure they get rendered
295-
for (i = 0; i < this.children.length; i++)
302+
for (var i = 0; i < this.children.length; i++)
296303
{
297304
this.children[i]._renderWebGL(renderSession);
298305
}
@@ -307,12 +314,12 @@ PIXI.Sprite.prototype._renderWebGL = function(renderSession)
307314
}
308315
else
309316
{
310-
renderSession.spriteBatch.render(this);
317+
renderSession.spriteBatch.render(this, wt);
311318

312-
// simple render children!
313-
for (i = 0; i < this.children.length; i++)
319+
// Render children!
320+
for (var i = 0; i < this.children.length; i++)
314321
{
315-
this.children[i]._renderWebGL(renderSession);
322+
this.children[i]._renderWebGL(renderSession, wt);
316323
}
317324

318325
}
@@ -323,13 +330,22 @@ PIXI.Sprite.prototype._renderWebGL = function(renderSession)
323330
*
324331
* @method _renderCanvas
325332
* @param renderSession {RenderSession}
333+
* @param {Matrix} [matrix] - Optional matrix. If provided the Display Object will be rendered using this matrix, otherwise it will use its worldTransform.
326334
* @private
327335
*/
328-
PIXI.Sprite.prototype._renderCanvas = function(renderSession)
336+
PIXI.Sprite.prototype._renderCanvas = function(renderSession, matrix)
329337
{
330338
// If the sprite is not visible or the alpha is 0 then no need to render this element
331339
if (this.visible === false || this.alpha === 0 || this.renderable === false || this.texture.crop.width <= 0 || this.texture.crop.height <= 0) return;
332340

341+
// They provided an alternative rendering matrix, so use it
342+
var wt = this.worldTransform;
343+
344+
if (matrix)
345+
{
346+
wt = matrix;
347+
}
348+
333349
if (this.blendMode !== renderSession.currentBlendMode)
334350
{
335351
renderSession.currentBlendMode = this.blendMode;
@@ -363,24 +379,24 @@ PIXI.Sprite.prototype._renderCanvas = function(renderSession)
363379
if (renderSession.roundPixels)
364380
{
365381
renderSession.context.setTransform(
366-
this.worldTransform.a,
367-
this.worldTransform.b,
368-
this.worldTransform.c,
369-
this.worldTransform.d,
370-
(this.worldTransform.tx * renderSession.resolution) | 0,
371-
(this.worldTransform.ty * renderSession.resolution) | 0);
382+
wt.a,
383+
wt.b,
384+
wt.c,
385+
wt.d,
386+
(wt.tx * renderSession.resolution) | 0,
387+
(wt.ty * renderSession.resolution) | 0);
372388
dx = dx | 0;
373389
dy = dy | 0;
374390
}
375391
else
376392
{
377393
renderSession.context.setTransform(
378-
this.worldTransform.a,
379-
this.worldTransform.b,
380-
this.worldTransform.c,
381-
this.worldTransform.d,
382-
this.worldTransform.tx * renderSession.resolution,
383-
this.worldTransform.ty * renderSession.resolution);
394+
wt.a,
395+
wt.b,
396+
wt.c,
397+
wt.d,
398+
wt.tx * renderSession.resolution,
399+
wt.ty * renderSession.resolution);
384400
}
385401

386402
if (this.tint !== 0xFFFFFF)

src/pixi/renderers/canvas/CanvasRenderer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,14 @@ PIXI.CanvasRenderer.prototype.resize = function(width, height)
269269
* @method renderDisplayObject
270270
* @param displayObject {DisplayObject} The displayObject to render
271271
* @param context {CanvasRenderingContext2D} the context 2d method of the canvas
272+
* @param [matrix] {Matrix} Optional matrix to apply to the display object before rendering.
272273
* @private
273274
*/
274-
PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject, context)
275+
PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject, context, matrix)
275276
{
276277
this.renderSession.context = context || this.context;
277278
this.renderSession.resolution = this.resolution;
278-
displayObject._renderCanvas(this.renderSession);
279+
displayObject._renderCanvas(this.renderSession, matrix);
279280
};
280281

281282
/**

src/pixi/renderers/webgl/WebGLRenderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ PIXI.WebGLRenderer.prototype.render = function(stage)
327327
* @param projection {Point} The projection
328328
* @param buffer {Array} a standard WebGL buffer
329329
*/
330-
PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer)
330+
PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer, matrix)
331331
{
332332
this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL);
333333

@@ -350,7 +350,7 @@ PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, proje
350350
this.filterManager.begin(this.renderSession, buffer);
351351

352352
// render the scene!
353-
displayObject._renderWebGL(this.renderSession);
353+
displayObject._renderWebGL(this.renderSession, matrix);
354354

355355
// finish the sprite batch
356356
this.spriteBatch.end();

src/pixi/renderers/webgl/utils/WebGLSpriteBatch.js

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -201,23 +201,35 @@ PIXI.WebGLSpriteBatch.prototype.end = function()
201201
/**
202202
* @method render
203203
* @param sprite {Sprite} the sprite to render when using this spritebatch
204+
* @param {Matrix} [matrix] - Optional matrix. If provided the Display Object will be rendered using this matrix, otherwise it will use its worldTransform.
204205
*/
205-
PIXI.WebGLSpriteBatch.prototype.render = function(sprite)
206+
PIXI.WebGLSpriteBatch.prototype.render = function(sprite, matrix)
206207
{
207208
var texture = sprite.texture;
208209

209-
//TODO set blend modes..
210+
// They provided an alternative rendering matrix, so use it
211+
var wt = sprite.worldTransform;
212+
213+
if (matrix)
214+
{
215+
wt = matrix;
216+
}
217+
210218
// check texture..
211-
if(this.currentBatchSize >= this.size)
219+
if (this.currentBatchSize >= this.size)
212220
{
213221
this.flush();
214222
this.currentBaseTexture = texture.baseTexture;
215223
}
216224

217225
// get the uvs for the texture
218226
var uvs = texture._uvs;
227+
219228
// if the uvs have not updated then no point rendering just yet!
220-
if(!uvs)return;
229+
if (!uvs)
230+
{
231+
return;
232+
}
221233

222234
// TODO trim??
223235
var aX = sprite.anchor.x;
@@ -227,15 +239,14 @@ PIXI.WebGLSpriteBatch.prototype.render = function(sprite)
227239

228240
if (texture.trim)
229241
{
230-
// if the sprite is trimmed then we need to add the extra space before transforming the sprite coords..
242+
// if the sprite is trimmed then we need to add the extra space before transforming the sprite coords.
231243
var trim = texture.trim;
232244

233245
w1 = trim.x - aX * trim.width;
234246
w0 = w1 + texture.crop.width;
235247

236248
h1 = trim.y - aY * trim.height;
237249
h0 = h1 + texture.crop.height;
238-
239250
}
240251
else
241252
{
@@ -246,83 +257,80 @@ PIXI.WebGLSpriteBatch.prototype.render = function(sprite)
246257
h1 = texture.frame.height * -aY;
247258
}
248259

249-
var index = this.currentBatchSize * 4 * this.vertSize;
250-
260+
var i = this.currentBatchSize * 4 * this.vertSize;
251261
var resolution = texture.baseTexture.resolution;
252262

253-
var worldTransform = sprite.worldTransform;
254-
255-
var a = worldTransform.a / resolution;
256-
var b = worldTransform.b / resolution;
257-
var c = worldTransform.c / resolution;
258-
var d = worldTransform.d / resolution;
259-
var tx = worldTransform.tx;
260-
var ty = worldTransform.ty;
263+
var a = wt.a / resolution;
264+
var b = wt.b / resolution;
265+
var c = wt.c / resolution;
266+
var d = wt.d / resolution;
267+
var tx = wt.tx;
268+
var ty = wt.ty;
261269

262270
var colors = this.colors;
263271
var positions = this.positions;
264272

265-
if(this.renderSession.roundPixels)
273+
if (this.renderSession.roundPixels)
266274
{
267275
// xy
268-
positions[index] = a * w1 + c * h1 + tx | 0;
269-
positions[index+1] = d * h1 + b * w1 + ty | 0;
276+
positions[i] = a * w1 + c * h1 + tx | 0;
277+
positions[i+1] = d * h1 + b * w1 + ty | 0;
270278

271279
// xy
272-
positions[index+5] = a * w0 + c * h1 + tx | 0;
273-
positions[index+6] = d * h1 + b * w0 + ty | 0;
280+
positions[i+5] = a * w0 + c * h1 + tx | 0;
281+
positions[i+6] = d * h1 + b * w0 + ty | 0;
274282

275283
// xy
276-
positions[index+10] = a * w0 + c * h0 + tx | 0;
277-
positions[index+11] = d * h0 + b * w0 + ty | 0;
284+
positions[i+10] = a * w0 + c * h0 + tx | 0;
285+
positions[i+11] = d * h0 + b * w0 + ty | 0;
278286

279287
// xy
280-
positions[index+15] = a * w1 + c * h0 + tx | 0;
281-
positions[index+16] = d * h0 + b * w1 + ty | 0;
288+
positions[i+15] = a * w1 + c * h0 + tx | 0;
289+
positions[i+16] = d * h0 + b * w1 + ty | 0;
282290
}
283291
else
284292
{
285293
// xy
286-
positions[index] = a * w1 + c * h1 + tx;
287-
positions[index+1] = d * h1 + b * w1 + ty;
294+
positions[i] = a * w1 + c * h1 + tx;
295+
positions[i+1] = d * h1 + b * w1 + ty;
288296

289297
// xy
290-
positions[index+5] = a * w0 + c * h1 + tx;
291-
positions[index+6] = d * h1 + b * w0 + ty;
298+
positions[i+5] = a * w0 + c * h1 + tx;
299+
positions[i+6] = d * h1 + b * w0 + ty;
292300

293301
// xy
294-
positions[index+10] = a * w0 + c * h0 + tx;
295-
positions[index+11] = d * h0 + b * w0 + ty;
302+
positions[i+10] = a * w0 + c * h0 + tx;
303+
positions[i+11] = d * h0 + b * w0 + ty;
296304

297305
// xy
298-
positions[index+15] = a * w1 + c * h0 + tx;
299-
positions[index+16] = d * h0 + b * w1 + ty;
306+
positions[i+15] = a * w1 + c * h0 + tx;
307+
positions[i+16] = d * h0 + b * w1 + ty;
300308
}
301309

302310
// uv
303-
positions[index+2] = uvs.x0;
304-
positions[index+3] = uvs.y0;
311+
positions[i+2] = uvs.x0;
312+
positions[i+3] = uvs.y0;
305313

306314
// uv
307-
positions[index+7] = uvs.x1;
308-
positions[index+8] = uvs.y1;
315+
positions[i+7] = uvs.x1;
316+
positions[i+8] = uvs.y1;
309317

310318
// uv
311-
positions[index+12] = uvs.x2;
312-
positions[index+13] = uvs.y2;
319+
positions[i+12] = uvs.x2;
320+
positions[i+13] = uvs.y2;
313321

314322
// uv
315-
positions[index+17] = uvs.x3;
316-
positions[index+18] = uvs.y3;
323+
positions[i+17] = uvs.x3;
324+
positions[i+18] = uvs.y3;
317325

318326
// color and alpha
319327
var tint = sprite.tint;
320-
colors[index+4] = colors[index+9] = colors[index+14] = colors[index+19] = (tint >> 16) + (tint & 0xff00) + ((tint & 0xff) << 16) + (sprite.worldAlpha * 255 << 24);
328+
329+
colors[i+4] = colors[i+9] = colors[i+14] = colors[i+19] = (tint >> 16) + (tint & 0xff00) + ((tint & 0xff) << 16) + (sprite.worldAlpha * 255 << 24);
321330

322331
// increment the batchsize
323332
this.sprites[this.currentBatchSize++] = sprite;
324333

325-
326334
};
327335

328336
/**

0 commit comments

Comments
 (0)