Download

Using Mesh Distance Fields to obtain the distance to the closest surface from the player location

I have noticed that Unreal has the neat capability of creating global distance fields, which can be used for computing shadows, ambient occlusion etc. Is it possible to access this functionality from the gameplay side? In my use case, I would like to be able to compute the distance to the closest object - not to the center of the object, but more like the closest point on the object surface, with a threshold around it, exactly what a signed distance field represents. Can I exploit features like GetDistanceToNearestSurfaceGlobal from the gameplay side of things, instead of just materials/lighting as it is typically used for?

It’s not 1 bit reliable but you could read the value by setting up a render target and having the same material function applied to it.

so youd have 2 material instance dynamic in the BP. One is the one screen, actually doing, the other is just constantly writing out to a render target.
you can then read the render target.

This may work OK if say you have an area of 50m and you map it to 50pixel.
you can then estimate that the first colored pixel around you is the surface and you therefore know a somewhat correct distance with minimal render target reading - which is slow by nature.

If anyone has a way to actually leverage anything else I’d be interested as well.

the distance to nearest surface needs to be calculated on a single Z “axis” against the landscape or any other meshes one can walk on that intersect with that Z axis.
a manual calculation of the immediate area around the player is therefore very heavy in cost.

Check every object in level for x/y/z compared to player, make a line trace to the object, find hit result for distance to the actual normal.
Even with only 360 (one per deg) ray traces around you it’s still crazy expensive.
oh, and the reason for 360 is that the landscape needs to be kept into account as well you can’t just assume its not there or read where it’s at in any other way that I have though of yet.

Slightly less expensive is just collision checking - I made a large cylinder of about 50m that sits around the player and on-overlap I detect the item and keep track of distance. Not a good approach either tbh.

Do a ray-cast and get the impact point.

You won’t get the distance from the player to the surface with a distance field anyway. That value is the distance from that texel to the next nearest surface. It’ll inevitably be something other than the player.