Shader to turn sprite into grayscale

I’m trying to use a shader to turn my sprites into grayscale.
All the examples I see use the GLProgram class which apparently doesn’t exist anymore (or at least I cannot find it)
Can anyone help me on this?

Hey, I wrote a utility to make shaders easier to use in v4 https://github.com/JRCocos/SimpleShader

for a simple grayscale shader you could try this

void main(void) {
	vec4 color = texture2D(u_texture, cc_FragTexCoord1);
	float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
	gl_FragColor = vec4(vec3(gray), color.a);
}
1 Like

Thanks.
This works really good on sprites, but nothing happens with a DrawNode?
Can we add a shader to a DrawNode?

I’ve never tried that, but with a DrawNode you can just specify the colors that you want to draw directly anyway.

for the colors yes, but DrawNode generates very pixelated lines.
I was trying to add a shader to get some anti aliasing and more smooth lines.

do you have a v3 version too? )

I’m using v4.0 of cocos2d-x

No, sorry I only work with v4

Hi @pccsoares,
you can do a search in the forum pertaining to this topic.

i’ve found one here

Hi. I don’t know if there are still people interested in the question but I think the simplest way of achieving this in Cocos2d-x v4 is by using the predefined grayscale shader:

auto sprite = Sprite::createWithTexture("sometexture.png");
auto program = cocos2d::backend::Device::getInstance()->newProgram(
                positionTextureColor_vert,
                grayScale_frag);
auto programState = new backend::ProgramState(program);
sprite->setProgramState(programState);