Search Unity

Question Using the Fog node with a unlit master

Discussion in 'Shader Graph' started by YuriyPopov, Jan 27, 2020.

  1. YuriyPopov

    YuriyPopov

    Joined:
    Sep 5, 2017
    Posts:
    237
    Using the fog node with a unlit master node being active result in the return value of the density being completely black (understand 0). When a PBR master node is active however the density value is correct. This is happening on URP 7.1.7/unity 2019.3 and URP 7.1.8/unity 2020. Has anyone encountered this problem and how can it be solved?
     
  2. GilG

    GilG

    Joined:
    Jan 30, 2017
    Posts:
    27
    Hello, I'm currently trying to add fog to a shader and have the exact same issue.
    Did you already reported it as a bug ?
     
  3. alexandral_unity

    alexandral_unity

    Unity Technologies

    Joined:
    Jun 18, 2018
    Posts:
    163
  4. GilG

    GilG

    Joined:
    Jan 30, 2017
    Posts:
    27
    Thank you very much !
     
  5. YuriyPopov

    YuriyPopov

    Joined:
    Sep 5, 2017
    Posts:
    237
    Hey I finally got around to fixing this issue. I ended up implementing a exponential squared fog since this is what we would be using but the other formulas are as follows and people can implement them easily.
    Code (CSharp):
    1. Linear Fog = (FogEnd - ViewpointDistance) / (FogEnd - FogStart)
    2. Exponential Fog = 1.0 / 2.71828 power (ViewpointDistance * FogDensity)
    3. Exponential Fog 2 = 1.0 / 2.71828 power ((ViewpointDistance * FogDensity) * (ViewpointDistance * FogDensity))
    I'm also uploading the relevant nodes in the shader graph.
     

    Attached Files:

    GilG likes this.
  6. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    484
    Code from the URP Core.hlsl
    Code (CSharp):
    1. real ComputeFogIntensity(real fogFactor)
    2. {
    3.     real fogIntensity = 0.0h;
    4. #if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
    5. #if defined(FOG_EXP)
    6.     // factor = exp(-density*z)
    7.     // fogFactor = density*z compute at vertex
    8.     fogIntensity = saturate(exp2(-fogFactor));
    9. #elif defined(FOG_EXP2)
    10.     // factor = exp(-(density*z)^2)
    11.     // fogFactor = density*z compute at vertex
    12.     fogIntensity = saturate(exp2(-fogFactor * fogFactor));
    13. #elif defined(FOG_LINEAR)
    14.     fogIntensity = fogFactor;
    15. #endif
    16. #endif
    17.     return fogIntensity;
    18. }
    upload_2020-8-13_22-0-41.png