DistanceToNearestSurface not maintaining consistent edges around intersecting objects

I’m running into a weird issue with the DistanceToNearestSurface node not behaving how I would expect it to. I am trying to create foam that goes along the border of the water plane as it intersects objects in the scene. Basically, I’m trying to make this

I changed the colors to make it easier to see the problem

As you can see, the “foam” is inconsistent as it goes along objects.

Here is my material setup

As far as I can tell, I am using the node how it should be used… but the results are less than ideal as you can see.

Any idea what’s going on? Is there a way to make the node more accurate?

Edit: The solution is adjusting the Global DistanceField View Distance under the World Settings. I made a gumroad with the water material I was trying to make, check it out here.

Check your distance fields in show> visualize> mesh distance fields, they cannot accurately represent small details. Increasing distance field resolution scale in your mesh might help a bit.

DistanceToNearestSurface is working on Vertices not Pixels I guess.
To me it looks like it.

So here’s a few shots further breaking down the problem

I have tried changing the resolution scale from 1, to 0.1, to 10, and to 100, no difference at all. (My scene objects are scaled to the 3rd person blueprint scale)

Here’s a gif of what is happening


It seems to be limited in its precision. However it is possible to use it for foam with high enough range, but with some issues. Dividing distance by 100 should get rid of inconsistent transitions. Also thin or small meshes will not produce good results. Gif

The resolution of the global DF falls off quickly with distance. It looks to me like the last comparison image is using a distance clipmap at lower resolution. Does it get slightly more detailed when you move the camera closer? If so that is expected.

The rounding around the corner issue shown in the OP looks to be something that could be improved by increasing the Mesh Distance Field Resolution scale for that asset in the static mesh settings.

It looks to me that you are non-uniformly scaling the mesh, which distance fields can’t support properly. Is that the case?

I found the solution for this, it was something silly of course, but under the World Settings, under Rendering, I needed to adjust the Global DistanceField Distance. The lower it is, the more accurate it is. I’ll make a post soon with the shader setup and all required information, for the next person who tries to solve it.

DanielW = Uniformity of a mesh scale doesn’t impact the outline effect all that much, at least once I solved the issue. If your scale is within 2x in one direction, it still looks acceptable.

I have figured out the problem, I figured I’d update this thread with the solution. Under the World Settings, under the rendering dropdown, there is a Global DistanceField View Distance setting. Adjusting this value fixes all of the wonkyness I was experiencing.

I have also created a gumroad with a download for the water material I was trying to make! Check it out here.

Distance fields are not really designed for precision things. They are to be used for rough approximations at best. If you want precision you can use a depthfade. However depthfade can only be used with translucent materials and they too have limitations.