Trying to write/bake DistanceField data to texture. DrawMaterialToRT doesn't support it.

Hey,

I was wondering if you guys know if it’s possible to read and write distance field data onto a render target, I tried the DrawMaterialToRenderTarget function but to my disappointment it seems to return 0 when using the DistanceToNearestSurface material node.

Basically I want to sample the distant field data (specific world positions) and bake it to a render target so I can do further pixel processing.
I will try doing it in C++ next but I’m not entirely sure how to get and pass the distance field textures to a custom pixel shader for sampling…

Can someone point me to the right direction? :slight_smile:

It is expected that DF won’t work with RT. The global DF volume texture is created in the world dynamically only around the camera. The RT exists in its own world so doesn’t have a camera in your world. I am sure you could fix this in C++ but probably only by making it more expensive or by making a complete second copy of it that uses a specified world center instead of the camera, which might be tricky. @DanielW would be the person to answer that part better.

Another option is to capture your own 2d height using scene capture set to scene depth, and use a manual shader pass to convert it into a distance field. there are some methods like Jump flooding that can work fairly quickly with a few iterations. But note it would be limited and you’d need multiple slices to make a real 3d field vs a psuedo ‘at the surface’ field. at the surface is basically what landscape does right now in DF to give you an idea.

Thanks :slight_smile:

I’m aware that it generates the distance fields around the camera, that’s the data I probably mostly need to sample from anyways :).

Is it possible in theory to pass the same data to a custom pixel shader without a perfomance cost? I possibly need to do this every frame so I’m not looking into re-generating the DF or anything, just sample what is already available for the scene.

I have a scene capture 2d version that somewhat ‘works’ but of course it’s too expensive :stuck_out_tongue: it would be far more useful to be able to access the dynamic DF data at runtime.

There is probably some way to do what you want but I am not aware of it.

Here is one potential hack you could try (assuming you need it on a plane).

  1. Place a geo plane in the level where you want to capture the data.
  2. place scenecapture2d, but set it to have “Show Only Actors” with just the plane. Should be minimal cost to render the plane.
  3. Place the DF sampling mat on your plane.
  4. After telling the scene to update, check the contents of your RT and see if it captured the DF or not. I haven’t tried this but it might work.

Hi, . I have the same problem recently. I’m now using the method you suggested in this post . It worked almost well, except that when my blue print moves far away from zero point of the scene, the scene capture2d will not capture distance field data anymore, while the DF mateiral on the plane shows the totally correct result.

Which means the capture2d cannot capture the plane’s data after some distance.

After some research, I found it was effected by the " Global DistanceField View Distance " in the world settings pannel. Whenever I move outside this ‘distance’ the capture2d won’t work.

I guess it was cause by the ‘camera position’ when the capture happens. UE4 think my camera is fixed at location (0,0,0) ,although my capture camera has already moved a long distance away.

Is there any way to fix this issue? I really need to use distance field data in render target.