diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 854f5f1..31511a1 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -61,11 +61,12 @@ this.indices = createIndicesForQuads(this.size); /** - * The default shader that is used if a sprite doesn't have a more specific one. - * + * The default shaders that is used if a sprite doesn't have a more specific one. + * there is a shader for each number of textures that can be rendererd. + * These shaders will also be generated on the fly as required. * @member {PIXI.Shader} */ - this.shader = null; + this.shaders = null; this.textureCount = 0; this.currentIndex = 0; @@ -110,19 +111,23 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader( this.MAX_TEXTURES, gl ); - this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); + this.shaders = new Array(this.MAX_TEXTURES); + this.shaders[0] = generateMultiTextureShader(gl, 1); + // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); + var shader = this.shaders[0]; + for (var i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); // build the vao object that will render.. this.vaos[i] = this.renderer.createVao() .addIndex(this.indexBuffer) - .addAttribute(this.vertexBuffers[i], this.shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) - .addAttribute(this.vertexBuffers[i], this.shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], this.shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - .addAttribute(this.vertexBuffers[i], this.shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) + .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) + .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); } this.vao = this.vaos[0]; @@ -293,14 +298,15 @@ if(this.vaoMax <= this.vertexCount) { this.vaoMax++; + var shader = this.shaders[0]; this.vertexBuffers[this.vertexCount] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); // build the vao object that will render.. this.vaos[this.vertexCount] = this.renderer.createVao() .addIndex(this.indexBuffer) - .addAttribute(this.vertexBuffers[this.vertexCount], this.shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) - .addAttribute(this.vertexBuffers[this.vertexCount], this.shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[this.vertexCount], this.shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - .addAttribute(this.vertexBuffers[this.vertexCount], this.shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + .addAttribute(this.vertexBuffers[this.vertexCount], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) + .addAttribute(this.vertexBuffers[this.vertexCount], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) + .addAttribute(this.vertexBuffers[this.vertexCount], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) + .addAttribute(this.vertexBuffers[this.vertexCount], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); } this.vertexBuffers[this.vertexCount].upload(buffer.vertices, 0); @@ -310,8 +316,19 @@ for (i = 0; i < groupCount; i++) { var group = groups[i]; + var textureCount = group.textureCount; + var shader = this.shaders[textureCount-1]; - for (var j = 0; j < group.textureCount; j++) { + if(!shader) + { + shader = this.shaders[textureCount-1] = generateMultiTextureShader(gl, textureCount); + // console.log("SHADER generated for " + textureCount + " textures") + } + + renderer.bindShader(shader); + + for (var j = 0; j < textureCount; j++) + { this.renderer.bindTexture(group.textures[j], j); } @@ -331,7 +348,7 @@ */ SpriteRenderer.prototype.start = function () { - this.renderer.bindShader(this.shader); + // this.renderer.bindShader(this.shader); this.tick %= 1000; }; @@ -356,7 +373,13 @@ this.renderer.off('prerender', this.onPrerender, this); ObjectRenderer.prototype.destroy.call(this); - this.shader.destroy(); + for (i = 0; i < this.shaders.length; i++) { + + if(this.shaders[i]) + { + this.shaders[i].destroy(); + } + } this.vertexBuffers = null; this.vaos = null; @@ -364,7 +387,6 @@ this.indices = null; this.sprites = null; - this.shader = null; for (i = 0; i < this.buffers.length; i++) { this.buffers[i].destroy();