본문으로 바로가기

URP Realtime Shadow color control

category Technical Report/Unity Shader 2022. 1. 22. 03:22
반응형

 

URP에서는 Realtime casting으로는 Environment의 Realtime Shadow color 옵션이 작동하지 않는다.

이걸 조절할수 있게 바꾸는 방법은 Shader를 다 뜯어야 하는데....

Lighting.hlsl에는 아래와 같이 정의되어 있다.


half3 SubtractDirectMainLightFromLightmap(Light mainLight, half3 normalWS, half3 bakedGI)
{
    // Let's try to make realtime shadows work on a surface, which already contains
    // baked lighting and shadowing from the main sun light.
    // Summary:
    // 1) Calculate possible value in the shadow by subtracting estimated light contribution from the places occluded by realtime shadow:
    //      a) preserves other baked lights and light bounces
    //      b) eliminates shadows on the geometry facing away from the light
    // 2) Clamp against user defined ShadowColor.
    // 3) Pick original lightmap value, if it is the darkest one.


    // 1) Gives good estimate of illumination as if light would've been shadowed during the bake.
    // We only subtract the main direction light. This is accounted in the contribution term below.
    half shadowStrength = GetMainLightShadowStrength();
    half contributionTerm = saturate(dot(mainLight.direction, normalWS));
    half3 lambert = mainLight.color * contributionTerm;
    half3 estimatedLightContributionMaskedByInverseOfShadow = lambert * (1.0 - mainLight.shadowAttenuation);
    half3 subtractedLightmap = bakedGI - estimatedLightContributionMaskedByInverseOfShadow;

    // 2) Allows user to define overall ambient of the scene and control situation when realtime shadow becomes too dark.
    half3 realtimeShadow = max(subtractedLightmap, _SubtractiveShadowColor.xyz);
    realtimeShadow = lerp(bakedGI, realtimeShadow, shadowStrength);

    // 3) Pick darkest color
    return min(bakedGI, realtimeShadow);
}

 

이걸 조금 뜯어고치면

  
 half4 color = half4(diffuse * NdotL * mainlight.color * mainlight.shadowAttenuation + (1.0 - mainlight.shadowAttenuation) * _SubtractiveShadowColor.xyz, diffuse.a);

로 바꿔서 작동하게 할 수 있다.

실제 내부에서 작동하는 code는 subtractive에서 활성화 했을때만 작동한다.


void MixRealtimeAndBakedGI(inout Light light, half3 normalWS, inout half3 bakedGI)
{
#if defined(LIGHTMAP_ON) && defined(_MIXED_LIGHTING_SUBTRACTIVE)
    bakedGI = SubtractDirectMainLightFromLightmap(light, normalWS, bakedGI);
#endif
}

 

PBR을 뜯어고치는번거로움이 싫다면 보이지 않는 씬에 아주 작게 Mixed/Subtractive로 Bake를 해두고 메인씬에 적용하면 간단하게 해당 옵션을 사용할 수 있다.

 

 

 

반응형

'Technical Report > Unity Shader' 카테고리의 다른 글

stochastic tiling shader  (0) 2022.05.29
PBR-BRDF-Disney-Unity-1  (0) 2022.03.02
URP BlinnPhong Default Character Shader  (0) 2021.07.14
URP Shader BlinnPhong with Normal mapping  (0) 2021.07.08
URP Shader coding guide Basic term  (3) 2021.05.01