Skip to content

Commit 9649347

Browse files
committed
Static Tilemap WebGL Renderer now applies camera transform matrix correctly
1 parent 959a920 commit 9649347

5 files changed

Lines changed: 45 additions & 20 deletions

File tree

v3/src/gameobjects/tilemap/static/StaticTilemap.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,19 @@ var StaticTilemap = new Class({
162162

163163
this.dirty = false;
164164
}
165-
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.scrollLocation, -camera.scrollX, -camera.scrollY);
165+
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.scrollLocation, camera.scrollX, camera.scrollY);
166166
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.scrollFactorLocation, this.scrollFactorX, this.scrollFactorY);
167167
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.tilemapPositionLocation, this.x, this.y);
168+
var cmat = camera.matrix.matrix;
169+
170+
this.tilemapRenderer.shader.setConstantMatrix3x3(
171+
this.tilemapRenderer.cameraTransformLocation,
172+
[
173+
cmat[0], cmat[1], 0.0,
174+
cmat[2], cmat[3], 0.0,
175+
cmat[4], cmat[5], 1.0
176+
]
177+
);
168178
}
169179
else if (this.dirty && !this.gl)
170180
{

v3/src/gameobjects/tilemap/static/StaticTilemapCanvasRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var StaticTilemapCanvasRenderer = function (renderer, gameObject, interpolationP
99

1010
gameObject.upload(camera);
1111

12-
var tiles = camera.cullTilemap(gameObject);
12+
var tiles = gameObject.tiles;
1313
var tileWidth = gameObject.tileWidth;
1414
var tileHeight = gameObject.tileHeight;
1515
var frame = gameObject.frame;

v3/src/renderer/canvas/CanvasRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ var CanvasRenderer = new Class({
180180
if (!camera.transparent)
181181
{
182182
ctx.fillStyle = camera.backgroundColor.rgba;
183-
ctx.fillRect(0, 0, camera.width, camera.height);
183+
ctx.fillRect(camera.x, camera.y, camera.width, camera.height);
184184
}
185185

186186
if (this.currentAlpha !== 1)

v3/src/renderer/webgl/renderers/tilemaprenderer/TilemapRenderer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ var TilemapRenderer = new Class({
5050
var scrollLocation = shader.getUniformLocation('u_scroll');
5151
var scrollFactorLocation = shader.getUniformLocation('u_scroll_factor');
5252
var tilemapPositionLocation = shader.getUniformLocation('u_tilemap_position');
53+
var cameraTransformLocation = shader.getUniformLocation('u_camera_matrix');
5354

5455
this.shader = shader;
5556
this.viewMatrixLocation = viewMatrixLocation;
5657
this.scrollLocation = scrollLocation;
5758
this.scrollFactorLocation = scrollFactorLocation;
5859
this.tilemapPositionLocation = tilemapPositionLocation;
60+
this.cameraTransformLocation = cameraTransformLocation;
5961

6062
this.resize(this.width, this.height, this.game.config.resolution);
6163
},
Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
module.exports = {
22
vert: [
3-
'uniform mat4 u_view_matrix;',
4-
'uniform vec2 u_scroll;',
5-
'uniform vec2 u_scroll_factor;',
6-
'uniform vec2 u_tilemap_position;',
7-
'attribute vec2 a_position;',
8-
'attribute vec2 a_tex_coord;',
9-
'varying vec2 v_tex_coord;',
10-
'void main () {',
11-
' gl_Position = u_view_matrix * vec4(u_tilemap_position + a_position + (u_scroll * u_scroll_factor), 1.0, 1.0);',
12-
' v_tex_coord = a_tex_coord;',
13-
'}'
3+
'precision mediump float;',
4+
'',
5+
'uniform mat4 u_view_matrix;',
6+
'uniform mat3 u_camera_matrix;',
7+
'uniform vec2 u_scroll;',
8+
'uniform vec2 u_scroll_factor;',
9+
'uniform vec2 u_tilemap_position;',
10+
'',
11+
'attribute vec2 a_position;',
12+
'attribute vec2 a_tex_coord;',
13+
'varying vec2 v_tex_coord;',
14+
'',
15+
'void main()',
16+
'{',
17+
' vec2 position = u_tilemap_position + a_position;',
18+
' ',
19+
' position = position - (u_scroll * u_scroll_factor);',
20+
' position = (u_camera_matrix * vec3(position, 1.0)).xy;',
21+
' ',
22+
' gl_Position = u_view_matrix * vec4(position, 1.0, 1.0);',
23+
' v_tex_coord = a_tex_coord;',
24+
'}',
25+
''
1426
].join('\n'),
1527
frag: [
16-
'precision mediump float;',
17-
'uniform sampler2D u_sampler2D;',
18-
'varying vec2 v_tex_coord;',
19-
'void main() {',
20-
' gl_FragColor = texture2D(u_sampler2D, v_tex_coord);',
21-
'}'
28+
'precision mediump float;',
29+
'uniform sampler2D u_sampler2D;',
30+
'varying vec2 v_tex_coord;',
31+
'void main()',
32+
'{',
33+
' gl_FragColor = texture2D(u_sampler2D, v_tex_coord);',
34+
'}'
2235
].join('\n')
2336
};

0 commit comments

Comments
 (0)