Skip to content

Commit 7c2dae0

Browse files
committed
Add offset parameter to TilemapLayerGL and use it with clipping to draw a view into a map at a specified location and size in the game window.
1 parent c36babc commit 7c2dae0

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/pixi/extras/Tilemap.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ PIXI.Tilemap.prototype._renderWholeTilemap = function (renderSession) {
350350
// set the global offset (e.g. screen shake)
351351
gl.uniform2f(shader.uOffset, renderSession.offset.x / this.game.width * 2, -renderSession.offset.y / this.game.height * 2);
352352

353-
// set the clippling limits
353+
// set the clipping limits
354+
gl.uniform2f(shader.uClipOffset, this.offset.x / this.game.width * 2, this.offset.y / this.game.height * 2);
355+
gl.uniform2f(shader.uClipLoc, this.offset.x, this.offset.y);
354356
gl.uniform2f(shader.uClipping, this.displayWidth, this.game.height - this.displayHeight);
355357

356358
// set the offset in screen units to the center of the screen

src/pixi/renderers/webgl/shaders/TilemapShader.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ PIXI.TilemapShader = function (gl) {
5050
'precision lowp float;',
5151
'uniform sampler2D uImageSampler;',
5252
'uniform float uAlpha;',
53-
'uniform vec2 uClipping;',
53+
'uniform vec2 uClipLoc;',
54+
'uniform vec2 uClipLimit;',
5455
'varying vec2 vTexCoord;',
5556
'void main(void) {',
56-
' if ( gl_FragCoord.x < uClipping.x && gl_FragCoord.y > uClipping.y)',
57+
' if ( gl_FragCoord.x >= uClipLoc.x && gl_FragCoord.y >= uClipLoc.y && gl_FragCoord.x < uClipLimit.x && gl_FragCoord.y > uClipLimit.y )',
5758
' gl_FragColor = texture2D(uImageSampler, vTexCoord) * uAlpha;',
5859
'}'
5960
];
@@ -63,11 +64,12 @@ PIXI.TilemapShader = function (gl) {
6364
'uniform vec2 uOffset;',
6465
'uniform vec2 uCentreOffset;',
6566
'uniform vec2 uScale;',
67+
'uniform vec2 uClipOffset;',
6668
'attribute vec4 aPosition;',
6769
'varying vec2 vTexCoord;',
6870
'void main(void) {',
6971
' gl_Position.zw = vec2(1, 1);',
70-
' gl_Position.xy = (aPosition.xy + uOffset + uCentreOffset) * uScale - uCentreOffset;',
72+
' gl_Position.xy = (aPosition.xy + uClipOffset + uOffset + uCentreOffset) * uScale - uCentreOffset;',
7173
' vTexCoord = aPosition.zw;',
7274
'}'
7375
];
@@ -100,14 +102,29 @@ PIXI.TilemapShader.prototype.init = function () {
100102
// get and store the attributes
101103
this.aPosition = gl.getAttribLocation(program, 'aPosition');
102104
this.uSampler = gl.getUniformLocation(program, 'uImageSampler');
103-
this.uClipping = gl.getUniformLocation(program, 'uClipping');
105+
106+
// clipping uniforms...
107+
108+
// clipping start location (pixels)
109+
this.uClipLoc = gl.getUniformLocation(program, 'uClipLoc');
110+
// clipping width/height (pixels)
111+
this.uClipLimit = gl.getUniformLocation(program, 'uClipLimit');
112+
// clipping start location in webGl coordinates (-1...+1)
113+
this.uClipOffset = gl.getUniformLocation(program, 'uClipOffset');
114+
115+
// offset for screen shake effect etc
104116
this.uOffset = gl.getUniformLocation(program, 'uOffset');
117+
118+
// centre of a tile for offset before scaling (so tiles scale from the centre out)
105119
this.uCentreOffset = gl.getUniformLocation(program, 'uCentreOffset');
106-
this.uAlpha = gl.getUniformLocation(program, 'uAlpha');
120+
// scale factor for tiles
107121
this.uScale = gl.getUniformLocation(program, 'uScale');
108122

123+
// alpha blending
124+
this.uAlpha = gl.getUniformLocation(program, 'uAlpha');
125+
109126
this.attributes = [this.aPosition];
110-
this.uniforms = [this.uClipping, this.uOffset, this.uCentreOffset, this.uAlpha, this.uScale, this.uSampler];
127+
this.uniforms = [this.uClipOffset, this.uClipLoc, this.uClipLimit, this.uOffset, this.uCentreOffset, this.uAlpha, this.uScale, this.uSampler];
111128

112129
this.program = program;
113130

src/tilemap/TilemapLayerGL.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Phaser.TilemapLayerGL = function (game, tilemap, index, width, height) {
2828

2929
this.game = game;
3030

31+
32+
this.offset = new Phaser.Point();
33+
3134
/**
3235
* The Tilemap to which this layer is bound.
3336
* @property {Phaser.Tilemap} map

0 commit comments

Comments
 (0)