Skip to content

Commit e5085be

Browse files
author
Travis Gesslein
committed
added color boost, adds color constant onto color of a sprite in the shader
1 parent b22c5ee commit e5085be

File tree

6 files changed

+53
-25
lines changed

6 files changed

+53
-25
lines changed

cocos2d/core/base-nodes/CCNode.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
171171

172172
_realOpacity: 255,
173173
_realColor: null,
174+
_colorBoost: 0,
174175
_cascadeColorEnabled: false,
175176
_cascadeOpacityEnabled: false,
176177

@@ -306,7 +307,17 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
306307
getSkewX: function () {
307308
return this._skewX;
308309
},
309-
310+
//adds a constant to every component of the displayed color of the entity
311+
setColorBoost: function(value)
312+
{
313+
this._colorBoost = value;
314+
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.colorDirty);
315+
this._renderCmd._updateDisplayColor();
316+
},
317+
getColorBoost: function()
318+
{
319+
return this._colorBoost;
320+
},
310321
/**
311322
* <p>
312323
* Changes the X skew angle of the node in degrees. <br/>

cocos2d/core/base-nodes/CCNodeCanvasRenderCmd.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ cc.Node.RenderCmd = function(renderable){
7070

7171
this._displayedOpacity = 255;
7272
this._displayedColor = cc.color(255, 255, 255, 255);
73+
this._displayedColorBoost = 0;
7374
this._cascadeColorEnabledDirty = false;
7475
this._cascadeOpacityEnabledDirty = false;
7576
this._frame = -1;
@@ -132,20 +133,23 @@ cc.Node.RenderCmd.prototype = {
132133
return null;
133134
},
134135

135-
_updateDisplayColor: function (parentColor) {
136+
_updateDisplayColor: function (parentColor, parentBoost) {
136137
var node = this._node;
137138
var locDispColor = this._displayedColor, locRealColor = node._realColor;
139+
parentBoost = parentBoost || 0;
138140
var i, len, selChildren, item;
139141
if (this._cascadeColorEnabledDirty && !node._cascadeColorEnabled) {
140142
locDispColor.r = locRealColor.r;
141143
locDispColor.g = locRealColor.g;
142144
locDispColor.b = locRealColor.b;
145+
this._displayedColorBoost = node._colorBoost;
146+
143147
var whiteColor = new _cc.Color(255, 255, 255, 255);
144148
selChildren = node._children;
145149
for (i = 0, len = selChildren.length; i < len; i++) {
146150
item = selChildren[i];
147151
if (item && item._renderCmd)
148-
item._renderCmd._updateDisplayColor(whiteColor);
152+
item._renderCmd._updateDisplayColor(whiteColor, 0);
149153
}
150154
this._cascadeColorEnabledDirty = false;
151155
} else {
@@ -159,12 +163,14 @@ cc.Node.RenderCmd.prototype = {
159163
locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0);
160164
locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0);
161165
locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0);
166+
this._displayedColorBoost = node._colorBoost + parentBoost;
167+
162168
if (node._cascadeColorEnabled) {
163169
selChildren = node._children;
164170
for (i = 0, len = selChildren.length; i < len; i++) {
165171
item = selChildren[i];
166172
if (item && item._renderCmd){
167-
item._renderCmd._updateDisplayColor(locDispColor);
173+
item._renderCmd._updateDisplayColor(locDispColor, this._displayedColorBoost);
168174
item._renderCmd._updateColor();
169175
}
170176
}
@@ -207,8 +213,9 @@ cc.Node.RenderCmd.prototype = {
207213
this._dirtyFlag = this._dirtyFlag & _cc.Node._dirtyFlags.opacityDirty ^ this._dirtyFlag;
208214
},
209215

210-
_syncDisplayColor : function (parentColor) {
216+
_syncDisplayColor : function (parentColor, parentBoost) {
211217
var node = this._node, locDispColor = this._displayedColor, locRealColor = node._realColor;
218+
parentBoost = parentBoost || 0;
212219
if (parentColor === undefined) {
213220
var locParent = node._parent;
214221
if (locParent && locParent._cascadeColorEnabled)
@@ -219,6 +226,7 @@ cc.Node.RenderCmd.prototype = {
219226
locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0);
220227
locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0);
221228
locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0);
229+
this._displayedColorBoost = node._colorBoost + parentBoost;
222230
},
223231

224232
_syncDisplayOpacity : function (parentOpacity) {

cocos2d/core/platform/CCTypesWebGL.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,22 +713,21 @@ cc.makeVertexFormat = function(location, components, type, normalize, stride, of
713713
};
714714
}
715715

716-
cc.flatQuadSetColor = function(u32View, r,g,b,a)
716+
cc.flatQuadSetColor = function(u32View, float32View, r,g,b, colorScale, a)
717717
{
718+
718719
var u8View = new Uint8Array(u32View.buffer)
719-
/*
720-
var offset = 3; //first 3 4byte sets are positions
721-
var stride = cc.V3F_C4B_T2F.BYTES_PER_ELEMENT/4;
722-
723-
var value = (r << 24) | (g << 16) | (b << 8) | a;
724-
u32View[offset] = value;
725-
u32View[offset + stride] = value;
726-
u32View[offset + stride * 2] = value;
727-
u32View[offset + stride * 3] = value;*/
728720

