Search Unity

Resolved Struggling to get custom fog to work with scene

Discussion in 'Shaders' started by TheJinxedArtist, Mar 21, 2024.

  1. TheJinxedArtist

    TheJinxedArtist

    Joined:
    Jun 4, 2020
    Posts:
    3
    Sorry for the awful title, but oh well. So, I have been working on a volumetric fog system for my project in URP, I have got the raymarching stuff set up and that all works, but I am now struggling to get this fog (which is being applied as a full-screen pass) to interact with the scene, it's currently behaving as though there was nothing there and it is just being pasted on top of it. I know why this is happening, and that's because I need to combine the depth textures, but I am struggling to find a way to do it. Here's the code:

    Code (CSharp):
    1. half sdScene(half3 p, half h)
    2. {
    3.     return p.y - h; // Signed distance field, kinda, i think...
    4. }
    5.  
    6. inline void CalculateVolumeDensity_half(int samples, half maxDist, half3 rayOrigin, half3 rayDir, half height, out float accum)
    7. {
    8.     half stepSize = maxDist / half(samples);
    9.  
    10.     half d = 0.0; // Distance
    11.     half a = 0.0; // Accumulation
    12.     for(int i = 0; i < samples; i++)
    13.     {
    14.         half3 p = rayOrigin + rayDir * d; // Position
    15.         half s = sdScene(p, height); // Distance to scene at any point.
    16.  
    17.         // Check if we are inside the fog (below the specified height value),
    18.         // If we are, use a fixed step rate.
    19.         if(s < 0.01)
    20.         {
    21.             d += stepSize;
    22.             a += stepSize;
    23.         }
    24.         // If we are not inside the fog, but still in range, use Spheretracing.
    25.         else if(s >= 0.01 && d < maxDist)
    26.         {
    27.             d += s;
    28.         }
    29.         // If we are outside of the fog's range, just break out of the loop.
    30.         else
    31.         {
    32.             break;
    33.         }
    34.     }
    35.     //return the accumulated fog.
    36.     accum = a;
    37. }
    Thank you to anyone who is able to help, and sorry for wording this whole thing poorly.
     
  2. TheJinxedArtist

    TheJinxedArtist

    Joined:
    Jun 4, 2020
    Posts:
    3
    After messing around in Shadertoy, I have figured out how to fix this. For anyone wondering, I just have to check if the fog's distance is greater than or equal to the Unity scene's distance.