Skip to content

Commit afd4484

Browse files
author
SeanLin
committed
Merge pull request cocos2d#492 from ricardoquesada/blendfunc
Improves blending function
2 parents a1a6b46 + 701686d commit afd4484

File tree

15 files changed

+229
-136
lines changed

15 files changed

+229
-136
lines changed

cocos2d/base_nodes/CCAtlasNode.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ cc.AtlasNode = cc.Node.extend(/** @lends cc.AtlasNode# */{
4646
_itemWidth:0,
4747
//! height of each char
4848
_itemHeight:0,
49-
_colorUnmodified:new cc.Color3B(0, 0, 0),
49+
_colorUnmodified:cc.c3b(0, 0, 0),
5050
_textureAtlas:null,
5151
// protocol variables
5252
_isOpacityModifyRGB:false,
53-
_blendFunc:new cc.BlendFunc(cc.BLEND_SRC, cc.BLEND_DST),
53+
_blendFunc: {src:cc.BLEND_SRC, dst:cc.BLEND_DST},
5454
_opacity:0,
5555
_color:null,
5656
_originalTexture:null,
@@ -216,8 +216,8 @@ cc.AtlasNode = cc.Node.extend(/** @lends cc.AtlasNode# */{
216216
/**
217217
* @param {cc.BlendFunc} blendFunc
218218
*/
219-
setBlendFunc:function (blendFunc) {
220-
this._blendFunc = blendFunc;
219+
setBlendFunc:function (src, dst) {
220+
this._blendFunc = {src:src, dst:dst};
221221
},
222222

223223
// cc.Texture protocol
@@ -280,8 +280,8 @@ cc.AtlasNode = cc.Node.extend(/** @lends cc.AtlasNode# */{
280280

281281
_updateBlendFunc:function () {
282282
/* if (!this._textureAtlas.getTexture().hasPremultipliedAlpha()) {
283-
this._blendFunc.src = cc.GL_SRC_ALPHA;
284-
this._blendFunc.dst = cc.GL_ONE_MINUS_SRC_ALPHA;
283+
this._blendFunc.src = gl.SRC_ALPHA;
284+
this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
285285
}*/
286286
},
287287

cocos2d/base_nodes/CCdomNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,4 +598,4 @@ cc.DOM.convert = function () {
598598
}
599599

600600

601-
};
601+
};

cocos2d/layers_scenes_transitions_nodes/CCLayer.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,11 @@ cc.LayerColor = cc.Layer.extend(/** @lends cc.LayerColor# */{
381381

382382
/**
383383
* blendFunc setter
384-
* @param {cc.BlendFunc} Var
385-
*/
386-
setBlendFunc:function (Var) {
387-
this._blendFunc = Var;
384+
* @param {Number} src
385+
* @param {Number} dst
386+
*/
387+
setBlendFunc:function (src, dst) {
388+
this._blendFunc = {src:src, dst:dst};
388389
},
389390

390391
/**

cocos2d/layers_scenes_transitions_nodes/CCTransition.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,14 +1405,10 @@ cc.TransitionCrossFade = cc.TransitionScene.extend(/** @lends cc.TransitionCross
14051405
this._outScene.visit();
14061406
outTexture.end();
14071407

1408-
// create blend functions
1409-
1410-
var blend1 = new cc.BlendFunc(cc.GL_ONE, cc.GL_ONE); // inScene will lay on background and will not be used with alpha
1411-
var blend2 = cc.BlendFunc(cc.GL_SRC_ALPHA, cc.GL_ONE_MINUS_SRC_ALPHA); // we are going to blend outScene via alpha
1412-
1413-
// set blendfunctions
1414-
inTexture.getSprite().setBlendFunc(blend1);
1415-
outTexture.getSprite().setBlendFunc(blend2);
1408+
// inScene will lay on background and will not be used with alpha
1409+
inTexture.getSprite().setBlendFunc(gl.ONE, gl.ONE);
1410+
// we are going to blend outScene via alpha
1411+
outTexture.getSprite().setBlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
14161412

14171413
// add render textures to the layer
14181414
layer.addChild(inTexture);

cocos2d/misc_nodes/CCMotionStreak.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ cc.MotionStreak = cc.Node.extend(/** @lends cc.MotionStreak# */{
7676
},
7777

7878
/**
79-
* @param {cc.BlendFunc} blendFunc
79+
* @param {Number} src
80+
* @param {Number} dst
8081
*/
81-
setBlendFunc:function (blendFunc) {
82-
this._ribbon.setBlendFunc(blendFunc);
82+
setBlendFunc:function (src, dst) {
83+
this._ribbon.setBlendFunc(src, dst);
8384
},
8485

8586
/**

cocos2d/misc_nodes/CCRenderTexture.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ cc.RenderTexture = cc.Node.extend(/** @lends cc.RenderTexture# */{
168168
width *= cc.CONTENT_SCALE_FACTOR();
169169
height *= cc.CONTENT_SCALE_FACTOR();
170170

171-
glGetIntegerv(cc.GL_FRAMEBUFFER_BINDING, this._oldFBO);
171+
glGetIntegerv(gl.FRAMEBUFFER_BINDING, this._oldFBO);
172172

173173
// textures must be power of two squared
174174
var powW = 0;
@@ -206,7 +206,7 @@ cc.RenderTexture = cc.Node.extend(/** @lends cc.RenderTexture# */{
206206
glBindFramebuffer(GL_FRAMEBUFFER, this._fBO);
207207

208208
// associate texture with FBO
209-
glFramebufferTexture2D(cc.GL_FRAMEBUFFER, cc.GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this._texture.getName(), 0);
209+
glFramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, GL_TEXTURE_2D, this._texture.getName(), 0);
210210

211211
if (this._depthRenderBuffer != 0) {
212212
//create and attach depth buffer
@@ -216,7 +216,7 @@ cc.RenderTexture = cc.Node.extend(/** @lends cc.RenderTexture# */{
216216
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, this._depthRenderBuffer);
217217

218218
// if depth format is the one with stencil part, bind same render buffer as stencil attachment
219-
if (depthStencilFormat == cc.GL_DEPTH24_STENCIL8)
219+
if (depthStencilFormat == gl.DEPTH24_STENCIL8)
220220
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, this._depthRenderBuffer);
221221
}
222222

@@ -230,8 +230,7 @@ cc.RenderTexture = cc.Node.extend(/** @lends cc.RenderTexture# */{
230230
this._sprite.setScaleY(-1);
231231
this.addChild(this._sprite);
232232

233-
var tBlendFunc = new cc.BlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
234-
this._sprite.setBlendFunc(tBlendFunc);
233+
this._sprite.setBlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
235234

236235
glBindRenderbuffer(GL_RENDERBUFFER, oldRBO);
237236
glBindFramebuffer(GL_FRAMEBUFFER, this._oldFBO);
@@ -265,8 +264,8 @@ cc.RenderTexture = cc.Node.extend(/** @lends cc.RenderTexture# */{
265264
-1.0 / heightRatio, 1.0 / heightRatio, -1, 1);
266265
kmGLMultMatrix(orthoMatrix);
267266

268-
glGetIntegerv(cc.GL_FRAMEBUFFER_BINDING, this._oldFBO);
269-
glBindFramebuffer(cc.GL_FRAMEBUFFER, this._fBO);//Will direct drawing to the frame buffer created above
267+
glGetIntegerv(gl.FRAMEBUFFER_BINDING, this._oldFBO);
268+
glBindFramebuffer(gl.FRAMEBUFFER, this._fBO);//Will direct drawing to the frame buffer created above
270269
},
271270

272271
/**

cocos2d/particle_nodes/CCParticleBatchNode.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ cc.PARTICLE_DEFAULT_CAPACITY = 100;
5858
cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
5959
TextureProtocol:true,
6060
//the blend function used for drawing the quads
61-
_blendFunc:new cc.BlendFunc(cc.BLEND_SRC, cc.BLEND_DST),
61+
_blendFunc:{src:cc.BLEND_SRC, dst:cc.BLEND_DST},
6262
_textureAtlas:null,
6363

6464
ctor:function () {
@@ -116,7 +116,8 @@ cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
116116
cc.Assert(child.getTexture() == this._textureAtlas.getTexture(), "cc.ParticleSystem is not using the same texture id");
117117
// If this is the 1st children, then copy blending function
118118
if (this._children.length == 0) {
119-
this.setBlendFunc(child.getBlendFunc());
119+
var blend = child.getBlendFunc();
120+
this.setBlendFunc(blend.src, blend.dst);
120121
}
121122

122123
cc.Assert(this._blendFunc.src == child.getBlendFunc().src && this._blendFunc.dst == pChild.getBlendFunc().dst,
@@ -307,14 +308,14 @@ cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
307308
this._textureAtlas.setTexture(texture);
308309

309310
// If the new texture has No premultiplied alpha, AND the blendFunc hasn't been changed, then update it
310-
if (texture && !texture.hasPremultipliedAlpha() && ( m_tBlendFunc.src == CC_BLEND_SRC && m_tBlendFunc.dst == CC_BLEND_DST )) {
311-
m_tBlendFunc.src = GL_SRC_ALPHA;
312-
m_tBlendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
311+
if (texture && !texture.hasPremultipliedAlpha() && ( m_tBlendFunc.src == gl.BLEND_SRC && m_tBlendFunc.dst == gl.BLEND_DST )) {
312+
this._blendFunc.src = gl.SRC_ALPHA;
313+
this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
313314
}
314315
},
315316

316-
setBlendFunc:function (blendFunc) {
317-
m_tBlendFunc = blendFunc;
317+
setBlendFunc:function (src, dst) {
318+
this._blendFunc = {src:src, dst:dst};
318319
},
319320

320321
/**
@@ -457,8 +458,8 @@ cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
457458

458459
_updateBlendFunc:function () {
459460
if (!this._textureAtlas.getTexture().hasPremultipliedAlpha()) {
460-
m_tBlendFunc.src = GL_SRC_ALPHA;
461-
m_tBlendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
461+
this._blendFunc.src = gl.SRC_ALPHA;
462+
this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
462463
}
463464
},
464465

cocos2d/particle_nodes/CCParticleSystem.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
10291029
},
10301030

10311031
/** conforms to CocosNodeTexture protocol */
1032-
_blendFunc:new cc.BlendFunc(0, 0),
1032+
_blendFunc: {src:gl.ONE, dst:gl.ONE},
10331033
/**
10341034
* get BlendFunc of Particle System
10351035
* @return {cc.BlendFunc}
@@ -1040,11 +1040,12 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
10401040

10411041
/**
10421042
* set BlendFunc of Particle System
1043-
* @param {cc.BlendFunc} blendFunc
1043+
* @param {Number} src
1044+
* @param {Number} dst
10441045
*/
1045-
setBlendFunc:function (blendFunc) {
1046-
if (this._blendFunc.src != blendFunc.src || this._blendFunc.dst != blendFunc.dst) {
1047-
this._blendFunc = blendFunc;
1046+
setBlendFunc:function (src, dst) {
1047+
if (this._blendFunc.src != src || this._blendFunc.dst != dst) {
1048+
this._blendFunc = {src:src, dst:dst};
10481049
this._updateBlendFunc();
10491050
}
10501051
},
@@ -1078,7 +1079,7 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
10781079
*/
10791080
isBlendAdditive:function () {
10801081
//return this._isBlendAdditive;
1081-
return( this._blendFunc.src == cc.GL_SRC_ALPHA && this._blendFunc.dst == cc.GL_ONE);
1082+
return( this._blendFunc.src == gl.SRC_ALPHA && this._blendFunc.dst == gl.ONE);
10821083
},
10831084

10841085
/**
@@ -1091,14 +1092,14 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
10911092
//TODO
10921093
this._isBlendAdditive = isBlendAdditive;
10931094
if (isBlendAdditive) {
1094-
this._blendFunc.src = cc.GL_SRC_ALPHA;
1095-
this._blendFunc.dst = cc.GL_ONE;
1095+
this._blendFunc.src = gl.SRC_ALPHA;
1096+
this._blendFunc.dst = gl.ONE;
10961097
} else {
10971098
this._blendFunc.src = cc.BLEND_SRC;
10981099
this._blendFunc.dst = cc.BLEND_DST;
10991100
/*if (this._texture && !this._texture.hasPremultipliedAlpha()) {
1100-
this._blendFunc.src = GL_SRC_ALPHA;
1101-
this._blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
1101+
this._blendFunc.src = gl.SRC_ALPHA;
1102+
this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
11021103
} else {
11031104
this._blendFunc.src = cc.BLEND_SRC;
11041105
this._blendFunc.dst = cc.BLEND_DST;
@@ -1173,7 +1174,7 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
11731174
this._emitterMode = cc.PARTICLE_MODE_GRAVITY;
11741175
this.modeA = new cc.ParticleSystem.ModeA();
11751176
this.modeB = new cc.ParticleSystem.ModeB();
1176-
this._blendFunc = new cc.BlendFunc(cc.BLEND_SRC, cc.BLEND_DST);
1177+
this._blendFunc = {src:cc.BLEND_SRC, dst:cc.BLEND_DST};
11771178
},
11781179

11791180
/**
@@ -1764,8 +1765,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
17641765
if (premultiplied) {
17651766
this._opacityModifyRGB = true;
17661767
} else {
1767-
this._blendFunc.src = cc.GL_SRC_ALPHA;
1768-
this._blendFunc.dst = cc.GL_ONE_MINUS_SRC_ALPHA;
1768+
this._blendFunc.src = gl.SRC_ALPHA;
1769+
this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
17691770
}
17701771
}
17711772
}

cocos2d/platform/CCMacro.js

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ cc.BLEND_SRC = cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA ? 1 : 0x0302;
136136
*/
137137
cc.BLEND_DST = 0x0303;
138138

139-
cc.GL_ONE = 1;
140-
cc.GL_SRC_ALPHA = 0x0302;
141-
142139
/**
143140
* Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix
144141
* @param {cc.Node} node setup node
@@ -285,3 +282,101 @@ cc.RECT_POINTS_TO_PIXELS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (point) {
285282
} : function (p) {
286283
return p;
287284
};
285+
286+
287+
//
288+
// WebGL constants
289+
//
290+
var gl = gl || {};
291+
/**
292+
* @constant
293+
* @type Number
294+
*/
295+
gl.NEAREST = 0x2600;
296+
297+
/**
298+
* @constant
299+
* @type Number
300+
*/
301+
gl.LINEAR = 0x2601;
302+
/**
303+
* @constant
304+
* @type Number
305+
*/
306+
gl.REPEAT = 0x2901;
307+
/**
308+
* @constant
309+
* @type Number
310+
*/
311+
gl.CLAMP_TO_EDGE = 0x812F;
312+
/**
313+
* @constant
314+
* @type Number
315+
*/
316+
gl.CLAMP_TO_BORDER = 0x812D;
317+
/**
318+
* @constant
319+
* @type Number
320+
*/
321+
gl.LINEAR_MIPMAP_NEAREST = 0x2701;
322+
/**
323+
* @constant
324+
* @type Number
325+
*/
326+
gl.NEAREST_MIPMAP_NEAREST = 0x2700;
327+
/**
328+
* @constant
329+
* @type Number
330+
*/
331+
gl.ZERO = 0;
332+
/**
333+
* @constant
334+
* @type Number
335+
*/
336+
gl.ONE = 1;
337+
/**
338+
* @constant
339+
* @type Number
340+
*/
341+
gl.SRC_COLOR = 0x0300;
342+
/**
343+
* @constant
344+
* @type Number
345+
*/
346+
gl.ONE_MINUS_SRC_COLOR = 0x0301;
347+
/**
348+
* @constant
349+
* @type Number
350+
*/
351+
gl.SRC_ALPHA = 0x0302;
352+
/**
353+
* @constant
354+
* @type Number
355+
*/
356+
gl.ONE_MINUS_SRC_ALPHA = 0x0303;
357+
/**
358+
* @constant
359+
* @type Number
360+
*/
361+
gl.DST_ALPHA = 0x0304;
362+
/**
363+
* @constant
364+
* @type Number
365+
*/
366+
gl.ONE_MINUS_DST_ALPHA = 0x0305;
367+
/**
368+
* @constant
369+
* @type Number
370+
*/
371+
gl.DST_COLOR = 0x0306;
372+
/**
373+
* @constant
374+
* @type Number
375+
*/
376+
gl.ONE_MINUS_DST_COLOR = 0x0307;
377+
/**
378+
* @constant
379+
* @type Number
380+
*/
381+
gl.SRC_ALPHA_SATURATE = 0x0308;
382+

0 commit comments

Comments
 (0)