Skip to content

Commit 9e3a7ec

Browse files
committed
Modified the batch creation code and the batch drawing code to only insert degenerate triangles at the end of rows or when a row is broken (e.g. by some empty tiles which we won't draw at all). This should speed things up by optimising the draw, and reducing the amount of data required to describe the batch.
PIXI.Tilemap - _renderBatch using a local 'degenerate' flag to signal to next iteration that one should be inserted before the next triangle. Phaser.TilemapLayerGL - add degenerate markers at end of rows and whenever a row has a break in it (e.g. empty tiles) Phaser.Tileset - new addDegenerate function which prevents double markers in a row
1 parent 626d0cf commit 9e3a7ec

4 files changed

Lines changed: 33 additions & 4 deletions

File tree

docs/_phaser_tilemap_GL_progress.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,6 @@ Added alpha blending based on TilemapLayerGL.alpha property. NOTE: this appears
188188
Prepared code to pass each Tile alpha value through to the batch renderer, however realised that implementing this will break the batching!
189189
Need to talk with Rich about it before proceeding further.
190190

191+
Modified the batch creation code and the batch drawing code to only insert degenerate triangles at the end of rows or when a row is broken (e.g. by some empty tiles which we won't draw at all). This should speed things up by optimising the draw, and reducing the amount of data required to describe the batch.
192+
193+

src/pixi/extras/Tilemap.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ PIXI.Tilemap.prototype._renderBatch = function( renderSession )
191191

192192
// process entire glBatch into a single webGl draw buffer for a TRIANGLE_STRIP blit
193193
var c = 0;
194+
var degenerate = false;
194195
for(var i = 0, l = this.glBatch.length; i < l; i++)
195196
{
196197
// sx: this.drawCoords[coordIndex],
@@ -203,6 +204,13 @@ PIXI.Tilemap.prototype._renderBatch = function( renderSession )
203204
// dh: this.tileHeight
204205
var t = this.glBatch[i];
205206

207+
if ( !t )
208+
{
209+
// insert a degenerate triangle when null is found in the list of batch objects
210+
degenerate = true;
211+
continue;
212+
}
213+
206214
var x = t.dx * iWide - 1;
207215
var y = 1 - t.dy * iHigh;
208216

@@ -212,9 +220,8 @@ PIXI.Tilemap.prototype._renderBatch = function( renderSession )
212220
var uvl = t.sx * iTextureWide;
213221
var uvt = t.sy * iTextureHigh;
214222

215-
// for every tile except the first one, use degenerate triangle to separate the tiles
216-
// TODO: it might be possible to avoid this by drawing triangles more intelligently (e.g. each row with inverting the first/second triangle alternately)
217-
if ( c > 0 )
223+
// insert a degenerate triangle to separate the tiles
224+
if ( degenerate )
218225
{
219226
// add a degenerate triangle: repeat the last vertex
220227
buffer[ c ] = oldR;
@@ -228,6 +235,7 @@ PIXI.Tilemap.prototype._renderBatch = function( renderSession )
228235

229236
// advance the buffer index
230237
c += 8;
238+
degenerate = false;
231239
}
232240

233241
// calculate the destination location of the tile in screen units (-1..1)

src/tilemap/TilemapLayerGL.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ Phaser.TilemapLayerGL.prototype.renderRegion = function (scrollX, scrollY, left,
645645
var tw = this._mc.tileWidth;
646646
var th = this._mc.tileHeight;
647647

648-
var lastAlpha = NaN;
648+
// var lastAlpha = NaN;
649649

650650
offx = offx || 0;
651651
offy = offy || 0;
@@ -699,6 +699,8 @@ Phaser.TilemapLayerGL.prototype.renderRegion = function (scrollX, scrollY, left,
699699

700700
if (!tile || tile.index < 0)
701701
{
702+
// skipping some tiles, add a degenerate marker into the batch list
703+
this._mc.tileset.addDegenerate( this.glBatch );
702704
continue;
703705
}
704706

@@ -742,6 +744,8 @@ Phaser.TilemapLayerGL.prototype.renderRegion = function (scrollX, scrollY, left,
742744

743745
}
744746

747+
// at end of each row, add a degenerate marker into the batch list
748+
this._mc.tileset.addDegenerate( this.glBatch );
745749
}
746750

747751
};

src/tilemap/Tileset.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ Phaser.Tileset.prototype = {
189189

190190
},
191191

192+
/**
193+
* adds a marker for the batch display to insert a degenerate triangle (eg. at the end of each row of tiles)
194+
*
195+
* @param {[type]} glBatch [description]
196+
*/
197+
addDegenerate: function( glBatch )
198+
{
199+
// don't insert multiple degenerate markers in a row
200+
if ( glBatch[ glBatch.length - 1] )
201+
{
202+
glBatch.push( null );
203+
}
204+
},
205+
192206
/**
193207
* Returns true if and only if this tileset contains the given tile index.
194208
*

0 commit comments

Comments
 (0)