Why is my distance field that inaccurate?

I don’t get a clear result when I use DistanceToNearestSurface to detect the edges around my little lake. It will be used to spawn foam around objects. Additionally I get this weird effect when I move my plane up- or downwards. Any Idea where this is coming from/ how to fix this?

338421-distance-field-bug.gif

1 Like

Think of the distance field as a 3D texture and you’ll see that those dark grey blocks are essentially the pixels. The distance sampled by the ‘Distance to Nearest Surface’ node is the distance from the centre of each volumetric grid cell.

If you go into World Settings in the main editor window, you can increase the resolution of the distance field by reducing the value of ‘Global DistanceField View Distance’. The default is quite big. I went down to 1,000 without any noticeable hit on performance, although that will depend on the complexity of the scene.

1 Like

I believe the signed distance field is only calculated within a certain distance of the camera, so once you’re too far away there isn’t anything for the ‘Distance to Nearest Surface’ to sample. Perhaps it defaults to a value (1,1,1) (white) if it doesn’t find anything, but I’m speculating a bit.

I wonder if this distance is the other setting in the Rendering section, i.e. ‘Default Max DistanceField Occlusion Distance’. Try increasing that.

This will of course make the volumetric texture bigger and so use more memory (and also more time to calculate as it will need to search for objects within that radius and add up their individual mesh distance fields, which is how the global distance field is created).

1 Like

Thank you, now are the pixels smaller, but when I zoom a bit out, the material gets completely white. Any solution?

1 Like

It works now, but I’m not really sure why xD. Mainly I found out that the mesh was measuring the distance to itself, that was the reason for the weird flickering. I turned off “Affect Distance Field Lighting” on my plane and now that’s fixed. For the white effect, I changed my material to the one described on the unreal page. Thanks for your help.

“There are probably other, albeit less convenient, ways to do that.”
I didn’t find a way for doing that. The Calculation of the depth of the water doesn’t work because I have no Scene Depth. (Single Layer Water Shading Model, Opaque)

“By the way, I’ve been meaning to ask - is that a poo?”
Could technically be possible…

3 Likes

I already tried playing around with the Settings, ‘Default Max DistanceField Occlusion Distance’ does’nt do anything. I put a screenshot in, where it shows the distance, from where it gets white, and for me it’s absolutely unusable. I tried putting it into another project, but nothing changed. I watched a few Tutorials on this, but it seems I’m the only one where this is behaving like this.

I’d appreciate any help!

1 Like

Oh how aggravating. That looks an awful lot like the corner of the cube around the camera that contains the SDF. I can’t find anything that suggests how to change that distance, so it might be a hard-coded value in the engine.

What’s the intended effect? To get a faded border around objects on that plane? There are probably other, albeit less convenient, ways to do that.

By the way, I’ve been meaning to ask - is that a poo?

1 Like

Ah, excellent. I’m glad you cracked it!

1 Like

I am getting the same issue with the big white plane seems to do that when the camera moves away

1 Like

Hey there! Any info on distance fiels for 5.0+? Looks like it’s kinda broken now.

Same test with 4.27.2 and 5.0.3

4.27.2

5.0.3

The same material applied on a plane:

In the 5.0 version, when you move away those lines are getting away but it looks so bad on close range. I want to use it to create shoreline but it makes everything jaggy and glitchy.
Tried to turn off lumen but nothing changed (that was my only guess on a potential issue).

1 Like

A more obvious test: comparing Distance To nearest surface node between 4.27 and 5.0:

4.27

5.0

Like what’s going on??

Graph:

2 Likes

A quick remark I figured out: if you turn off Lumen in the post process volume you can adjust the “Global DistanceField View Distance” in the World settings but once Lumen is turned on this parameter will have no influence anymore. So it looks like it’s link to some Lumen parts, but I can’t find a way to fix it with Lumen parameters…Weird nobody seen that yet, since it’s there in 5.0 (so for more than a year?).

6 Likes

Keeping this thread alive with something. This Stair Stepping Distance Field effect still happens. My example here is just adding a scalar to the distance field. This is also in UEFN. Where there seems to be even less control over this issue.
Any Suggestions other then what has already been mentioned would be helpful.

1 Like

New information and a potential solution. But first a summary just with some fresh examples of what has been stated earlier.

For reference here’s what a basic material referencing the DistanceToNearestSurface looked like in UE 4.7. You can likely tell I’m working towards a raked gravel type effect, this is just the debug for setting that up.

Now compare that to how the exact same setup looks in UE 5.3:

These look different enough that you could be forgiven in assuming that I’m just doing something wrong. That’s what I assumed too. In fact the whole reason I made the 4.7 project was that I could swear I had something like this working 10 years ago.

So before we talk solutions it’s worth sharing why it is that 5.3 looks this way. The answer is that Unreal 5+ introduces the idea of a Global Distance Field. And DistanceToNearestSurface uses that. And with default project settings the Global Distance Field is incredibly crude.

The first image is my Mesh Distance Field, you can see it’s fairly precise. The second image is my Global Distance Field. It is sparse enough that’s it’s functionally useless in a shader like this. The Global Distance Field takes the in the raw data from the Mesh Distance Field and makes a much more optimized version of it.

Changing the project settings for how detailed your mesh distance fields are will not change this, as the Global DF will still down-sample it. Similarly changing resolution settings at an individual mesh level will not change the Global DF either.

If you need to keep your project in Unreal 5.3 etc there’s only one way I’ve found to adjust this: you must disable lumen and then and only then can you change the Global DistanceField View Distance from its default of 20,000 to something more precise.

The earlier results were at the 20k default, here are the results at 10k, 5k, and 2k respectively:



Of course this creates issues where as you get lower and lower you may now start to have clipping problems. Because that View Distance is also the maximum distance at which your SDF renders:

GlobalSDFClipping

If like me you’re using that for a shader then your shader will be receiving a value of 0 on anything outside the range. So if your project doesn’t need Lumen and if you can find a value range that gives you a good tradeoff in precision vs clipping then that’s the fix.

But here’s the good news, and why I bother to resurrect this thread:
It seems like things were fixed in UE 5.6!
Here is the same content in 5.6:

Importantly this is with Lumen ON, and the Global DistanceField View Distance set to 20,000 (default), it’s a new default project with no settings changed.

While it’s entirely possible that there is some setting in 5.3 that can make it behave like 4.7 or 5.6 I was not able to find it. The Global View Distance obviously had major potential downsides, namely no Lumen and potential clipping.

The only issue I’ve found is that if your mesh is exactly at World 0 height, you may see some flashing artifacts. I’d guess it’s a precision/rounding issue but idk

GlobalSDFArtifacts2

These artifacts go away if you raise your mesh by 1cm or so (0.01 units). In my case that’s easy enough to deal with, but obviously we can hope that is also addressed in future releases.

2 Likes

I suppose UEFN is screwed until further notice

2 Likes