Skip to content

Commit 6d1b172

Browse files
committed
Add flip capability to dynamic webgl & canvas tilemap renderers
1 parent 6f4f571 commit 6d1b172

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayerCanvasRenderer.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,28 @@ var DynamicTilemapLayerCanvasRenderer = function (renderer, gameObject, interpol
3131
var tileTexCoords = tileset.getTileTextureCoordinates(tile.index);
3232
if (tileTexCoords === null) { continue; }
3333

34+
var halfWidth = tile.width / 2;
35+
var halfHeight = tile.height / 2;
36+
37+
ctx.save();
38+
ctx.translate(tile.worldX - halfWidth, tile.worldY - halfHeight);
39+
40+
if (tile.flipX || tile.flipY)
41+
{
42+
ctx.scale(tile.flipX ? -1 : 1, tile.flipY ? -1 : 1);
43+
}
44+
3445
renderer.setAlpha(gameObject.alpha * tile.alpha);
3546

3647
ctx.drawImage(
3748
image,
3849
tileTexCoords.x, tileTexCoords.y,
3950
tile.width, tile.height,
40-
tile.worldX, tile.worldY,
51+
-halfWidth, -halfHeight,
4152
tile.width, tile.height
4253
);
54+
55+
ctx.restore();
4356
}
4457

4558
ctx.restore();

v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayerWebGLRenderer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ var DynamicTilemapLayerWebGLRenderer = function (renderer, gameObject, interpola
3030
var tileTexCoords = tileset.getTileTextureCoordinates(tile.index);
3131
if (tileTexCoords === null) { continue; }
3232

33+
var frameWidth = tile.width * (tile.flipX ? -1 : 1);
34+
var frameHeight = tile.height * (tile.flipY ? -1 : 1);
35+
var frameX = tileTexCoords.x + (tile.flipX ? tile.width : 0);
36+
var frameY = tileTexCoords.y + (tile.flipY ? tile.height : 0);
37+
3338
batch.addTileTextureRect(
3439
texture,
3540
x + tile.worldX, y + tile.worldY, tile.width, tile.height, alpha * tile.alpha, tile.tint,
3641
scrollFactorX, scrollFactorY,
3742
textureWidth, textureHeight,
38-
tileTexCoords.x, tileTexCoords.y, tile.width, tile.height,
43+
frameX, frameY, frameWidth, frameHeight,
3944
camera,
4045
renderTarget
4146
);

v3/src/renderer/webgl/renderers/spritebatch/SpriteBatch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ var SpriteBatch = new Class({
385385
// Inset UV coordinates by 0.5px to prevent tile bleeding
386386
var u0 = (rectX + 0.5) / textureWidth;
387387
var v0 = (rectY + 0.5) / textureHeight;
388-
var u1 = (rectX - 0.5 + width) / textureWidth;
389-
var v1 = (rectY - 0.5 + height) / textureHeight;
388+
var u1 = (rectX - 0.5 + rectW) / textureWidth;
389+
var v1 = (rectY - 0.5 + rectH) / textureHeight;
390390

391391
mva = cameraMatrix[0];
392392
mvb = cameraMatrix[1];

0 commit comments

Comments
 (0)