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. Dismiss Notice

Discussion Ray Marching Optimization Options?

Discussion in 'Image Effects' started by Reid_Taylor, Aug 13, 2023.

  1. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    Hey,

    I have been working on Screen Space Shaders for a new project and I have been a little concerned about efficiency due to this project being target towards the mobile platform.

    Specifically, for my clouds and fog, I have utilized the Ray Marching technique. If I set the march count over around 50-75 I can get away with a decently smooth looking visual. This does run fine on my build tests, but never the less, I would like to get away with under 10-15 marches.

    This is what 60 marches looks like:
    Screenshot 2023-08-12 at 4.17.38 PM.png

    And this is 10:
    Screenshot 2023-08-12 at 4.18.24 PM.png

    As you can see any lower than 30 looks a tad unattractive. After a good amount of thinking, I wanted to open a discussion for any thought, ideas, or speculation on either optimizing solutions to the current ray marching technique or finding ways to patch the blend issues with the low march count.

    Like maybe one could find a way to smooth the areas between marches, in effect blending instead of stepping.
     
  2. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,133
    I've dabbled with raymarching a little bit, and I was posed with the same questions as you. There are a few techniques that are worth reading in to, and are certainly common industry practices.

    If you consider that the number of raymarching steps is the primary factor that negatively affects performance, it makes sense to reduce that as far as possible. Though in turn this greatly sacrifices the visual quality as you've found out.

    For example, this scene looks acceptable with 32 samples.
    upload_2023-8-14_10-49-55.png
    But at 4K resolution this stretches GPU bandwidth too thin (even when downsampled to 1/4th resolution).

    At 8 samples, it runs a lot faster, but the visual quality isn't acceptable:
    upload_2023-8-14_10-52-40.png

    A very common technique to, keep a low sample count and counter the stepped effect, is to offset the ray step by a random distance (between 0 and the maximum step distance). So that you are not sampling strictly at regular intervals, but cover a more generalized area:

    upload_2023-8-14_10-54-36.png
    The same scene, with just 8 samples, but the rays are offset by an animated blue noise texture. This is commonly known as "stochastic sampling".

    The problem now is that you're dealing with visually apparent noise, which needs to be filtered out in some way.

    Blurring the result, using a bi-lateral blur pass (aka depth-aware blurring) gets you some of the splendor back, but at the cost of loosing finer details:
    upload_2023-8-14_11-1-27.png

    Additionally, many implementations rely on "temporal reprojection" for filtering. Broadly speaking, it is a means of using depth/occlusion data from the previous frame and comparing it to the current frame. It is a deeper rabbit hole that I've personally gone into, but haven't nailed the practical implementation yet, so perhaps some one else can chime in there ;)
     
  3. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    Definitely some great information! I'm gonna have to look into stochastic sampling!
     

    Attached Files: