Skip to content

Commit b03d3ff

Browse files
committed
Added support for cull padding
1 parent 52f5002 commit b03d3ff

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ There is a new Game Object Component called `TextureCrop`. It replaces the Textu
115115
* `DynamicTilemapLayer.tilesTotal` is a read-only property that contains the total number of tiles in the layer, updated every frame.
116116
* `DynamicTilemapLayer.skipCull` and its associated chainable method `setSkipCull` allows you to control if the cameras should cull the layer tiles before rendering them or not. By default they will cull, to avoid over-rendering, but in some circumstances you may wish to disable this and can now do so by toggling this property.
117117
* The `CullTiles` component, as used by the Dynamic Tilemap, has been recoded from scratch to take advantage of updates in the Camera system. It will now properly cull tiles, irrespective of the layer scale, or camera zoom. It also now supports the layers `skipCull` property, allowing you to override the culling. The Dungeon Generator labs demo now works again as a result of this fix, and has been updated with a debug mode and camera control UI. You can edit the example source to swap between 4 different dungeon layouts, from 2500 tiles up to 1 million tiles. There are limitations to the way the culling works though. If you rotate the camera you may find you see the cull edge. You can disable this using the new `skipCull` property. Fixing this also fixed #3818 (thanks @Mursaat)
118+
* `DynamicTilemapLayer.cullPadding` and its associated chainable method `setCullPadding` allows you to control how many additional tiles are added into the cull rectangle when it is calculated. If you find that your camera size and zoom settings are causing tiles to get prematurely culled, resulting in clipping during scrolling, then set the `cullPadding` values to add extra layers of tiles to the calculations in both directions.
118119

119120
### New Features
120121

src/tilemaps/components/CullTiles.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ var CullTiles = function (layer, camera, outputArray)
3939

4040
// Camera world view bounds, snapped for tile size
4141

42-
var boundsLeft = SnapFloor(camera.worldView.x, tileW) - tileW;
43-
var boundsRight = SnapCeil(camera.worldView.right, tileW) + tileW;
44-
var boundsTop = SnapFloor(camera.worldView.y, tileH) - tileH;
45-
var boundsBottom = SnapCeil(camera.worldView.bottom, tileH) + tileH;
42+
var boundsLeft = SnapFloor(camera.worldView.x, tileW) - (tilemapLayer.cullPadding.x * tileW);
43+
var boundsRight = SnapCeil(camera.worldView.right, tileW) + (tilemapLayer.cullPadding.x * tileW);
44+
var boundsTop = SnapFloor(camera.worldView.y, tileH) - (tilemapLayer.cullPadding.y * tileH);
45+
var boundsBottom = SnapCeil(camera.worldView.bottom, tileH) + (tilemapLayer.cullPadding.y * tileH);
4646

4747
for (var y = 0; y < mapHeight; y++)
4848
{

src/tilemaps/dynamiclayer/DynamicTilemapLayer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Components = require('../../gameobjects/components');
99
var DynamicTilemapLayerRender = require('./DynamicTilemapLayerRender');
1010
var GameObject = require('../../gameobjects/GameObject');
1111
var TilemapComponents = require('../components');
12+
var Vector2 = require('../../math/Vector2');
1213

1314
/**
1415
* @classdesc
@@ -164,6 +165,17 @@ var DynamicTilemapLayer = new Class({
164165
*/
165166
this.tilesTotal = this.layer.width * this.layer.height;
166167

168+
/**
169+
* The amount of tiles to add into the cull rectangle.
170+
*
171+
* See the method `setCullPadding` for more details.
172+
*
173+
* @name Phaser.Tilemaps.DynamicTilemapLayer#cullPadding
174+
* @type {Phaser.Math.Vector2}
175+
* @since 3.11.0
176+
*/
177+
this.cullPadding = new Vector2(1, 1);
178+
167179
this.setAlpha(this.layer.alpha);
168180
this.setPosition(x, y);
169181
this.setOrigin();
@@ -797,6 +809,31 @@ var DynamicTilemapLayer = new Class({
797809
return this;
798810
},
799811

812+
/**
813+
* When a Camera culls the tiles in this layer it does so using its view into the world, building up a
814+
* rectangle inside which the tiles must exist or they will be culled. Sometimes you may need to expand the size
815+
* of this 'cull rectangle', especially if you plan on rotating the Camera viewing the layer. Do so
816+
* by providing the padding values. The values given are in tiles, not pixels. So if the tile width was 32px
817+
* and you set `paddingX` to be 4, it would add 32px x 4 to the cull rectangle (adjusted for scale)
818+
*
819+
* @method Phaser.Tilemaps.DynamicTilemapLayer#setCullPadding
820+
* @since 3.11.0
821+
*
822+
* @param {number} [paddingX=1] - The amount of extra horizontal tiles to add to the cull check padding.
823+
* @param {number} [paddingY=1] - The amount of extra vertical tiles to add to the cull check padding.
824+
*
825+
* @return {this} This Tilemap Layer object.
826+
*/
827+
setCullPadding: function (paddingX, paddingY)
828+
{
829+
if (paddingX === undefined) { paddingX = 1; }
830+
if (paddingY === undefined) { paddingY = 1; }
831+
832+
this.cullPadding.set(paddingX, paddingY);
833+
834+
return this;
835+
},
836+
800837
/**
801838
* Sets collision on the given tile or tiles within a layer by index. You can pass in either a
802839
* single numeric index or an array of indexes: [2, 3, 15, 20]. The `collides` parameter controls if

0 commit comments

Comments
 (0)