729721
var offset = 12; //first 3 4byte sets are positions
730722
var stride = cc.V3F_C4B_T2F.BYTES_PER_ELEMENT;
731-
723+
var stride4 = stride/4;
724+
//we squeeze the color scale into the z position of the u32 view!
725+
float32View[2] = colorScale;
726+
float32View[2 + stride4] = colorScale;
727+
float32View[2 + stride4 * 2] = colorScale;
728+
float32View[2 + stride4 * 3] = colorScale;
729+
730+
//now the color data
732731
u8View[offset] = r;
733732
u8View[offset+1] = g;
734733
u8View[offset+2] = b;

cocos2d/core/renderer/RendererWebGL.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ cc.rendererWebGL = {
6666
formats: []
6767
};
6868

69-
vertexDataFormat.formats.push(cc.makeVertexFormat(0, 3, gl.FLOAT, false, 24, 0));
70-
vertexDataFormat.formats.push(cc.makeVertexFormat(1, 4, gl.UNSIGNED_BYTE, true, 24, 12));
71-
vertexDataFormat.formats.push(cc.makeVertexFormat(2, 2, gl.FLOAT, false, 24, 16));
69+
var bytesPerVertex = 24;
70+
vertexDataFormat.formats.push(cc.makeVertexFormat(0, 3, gl.FLOAT, false, bytesPerVertex, 0));
71+
vertexDataFormat.formats.push(cc.makeVertexFormat(1, 4, gl.UNSIGNED_BYTE, true, bytesPerVertex, 12));
72+
vertexDataFormat.formats.push(cc.makeVertexFormat(2, 2, gl.FLOAT, false, bytesPerVertex, 16));
7273
formats.push(vertexDataFormat);
7374

7475
var matrixDataFormat = {

cocos2d/core/sprites/CCSpriteWebGLRenderCmd.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
cc.QuadRenderCmdBase.call(this, renderable);
3030
this._needDraw = true;
3131
this._quadU32View = new Uint32Array(cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT / 4);
32+
this._float32View = new Float32Array(this._quadU32View.buffer);
3233
this._firstQuad = -1;
3334
this._batchedCount = 1;
3435
};
@@ -55,7 +56,7 @@
5556
};
5657

5758
proto._init = function () {
58-
cc.flatQuadSetColor(this._quadU32View, 255, 255, 255, 255);
59+
cc.flatQuadSetColor(this._quadU32View,this._float32View, 255, 255, 255, 0, 255);
5960
};
6061

6162
proto._updateQuadVertices = function()
@@ -193,7 +194,7 @@
193194
b *= locDisplayedOpacity / 255.0;
194195
}
195196

