Using the new Volumetric Fog features in 2018.2 HDRP

Hey all,

I was wondering if someone had some ways to get started with using the Volumetric Fog support that was added in HDRP. Using the “Volume” component I added the Volumetric Fog and Volumetric Lighting Controller overrides and tried to make a DensityVolume but I couldn’t pass anything to the “Density Mask Texture” slot.

I tried adding in the texture generated here Unity - Manual: 3D textures at runtime but even then it didn’t produce any effect at all.

Thanks, ~Arhowk

Here are the steps to have volumetric fog (this was done with unity 2018.2.0f2, HDRP Template project and HDRP version 2.0.5-preview) :

  • Be sure to enable “Support Volumetrics” and optionally enable “Increase resolution of volumetrics” in your HDRP asset:

  • In the Volume Settings of your scene, add a “Volumetric Fog” and a “Volumetric Lighting Controller” component.
    Change the “Fog Type” value of the “Visual Environment” to “Volumetric”.

  • You should already be able to see the volumetric fog in the scene and game view.
    If it doesn’t show up in the scene view, check that the fog toggle is enabled in the scene view :

  • Note that by default, the fog density is very low. This is set by the “Mean Free Path” parameter : the lower the parameter, the higher is the density of the fog.

  • Now you can add a density volume object in your scene to control the fog density locally :
    3567940--287658--upload_2018-7-18_13-30-7.png

  • Here I added a blue fog with a point light inside in the scene :

Now for the density texture :
I modified the example script of texture 3D so that the generated texture can be use in a density volume. Here is my code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering.HDPipeline;
using UnityEngine.Experimental.Rendering;

[RequireComponent(typeof(DensityVolume))]
public class Texture3DToDensityVolume : MonoBehaviour
{

    Texture3D texture;

    void Start ()
    {
        // The curent density volume texture size is hard coded to be 32
        texture = CreateTexture3D (32);

        DensityVolume densityVolume = GetComponent<DensityVolume>();
        densityVolume.parameters.volumeMask = texture;
    }

    Texture3D CreateTexture3D (int size)
    {
        Color[] colorArray = new Color[size * size * size];
        texture = new Texture3D (size, size, size, TextureFormat.Alpha8, true);
        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                for (int z = 0; z < size; z++) {
                    // Calculate the radius
                    float f =Mathf.Sqrt(
                        Mathf.Pow( 2f * ( x - 0.5f*size ) / size, 2 ) +
                        Mathf.Pow( 2f * ( y - 0.5f*size ) / size, 2 ) +
                        Mathf.Pow( 2f * ( z - 0.5f*size ) / size, 2 )
                        );
                    // Fill pixels of radius <=1 with alpha = 1
                    f = (f <= 1f )? 1f : 0f;
                    Color c = new Color (1.0f, 1.0f, 1.0f, f);
                    colorArray[x + (y * size) + (z * size * size)] = c;
                }
            }
        }
        texture.SetPixels (colorArray);
        texture.Apply ();
        return texture;
    }
       
}

As you can see, the texture format used has to be Alpha8, as we only need one channel to store the density multiplier.

Alternatively, you can create a static density 3D texture.
For the example, I used this texture, authored in photoshop :

It contains 32 slices of 32x32 pixels.
Import it using these settings :
3567940--287671--upload_2018-7-18_14-23-10.png

Now, open the 3D texture creation tool:
3567940--287672--upload_2018-7-18_14-23-46.png

Drop the texture in the texture field and hit “Generate 3D Texture”:

The newly created 3D texture is saved in the same folder as the original one, and ce be used in the density volume settings.
This one will render rings like this :

I hope that I covered the subject and that now you can enter the world of volumetrics :slight_smile:

15 Likes

Thanks alot for the in depth response! I got my setup working complete with a custom volumetric mask and it looks great.

~Jake.

Awesome! Do you plan to add some pre-integer shapes (I think about ellipsoidal fog from Robert C) with feather etc? and some exemple of cool 3D texture (cloudy etc) to play with? or will it be to users to create there own from scratch?
big up, i think i won’t use fog volume anymore, the HDRP is amazing

