Understanding distance fields

When I visualize Distance Field Ambient Occlusion in the editor, I can see the correct shape of objects.


However, when I try to do something similar with a material using DistanceToNearestSurface, the shapes look strange. Also I would expect to see a gradient and not only black and white.

It feels like I’m not understanding distance fields correctly. Can anyone point me into the right direction?

‘Distance to nearest surface’ is going to return the distance in centimeters. Since you are clamping the value between 0 and 1, it makes sense that you’d basically see no soft transition between black and white :slight_smile:

Scale the value by multiplying it with some small number (like 0.001) to get a softer transition.

Thanks. I see the transition now.

In the pictures above, a static mesh plane is intersected by a sphere, cube and cone (all with a transparent material). The DistanceToNearestSurface-Material covers the static mesh plane. So, for the lerp alpha should be ~0 at the intersection areas such that they are painted white. But it’s just the other way around. Why is that? How can I modify my material, such that the intersection areas are precisely colored?

Sorry, not sure what you mean by that description. Could you be more specific or post pictures?

The initial problem I had is described here: https://answers.unrealengine.com/questions/350101/is-it-possible-to-draw-objects-which-are-sliced-by.html

With “regular” materials, you can see how the static mesh plane intersects the sphere, cube and cone:

In my case, the sphere, cube and cone are transparent/invisible. I depict this with a grid:


I would like to paint the area where the static mesh plane intersects the sphere, cube and cone, like this:

I believe that this can be achieved by covering the static mesh plane with my DistanceToNearestSurface-material if I configure it in the correct way.
I.e. following the logic:

IF inside sphere/cube/cone
  THEN color = red
  ELSE color = white

It should be possible to determine “inside” using DistanceToNearestSurface, right?

I hope this is clearer now. Otherwise please tell me and I will try to clarify more.

In theory this is possible, but the mesh distance fields will produce round/blobby results because the resolution is much lower than the mesh itself.

The visualization of the DFAO seems reasonbly accurate to me. Or do you have any idea how I could solve my problem otherwise?

Have you figured this out?

Unfortunately no.

Try upping the distance field resolution by a HUGE amount on the meshes?

FWIW, scaling up your meshes will make the intersection cleaner, but as you can see, it’s still imprecise near the angles.

Here the cone is using a scale of (10, 10, 10).