>> Welcome back to another episode of
Unreal Tips and Tricks.
This time, we'll look at using Distance Fields in Unreal.
While Distance Fields have many uses,
including soft shadows, real-time ambient occlusion,
and other special effects,
we'll be focusing on their use to generate real-time
dynamic soft shadows in this episode.
Keep in mind however, it's best to avoid using them
for a single advantage, such as soft shadows,
but rather, to make use of all the other options
that Distance Fields provide.
In Unreal, you can generate a 3D Texture at edit time
for Rigid Static Mesh Actors.
This Texture contains a collection of distances to
the nearest surface of the Mesh.
Positive values for points on the exterior of the Mesh
and negative values for points inside the Mesh.
This data is used to create very soft
dynamic area shadows at runtime at relatively little cost.
The performance savings can be anywhere from 25
to 50 percent faster than standard shadow projection
and Cascade Shadow Maps.
Bear in mind that Distance Field Shadows do have some limitations
and performance issues
that should be taken into consideration.
We'll discuss a few of them toward the end of this episode.
Choosing to use them should be a project-wide consideration,
as they will impact the workflow of your artists and designers.
To make use of Distance Fields,
we first need to enable them in our project.
Let's open the Project Settings window.
On the left side, we can scroll down under Engine
and select Rendering.
From there, we need to go to the Lighting section.
Here we'll find Generate Mesh Distance Fields.
This will need to be enabled.
Once we've enabled it, we're promoted to restart the Engine.
Before we do that though, let's look at some of the
optimization options here.
First up is the Eight Bit Mesh Distance Fields option.
This will convert Distance Fields from 16-bit to 8-bit,
reducing memory use considerably.
But it can introduce artifacts when you have large
or thin Meshes, so keep that in mind.
The next option is to Compress Distance Fields.
This, again, will save memory, but without losing quality.
However, if you're doing Level Streaming at all,
this can cause loading hitches,
as they need to be decompressed at load time of the Level.
When the project restarts, Distance Fields will need to be
built for all of the Meshes in your project.
This can take a considerable amount of time,
depending on the size of your project,
so keep that in mind.
Now that the Distance Fields have all been built,
we can visualize them in the viewport.
We'll click on the Show button, then go down to Visualize,
and then select Mesh Distance Fields.
Let's take note of a few things here.
First of all, you'll want your Meshes to be mostly gray
and not mostly white.
White means that more ray traces were needed to calculate
the Distance Fields.
Secondly, you can see that some objects, such as the bench here,
are not very detailed.
To use Distance Fields for this object, we're going to need to
increase their resolution.
But first, we need to enable any lights that we want to
use Distance Field Shadows,
rather than the usual shadow projection technique.
On the Lights Detail Panel,
first we need to set the light to movable,
making it a Dynamic Light.
Now we'll scroll down to the Distance Field Shadows section
and we'll enable Ray Traced Distance Field Shadows.
We can see that the bench now is projecting a pretty rough
low res shadow.
We simply need to adjust the resolution
by selecting the bench,
then right-clicking and selecting to edit it,
opening it in the Static Mesh Editor window.
In the viewport, on the top left, we can see that we have
a Distance Field Texture here
and these three values are the resolution of that Texture.
It's a Cubic Texture and it takes up about
0.05 MB of video card memory or VRAM.
Take note that the maximum Texture size and RAM usage of
a Distance Field Texture
is going to be 128 cubed, or 128x128x128
and 8 MB respectively.
To increase the resolution of our Texture,
we need to go to LOD 0 on the Details Panel.
From there, we'll open the Build Settings
and then towards the bottom of the Build Settings,
locate Distance Field Resolution Scale.
Currently, it's at a value of 1.0.
Let's go ahead and raise it up to a value of 5
and hit apply changes.
Looking in the viewport, we can see the Distance Field Texture
is now quite a bit larger, nearly the maximum resolution,
and it's taking up 4.28 MB of VRAM,
or about half of what it's allowed.
If you don't want a particular Mesh to generate Distance Fields,
we can set the resolution to zero to completely disable it.
Now we can go back to the viewport,
and you can see the shadow here is much higher res,
much higher quality, and we get a nice,
soft area lighting effect.
As Distance Field Shadows are fully dynamic,
you can see that as I move the chair and the light around,
the shadow will update in real-time
and you get a very nice effect.
Although Distance Field Shadows are dynamic,
they will not work on Animated or Skeletal Mesh Actors.
This includes characters moving through the scene
as well as animated props, such as wind blown trees.
In the case of Skeletal Mesh Actors and other
Animated Static Mesh Actors, the light will not
generate a shadow for that Actor at all.
Distance Fields can be used to generate shadows from
Billboard Mesh Faces, such as those used in trees and bushes.
However, in order for them to look more full,
we need to go back into the Static Mesh Editor window
for these types of Actors.
Then we need to enable Two Sided Distance Field Generation,
which can be found below the Resolution Scale.
Alright, so using Distance Fields
you're going to be able to achieve a great number of
real-time dynamic effects in your project,
such as we looked at today with Dynamic Soft Shadows.
Thank you for watching and we'll see you on the next
Unreal Tips and Tricks.