@@ -44,6 +44,7 @@ var SpriteBatch = function (game, gl, manager)
4444 this . manager = manager ;
4545 this . dirty = false ;
4646 this . drawIndexed = true ;
47+ this . lastDrawIndexed = true ;
4748 this . vertexCount = 0 ;
4849
4950 this . init ( this . glContext ) ;
@@ -95,6 +96,11 @@ SpriteBatch.prototype = {
9596
9697 shouldFlush : function ( )
9798 {
99+ if ( this . drawIndexed != this . lastDrawIndexed )
100+ {
101+ this . lastDrawIndexed = this . drawIndexed ;
102+ return true ;
103+ }
98104 return false ;
99105 } ,
100106
@@ -123,7 +129,7 @@ SpriteBatch.prototype = {
123129 var gl = this . glContext ;
124130 var vertexDataBuffer = this . vertexDataBuffer ;
125131
126- if ( this . elementCount === 0 )
132+ if ( this . elementCount === 0 && this . vertexCount === 0 )
127133 {
128134 return ;
129135 }
@@ -191,7 +197,100 @@ SpriteBatch.prototype = {
191197
192198 addMesh : function ( gameObject , camera )
193199 {
200+ var tempMatrix = this . tempMatrix ;
201+ var frame = gameObject . frame ;
202+ var vertexDataBuffer = this . vertexDataBuffer ;
203+ var vertexBufferObjectF32 = vertexDataBuffer . floatView ;
204+ var vertexBufferObjectU32 = vertexDataBuffer . uintView ;
205+ var vertexOffset = 0 ;
206+ var translateX = gameObject . x - camera . scrollX ;
207+ var translateY = gameObject . y - camera . scrollY ;
208+ var scaleX = gameObject . scaleX ;
209+ var scaleY = gameObject . scaleY ;
210+ var rotation = - gameObject . rotation ;
211+ var tempMatrixMatrix = tempMatrix . matrix ;
212+ var cameraMatrix = camera . matrix . matrix ;
213+ var mva , mvb , mvc , mvd , mve , mvf , tx0 , ty0 , tx1 , ty1 , tx2 , ty2 , tx3 , ty3 ;
214+ var sra , srb , src , srd , sre , srf , cma , cmb , cmc , cmd , cme , cmf ;
215+ var alpha = gameObject . alpha ;
216+ var vertices = gameObject . vertices ;
217+ var uv = gameObject . uv ;
218+ var length = vertices . length ;
219+ var totalVertices = ( length / 2 ) | 0 ;
220+
221+ tempMatrix . applyITRS ( translateX , translateY , rotation , scaleX , scaleY ) ;
222+
223+ sra = tempMatrixMatrix [ 0 ] ;
224+ srb = tempMatrixMatrix [ 1 ] ;
225+ src = tempMatrixMatrix [ 2 ] ;
226+ srd = tempMatrixMatrix [ 3 ] ;
227+ sre = tempMatrixMatrix [ 4 ] ;
228+ srf = tempMatrixMatrix [ 5 ] ;
229+
230+ cma = cameraMatrix [ 0 ] ;
231+ cmb = cameraMatrix [ 1 ] ;
232+ cmc = cameraMatrix [ 2 ] ;
233+ cmd = cameraMatrix [ 3 ] ;
234+ cme = cameraMatrix [ 4 ] ;
235+ cmf = cameraMatrix [ 5 ] ;
236+
237+ mva = sra * cma + srb * cmc ;
238+ mvb = sra * cmb + srb * cmd ;
239+ mvc = src * cma + srd * cmc ;
240+ mvd = src * cmb + srd * cmd ;
241+ mve = sre * cma + srf * cmc + cme ;
242+ mvf = sre * cmb + srf * cmd + cmf ;
243+
244+ this . drawIndexed = false ;
245+ this . manager . setRenderer ( this , frame . texture . source [ frame . sourceIndex ] . glTexture , gameObject . renderTarget ) ;
246+ this . vertexCount += totalVertices ;
247+
248+ vertexOffset = vertexDataBuffer . allocate ( totalVertices * 6 ) ;
249+
250+ for ( var index = 0 ; index < length ; index += 2 )
251+ {
252+ var x = vertices [ index + 0 ] ;
253+ var y = vertices [ index + 1 ] ;
254+ var tx = x * mva + y * mvc + mve ;
255+ var ty = x * mvb + y * mvd + mvf ;
256+ vertexBufferObjectF32 [ vertexOffset ++ ] = tx ;
257+ vertexBufferObjectF32 [ vertexOffset ++ ] = ty ;
258+ vertexBufferObjectF32 [ vertexOffset ++ ] = uv [ index + 0 ] ;
259+ vertexBufferObjectF32 [ vertexOffset ++ ] = uv [ index + 1 ] ;
260+ vertexBufferObjectU32 [ vertexOffset ++ ] = 0xFFFFFF ;
261+ vertexBufferObjectF32 [ vertexOffset ++ ] = alpha ;
262+ }
194263
264+ /*vertexOffset = vertexDataBuffer.allocate(24);
265+ this.vertexCount += 6;
266+
267+ vertexBufferObjectF32[vertexOffset++] = tx0;
268+ vertexBufferObjectF32[vertexOffset++] = ty0;
269+ vertexBufferObjectF32[vertexOffset++] = uvs.x0;
270+ vertexBufferObjectF32[vertexOffset++] = uvs.y0;
271+ vertexBufferObjectU32[vertexOffset++] = 0xFFFFFF; //vertexColor.topLeft;
272+ vertexBufferObjectF32[vertexOffset++] = alpha;
273+
274+ vertexBufferObjectF32[vertexOffset++] = tx1;
275+ vertexBufferObjectF32[vertexOffset++] = ty1;
276+ vertexBufferObjectF32[vertexOffset++] = uvs.x1;
277+ vertexBufferObjectF32[vertexOffset++] = uvs.y1;
278+ vertexBufferObjectU32[vertexOffset++] = 0xFFFFFF; //vertexColor.bottomLeft;
279+ vertexBufferObjectF32[vertexOffset++] = alpha;
280+
281+ vertexBufferObjectF32[vertexOffset++] = tx2;
282+ vertexBufferObjectF32[vertexOffset++] = ty2;
283+ vertexBufferObjectF32[vertexOffset++] = uvs.x2;
284+ vertexBufferObjectF32[vertexOffset++] = uvs.y2;
285+ vertexBufferObjectU32[vertexOffset++] = 0xFFFFFF; //vertexColor.bottomRight;
286+ vertexBufferObjectF32[vertexOffset++] = alpha;
287+
288+ vertexBufferObjectF32[vertexOffset++] = tx3;
289+ vertexBufferObjectF32[vertexOffset++] = ty3;
290+ vertexBufferObjectF32[vertexOffset++] = uvs.x3;
291+ vertexBufferObjectF32[vertexOffset++] = uvs.y3;
292+ vertexBufferObjectU32[vertexOffset++] = 0xFFFFFF; //vertexColor.topRight;
293+ vertexBufferObjectF32[vertexOffset++] = alpha;*/
195294 } ,
196295
197296 addSpriteTexture : function ( gameObject , camera , texture , textureWidth , textureHeight )
@@ -251,6 +350,7 @@ SpriteBatch.prototype = {
251350 tx3 = xw * mva + y * mvc + mve ;
252351 ty3 = xw * mvb + y * mvd + mvf ;
253352
353+ this . drawIndexed = true ;
254354 this . manager . setRenderer ( this , texture , gameObject . renderTarget ) ;
255355 vertexOffset = vertexDataBuffer . allocate ( 24 ) ;
256356 this . elementCount += 6 ;
@@ -346,6 +446,7 @@ SpriteBatch.prototype = {
346446 tx3 = xw * mva + y * mvc + mve ;
347447 ty3 = xw * mvb + y * mvd + mvf ;
348448
449+ this . drawIndexed = true ;
349450 this . manager . setRenderer ( this , frame . texture . source [ frame . sourceIndex ] . glTexture , gameObject . renderTarget ) ;
350451 vertexOffset = vertexDataBuffer . allocate ( 24 ) ;
351452 this . elementCount += 6 ;
0 commit comments