jiaozi158 / UnityVolumetricCloudsURP

Volumetric Clouds for Unity URP (Universal Render Pipeline).
MIT License
127 stars 9 forks source link

Accessing Cloud Shadow (Light Cookie) via Shader Graph #5

Open MountainTigerCoding opened 3 months ago

MountainTigerCoding commented 3 months ago

Hi, I currently have a scene filled with foliage which greatly relies on SSS. It normally functions by multiplying the SSS with the directional lights' shadow map, except due to the cloud shadows using light cookies instead, it causes the foliage to glow while it's in shadow.

I have followed the instructions from this post, but I can't get it to work. "Just defining a multicompile _LIGHT_COOKIES keyword will be enough. After that your light.color will automatically be multipled by the cookie if there is one, you dont need to do anything manually." https://www.reddit.com/r/Unity3D/comments/yjsryp/soooo_how_are_light_cookies_supposed_to_work_with/

Example graph: Screenshot 2024-06-24 201104

How would you go about fixing this?

Thanks

jiaozi158 commented 3 months ago

Hello, maybe you can try changing the keyword scope to "Global", because "_LIGHT_COOKIES" is a global shader keyword.

But I don't think the issue can be solved in this way because Lit shader graph should have already defined that keyword.

To find out a solution, let's check out how main light cookie is applied:

Light GetMainLight(float4 shadowCoord, float3 positionWS, half4 shadowMask)
{
    Light light = GetMainLight();
    // A 0-1 shadow value
    light.shadowAttenuation = MainLightShadow(shadowCoord, positionWS, shadowMask, _MainLightOcclusionProbes);

    // Note: the clouds shadow cookie is always a black & white (0-1) texture, so the 3 color channels are the same
    #if defined(_LIGHT_COOKIES)
        // Get the main light cookie color
        real3 cookieColor = SampleMainLightCookie(positionWS);
        // Modify main light color by multiplying with cookie color
        light.color *= cookieColor;
    #endif

    return light;
}

So, you can get the cookie color with SampleMainLightCookie(positionWS); in a custom function node and multiply with "Shadow" (or "Emission") in your shader graph.

MountainTigerCoding commented 3 months ago

Thank you for the explanation @jiaozi158, works perfectly now!