diff --git a/src/pixi/renderers/webgl/utils/WebGLSpriteBatch.js b/src/pixi/renderers/webgl/utils/WebGLSpriteBatch.js index 6ad8384..e30cd4f 100755 --- a/src/pixi/renderers/webgl/utils/WebGLSpriteBatch.js +++ b/src/pixi/renderers/webgl/utils/WebGLSpriteBatch.js @@ -78,6 +78,9 @@ this.setContext(gl); this.dirty = true; + + this.textures = []; + this.blendModes = []; }; /** @@ -140,15 +143,14 @@ PIXI.WebGLSpriteBatch.prototype.render = function(sprite) { var texture = sprite.texture; - - var blendChange = this.renderSession.blendModeManager.currentBlendMode !== sprite.blendMode; - + + //TODO set blend modes.. // check texture.. - if(texture.baseTexture !== this.currentBaseTexture || this.currentBatchSize >= this.size || blendChange) + if(this.currentBatchSize >= this.size) { + //return; this.flush(); this.currentBaseTexture = texture.baseTexture; - this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } // get the uvs for the texture @@ -191,17 +193,17 @@ } var index = this.currentBatchSize * 4 * this.vertSize; - - var worldTransform = sprite.worldTransform;//.toArray(); - + var resolution = texture.baseTexture.resolution; - var a = worldTransform.a / resolution; - var b = worldTransform.c / resolution; - var c = worldTransform.b / resolution; - var d = worldTransform.d / resolution; - var tx = worldTransform.tx; - var ty = worldTransform.ty; + var worldTransform = sprite.worldTransform; + + var a = worldTransform.a / resolution;//[0]; + var b = worldTransform.c / resolution;//[3]; + var c = worldTransform.b / resolution;//[1]; + var d = worldTransform.d / resolution;//[4]; + var tx = worldTransform.tx;//[2]; + var ty = worldTransform.ty;///[5]; // xy verticies[index++] = a * w1 + c * h1 + tx; @@ -244,8 +246,10 @@ verticies[index++] = tint; // increment the batchsize - this.currentBatchSize++; + this.textures[this.currentBatchSize] = sprite.texture.baseTexture; + this.blendModes[this.currentBatchSize] = sprite.blendMode; + this.currentBatchSize++; }; @@ -259,14 +263,13 @@ { var texture = tilingSprite.tilingTexture; - var blendChange = this.renderSession.blendModeManager.currentBlendMode !== tilingSprite.blendMode; - + // check texture.. - if(texture.baseTexture !== this.currentBaseTexture || this.currentBatchSize >= this.size || blendChange) + if(this.currentBatchSize >= this.size) { + //return; this.flush(); this.currentBaseTexture = texture.baseTexture; - this.renderSession.blendModeManager.setBlendMode(tilingSprite.blendMode); } // set the textures uvs temporarily @@ -307,8 +310,8 @@ var height = tilingSprite.height; // TODO trim?? - var aX = tilingSprite.anchor.x; // - tilingSprite.texture.trim.x - var aY = tilingSprite.anchor.y; //- tilingSprite.texture.trim.y + var aX = tilingSprite.anchor.x; + var aY = tilingSprite.anchor.y; var w0 = width * (1-aX); var w1 = width * -aX; @@ -339,7 +342,7 @@ verticies[index++] = tint; // xy - verticies[index++] = a * w0 + c * h1 + tx; + verticies[index++] = (a * w0 + c * h1 + tx); verticies[index++] = d * h1 + b * w0 + ty; // uv verticies[index++] = uvs.x1; @@ -369,6 +372,8 @@ verticies[index++] = tint; // increment the batchs + this.textures[this.currentBatchSize] = texture.baseTexture; + this.blendModes[this.currentBatchSize] = tilingSprite.blendMode; this.currentBatchSize++; }; @@ -385,10 +390,9 @@ if (this.currentBatchSize===0)return; var gl = this.gl; - + this.renderSession.shaderManager.setShader(this.renderSession.shaderManager.defaultShader); - //TODO - im usre this can be done better - will look to tweak this for 1.7.. if(this.dirty) { this.dirty = false; @@ -411,15 +415,6 @@ } - // bind the current texture - gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id] || PIXI.createWebGLTexture(this.currentBaseTexture, gl)); - - // check if a texture is dirty.. - if(this.currentBaseTexture._dirty[gl.id]) - { - PIXI.updateWebGLTexture(this.currentBaseTexture, gl); - } - // upload the verts to the buffer if(this.currentBatchSize > ( this.size * 0.5 ) ) { @@ -428,19 +423,59 @@ else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); - gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - // var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); - //gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); - - // now draw those suckas! - gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + var nextTexture, nextBlendMode; + var batchSize = 0; + var start = 0; + + var currentBaseTexture = null; + var currentBlendMode = this.renderSession.blendModeManager.currentBlendMode; + + for (var i = 0, j = this.currentBatchSize; i < j; i++) { + + nextTexture = this.textures[i]; + nextBlendMode = this.blendModes[i]; + + if(currentBaseTexture !== nextTexture || currentBlendMode !== nextBlendMode) + { + this.renderBatch(currentBaseTexture, batchSize, start); + + start = i; + batchSize = 0; + currentBaseTexture = nextTexture; + currentBlendMode = nextBlendMode; + + this.renderSession.blendModeManager.setBlendMode( currentBlendMode ); + } + + batchSize++; + } + + this.renderBatch(currentBaseTexture, batchSize, start); + // then reset the batch! this.currentBatchSize = 0; +}; +PIXI.WebGLSpriteBatch.prototype.renderBatch = function(texture, size, startIndex) +{ + if(size === 0)return; + + var gl = this.gl; + // bind the current texture + gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id] || PIXI.createWebGLTexture(texture, gl)); + + // check if a texture is dirty.. + if(texture._dirty[gl.id]) + { + PIXI.updateWebGLTexture(this.currentBaseTexture, gl); + } + + // now draw those suckas! + gl.drawElements(gl.TRIANGLES, size * 6, gl.UNSIGNED_SHORT, startIndex * 6 * 2); + // increment the draw count this.renderSession.drawCount++; }; @@ -482,4 +517,3 @@ this.gl = null; }; -