Skip to content

Commit 6748dfc

Browse files
author
pandamicro
committed
Fix clipping node rendering issue when alphaThreshold = 1
1 parent 1b018b2 commit 6748dfc

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

cocos2d/clipping-nodes/CCClippingNode.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ cc.stencilBits = -1;
4747
* @property {cc.Node} stencil - he cc.Node to use as a stencil to do the clipping.
4848
*/
4949
cc.ClippingNode = cc.Node.extend(/** @lends cc.ClippingNode# */{
50-
alphaThreshold: 0,
5150
inverted: false,
51+
_alphaThreshold: 0,
5252

5353
_stencil: null,
5454
_className: "ClippingNode",
5555

56+
_originStencilProgram: null,
57+
5658
/**
5759
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
5860
* @param {cc.Node} [stencil=null]
@@ -61,6 +63,9 @@ cc.ClippingNode = cc.Node.extend(/** @lends cc.ClippingNode# */{
6163
stencil = stencil || null;
6264
cc.Node.prototype.ctor.call(this);
6365
this._stencil = stencil;
66+
if (stencil) {
67+
this._originStencilProgram = stencil.getShaderProgram();
68+
}
6469
this.alphaThreshold = 1;
6570
this.inverted = false;
6671
this._renderCmd.initStencilBits();
@@ -154,15 +159,19 @@ cc.ClippingNode = cc.Node.extend(/** @lends cc.ClippingNode# */{
154159
* @return {Number}
155160
*/
156161
getAlphaThreshold: function () {
157-
return this.alphaThreshold;
162+
return this._alphaThreshold;
158163
},
159164

160165
/**
161166
* set alpha threshold.
162167
* @param {Number} alphaThreshold
163168
*/
164169
setAlphaThreshold: function (alphaThreshold) {
165-
this.alphaThreshold = alphaThreshold;
170+
if (alphaThreshold === 1 && alphaThreshold !== this._alphaThreshold) {
171+
// should reset program used by _stencil
172+
this._renderCmd.resetProgramByStencil();
173+
}
174+
this._alphaThreshold = alphaThreshold;
166175
},
167176

168177
/**
@@ -202,6 +211,8 @@ cc.ClippingNode = cc.Node.extend(/** @lends cc.ClippingNode# */{
202211
setStencil: function (stencil) {
203212
if (this._stencil === stencil)
204213
return;
214+
if (stencil)
215+
this._originStencilProgram = stencil.getShaderProgram();
205216
this._renderCmd.setStencil(stencil);
206217
},
207218

@@ -219,6 +230,9 @@ var _p = cc.ClippingNode.prototype;
219230
/** @expose */
220231
_p.stencil;
221232
cc.defineGetterSetter(_p, "stencil", _p.getStencil, _p.setStencil);
233+
/** @expose */
234+
_p.alphaThreshold;
235+
cc.defineGetterSetter(_p, "alphaThreshold", _p.getAlphaThreshold, _p.setAlphaThreshold);
222236

223237

224238
/**

cocos2d/clipping-nodes/CCClippingNodeWebGLRenderCmd.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@
124124
node._stencil._parent = node;
125125
};
126126

127+
proto.resetProgramByStencil = function () {
128+
var node = this._node;
129+
if (node._stencil) {
130+
var program = node._originStencilProgram;
131+
setProgram(node._stencil, program);
132+
}
133+
};
134+
127135
proto._onBeforeVisit = function (ctx) {
128136
var gl = ctx || cc._renderContext, node = this._node;
129137
cc.ClippingNode.WebGLRenderCmd._layer++;

cocos2d/core/sprites/CCSpriteWebGLRenderCmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
this._dirty = false;
4040
this._recursiveDirty = false;
4141

42-
this._shaderProgram = cc.shaderCache.programForKey(cc.SHADER_SPRITE_POSITION_TEXTURECOLORALPHATEST);
42+
this._shaderProgram = cc.shaderCache.programForKey(cc.SHADER_SPRITE_POSITION_TEXTURECOLOR);
4343
};
4444

4545
var proto = cc.Sprite.WebGLRenderCmd.prototype = Object.create(cc.Node.WebGLRenderCmd.prototype);

0 commit comments

Comments
 (0)