Whether there is some performance issue about Directional Light?

I am trying to add directional light in my test game scene which is designed in cocos studio and loaded within code.

auto light = DirectionLight::create(Vec3(0,-2,-1), Color3B::YELLOW );
light->retain();
light->setEnabled(true);
addChild(light);
light->setLightFlag( LightFlag::LIGHT0);
light->setCameraMask((unsigned short) CameraFlag::USER1);

but, it seems that it will slow the fps from 60 FPS to 30FPS. so i want to confirm it with you guys. How to use light correctly with cocos2d-x 3.x ?

i have tried the cpp-tests, Node-Light test also under 30FPS.

update: on device cpp-tests works 60fps. but my case is still low fps.

Don’t test performance with Simulators, they are often have terrible performance more recently due to the retina display resolutions. Make sure you only test FPS and performance on a device where you intend to run your game.

@stevetranby sure, i have test my project on device also. it is not cpp-tests.
cpp-tests works find on device also. but with light in my project will cut down fps.
it seems when high drawcalls around 114, fps slow to 40fps. but when light disable, fps can return to 60fps.

low drawcalls but low fps also when light is enabled.

Yes,same with me.
I found that if there is a big model which occupy the entire screen in my scene and enable the light at the same time,fps slow to 30fps,if i move the camera far away form the big model, fps can return to 60fps.

Same issue. This light it is simply impossible to use.

Interesting. I do know that shadows are often a major perf issue on mobile, but I’m surprised the lighting shader drops the FPS so drastically. Can I ask the minimum device you’re targeting? I wouldn’t be surprised if the A5 chips can’t handle lighting, but anything A6 or beyond should handle it well, I would think, but that’s without testing.

Did you say the cocos2d test project ran fine, but just not your specific project/code?

I’m interested to find out where the issue lies, whether with low-end devices, high vertex count models, the frag shader pipeline, or something else.

Dynamic lighting is generally quite expensive on mobiles especially. I can hazard a guess at the issue here.

I believe if a lit model is taking up the entire display then the lighting calculations in the pixel (frag) shader are calculated for all those pixels on the display, it is probably the case that whatever device you’re using simply can’t handle that amount of lighting calculations per frame. I don’t think the number of draw calls is making a difference (unless it was a huge amount of course).

Looking at the tests example it appears that the intention is to provide dynamic lighting on only one or two small objects in a scene that are always a fixed distance from the camera, such as a main character. This is ensures that there is only a small amount of pixels that require dynamic lighting calculations.

There are other factors too, it would be interesting to see what the frag shader looks like if anyone can be bothered to dig it out of the sdk and post it here. For example what type of light shading they use (gouraud, blinn-phong etc). It might not be optimized very well.

@cnsoft1 I’m not sure what your game does but it looks to me like you don’t really need dynamic lighting and pre baked lighting would be a much better solution. You would need a light map for the texture, but I’m not sure if cocos2d-x supports texture mapping yet and if not you would need some knowledge of shaders.

Edit: I just noticed that Cocos2D-x v3.7 supports texture mapping so go nuts. :smile:

@stevetranby @UKDeveloper99 sure, i am just testing it. i use pre-baked texture previously instead of dynamic light.
i post here, expect someone can do test also and figure out the issue.