Download

Is DistanceToNearestSurface bugged?

I’m using DistanceToNearestSurface in a material to blend two materials based on distance to the landscape. But I get some weird artifacts, and I don’t understand why they occur.

I was expecting this value to behave the same as DFAO.
The cube put sideways get weird artifacts. And both cubes are apparently occluded on their surface(?)
The sphere also have some slight occlusion at the top, same bug?

In these images I output DistanceToNearestSurface directly to base color.

7b75feb2d4538d812f648d979512fb27e51cecef.jpeg
5ee227c8461ae95f956d31266e16ef4a0ab69e38.jpeg

Here is an example of the blending I’m trying to get.

I have the similar problem

Here is a video of the blending in action. I use WorldAlignBlend to mask it once the normal is less than 45 degrees.

Since the object occludes itself, it’s not a very practical approach.

You might need to add some bias to it(within the material editor), just like when dealing with DFAO meshing. In fact, now that I look at it, if it’s using the DF meshes, set your visualization mode to show the mesh distance fields. Look for any artifacts on the meshes BEFORE trying to add any sort of bias in on the material. If you see some, open up the static mesh editor=>details panel=>lod0=>build settings=>distance field resolution scale=>if it’s at 1, try 1.1=>click apply changes=>go back into the level viewport=>check the mesh distance field for artifacts. If it still has any artifacts, keep on slowly raising the resolution scale in small increments until it goes away. You want to keep that resolution scale as low as possibly. If the mesh is all good, then yeah, you’d have to do some biasing work in the material.

ALSO: It’s very important to remember that mesh distance fields do NOT like non-uniformly scaled models. Even if you simply take a cube mesh and flatten it out into a panel, it will hate you and give you all sorts of artifact/banding issues. So if you’re going to scale anything, try to only do it uniformly.

This works a ton better if the object itself does not generate distance fields.

maybe distance field resolution for that meshes is low?

The problem was that I didn’t know that DistanceToNearestSurface would also include the Distance Field of the object I’m on. So It worked after I turned off “affect distance field lighting” on that object.

It would be awesome to be able to call DistanceToNearestSurfaceExcludeSelf, but is that feature even possible?

I ended up creating this: Distance Field Edge Blending - Community Content, Tools and Tutorials - Unreal Engine Forums