How can I add a post process shader?

Hi.

It’s pretty hard if you did not try to read cocos2d-x source code but I will try to help you.
First of all there are 2 ways to do it but any of them is pretty similar.

  1. You can render all the scene to the texture by using RenderTexture;
  2. You can render all the scene to the texture by using FrameBuffer.

Examples (untested):
1 (RenderTexture).auto render = RenderTexture::create(width, height); render->begin(); scene->visit(); render->end(); // TODO: You've got a texture: render->getSprite()->getTexture();
2. (FrameBuffer)auto buffer = FrameBuffer::create(1, width, height); auto target = RenderTarget::create(width, height); auto camera = ... buffer->attachRenderTarget(target); camera->setFrameBuffer(buffer); ... // TODO: You've got a texture: frame->getTarget()->getTexture();

So now you have got a texture with whole scene and all scene elements and you can easily apply any shader to this texture.

Example:
GLProgramCache::getInstance()->addGLProgram( GLProgram::createWithFilenames("shader.vert", "shader.frag"), "@shader" ); auto sprite = Sprite::create(texture); sprite->setGLProgram( GLProgramCache::getInstance()->getGLProgram("@shader") );

Feel free to ask any questions.
Good luck :slight_smile:

4 Likes