From 6f5d4b93b8baeb8e9c0fc23f1adfdacb195d9e8e Mon Sep 17 00:00:00 2001 From: nextht Date: Mon, 8 Jun 2015 20:43:20 +0800 Subject: [PATCH] Fix mask combine filter bug In the demo (code below), i create a group with a rect mask, and create a sprite as child with a blurXY filter, then nothing is displayed. So i hack WebGLFilterManager's pushFilter and popFilter method, in pushFilter method i create a new stencilManager instance and cache the old one, in popFilter i destroy the new stencilManager and restore the old one, then this workaround works rightly as i expect. I think this is bug for PIXI, so i hope @GoodBoyDigital and @photonstorm can help me to check whether this fixing is right or not, thanks. var game = new Phaser.Game(800, 600, Phaser.WEBGL, 'phaser-example', { preload: preload, create: create }); function preload() { game.load.image('phaser', 'assets/sprites/phaser2.png'); game.load.script('filterX', '../filters/BlurX.js'); game.load.script('filterY', '../filters/BlurY.js'); } function create() { var logo = game.add.sprite(game.world.centerX, game.world.centerY, 'phaser'); logo.anchor.setTo(0.5, 0.5); var blurX = game.add.filter('BlurX'); var blurY = game.add.filter('BlurY'); logo.filters = [blurX, blurY]; var group = game.add.group(); group.addChild(logo); group.mask = game.add.graphics(); group.mask.beginFill(0xff); group.mask.drawRect(0,0,500,500); } --- .../webgl/utils/WebGLFilterManager.js | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/pixi/renderers/webgl/utils/WebGLFilterManager.js b/src/pixi/renderers/webgl/utils/WebGLFilterManager.js index e726891bf..c242a4bdd 100644 --- a/src/pixi/renderers/webgl/utils/WebGLFilterManager.js +++ b/src/pixi/renderers/webgl/utils/WebGLFilterManager.js @@ -73,7 +73,14 @@ PIXI.WebGLFilterManager.prototype.pushFilter = function(filterBlock) var offset = this.renderSession.offset; filterBlock._filterArea = filterBlock.target.filterArea || filterBlock.target.getBounds(); - + + // >>> modify by nextht + filterBlock._previous_stencil_mgr = this.renderSession.stencilManager; + this.renderSession.stencilManager = new PIXI.WebGLStencilManager(); + this.renderSession.stencilManager.setContext(gl); + gl.disable(gl.STENCIL_TEST); + // <<< modify by nextht + // filter program // OPTIMISATION - the first filter is free if its a simple color change? this.filterStack.push(filterBlock); @@ -298,6 +305,20 @@ PIXI.WebGLFilterManager.prototype.popFilter = function() gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, texture.texture); + // >>> modify by nextht + if (this.renderSession.stencilManager) { + this.renderSession.stencilManager.destroy(); + } + this.renderSession.stencilManager = filterBlock._previous_stencil_mgr; + filterBlock._previous_stencil_mgr = null; + if (this.renderSession.stencilManager.count > 0) { + gl.enable(gl.STENCIL_TEST); + } + else { + gl.disable(gl.STENCIL_TEST); + } + // <<< modify by nextht + // apply! this.applyFilterPass(filter, filterArea, sizeX, sizeY);