Skip to content

Commit 70b2349

Browse files
committed
Updated the canvas tilemap layer renderers to support parent matrix and tidied up the internal flow
1 parent 6ef7033 commit 70b2349

2 files changed

Lines changed: 75 additions & 39 deletions

File tree

src/tilemaps/dynamiclayer/DynamicTilemapLayerCanvasRenderer.js

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,96 @@
1717
* @param {Phaser.Tilemaps.DynamicTilemapLayer} src - The Game Object being rendered in this call.
1818
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
1919
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
20+
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
2021
*/
21-
var DynamicTilemapLayerCanvasRenderer = function (renderer, src, interpolationPercentage, camera)
22+
var DynamicTilemapLayerCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2223
{
2324
src.cull(camera);
2425

2526
var renderTiles = src.culledTiles;
26-
var length = renderTiles.length;
27-
var image = src.tileset.image.getSourceImage();
28-
var tileset = this.tileset;
27+
var tileCount = renderTiles.length;
2928

30-
var tx = src.x - camera.scrollX * src.scrollFactorX;
31-
var ty = src.y - camera.scrollY * src.scrollFactorY;
32-
var ctx = renderer.currentContext;
29+
if (tileCount === 0)
30+
{
31+
return;
32+
}
3333

34-
ctx.save();
35-
ctx.translate(tx, ty);
36-
ctx.rotate(src.rotation);
37-
ctx.scale(src.scaleX, src.scaleY);
38-
ctx.scale(src.flipX ? -1 : 1, src.flipY ? -1 : 1);
34+
var camMatrix = renderer._tempMatrix1;
35+
var layerMatrix = renderer._tempMatrix2;
36+
var calcMatrix = renderer._tempMatrix3;
37+
38+
layerMatrix.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY);
39+
40+
camMatrix.copyFrom(camera.matrix);
3941

40-
for (var index = 0; index < length; ++index)
42+
if (parentMatrix)
4143
{
42-
var tile = renderTiles[index];
44+
// Multiply the camera by the parent matrix
45+
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
4346

44-
var tileTexCoords = tileset.getTileTextureCoordinates(tile.index);
45-
if (tileTexCoords === null) { continue; }
47+
// Undo the camera scroll
48+
layerMatrix.e = src.x;
49+
layerMatrix.f = src.y;
4650

47-
var halfWidth = tile.width / 2;
48-
var halfHeight = tile.height / 2;
51+
// Multiply by the Sprite matrix, store result in calcMatrix
52+
camMatrix.multiply(layerMatrix, calcMatrix);
53+
}
54+
else
55+
{
56+
layerMatrix.e -= camera.scrollX * src.scrollFactorX;
57+
layerMatrix.f -= camera.scrollY * src.scrollFactorY;
4958

50-
ctx.save();
51-
ctx.translate(tile.pixelX + halfWidth, tile.pixelY + halfHeight);
59+
// Multiply by the Sprite matrix, store result in calcMatrix
60+
camMatrix.multiply(layerMatrix, calcMatrix);
61+
}
5262

53-
if (tile.rotation !== 0)
54-
{
55-
ctx.rotate(tile.rotation);
56-
}
63+
var tileset = src.tileset;
64+
var ctx = renderer.currentContext;
65+
var image = tileset.image.getSourceImage();
5766

58-
if (tile.flipX || tile.flipY)
59-
{
60-
ctx.scale(tile.flipX ? -1 : 1, tile.flipY ? -1 : 1);
61-
}
67+
ctx.save();
68+
69+
calcMatrix.copyToContext(ctx);
6270

63-
ctx.globalAlpha = camera.alpha * src.alpha * tile.alpha;
71+
var alpha = camera.alpha * src.alpha;
6472

65-
ctx.drawImage(
66-
image,
67-
tileTexCoords.x, tileTexCoords.y,
68-
tile.width, tile.height,
69-
-halfWidth, -halfHeight,
70-
tile.width, tile.height
71-
);
73+
for (var i = 0; i < tileCount; i++)
74+
{
75+
var tile = renderTiles[i];
7276

73-
ctx.restore();
77+
var tileTexCoords = tileset.getTileTextureCoordinates(tile.index);
78+
79+
if (tileTexCoords)
80+
{
81+
var halfWidth = tile.width / 2;
82+
var halfHeight = tile.height / 2;
83+
84+
ctx.save();
85+
86+
ctx.translate(tile.pixelX + halfWidth, tile.pixelY + halfHeight);
87+
88+
if (tile.rotation !== 0)
89+
{
90+
ctx.rotate(tile.rotation);
91+
}
92+
93+
if (tile.flipX || tile.flipY)
94+
{
95+
ctx.scale((tile.flipX) ? -1 : 1, (tile.flipY) ? -1 : 1);
96+
}
97+
98+
ctx.globalAlpha = alpha * tile.alpha;
99+
100+
ctx.drawImage(
101+
image,
102+
tileTexCoords.x, tileTexCoords.y,
103+
tile.width, tile.height,
104+
-halfWidth, -halfHeight,
105+
tile.width, tile.height
106+
);
107+
108+
ctx.restore();
109+
}
74110
}
75111

76112
ctx.restore();

src/tilemaps/staticlayer/StaticTilemapLayerCanvasRenderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ var StaticTilemapLayerCanvasRenderer = function (renderer, src, interpolationPer
7070

7171
ctx.globalAlpha = camera.alpha * src.alpha;
7272

73-
for (var index = 0; index < tileCount; ++index)
73+
for (var i = 0; i < tileCount; i++)
7474
{
75-
var tile = renderTiles[index];
75+
var tile = renderTiles[i];
7676

7777
var tileTexCoords = tileset.getTileTextureCoordinates(tile.index);
7878

0 commit comments

Comments
 (0)