We didn’t plan this, but I’ll keep it in mind.

2 Likes

Hey Remy!

I’m trying to get this working in Unity 2018.2.10f1 but get some wonky results when I enter play mode. The same was happening in 2018.2.9, FWIW, and I just upgraded to see if that would fix it. (See screenshots below.)

I’m using the HDRP template, and added “Volumetric Fog” and “Volumetric Lighting Controller” to Volume Settings. I’m using the same settings as your examples (defaults). I’ve also added a Density Volume at the world origin w/ no rotation and uniform scale 10, with default settings on the Density Volume script. (See screenshots for exact details).

I’m on macOS High Sierra v10.13.6 on a brand new 2018 MacBook Pro with a Radeon Pro 560X.

If while in play mode I change the Visual Environment → Fog Type from “Volumetric” to anything else, the scene renders as expected.

Interestingly, when not in play mode, the Game viewport renders the scene with volumetric fog beautifully.

It’s killing me that I can’t run and see things running in realtime. Any suggestions? Thanks a lot!



Looks like one of my images didn’t make it. Let me try again…

This is very strange. I did the setup on a very similar machine (2017 MacBook pro, Radeon Pro 560, macOS 10.13.6), and it’s working fine.
It’s worth the try to delete the library folder and re-open the project ?

I deleted Library and rebooted. Same deal. I put up the project on github. Any chance you can take a look?

https://github.com/drone1/hdrptest

@Remy_Unity I just upgraded to 2018.2.11f1 today and started a new project. It actually seems to work at random, although the results are still a bit off, in that the fog animates (when the camera is static).

I’ve captured 2 videos, one where it’s as close to working as I’ve seen (with animating/thrashing/z-fighting/or something fog), and another where it’s crazytown.

I hope this helps. I am dying to use this tech so please let me know if there is anything else I can do to help.

1 Like

@4Xrn7oIe

I was able to reproduce the issue with the mentioned versions.

I also tried updating to Unity version 2018.3.0b8 and HDRP 4.1.0, and now the bug doesn’t occur anymore.
You will have to upgrade your settings (re-enable volumetric fog and move the density volume size from the object scale to the size property).
Could you please try with those newer version ?

In 2018.3.0b8 (verified via “About Unity” after installing), I created a HDRP project and followed the same steps in your initial post.

Both with a Density Volume and without, setting Mean Free Path to “2” rendered perfectly fine in the preview pane, but was black every time in Play mode.

I tried to make a movie but Unity crashed and did not save. Do not have time to do it again. But basically I hold down the “back” key to move the main camera backwards, and the blackness scales down. It seems the blackness encompasses the scene (maybe 3x3x3 units or whatever). When I rotate the camera (right mouse click and drag), I can see big steppy-aliased edges to the black area. Not sure if that helps.

As I mentioned, I’ve tested with HDRP 4.1.0 → go in Window/Package Manager, and update the HDRP package.

@Remy_Unity it works! Thanks.

How can I get the “3D texture creation tool” ?

Maybe i’m a bit late but Windows>Rendering>Density Volume tool

1 Like

Nope, you are not…
Thank!

Thanks for the useful post. I was able to get the fog up and running.

I have a kind of specific question though, I’m not sure it fits here but I’ll ask it anyway:

Can you use the volumetric fog density values in custom shaders made in Shader Graph?

There is a Fog Node in there but the density seems to be always zero and my shader is not being affected by the fog at all. I’m assuming from the shader source that it uses the old fog system, could that be the case?

I attached a screenshot of my scene. The custom shader is on the water and it’s not being affected by the fog at all… I also attached a screenshot of the relevant part of my shader.

Thanks for any help!

4182478--370033--water_on.png 4182478--370036--ocean_shader.png

@bkristof The fog node here won’t help you. When you’re using any lit master node, the fog is already implemented.
But beeing able to get the volumetric density from a node is a interesting demand, and we’re adding this to our (long) to-do list.
No ETA tough, sorry.

1 Like