Global distance field sample in Custom node problems

So there is this really simple setup to just read the distance field and write it to Emissive colour.



No custom node


What I would like to do is acces this same information inside of a custom node.

float3 GlobalUV = ComputeGlobalUV(WorldPosition, 0);
float DistanceToSurface = 0;
DistanceToSurface = Texture3DSampleLevel(GlobalDistanceFieldTexture0, SharedGlobalDistanceFieldSampler0, GlobalUV, 0).x;
return DistanceToSurface;

This works, but only partially.

The results show up in preview viewports, and in the global distance field, and mesh distance field visualisation modes, but not in the main viewport.


Default Viewmode

SDF Visualisation mode

If anyone has any ideas, that would be super appreciated.

Hi Wyvery,

Try to put a float instead of a Sampler in your second input. Maybe that’s helps.

Texture3DSampleLevel(GlobalDistanceFieldTexture0, (float instead of sampler), GlobalUV, 0).x;

Here is link for this function.

2 Likes

Hey yunus, thank you for your reply!

That’s a very good suggestion, I hadn’t thought to try it.
Unfortunately this also doesn’t work.

First of all the link you sent is for Texture3D::SampleLevel, not Texture3DSampleLevel.
Or in other words, the function shouldn’t become
Texture3DSampleLevel(GlobalDistanceFieldTexture0, (float instead of sampler), GlobalUV, 0).x;
but
GlobalDistanceFieldTexture0.SampleLevel(SharedGlobalDistanceFieldSampler0, GlobalUV, 0).x;
Minor problem, mostly just saying this for people coming here from google searching for a similar problem
GlobalDistanceFieldTexture0 is my texture3D object here.

I believe Texture3DSampleLevel(Texture3D, samplerState, float, float [,int]) might just be a function in unreal to wrap the function you showed.

This code compiles, but the same issue still persists.



I tried a few other things while I’m at it.
When using the DistanceToNearestSurface node, this code gets generated.

So with the custom node this would be replaced by my custom expression

Where I call the same function

I ran this through a code compare tool and the lines I’m showing are the only differences between the two
shaders. Except for that, the two shaders are identical.

Yet the issue still persists

:sob::sob::sob:

Hey Wyvery,

you are welcome!

I see…

i guess the “Get Distance To Nearest Surface Global” is included by any function library.
You will find the “include” files and the path in the upper part of your hlsl code.
Mostly this include file collecting a bunch of functions also macros etc or other methods.

I hope this will help.

It’s part of GlobalDistanceFieldShared.ush
I opened this file inside of vs and copied over the code from there originally, that’s where the ComputeGlobalUV and Texture3DSampleLevel Functions came from.

1 Like

Hey @Niels sorry to resurrect an old thread but were you able to get this figured out? I came across this thread while trying to write a custom node that I could use for ray marching while sampling the scene. I’m really new to using the custom node and not sure where to find documentation for the available functions.

Can’t remember, this is a long time ago.
But more than likely the solution in one of the recent engine versions is to either add GlobalDistanceFieldShared.ush as an include on the custom node, or add a dummy node that has that file as it’s dependency so unreal includes it.

1 Like

Thank you this was just the hint I needed. I had to look around in the engine files and learn about FLWCvectors and how to convert between them and regular floats but I finally got it to work.
Just wish there was better documentation for these things…

1 Like