@@ -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} ) ;
0 commit comments