Skip to content

Commit 441becd

Browse files
committed
Static Tilemap Culling
1 parent b5d3d5a commit 441becd

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'db055440-5835-11e7-821c-f3e448a2ca69'
2+
build: '4b54cc50-5861-11e7-9b35-1d9d7e24430e'
33
};
44
module.exports = CHECKSUM;

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ var StaticTilemap = new Class({
4343
this.mapHeight = mapHeight;
4444
this.dirty = true;
4545
this.vertexCount = 0;
46+
this.cullStart = 0;
47+
this.cullEnd = 0;
4648
this.setTexture(texture, frame);
4749
this.setPosition(x, y);
4850
this.setSizeToFrame();
4951
this.setOrigin();
5052
this.setSize(tileWidth * mapWidth, tileHeight * mapHeight);
5153
},
5254

53-
upload: function (scrollX, scrollY)
55+
upload: function (camera)
5456
{
5557
if (this.gl)
5658
{
@@ -147,10 +149,52 @@ var StaticTilemap = new Class({
147149

148150
this.dirty = false;
149151
}
150-
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.scrollLocation, -scrollX, -scrollY);
152+
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.scrollLocation, -camera.scrollX, -camera.scrollY);
151153
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.scrollFactorLocation, this.scrollFactorX, this.scrollFactorY);
152154
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.tilemapPositionLocation, this.x, this.y);
153155
}
156+
},
157+
158+
getTotalTileCount: function ()
159+
{
160+
return this.mapData.length;
161+
},
162+
163+
getVisibleTileCount: function (camera)
164+
{
165+
this.cull(camera);
166+
return (this.cullEnd - this.cullStart) / 6;
167+
},
168+
169+
cull: function (camera)
170+
{
171+
this.cullStart = 0;
172+
this.cullEnd = 0;
173+
var tileWidth = this.tileWidth;
174+
var tileHeight = this.tileHeight;
175+
var pixelX = this.x - (camera.scrollX * this.scrollFactorX);
176+
var pixelY = this.y - (camera.scrollY * this.scrollFactorY);
177+
var pixelWidth = this.mapWidth * tileWidth;
178+
var pixelHeight = this.mapHeight * tileHeight;
179+
180+
if (pixelX < camera.width + tileWidth &&
181+
pixelX + pixelWidth > -tileWidth &&
182+
pixelY < camera.height + tileHeight &&
183+
pixelY + pixelHeight > -tileHeight)
184+
{
185+
var interX = Math.max(pixelX, -tileWidth);
186+
var interY = Math.max(pixelY, -tileHeight);
187+
var interWidth = Math.min(pixelX + pixelWidth, camera.width + tileWidth) - interX;
188+
var interHeight = Math.min(pixelY + pixelHeight, camera.height + tileHeight) - interY;
189+
190+
interX = ((interX + (camera.scrollX * this.scrollFactorX)) / tileWidth)|0;
191+
interY = ((interY + (camera.scrollY * this.scrollFactorY)) / tileHeight)|0;
192+
interWidth = (interWidth / tileWidth)|0;
193+
interHeight = (interHeight / tileHeight)|0;
194+
195+
this.cullStart = (interY * this.mapWidth + interX) * 6;
196+
this.cullEnd = ((interY + interHeight) * this.mapWidth + interX) * 6;
197+
}
154198
}
155199

156200
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var StaticTilemapWebGLRenderer = function (renderer, src, interpolationPercentag
1111

1212
renderer.setRenderer(gameObject.tilemapRenderer, frame.texture.source[frame.sourceIndex].glTexture, gameObject.renderTarget);
1313
gameObject.tilemapRenderer.bind();
14-
gameObject.upload(camera.scrollX * src.scrollFactorX, camera.scrollY * src.scrollFactorY);
14+
gameObject.upload(camera);
1515
gameObject.vbo.bind();
1616
gl.drawArrays(gl.TRIANGLES, 0, gameObject.vertexCount);
1717
};

0 commit comments

Comments
 (0)