Hi, cocos community! ))
I’ve started to dive deep into the cocos-creator-based development several months ago. Currently, I’m using version 3.1.1. The majority of the things are straightforward with the typescript version of the engine. Creating sprites, animations, work with vectors, graphics, physics, etc. But when it comes to 2D lighting there is a “fog of war” situation.
From what I’ve got, it seems that cocos creator does not support 2D lighting at all. According to documentation currently, only 3D light is supported, meaning that only 3D objects would be impacted by the currently available lighting sources. Sprites, spine animations, and other 2D objects are out of scope.
Thus, I’ve tried to figure out how to implement my own lighting.
I’ve found quite a useful topic, which explains the math behind the 2D lighting:
Also, I’ve done a basic tutorial regarding the GLSL shaders within the engine. Creating material, effect, developing a shader. That was quite a simple implementation of the partially wiped sprite.
But, it seems, that I still do not have enough knowledge. It is not complex to do raycasting, using the raycast method of the PhysicsSystem2D class, and form a polygon that would contain a highlighted area. But what should I pass to the vertex and fragment shaders? Would form of that polygon be enough?
What does light mean in terms of the shaders? Does light mean changing the pixel color based on the distance from the source of light? All not highlighted areas could be black in the simplest case. All highlighted - not impacted or slightly brighter. Also, it is possible to pass the radius of the lighting source, and implement a gradient-based light, meaning that the light strength would be lower depending on how far are you from the light source. Right?
Also, how should I implement the lighting for multiple sources of light? Should then the shader get all polygons of all highlighted areas? Or it is somehow the other way around?
I’m not quite sure how should I support 2D lighting for all the objects, which I’m currently using in my project. Those are Graphics, Sprite, Spine animations. Should I implement different shaders for each of the objects? Or it will be enough to implement one shader and assign it to all these objects in order to have them all highlighted by the lighting source?
I’ve found evidence, that there is some task in cocos creator’s Trello regarding the 2D lighting:
Is it planned to provide support of 2D lighting to the engine any time soon?
Or, maybe, there exists a cocos creator typescript project, which contains 2D lighting implementation? The one which I could use as a reference?
Please, help me to figure out how should I move on in this area. ))