Skip to content

Commit 98e0fce

Browse files
committed
Moved transform for rotated texture clipping to a single transform. Added support for webgl rotated textures
1 parent f3c5b7a commit 98e0fce

3 files changed

Lines changed: 80 additions & 13 deletions

File tree

src/pixi/display/Sprite.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,33 @@ PIXI.Sprite.prototype._renderCanvas = function(renderSession, matrix)
482482
var tx = (wt.tx * renderSession.resolution) + renderSession.shakeX;
483483
var ty = (wt.ty * renderSession.resolution) + renderSession.shakeY;
484484

485+
var cw = this.texture.crop.width;
486+
var ch = this.texture.crop.height;
487+
488+
if (this.texture.rotated)
489+
{
490+
var a = wt.a;
491+
var b = wt.b;
492+
var c = wt.c;
493+
var d = wt.d;
494+
var e = cw;
495+
496+
// Offset before rotating
497+
tx = wt.c * ch + tx;
498+
ty = wt.d * ch + ty;
499+
500+
// Rotate matrix by 90 degrees
501+
// We use precalculated values for sine and cosine of rad(90)
502+
wt.a = a * 6.123233995736766e-17 + -c;
503+
wt.b = b * 6.123233995736766e-17 + -d;
504+
wt.c = a + c * 6.123233995736766e-17;
505+
wt.d = b + d * 6.123233995736766e-17;
506+
507+
// Update cropping dimensions.
508+
cw = ch;
509+
ch = e;
510+
}
511+
485512
// Allow for pixel rounding
486513
if (renderSession.roundPixels)
487514
{
@@ -494,19 +521,6 @@ PIXI.Sprite.prototype._renderCanvas = function(renderSession, matrix)
494521
renderSession.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
495522
}
496523

497-
var cw = this.texture.crop.width;
498-
var ch = this.texture.crop.height;
499-
500-
if (this.texture.rotated)
501-
{
502-
// 90 degree coordinate rotation
503-
cw = ch;
504-
ch = this.texture.crop.width;
505-
506-
renderSession.context.translate(0, cw);
507-
renderSession.context.rotate(-1.5707963267948966);
508-
}
509-
510524
dx /= resolution;
511525
dy /= resolution;
512526

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,39 @@ PIXI.WebGLSpriteBatch.prototype.render = function (sprite, matrix) {
286286
var tx = wt.tx;
287287
var ty = wt.ty;
288288

289+
var cw = texture.crop.width;
290+
var ch = texture.crop.height;
291+
292+
if (texture.rotated)
293+
{
294+
var a0 = wt.a;
295+
var b0 = wt.b;
296+
var c0 = wt.c;
297+
var d0 = wt.d;
298+
var _w1 = w1;
299+
var _w0 = w0;
300+
301+
// Offset before rotating
302+
tx = wt.c * ch + tx;
303+
ty = wt.d * ch + ty;
304+
305+
// Rotate matrix by 90 degrees
306+
// We use precalculated values for sine and cosine of rad(90)
307+
a = a0 * 6.123233995736766e-17 + -c0;
308+
b = b0 * 6.123233995736766e-17 + -d0;
309+
c = a0 + c0 * 6.123233995736766e-17;
310+
d = b0 + d0 * 6.123233995736766e-17;
311+
312+
// Update UV coordinates
313+
texture._updateUvsInverted();
314+
315+
// Rotate dimensions
316+
w0 = h0;
317+
w1 = h1;
318+
h0 = _w0;
319+
h1 = _w1;
320+
}
321+
289322
var colors = this.colors;
290323
var positions = this.positions;
291324
var tint = sprite.tint;

src/pixi/textures/Texture.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,26 @@ PIXI.Texture.prototype._updateUvs = function()
248248
this._uvs.y3 = (frame.y + frame.height) / th;
249249
};
250250

251+
PIXI.Texture.prototype._updateUvsInverted = function () {
252+
if(!this._uvs)this._uvs = new PIXI.TextureUvs();
253+
254+
var frame = this.crop;
255+
var tw = this.baseTexture.width;
256+
var th = this.baseTexture.height;
257+
258+
this._uvs.x0 = frame.x / tw;
259+
this._uvs.y0 = frame.y / th;
260+
261+
this._uvs.x1 = (frame.x + frame.height) / tw;
262+
this._uvs.y1 = frame.y / th;
263+
264+
this._uvs.x2 = (frame.x + frame.height) / tw;
265+
this._uvs.y2 = (frame.y + frame.width) / th;
266+
267+
this._uvs.x3 = frame.x / tw;
268+
this._uvs.y3 = (frame.y + frame.width) / th;
269+
};
270+
251271
/**
252272
* Helper function that creates a new a Texture based on the given canvas element.
253273
*

0 commit comments

Comments
 (0)