196-
cc.flatQuadSetColor(this._quadU32View, r, g, b, locDisplayedOpacity);
197+
cc.flatQuadSetColor(this._quadU32View,this._float32View, r, g, b,this._displayedColorBoost, locDisplayedOpacity);
197198

198199
this._quadDirty = true;
199200
};

cocos2d/shaders/CCShaders.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,11 @@ cc.SHADER_POSITION_TEXTURE_COLOR_FRAG =
217217
"precision lowp float;\n"
218218
+ "varying vec4 v_fragmentColor; \n"
219219
+ "varying vec2 v_texCoord; \n"
220+
+ "varying float v_colorScale; \n"
220221
+ "void main() \n"
221222
+ "{ \n"
222-
+ " gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord); \n"
223+
+ " vec4 colorBoost = vec4(v_colorScale,v_colorScale,v_colorScale,0); \n"
224+
+ " gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord) + colorBoost; \n"
223225
+ "}";
224226

225227
/* PITFOREST
@@ -231,12 +233,14 @@ cc.SHADER_POSITION_TEXTURE_COLOR_VERT_BATCHED =
231233
+ "attribute mat4 a_mvMatrix;"
232234
+ "varying lowp vec4 v_fragmentColor; \n"
233235
+ "varying mediump vec2 v_texCoord; \n"
236+
+ "varying float v_colorScale; \n"
234237
+ "void main() \n"
235238
+ "{ \n"
236239
//+ " gl_Position = CC_MVPMatrix * a_position; \n"
237-
+ " gl_Position = (CC_PMatrix * a_mvMatrix) * a_position; \n"
240+
+ " gl_Position = (CC_PMatrix * a_mvMatrix) * vec4(a_position.xy,1.0,1.0); \n"
238241
+ " v_fragmentColor = a_color; \n"
239242
+ " v_texCoord = a_texCoord; \n"
243+
+" v_colorScale = a_position.z; \n"
240244
+ "}";
241245

242246
/**
@@ -249,12 +253,14 @@ cc.SHADER_POSITION_TEXTURE_COLOR_VERT =
249253
+ "attribute vec4 a_color; \n"
250254
+ "varying lowp vec4 v_fragmentColor; \n"
251255
+ "varying mediump vec2 v_texCoord; \n"
256+
+ "varying float v_colorScale; \n"
252257
+ "void main() \n"
253258
+ "{ \n"
254259
//+ " gl_Position = CC_MVPMatrix * a_position; \n"
255-
+ " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
260+
+ " gl_Position = (CC_PMatrix * CC_MVMatrix) * vec4(a_position.xy,1.0,1.0); \n"
256261
+ " v_fragmentColor = a_color; \n"
257262
+ " v_texCoord = a_texCoord; \n"
263+
+" v_colorScale = a_position.z; \n"
258264
+ "}";
259265

260266
//-----------------------Shader_PositionTextureColorAlphaTest_frag Shader Source----------------------------
@@ -266,10 +272,12 @@ cc.SHADER_POSITION_TEXTURE_COLOR_ALPHATEST_FRAG =
266272
"precision lowp float; \n"
267273
+ "varying vec4 v_fragmentColor; \n"
268274
+ "varying vec2 v_texCoord; \n"
275+
+ "varying float v_colorScale; \n"
269276
+ "uniform float CC_alpha_value; \n"
270277
+ "void main() \n"
271278
+ "{ \n"
272-
+ " vec4 texColor = texture2D(CC_Texture0, v_texCoord, mipmapBias); \n"
279+
+ " vec4 colorBoost = vec4(v_colorScale,v_colorScale,v_colorScale,0); \n"
280+
+ " vec4 texColor = texture2D(CC_Texture0, v_texCoord, mipmapBias) + colorBoost; \n"
273281
// mimic: glAlphaFunc(GL_GREATER)
274282
//pass if ( incoming_pixel >= CC_alpha_value ) => fail if incoming_pixel < CC_alpha_value
275283
+ " if ( texColor.a <= CC_alpha_value ) \n"

0 commit comments

Comments
 (0)