Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to "unpack" unity_LightShadowBias and _WorldSpaceLightPos0 values?

Discussion in 'Shaders' started by Simod, Jan 10, 2017.

  1. Simod

    Simod

    Joined:
    Jan 29, 2014
    Posts:
    176
    Hi,

    The title might be a bit confusing. I am trying to understand the internal values of the listed variables. This is what I found so far:

    ====================================================

    Bias variable seems most complicated:
    unity_LightShadowBias.z - Normal Bias?
    unity_LightShadowBias.x - Bias value ?
    unity_LightShadowBias.y - What is here?


    ====================================================

    _WorldSpaceLightPos0.xyz - location of the light source
    _WorldSpaceLightPos0.w - what is this?

    For example:
    Code (CSharp):
    1. // Computes world space light direction, from world space position
    2. inline float3 UnityWorldSpaceLightDir( in float3 worldPos )
    3. {
    4.     #ifndef USING_LIGHT_MULTI_COMPILE
    5.  
    6.        // LINE OF INTEREST
    7.         return _WorldSpaceLightPos0.xyz - worldPos * _WorldSpaceLightPos0.w;
    8.     #else
    9.         #ifndef USING_DIRECTIONAL_LIGHT
    10.         return _WorldSpaceLightPos0.xyz - worldPos;
    11.         #else
    12.         return _WorldSpaceLightPos0.xyz;
    13.         #endif
    14.     #endif
    15. }
    My crazy guess _WorldSpaceLightPos0.xyz - worldPos * _WorldSpaceLightPos0.w normalizes vector somehow. Am I correct?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html
    _WorldSpaceLightPos0.w is either 0 or 1, and xyz is either a normalized world direction or a world space position. If it's a direction, and the w is 0, the worldPos is ignored as you already have a world space direction for directional lights. If it's a 1 the worldPos is subtracted from the world position, and the normalized result would be the world space direction for point / spot lights.
     
  3. Simod

    Simod

    Joined:
    Jan 29, 2014
    Posts:
    176
    Cool thanks for the link. This is very helpful. Though any ideas what unity_LightShadowBias.y might be for? What is inside?

    The code I am referring to is this:
    Code (CSharp):
    1. float4 UnityApplyLinearShadowBias(float4 clipPos)
    2. {
    3. #if defined(UNITY_REVERSED_Z)
    4.     clipPos.z += clamp(unity_LightShadowBias.x/clipPos.w, -1, 0);
    5.     float clamped = min(clipPos.z, clipPos.w*UNITY_NEAR_CLIP_VALUE);
    6. #else
    7.     clipPos.z += saturate(unity_LightShadowBias.x/clipPos.w);
    8.     float clamped = max(clipPos.z, clipPos.w*UNITY_NEAR_CLIP_VALUE);
    9. #endif
    10.     clipPos.z = lerp(clipPos.z, clamped, unity_LightShadowBias.y);
    11.     return clipPos;
    12. }
     
  4. Simod

    Simod

    Joined:
    Jan 29, 2014
    Posts:
    176
    Still trying to figure out unity_LightShadowBias components. Any clues on this?
     
  5. TheMasonX

    TheMasonX

    Joined:
    Nov 9, 2012
    Posts:
    27
    unity_LightShadowBias.x = Bias (the top one) * 2 for point + spot. Somehow related linearly to min(cam.farClip, shadow distance - shadow near plane offset) for directional

    unity_LightShadowBias.y = ?
    The only thing I've found so far is that it's 0 for point and spot, and 1 for directional. Switching to area doesn't change it (the value stays with whichever the last value was). It also seems to be using the value of the most "important" light. No other clues at the moment though, sorry.

    unity_LightShadowBias.z = something with normal bias, too busy to investigate further though :/
     
    Last edited: Jan 28, 2017
  6. vachel_l

    vachel_l

    Joined:
    Jan 25, 2018
    Posts:
    1
    why unity_LightShadowBias.x is divided by clipPos.w in UnityApplyLinearShadowBias()?
    Isn't it will be divide by clipPos.w by perspective divide later, so it's divide by clipPos.w twice, why?
     
  7. reach334

    reach334

    Joined:
    Jan 7, 2018
    Posts:
    1
    The bias should be propotional to change rate of the nonlinear z, which is in form of (a/z + b). Its derivative is -2a/(z^2),where the twice division come from.