Adding Lights in Cocos2d-x?

Hello,

Is there a way to add lights in cocos2d-x 3.0 RC1?

Like i have a sprite (a flashlight) and I want it to light up? is there a way to make this?

Thanks for reading,

@egordm

Definitely you could add lighting through shader.

Maybe you should refer to some lighting shaders for help.

It depends on what kind of effect you are after. You could just replace a sprite with a lit one (or maybe add a sprite with a halo on top of it). More complex lighting is possible with shaders. Here’s something I tried out with cocos2d-x 2.2.0 https://www.youtube.com/watch?v=iPkO1H1JYdw

@owen
Wow thanks for a quick answer, are there tutorials to add shaders like this to cocos2d-x game?

@KJS
Wow your lighting looks very good, have you did that with shaders? Could you please tell me how did you did that?

@egordm

Yes it’s done with shaders. You need to have a normal map for the light calculations for your sprites, and also pass the light position(s) to the shader (you need to make your own shader for that). Here’s a reference for the technique in 3D. 2D case is simpler to implement. http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/

If you are totally new to the shaders, this might help you: http://www.raywenderlich.com/10862/how-to-create-cool-effects-with-custom-shaders-in-opengl-es-2-0-and-cocos2d-2-x

@KJS

Wow thank you very much I will try that!

@KJS wrote:

@egordm

Yes it’s done with shaders. You need to have a normal map for the light calculations for your sprites, and also pass the light position(s) to the shader (you need to make your own shader for that). Here’s a reference for the technique in 3D. 2D case is simpler to implement. http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/

If you are totally new to the shaders, this might help you: http://www.raywenderlich.com/10862/how-to-create-cool-effects-with-custom-shaders-in-opengl-es-2-0-and-cocos2d-2-x

Hey Thanks for your replies I now know the basics of the shaders, and I had one more question. Is it possible to apply shaders to tilemaps? or just SpriteBatchNodes?

@egordm

All CCNodes have setShaderProgram(CCGLProgram *pShaderProgram) - method. Looking at the api, CCTMXLayer derives from CCSpriteBatchNode (which derives from CCNode).

Does anyone know of a light shader that works with multiple light sources?