I am using a simple shader on a CCBatchNode to make its children sprites grayscale. This works perfectly:
THE C++ CODE
CCGLProgram *shader = new CCGLProgram();
setShaderProgram(shader);
shader~~>initWithVertexShaderFilename;
shader~~>addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
shader~~>addAttribute;
shader~~>link();
shader~~>updateUniforms; The .vsh file
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
uniform mat4 u_MVPMatrix; #ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord; #else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord; #endif
void main
{
gl_Position = u_MVPMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
} The fsh file #ifdef GL_ES
precision lowp float; #endif
varying vec2 v_texCoord;
uniform sampler2D u_texture;
void main
{
// grayscale
vec4 color = texture2D;
float gray = + + ;
gl_FragColor = vec4;
}
The only problem with that is that I can’t set the opacity using stuff like mySprite~~>setOpacity() or CCFadeOut once I set my shader. I have been coming back to this problem for days without success.
How can I add the opacity to my shader? Thank you very much!
THE C++ CODE
CCGLProgram *shader = new CCGLProgram();
setShaderProgram(shader);
shader~~>initWithVertexShaderFilename;
shader~~>addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
shader~~>addAttribute;
shader~~>link();
shader~~>updateUniforms;
opacityLocation = glGetUniformLoaction; — added by minggo The fsh file #ifdef GL_ES
precision lowp float; #endif
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform float u_opacity; — added by minggo
void main {
// grayscale
vec4 color = texture2D;
float gray = + + ;
gl_FragColor = vec4;
}
Then you can set values to the uniform like this
shader~~>setUniformLocationWith1f(opacityLocation, value);
I’m still trying to wrap my head around some of these concepts though. I have been using the shader on CCSpriteBatchNodes and it is working great. But can I set a different opacity for every child of the batch node?