diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index a39f49e..de17ba5 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -64,7 +64,7 @@ * @type Number * @default 0xFFFFFF */ - this.tint = 0xFFFFFF;// * Math.random(); + this.tint = 0xFFFFFF; /** * The blend mode to be applied to the sprite @@ -75,6 +75,16 @@ */ this.blendMode = PIXI.blendModes.NORMAL; + + /** + * The shader that will be used to render the texture to the stage. + * + * @property shader + * @type PIXI.AbstractFilter + * @default null + */ + this.shader = null; + if(texture.baseTexture.hasLoaded) { this.onTextureUpdate(); @@ -86,6 +96,7 @@ } this.renderable = true; + }; // constructor @@ -292,10 +303,8 @@ { this.children[i]._renderWebGL(renderSession); } + } - - - //TODO check culling }; /** diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index a39f49e..de17ba5 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -64,7 +64,7 @@ * @type Number * @default 0xFFFFFF */ - this.tint = 0xFFFFFF;// * Math.random(); + this.tint = 0xFFFFFF; /** * The blend mode to be applied to the sprite @@ -75,6 +75,16 @@ */ this.blendMode = PIXI.blendModes.NORMAL; + + /** + * The shader that will be used to render the texture to the stage. + * + * @property shader + * @type PIXI.AbstractFilter + * @default null + */ + this.shader = null; + if(texture.baseTexture.hasLoaded) { this.onTextureUpdate(); @@ -86,6 +96,7 @@ } this.renderable = true; + }; // constructor @@ -292,10 +303,8 @@ { this.children[i]._renderWebGL(renderSession); } + } - - - //TODO check culling }; /** diff --git a/src/pixi/extras/Strip.js b/src/pixi/extras/Strip.js index f46d1c0..07f381e 100644 --- a/src/pixi/extras/Strip.js +++ b/src/pixi/extras/Strip.js @@ -125,7 +125,7 @@ gl.uniformMatrix3fv(shader.translationMatrix, false, this.worldTransform.toArray(true)); gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform1f(shader.alpha, 1); + gl.uniform1f(shader.alpha, this.worldAlpha); if(!this.dirty) { @@ -143,7 +143,7 @@ // check if a texture is dirty.. if(this.texture.baseTexture._dirty[gl.id]) { - this.renderSession.renderer.updateTexture(this.texture.baseTexture); + renderSession.renderer.updateTexture(this.texture.baseTexture); } else { diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index a39f49e..de17ba5 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -64,7 +64,7 @@ * @type Number * @default 0xFFFFFF */ - this.tint = 0xFFFFFF;// * Math.random(); + this.tint = 0xFFFFFF; /** * The blend mode to be applied to the sprite @@ -75,6 +75,16 @@ */ this.blendMode = PIXI.blendModes.NORMAL; + + /** + * The shader that will be used to render the texture to the stage. + * + * @property shader + * @type PIXI.AbstractFilter + * @default null + */ + this.shader = null; + if(texture.baseTexture.hasLoaded) { this.onTextureUpdate(); @@ -86,6 +96,7 @@ } this.renderable = true; + }; // constructor @@ -292,10 +303,8 @@ { this.children[i]._renderWebGL(renderSession); } + } - - - //TODO check culling }; /** diff --git a/src/pixi/extras/Strip.js b/src/pixi/extras/Strip.js index f46d1c0..07f381e 100644 --- a/src/pixi/extras/Strip.js +++ b/src/pixi/extras/Strip.js @@ -125,7 +125,7 @@ gl.uniformMatrix3fv(shader.translationMatrix, false, this.worldTransform.toArray(true)); gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform1f(shader.alpha, 1); + gl.uniform1f(shader.alpha, this.worldAlpha); if(!this.dirty) { @@ -143,7 +143,7 @@ // check if a texture is dirty.. if(this.texture.baseTexture._dirty[gl.id]) { - this.renderSession.renderer.updateTexture(this.texture.baseTexture); + renderSession.renderer.updateTexture(this.texture.baseTexture); } else { diff --git a/src/pixi/filters/AbstractFilter.js b/src/pixi/filters/AbstractFilter.js index f9ca0b3..5572e74 100644 --- a/src/pixi/filters/AbstractFilter.js +++ b/src/pixi/filters/AbstractFilter.js @@ -44,3 +44,16 @@ */ this.fragmentSrc = fragmentSrc || []; }; + +PIXI.AbstractFilter.prototype.syncUniforms = function() +{ + for(var i=0,j=this.shaders.length; i= this.size) { - //return; this.flush(); this.currentBaseTexture = texture.baseTexture; } @@ -164,7 +182,6 @@ var verticies = this.vertices; - // TODO trim?? var aX = sprite.anchor.x; var aY = sprite.anchor.y; @@ -205,6 +222,7 @@ var tx = worldTransform.tx; var ty = worldTransform.ty; + // xy verticies[index++] = a * w1 + c * h1 + tx; verticies[index++] = d * h1 + b * w1 + ty; @@ -246,10 +264,7 @@ verticies[index++] = tint; // increment the batchsize - this.textures[this.currentBatchSize] = sprite.texture.baseTexture; - this.blendModes[this.currentBatchSize] = sprite.blendMode; - - this.currentBatchSize++; + this.sprites[this.currentBatchSize++] = sprite; }; @@ -263,7 +278,8 @@ { var texture = tilingSprite.tilingTexture; - + if(this.customShader) + // check texture.. if(this.currentBatchSize >= this.size) { @@ -390,8 +406,8 @@ if (this.currentBatchSize===0)return; var gl = this.gl; + - this.renderSession.shaderManager.setShader(this.renderSession.shaderManager.defaultShader); if(this.dirty) { @@ -403,15 +419,13 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); - // set the projection - var projection = this.renderSession.projection; - gl.uniform2f(this.shader.projectionVector, projection.x, projection.y); + var shader = this.defaultShader.shaders[gl.id]; - // set the pointers + // this is the same for each shader? var stride = this.vertSize * 4; - gl.vertexAttribPointer(this.shader.aVertexPosition, 2, gl.FLOAT, false, stride, 0); - gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 2 * 4); - gl.vertexAttribPointer(this.shader.colorAttribute, 2, gl.FLOAT, false, stride, 4 * 4); + gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, stride, 0); + gl.vertexAttribPointer(shader.aTextureCoord, 2, gl.FLOAT, false, stride, 2 * 4); + gl.vertexAttribPointer(shader.colorAttribute, 2, gl.FLOAT, false, stride, 4 * 4); } // upload the verts to the buffer @@ -431,24 +445,65 @@ var currentBaseTexture = null; var currentBlendMode = this.renderSession.blendModeManager.currentBlendMode; + var currentShader = null; + var blendSwap = false; + var shaderSwap = false; + var sprite; for (var i = 0, j = this.currentBatchSize; i < j; i++) { - nextTexture = this.textures[i]; - nextBlendMode = this.blendModes[i]; - blendSwap = currentBlendMode !== nextBlendMode; + var sprite = this.sprites[i]; - if(currentBaseTexture !== nextTexture || blendSwap) + nextTexture = sprite.texture.baseTexture; + nextBlendMode = sprite.blendMode + nextShader = sprite.shader || this.defaultShader; + + blendSwap = currentBlendMode !== nextBlendMode; + shaderSwap = currentShader !== nextShader; // should I use _UIDS??? + + if(currentBaseTexture !== nextTexture || blendSwap || shaderSwap) { this.renderBatch(currentBaseTexture, batchSize, start); start = i; batchSize = 0; currentBaseTexture = nextTexture; - currentBlendMode = nextBlendMode; - - if( blendSwap )this.renderSession.blendModeManager.setBlendMode( currentBlendMode ); + + if( blendSwap ) + { + currentBlendMode = nextBlendMode; + this.renderSession.blendModeManager.setBlendMode( currentBlendMode ); + } + + if( shaderSwap ) + { + currentShader = nextShader; + + var shader = currentShader.shaders[gl.id]; + + if(!shader) + { + shader = new PIXI.PixiShader(gl); + + shader.fragmentSrc =currentShader.fragmentSrc; + shader.uniforms =currentShader.uniforms; + shader.init(); + + currentShader.shaders[gl.id] = shader; + } + + // set shader function??? + var change = this.renderSession.shaderManager.setShader(shader); + + if(shader.dirty)shader.syncUniforms(); + + // set the projection + var projection = this.renderSession.projection; + gl.uniform2f(shader.projectionVector, projection.x, projection.y); + + // set the pointers + } } batchSize++; @@ -485,6 +540,7 @@ this.renderSession.drawCount++; }; + /** * * @method stop diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index a39f49e..de17ba5 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -64,7 +64,7 @@ * @type Number * @default 0xFFFFFF */ - this.tint = 0xFFFFFF;// * Math.random(); + this.tint = 0xFFFFFF; /** * The blend mode to be applied to the sprite @@ -75,6 +75,16 @@ */ this.blendMode = PIXI.blendModes.NORMAL; + + /** + * The shader that will be used to render the texture to the stage. + * + * @property shader + * @type PIXI.AbstractFilter + * @default null + */ + this.shader = null; + if(texture.baseTexture.hasLoaded) { this.onTextureUpdate(); @@ -86,6 +96,7 @@ } this.renderable = true; + }; // constructor @@ -292,10 +303,8 @@ { this.children[i]._renderWebGL(renderSession); } + } - - - //TODO check culling }; /** diff --git a/src/pixi/extras/Strip.js b/src/pixi/extras/Strip.js index f46d1c0..07f381e 100644 --- a/src/pixi/extras/Strip.js +++ b/src/pixi/extras/Strip.js @@ -125,7 +125,7 @@ gl.uniformMatrix3fv(shader.translationMatrix, false, this.worldTransform.toArray(true)); gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform1f(shader.alpha, 1); + gl.uniform1f(shader.alpha, this.worldAlpha); if(!this.dirty) { @@ -143,7 +143,7 @@ // check if a texture is dirty.. if(this.texture.baseTexture._dirty[gl.id]) { - this.renderSession.renderer.updateTexture(this.texture.baseTexture); + renderSession.renderer.updateTexture(this.texture.baseTexture); } else { diff --git a/src/pixi/filters/AbstractFilter.js b/src/pixi/filters/AbstractFilter.js index f9ca0b3..5572e74 100644 --- a/src/pixi/filters/AbstractFilter.js +++ b/src/pixi/filters/AbstractFilter.js @@ -44,3 +44,16 @@ */ this.fragmentSrc = fragmentSrc || []; }; + +PIXI.AbstractFilter.prototype.syncUniforms = function() +{ + for(var i=0,j=this.shaders.length; i= this.size) { - //return; this.flush(); this.currentBaseTexture = texture.baseTexture; } @@ -164,7 +182,6 @@ var verticies = this.vertices; - // TODO trim?? var aX = sprite.anchor.x; var aY = sprite.anchor.y; @@ -205,6 +222,7 @@ var tx = worldTransform.tx; var ty = worldTransform.ty; + // xy verticies[index++] = a * w1 + c * h1 + tx; verticies[index++] = d * h1 + b * w1 + ty; @@ -246,10 +264,7 @@ verticies[index++] = tint; // increment the batchsize - this.textures[this.currentBatchSize] = sprite.texture.baseTexture; - this.blendModes[this.currentBatchSize] = sprite.blendMode; - - this.currentBatchSize++; + this.sprites[this.currentBatchSize++] = sprite; }; @@ -263,7 +278,8 @@ { var texture = tilingSprite.tilingTexture; - + if(this.customShader) + // check texture.. if(this.currentBatchSize >= this.size) { @@ -390,8 +406,8 @@ if (this.currentBatchSize===0)return; var gl = this.gl; + - this.renderSession.shaderManager.setShader(this.renderSession.shaderManager.defaultShader); if(this.dirty) { @@ -403,15 +419,13 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); - // set the projection - var projection = this.renderSession.projection; - gl.uniform2f(this.shader.projectionVector, projection.x, projection.y); + var shader = this.defaultShader.shaders[gl.id]; - // set the pointers + // this is the same for each shader? var stride = this.vertSize * 4; - gl.vertexAttribPointer(this.shader.aVertexPosition, 2, gl.FLOAT, false, stride, 0); - gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 2 * 4); - gl.vertexAttribPointer(this.shader.colorAttribute, 2, gl.FLOAT, false, stride, 4 * 4); + gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, stride, 0); + gl.vertexAttribPointer(shader.aTextureCoord, 2, gl.FLOAT, false, stride, 2 * 4); + gl.vertexAttribPointer(shader.colorAttribute, 2, gl.FLOAT, false, stride, 4 * 4); } // upload the verts to the buffer @@ -431,24 +445,65 @@ var currentBaseTexture = null; var currentBlendMode = this.renderSession.blendModeManager.currentBlendMode; + var currentShader = null; + var blendSwap = false; + var shaderSwap = false; + var sprite; for (var i = 0, j = this.currentBatchSize; i < j; i++) { - nextTexture = this.textures[i]; - nextBlendMode = this.blendModes[i]; - blendSwap = currentBlendMode !== nextBlendMode; + var sprite = this.sprites[i]; - if(currentBaseTexture !== nextTexture || blendSwap) + nextTexture = sprite.texture.baseTexture; + nextBlendMode = sprite.blendMode + nextShader = sprite.shader || this.defaultShader; + + blendSwap = currentBlendMode !== nextBlendMode; + shaderSwap = currentShader !== nextShader; // should I use _UIDS??? + + if(currentBaseTexture !== nextTexture || blendSwap || shaderSwap) { this.renderBatch(currentBaseTexture, batchSize, start); start = i; batchSize = 0; currentBaseTexture = nextTexture; - currentBlendMode = nextBlendMode; - - if( blendSwap )this.renderSession.blendModeManager.setBlendMode( currentBlendMode ); + + if( blendSwap ) + { + currentBlendMode = nextBlendMode; + this.renderSession.blendModeManager.setBlendMode( currentBlendMode ); + } + + if( shaderSwap ) + { + currentShader = nextShader; + + var shader = currentShader.shaders[gl.id]; + + if(!shader) + { + shader = new PIXI.PixiShader(gl); + + shader.fragmentSrc =currentShader.fragmentSrc; + shader.uniforms =currentShader.uniforms; + shader.init(); + + currentShader.shaders[gl.id] = shader; + } + + // set shader function??? + var change = this.renderSession.shaderManager.setShader(shader); + + if(shader.dirty)shader.syncUniforms(); + + // set the projection + var projection = this.renderSession.projection; + gl.uniform2f(shader.projectionVector, projection.x, projection.y); + + // set the pointers + } } batchSize++; @@ -485,6 +540,7 @@ this.renderSession.drawCount++; }; + /** * * @method stop diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index ed2705f..71763c6 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -69,7 +69,7 @@ this.source = source; //TODO will be used for futer pixi 1.5... - this.id = PIXI.BaseTextureCacheIdGenerator++; + this._UID = PIXI._UID++; /** * Controls if RGB channels should be premultiplied by Alpha (WebGL only)