Search Unity

Resolved Orthographic camera and fog in URP?

Discussion in 'Shaders' started by Sinterklaas, Sep 13, 2022.

  1. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    I'm trying to add fog to my shader, which will be used with an orthographic camera. Unfortunately, I'm not super familiar with how orthographic projections actually work. I'm using URP 12.1 and Unity 2021.3.

    In my vertex shader, I do this:
    Code (CSharp):
    1. VertexPositionInputs pos = GetVertexPositionInputs(input.position);
    2. output.fogFactor = ComputeFogFactor(pos.positionCS.z);
    In my fragment shader, I do this to test the output:
    Code (CSharp):
    1. return float4(input.fogFactor, input.fogFactor, input.fogFactor, 1);
    With a perspective camera, this works fine and I get the expected results. With an orthographic camera, I get a solid black color for all objects.

    I'm wondering why this happens? I've tried digging through the source code of ComputeFogFactor and related methods, but I just got confused. Anyone here that can give me some pointers?
     
  2. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    Nevermind, I kinda figured it out by isolating the fog rendering code from the standard URP lit shader and repurposing it. Outputting the fogCoord for testing purposes still gets me black objects everywhere, but if I do the following I get the fog effect that I want, for both perspective and orthographic cameras. So I'll just call that a win and mark the topic as "resolved".

    New fragment shader:
    Code (CSharp):
    1. float fogCoord = InitializeInputDataFog(float4(input.worldPos, 1.0), input.fogFactor);
    2. outputColor.rgb = MixFog(outputColor.rgb, fogCoord);
    The code from the vertex shader wasn't changed.