Light Lamp Effect in dark: Shader vs ShadowLayer?

I want my world to become dark , what should be better approach , make it dark using a shadow layer with little opacity and black color (easy one) or shader which’ll decrease brightness of whole world then i’ll attach light-effect shader with Lamp node ?

use shader maybe give you better quality.
just pass time value to uniform and update light level in shader, for light effect use another RenderTexture to get all light object and pass to shader.

https://dl.dropboxusercontent.com/u/42579181/GIF.gif

@namkazt Thanks for replying , yes i could use render texture but i fear that it’ll be costly and i want to run my thing on low end devices too , also i’m having problem while implementing shader i’ve described it on a seperate thread Lag or jerk as soon i implement shader [Code Inside][Solved]{Add Glprog to cache}

Btw your game looks great ! :smiley:

Thank You

If you apply shader for each sprite and you have so much sprite so i think it will lag.
But i think we just can merge all sprite to few texture then apply shader to each texture it will be ok.
Like my game. i have only 2 texture to apply shader and it running good on low end device.

For some special effect like dynamic light, as i say you can merge all like object into 1 RenderTexture and apply shader in it, it will be good.
check my dynamic light extension : https://github.com/namkazt/DynamicLight

note: if your sprite need disable and re-enable many time so it will be good if you pass a bool value to disable on shader to avoid init new GLProgram. maybe it will be help.

1 Like

@namkazt that dynamiclight looks great i was looking one seperate DynamicLight from avalon library and you created it :smiley: , but right now i have different problem i have only 10-15 sprites i guess i can shade them without any problem if i can get my GLprogram in cache and then i can change GLProgramState of each sprite but right now i have trouble with my code as i have implemented it in a wrong manner can you give it a look here is that code Lag or jerk as soon i implement shader [Code Inside][Solved]{Add Glprog to cache}

sory but what version of cocos2dx do you use ? and maybe you should pm ricardo for this problems.

cocos2d-x-3.8 , i found the reason why is it failing but dont know how to fix it, i you would like to know what i found i dicuss it on this thread you can join here Lag or jerk as soon i implement shader [Code Inside][Solved]{Add Glprog to cache}

Thank You

@namkazt sprite issue solved now we can discuss about light using render texture , i’ll tell you tommorow after i’ll check out your DynamicLight repo , i guess DynamicLight will solve my problem

@namkazt okay i tried dynamic light its great :smile: , but how do i decrease brightness.
You said

is light level shader already there by default or i have to implement shader manually to decrease brightness of whole world ( to simulate night ) like you did in your game ?

Thank you

Here is my day/night cycle shader

//========================================
// day/night cycle effect
//========================================
uniform vec3 ambientColor;               // the dark color
uniform float timeInDay;                    // time in day : 1.0 -> 24.0

shader content:

    vec4 diffuseColor = texture2D(CC_Texture0,v_texCoord);
    vec4 resultColor = diffuseColor;
    float indestyNight = 1.35;
    float indestyMidNight = 0.35;
    float indestyTwight = 4.35;
    float indestyDay = 8.0;
    float indesty = 1.0;
    ColorGradingPreset colorGradingSource;
    //-------------------------------------------------------------------
    // Calculate part
    //-------------------------------------------------------------------
    if(timeInDay > 18.0 && timeInDay < 24.0) {
        indesty = ((indestyTwight - indestyMidNight) - ((timeInDay - 18.0)/6.0 * (indestyTwight - indestyMidNight))) + indestyMidNight;
        colorGradingSource = colorGradingPresetLerp(ColorGradingNormal, ColorGradingNight, (timeInDay - 18.0)/6.0);
    }
    if(timeInDay >= 0.0 && timeInDay <= 3.0) {
        indesty = indestyMidNight;
    }
    if(timeInDay > 3.0 && timeInDay <= 6.0) {
        indesty = ((timeInDay - 3.0)/3.0 * (indestyNight - indestyMidNight)) + indestyMidNight;
    }
    if(timeInDay > 6.0 && timeInDay <= 8.0) {
        indesty = ((timeInDay - 6.0)/2.0 * (indestyDay - indestyNight)) + indestyNight;

    }
    if(timeInDay > 8.0 && timeInDay <= 15.0) {
        indesty = indestyDay;
    }
    if(timeInDay > 15.0 && timeInDay <= 18.0) {
        indesty = ((indestyDay - indestyTwight) - ((timeInDay - 15.0)/3.0 * (indestyDay - indestyTwight))) + indestyTwight;
    }
    //-------------------------------------------------------------------
    vec3 ambientResult = diffuseColor.rgb * (ambientColor * indesty);
    resultColor.xyz = resultColor.xyz * ambientResult;

   //=======================================================================
   // Final result
   //=======================================================================
   gl_FragColor = v_fragmentColor * resultColor;

you can apply color grading for better color result like Cold color at night and Warm color at Day.

I would recommend creating a 1D gradient texture that describes a 24h light cycle. This can be as small as 6x1 (1 colour per 4 hours), or even as large as 1440x1 (1 colour per minute).

Feed this texture to your shader along with a normalised ‘time’ value used to sample the texture. Where 0.0f = 00:00 and 1.0f = 24:00. If you have Linear Filtering enabled the sampler will blend between the colours for you.

This approach avoids branching in the shader which can be quite expensive, as well as giving you control over the light colour/intensity; visualized in a texture :smiley:

1 Like

@namkazt Worked like charm ! :smile: , this is how i implemented it , i had few light objects and i wanted them to glow in dark so i added all my world and light object (generated by https://github.com/namkazt/DynamicLight) to a shaderlayer ( renderTexture layer ) then i applied your day/night shader on final renderTexture before flushing it to screen and it worked , it is all good now but the day/night shader is also affecting luminiosity of my light object ( it is fine but could be better if i did not affect luminiosity of light objects ) otherwise everything is good

Thank You

@almax27 i tried @namkazt 's method and it is working just fine for me , haven’t tested it on old low spec devices but i guess it’ll work just fine but i’ll report back here as soon as i’m completed with testing

Thanks for replying :smile:

you should not visit light object on final render texture ( where you apply day/night shader ) because it will affected. so just add it on top of render texture or pass light texture to day/night shader and merge with final texture when day/night process done.

@namkazt @almax27 i was wondering, What is costly applying shader to simulate darkness or a external cocos layer with dark color ? i think adding external layer is less costly as compared to applying shader for darkness because applying shader uses same phenomena as multi-pass rendering