Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Question How do I use the new volumetric shader graph?

Discussion in '2022.2 Beta' started by Ruchir, Jul 13, 2022.

  1. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    I currently don't see how I should use the new volumetric material in unity.
    Are there any samples available?
     
    ashtorak and merpheus like this.
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  3. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    Hello,

    We are currently building volumetric material samples to help get started with the feature and the documentation is in review.
    In the meantime, I can share some details with you:

    Volumetric materials are a new way to customize the fog in Local Volumetric Fog using the ShaderGraph. Instead of authoring your shader for a 2D surface used on meshes, you'll author it for a volume evaluated in 3D. This mainly means that the input of your graph will be a 3D position instead of 2D UV.

    In the ShaderGraph you can use the UV node or the position node to get the 3D position and start creating your effect (UV has a 3rd dimension for this material type).
    upload_2022-7-18_19-14-0.png

    The Material asset can be used in a Local Volumetric Fog when you select the Mask Mode "Material"
    upload_2022-7-18_19-15-9.png

    Here's an example of a graph that applies a noise on a single axis of the fog:
    upload_2022-7-18_19-37-8.png

    Creating complex volumetric materials can quickly become expansive on the GPU, to ensure that your shader is running correctly here are some advice:
    • Try to keep the calculation in your Shader Graph to the minimum, it can quickly become a limiting factor regarding the parallelization of your shader on the GPU
    • Keep in mind that your shader is going to be executed for every voxel in the local volumetric fog, resulting in a lot of pixel shader invocation (similar to the case of transparent overdraw).
    • Use mipmaps when sampling textures, this helps the texture fetcher in the shader since the resolution of the volumetric buffer is usually a lot lower than the screen.
    • Keep the screen space size of the volume to the minimum, you can use the debug mode “LocalVolumetricFogOverdraw” located under Window > Rendering Debugger > Rendering > Fullscreen Debug Mode.
    • Prefer using medium-sized volumes over a lot of small ones. Each volume has a fixed overhead that can become big if using a lot of volumes.
    • In the GPU profiler, look for the “Fog Volume Mesh Voxelization” marker. Usually, the voxelization step is faster than the “Volumetric Lighting”, if it’s not the case, you can try reducing the number of volumes in your scene or lowering the quality of the volumetric fog.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The end goal for me would be to so something similar to what is happening with fluidninja over on Unreal engine. Is this feature a good fit for that behaviour? It can cover quite a large volume at times judging by the fluidninja samples.

    Here is one such example:


    The ability to bake and inject seems profoundly useful for Realtime 3D.
     
    laurentlavigne and valarnur like this.
  5. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    This post was really helpful.:)

    Although some more information about properly settings up and optimising volumetric fog setting would be really helpful.
    Right now it's hard to increase the fog distance more than 100units, otherwise we can quite easily see the lights flickering as we move closer to them.

    For the sample could you include scenarios where we might want volumetric fog in a distance(sun shaft from clouds) but still it should look good enough when close to camera (sun shafts from object shadows etc).

    That would be really helpful.:)
     
    koirat and PutridEx like this.
  6. merpheus

    merpheus

    Joined:
    Mar 5, 2013
    Posts:
    202
    Hi Anthony,
    Is it possible to use a mesh as a volumetric fog shape? So can we sample a mesh/skinned mesh for this? Or do we need to take an SDF-like approach here? (where we have spheres and cubes to make shapes etc)
     
  7. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    We'll not go that far, but plugins/assets can use this system to build effects like that. Though the volumetric fog is not really fit for that kind of effect. In the video showcasing Fluid Ninja on clouds, it uses the volumetric clouds of UE which is different from the volumetric fog (the biggest difference is that volumetric clouds can cast and receive shadows while volumetric fog can only receive them).

    I guess for that kind of effect it would be better to use the HDRP Volumetric Clouds in Manual mode and provide cloud textures from a 2D fluid simulation instead of doing it in 3D with volumetric fog.
     
    hippocoder likes this.
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I see, but this feature is still going to be good for local effects. Sounds like some kind of LOD on the processing of it might come in handy, skip calculations and so on.
     
    antoinel_unity likes this.
  9. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    Yes, we're aware that the current fog settings are pretty hard to configure and the quality isn't good enough compared to the cost of the system and we're actually working on improving this.

    The first thing to do when configuring the fog is to enable the "Show Additional Properties" toggle, it makes visible a lot of useful properties
    upload_2022-7-19_17-7-42.png

    Keep in mind that the setup is highly dependent on the content of your scene and it's not possible to have something that works for all cases.

    Now regarding the settings in the fog component:

    Volumetric Fog Distance: try to minimize the distance as smaller distances will increase the quality of the fog (fog slices will be denser), though you can tweak the other settings to have pretty good results with big distances like 500m or 1km.
    The point here is to have the max distance kind of match the "Distance Fade Start" and "Distance Fade End" settings in the Local Volumetric Fog so that when you reach the max distance of the volumetric fog, the volumes already started to fade out, this helps a lot reducing the flickering of lights.

    Denoising Mode: This one is important, if your fog has sharp features or hard corners, then either use the Reprojection mode or disable it, Gaussian mode will blur the fog so it's ideal for blurry fogs with no hard shapes defined. Keep in mind that the reprojection can cause some ghosting depending on the case.

    Slice Distribution Uniformity
    : This setting can help you reduce the flickering by changing how the slices are distributed, a higher value will increase the precision near the camera and lower values will use a more uniform distribution meaning that the slices' artifacts will be the same near and far away from the camera. Generally, a good value for this is between 0.5 and 0.9 but it depends on your scene.

    Quality and Fog Control Mode: Here I prefer to use Custom and Manual respectively as it provides precise control over the quality and the budget

    Screen Resolution Percentage: This value represents how the fog will be precise, depending on your content, try to lower this value as much as possible. For example, if you're making really diffuse fog you can put a really low value which saves a lot of memory and computation that you can spend elsewhere. I advise you to never go above 30% as the memory cost becomes insane after that.

    Volume Slice Count: This setting is the most important if you want to avoid flickering, as usual, try to minimize this value to reduce the memory cost but if you want to have a fog over long distances, then you'll need to have a lot of slices. It's interesting to tweak this with the slice uniformity distribution as these two settings are related to each other, for example, if you increase a lot the slice count, you may be able to decrease a bit the slice distribution uniformity to put more slices at the back and decrease flickering.

    In the Local Volumetric Fog - Blend Distance and Falloff Mode: Be sure to always have a blend distance above 0, it greatly helps reduce the "slice" artifacts on the border of the volume, also generally the falloff mode "Exponential" generally produces visually a better result for the blend interpolation.

    For more information on the fog settings, please refer to the documentation page: https://docs.unity3d.com/Packages/c...high-definition@15.0/manual/Override-Fog.html
     
  10. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    Right now you need to bake an SDF and sample it in the ShaderGraph if you want to make that. We're working on the second part of this feature that will hopefully allow doing that (it's a VFX graph output for the fog in HDRP)
     
    florianBrn likes this.
  11. merpheus

    merpheus

    Joined:
    Mar 5, 2013
    Posts:
    202
    So right now we can't have skinned meshes moving around, for things like ghosts etc. But VDB support and vfxgraph-fog output will land later on, is that correct?
     
  12. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    I can't say for VDB files because there is no plan on supporting them in Unity right now but for the VFX fog output yes it will land later.
     
  13. merpheus

    merpheus

    Joined:
    Mar 5, 2013
    Posts:
    202
    Cool, last question, will it be possible to have skinned meshes with this feature? (As I mentioned, from games perspective, having an animated fog character can be useful. For things like ghosts etc.)
     
  14. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    We discarded the possibility to have meshes directly voxelized into the fog as it was too expansive to execute in real-time. The recommended approach for this will be to sample the skinned mesh from the VFX graph and output fog
     
  15. Deleted User

    Deleted User

    Guest

    @antoinel_unity are their any plans to support this feature for existing volumetric clouds system in unity???
     
  16. ashtorak

    ashtorak

    Joined:
    Feb 19, 2014
    Posts:
    53
    Thanks, these simple samples are always very helpful. Just need to make sure you have fog enabled everywhere when you put it in your own project :D
    However, I don't see the fog in the scene view in this sample or in my project. Do you have to enable this separately somehow?
     
    Last edited: Jul 21, 2022
  17. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    Not currently, volumetric clouds are using a separate volumetric system which is not compatible with this kind of customization.
     
    Deleted User likes this.
  18. MaxWitsch

    MaxWitsch

    Joined:
    Jul 6, 2015
    Posts:
    114
    Could it be that there is a problem with fog since this change?
    I use two machines on which one the Fog is rendered and one where it isnt.
    Before i made the update it was visible on both machines.

    I tried to regenerate shader includes. Could that be the Problem?
     
  19. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    There are 2 known issues currently: on some GPUs, the fog doesn't display and the fog is not displayed correctly in VR.
    These have been fixed and the fix will be published when it lands
     
    laurentlavigne and MaxWitsch like this.
  20. MaxWitsch

    MaxWitsch

    Joined:
    Jul 6, 2015
    Posts:
    114
    Thank you :) Besides that, fog materials are friggin Awesome!

    Here is a small application scenario of what you can do with it.
     
    Last edited: Jul 22, 2022
    lacas8282, laurent-h, bb8_1 and 13 others like this.
  21. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    Could you share the shader graph, for some reason, I'm not able to make something like it :oops:
     
  22. ashtorak

    ashtorak

    Joined:
    Feb 19, 2014
    Posts:
    53
    So this all nice, but how do you deal with the slicing when moving the camera to and from the fog? I made a short video with this sample project to show what I mean:



    I only increased the light a bit to better see it. When you move the camera around you can see it wobbling between the slices. It's somewhat ok for high slice counts and low fog distances. But what if I want a higher distance or can't afford so many slices? Or am I missing something here? Is it just more suited for close-up scenes or where you stand inside?
     
    bb8_1 and valarnur like this.
  23. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I found I had to play with all the hidden options (show additional settings) and perhaps increase the resolution / near / far range and so on.
     
    ashtorak likes this.
  24. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,136
    Be careful with volumetric fog settings, it can get crazy expensive. Could eat up all your GPU budget alone.
    Unfortunately from my testing you can't really get rid of slicing completely. But trying to find a middle ground using the manual mode is your best bet, reduce it as much as possible while staying within reasonable perf costs.

    Also to see results better you should go in play mode, to see how the fog would really look. It can be more noisy in the scene view.
     
    ashtorak likes this.
  25. hugokostic

    hugokostic

    Joined:
    Sep 23, 2017
    Posts:
    84
    Volumetric Material that Pulse :

    GIF_Pulse_OS_resized.gif

    The Local Fog Volume is set as multiply, so it take effect only in a Point Light rays as you can see if I am moving this cube around :

    GIF_PulseLightMoving_resized.gif

    making Volumetric Fog materials using ShaderGraph is awesome :)
     
    bb8_1, Matjio, Ruchir and 5 others like this.
  26. hugokostic

    hugokostic

    Joined:
    Sep 23, 2017
    Posts:
    84
    If some poeple liked it, I did showcase building the Volumetric Shader (and other 2022.2beta features test) on a Tech-Blog post
     
  27. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    675
    I did something like this in much older unity versions like 2017 that uses the basic 3d template. Both the clouds and the fake circular light is created inside the shader. But I had to use hundreds of stacked quads all facing the camera to simulate the sliced layers of a volumetric area.
    If I recreate the same material in 2022 HDRP shader graph, would it look just the same way without needing any stacked quads trickery? And maybe calculate accurate shadows from actual light sources using the created cloud volume as well? Also is there a way to do the same volumetric rendering with given materials in older unity versions?

    upload_2022-8-14_17-42-23.png
     
    Last edited: Aug 14, 2022
    bb8_1, wm-VR, Onat-H and 4 others like this.
  28. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    looks epic. but i wonder about performance. whenever i tweak the volumetric fog settings to produce something more pleasent like flickering boxes it tanks on my gpu (rtx 2060) and easily needs 4+ ms. in order to create a crisp result as yours i would assume it going up to 10ms.
     
    MaxWitsch likes this.
  29. MaxWitsch

    MaxWitsch

    Joined:
    Jul 6, 2015
    Posts:
    114
    Try to use as few samples and resolution as possible and use Reprojection as a filter in your volume settings.
    Also try to minimize the area covered by the LocalFogVolume as each voxel has to go through the shader code.
     
  30. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    Short answer: no. This is mostly because of the lighting: In HDRP the lighting evaluation of local volumetric fogs is pretty simple and doesn't take into account the attenuation of the fog like in your image (i.e. the fog has no effect on the light emitted from the lights).
    This is pretty similar to the self shadow in volumetric effects and we don't support that yet in HDRP.

    Without writing a custom fog solution I don't think it's possible. The old HDRP fog was not flexible enough for this and only supported 3D textures.
     
  31. AydinDeveloper

    AydinDeveloper

    Joined:
    Sep 8, 2017
    Posts:
    92
  32. lacas8282

    lacas8282

    Joined:
    Apr 25, 2015
    Posts:
    138
    Hello!
    I just using local fog with a custom material, shadergraph.

    I have a very big terrain, layermask is Ground. Lets say it is 5000 unit width and 40 units in Z, and height 500 unit.

    So I put a local fog width 5000 unit width, 40 z, 500 height to fit the box the terrain.

    1, is it expensive or is it a good solution?
    2, my terrain is a custom terrain, but the height is changing. How can I do a terrain fit to my local fog somehow? (Like raycasts) In the shadergraph? How?

    Thanks,
    Leslie
     
  33. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    263
    The volume in itself will not be that expense, it mostly depends on your settings for the Volumetric Fog. Of course, doing a lot of computation in the ShaderGraph will be slower but generally, it's not a lot compared to the cost of the Volumetric Lighting (see my post above to set the settings correctly according to the content of your scene)

    If you don't have access to the heightmap of your terrain, one solution could be to capture a depth map from an orthographic camera viewing all your terrain. You can bake this map so there is no extra cost at runtime aside from sampling the texture in the volumetric ShaderGraph.
    Alternatively, you can make the depth texture fully in C# with raycasts by creating let's say a 4k R32 texture and raycasting your terrain for each pixel.
     
    lacas8282 likes this.
  34. hugokostic

    hugokostic

    Joined:
    Sep 23, 2017
    Posts:
    84
    Yes you can do couple of things using the Volumetric ShaderGraph :
    ProceduralFogLight.gif


    Please look at my Tech-Blog at this post, second part (after water) is on VolumetricFog using custom ShaderGraph made last year for 2022 beta but this part is still actual, I do explain few tricks regarding how to handle fog's UV, few shader tricks and some helper script and how to handle texture like 3D noise too. Hope it help
    As antoinel said, in order to have such effect looking good, you will have to setup a high volumetric fog quality.
     
    lacas8282 likes this.