Search Unity

Question Manually calculating shader parameters unity_WorldToShadow, _LightProjectionParams, _LightShadowData

Discussion in 'General Graphics' started by INeatFreak, Jun 11, 2021.

  1. INeatFreak

    INeatFreak

    Joined:
    Sep 12, 2018
    Posts:
    46
    I am trying to DrawRenderer with same lighting as Unity uses. But there's no documentation/posts/tutorials about how to calculate built-in parameters manually (Unity doesn't give an option to set them for you, so it's all up you) such as unity_WorldToShadow[4], _LightProjectionParams, _LightShadowData, _ShadowCenterAndType etc.

    Since there's no access to the source code of the built-in render pipeline Unity makes it impossible to make this feature work.

    There's has been many posts about this topic but never got proper response. Or ones that's given is just false/obsolete information.

    TLDR; Can we finally get an source code of how these parameters has been calculated? Thanks.
     
    Last edited: Jun 11, 2021
  2. INeatFreak

    INeatFreak

    Joined:
    Sep 12, 2018
    Posts:
    46
    I've managed to find the calculation for _LightShadowData parameter, but the Z value is still off.

    _LightShadowData.x = 1.0 - shadow distance.
    _LightShadowData.y = max(camera far clip / shadow distance, 1.0)
    // but not used in current built-in shader codebase
    _LightShadowData.z = 5.0f * max(camera far clip / shadow distance, 1.0) / min(camera far clip, shadow distance) // not sure about the 5.0f, but it seems like the value
    _LightShadowData.w = -1.0f * (2.0f + camera field of view / 180.0f * 2.0f) // fov is regarded as 0 when orthographic.

    Source: https://forum.unity.com/threads/what-is-in-float4-_lightshadowdata.165718/
     
  3. INeatFreak

    INeatFreak

    Joined:
    Sep 12, 2018
    Posts:
    46
    _LightShadowData.z = 5.0f / min(camera far clip, shadow distance)

    Works for 2019.1 version. But still struggling with unity_WorldToShadow matrix. There's isn't a single working information about how it's calculated.
     
  4. arTk_ev

    arTk_ev

    Joined:
    Mar 24, 2013
    Posts:
    11
    I use in
    Code (CSharp):
    1. void Update()
    2. {
    3. var world2ShadowMatrix = Shader.GetGlobalMatrix("unity_WorldToShadow");
    4. var unity_LightShadowBias = Shader.GetGlobalVector("unity_LightShadowBias");
    5. }
    But unity_WorldToShadow is not matrtix array, and may be old matrix from last frame;