Announcement

Collapse
No announcement yet.

DistanceToNearestSurface material node

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    DistanceToNearestSurface material node

    Could anyone explain how to use the DistanceToNearestSurface material node in 4.9? I've tried to just use it as the base color in a material, applied this material to multiple meshes and expected to see the meshes change color once I move them closer or further away from each other, but that does not really seem to be the case. Even with no other meshes around a mesh there are black areas on the mesh which randomly get bigger or smaller when moving around the object.

    I have checked the mesh distance fields with visualizing the distance fields, they are perfect, since I only use cubes.

    So I would really like to know how to correctly use this node
    Easy to use UMG Mini Map on the UE4 Marketplace.
    Forum thread: https://forums.unrealengine.com/show...-Plug-and-Play

    #2
    you probably need to make sure the current mesh isn't also generating distance fields to avoid sampling its own distance field.
    Ryan Brucks
    Principal Technical Artist, Epic Games

    Comment


      #3
      Well thats a problem then since all meshes need to have a distance field... But I have also tried it with a landscape, which does not generate a distance field, and it did not work there too. No matter how far I move meshes near the landscape, the color does not really change.
      Easy to use UMG Mini Map on the UE4 Marketplace.
      Forum thread: https://forums.unrealengine.com/show...-Plug-and-Play

      Comment


        #4
        I think ryanB and danielW are aware that landscapes do not generate distance fields. It would be amazing to get that implemented. Will have to see how epic want to use their resources. But they have my vote for that feature.
        Visual Effects Artist, Weta Digital, Wellington New Zealand
        BLOG www.danielelliott.co.uk
        @danielelliott3d https://twitter.com/danielelliott3d
        Unreal Engine and VFX Tutorials https://www.youtube.com/user/DokipenTechTutorials
        2015 Showreel: https://vimeo.com/116917817

        Comment


          #5
          The point was that Ryan said I have to use a mesh which does NOT generate a distance field to use this node in the material from the mesh. So a landscape would be perfect since it does not generate a distance field.
          Easy to use UMG Mini Map on the UE4 Marketplace.
          Forum thread: https://forums.unrealengine.com/show...-Plug-and-Play

          Comment


            #6
            The reason you dont want the shader reading distance fields to generate a distance field is that it will always return its own distance field value thus it will never really give valid information about other surfaces. You will basically see the interference pattern between the mesh and the low resolution distance fields. As you move the mesh you might see it flash black as it happens to line up with the sampling resolution etc. This is just a fundamental limitation of this method. In most cases it isn't a problem though.... what exactly are you trying to do?

            You *could* try to work around it by sampling at an offset position along the vertex normals. Basically take worldposition + (vertexnormal * offset) and that will let you sample a bit away from the actual mesh. As long as the intersection with the neighboring surface continues along in a straight-ish line a decent distance away, the result should be somewhat valid. But if there was an intersection that curved away from the object fairly close, it might be missed etc.

            FWIW, that is the same basic method you would use to trace a rain mask if you wanted rain splashes automatically on the ground, but have things like awnings block the rain. You would simply "get distance to nearest surface" a few times each with a greater Z offset and if any result gets within a certain threshold you mask the rain.
            Last edited by RyanB; 07-30-2015, 01:20 PM.
            Ryan Brucks
            Principal Technical Artist, Epic Games

            Comment


              #7
              Thanks!

              It should work fine with a landscape, right? So if I apply a material on the landscape which uses the DistanceToNearestSurface as the base color I should see the landscape turning more dark when moving meshes near it which generate distance fields, right?
              Easy to use UMG Mini Map on the UE4 Marketplace.
              Forum thread: https://forums.unrealengine.com/show...-Plug-and-Play

              Comment


                #8
                Yes. I just tested and its working for me. Is it possible you aren't dividing the distance by enough to make it visible?

                The quickest way to check if its working is to make "distance to nearest surface" goto emissive, then view in unlit and throw a giant cube on the landscape. Select the cube and find the property "render in main pass" and set it to FALSE. That way you can be sure you are seeing the black area inside the cube. Normally when you hide a mesh it will also hide the distance field so it can be tricky to debug sometimes. And of course if you dont see anything, visualize the mesh distance fields to make sure your cube shows up at all there.
                Ryan Brucks
                Principal Technical Artist, Epic Games

                Comment


                  #9
                  Most of this has already been said but just for completeness:

                  DistanceToNearestSurface only works if you have 'Generate Mesh Distance Fields' project setting enabled (you have to restart editor after changing), and you are on DX11 hardware. This means Geforce 4xx and above, Radeon 6xxx and above.

                  DistanceToNearestSurface will always return around 0 if sampled on a shadow casting opaque surface, by definition. It's the distance to the nearest opaque surface. However only static mesh components + foliage + instanced SMC's actually have distance field reprsentations, so on something like Landscape you should get a non-zero value. On other meshes you have to disable either CastShadows or bAffectDistanceFieldLighting in order to sample the distance values of other meshes.

                  Going forward we can have landscape affect the global distance field, let me know if that would be useful.

                  Comment


                    #10
                    Thanks for your answers, I still can't get it to work

                    If the cube is "going through" the landscape, I actually see the different color in the landscape.

                    Click image for larger version

Name:	b8b41684c0.png
Views:	1
Size:	16.7 KB
ID:	1082910

                    If it's just "touching" the landscape perfectly, I still see the black on the landscape:

                    Click image for larger version

Name:	640fb0422c.png
Views:	1
Size:	21.1 KB
ID:	1082911

                    But once it is slightly above the landscape, the landscape again is pure white.

                    The mesh distance fields are good:

                    "Local" Distance field:

                    Click image for larger version

Name:	1fc67169b0.png
Views:	1
Size:	50.4 KB
ID:	1082912

                    Global Distance Field:

                    Click image for larger version

Name:	248ac79f6d.png
Views:	1
Size:	69.4 KB
ID:	1082913



                    Another thing I noticed is, if I disable "cast shadows" on the cube, the landscape turns fully black. I tested a bit more and noticed that I need to have at least one shadow casting mesh in the world, otherwise the landscape is fully black. I have "cast static shadows" disabled in the project settings.

                    I have also tried to divide the output from the node and to power(0.2) or power(20) it, but it really just outputs a perfect white (0/0/0) even if the cube is just 0.01 cm above the landscape
                    Last edited by John Alcatraz; 07-30-2015, 04:56 PM.
                    Easy to use UMG Mini Map on the UE4 Marketplace.
                    Forum thread: https://forums.unrealengine.com/show...-Plug-and-Play

                    Comment


                      #11
                      What did you divide by? Try dividing by 1000. It is expected that it turns white as soon as the cube is slightly above since the distance is in world units or centimeters. So only 1 cm above means full "white". The color white isn't really meaningful since its just clamping the output color to white... the values keep climbing.

                      fwiw if the cube is not touching but you want to treat it as if it was, you will need to apply a bias by adding a scalar parameter value to the result. But that will bias all surfaces equally.

                      Also, it may in some cases help to fly the camera far away and then back up close after making changes to distance field settings. I have seen some bugs where the global distance fields get corrupted momentarily. The hard edges in your 2nd image look suspect to me.
                      Last edited by RyanB; 07-30-2015, 06:27 PM.
                      Ryan Brucks
                      Principal Technical Artist, Epic Games

                      Comment


                        #12
                        Thanks a lot, dividing by 1000 helped. It's awesome
                        Easy to use UMG Mini Map on the UE4 Marketplace.
                        Forum thread: https://forums.unrealengine.com/show...-Plug-and-Play

                        Comment


                          #13
                          What I want to achieve is actually masking the grass node in the material, so make grass not spawn where other meshes are near, so it won't spawn in the floor of a building etc.

                          This is the cube in the ground:

                          Click image for larger version

Name:	13ee752b58.png
Views:	1
Size:	22.6 KB
ID:	1082926

                          I lerp with the mask based on the DistanceToNearestSurface node between red and blue, so the mask is really good (I would really like to see a "Gaussian Smoothing" material node, I would use it so often ).

                          Now I try to do this:

                          Click image for larger version

Name:	3240a759b0.png
Views:	1
Size:	42.0 KB
ID:	1082927


                          And it would actually work, but after testing with a manual alpha value I noticed the grass only updates after I hit "apply" in the material again and I can't change the alpha e.g. in a material instance, changing it after the material is "compiled" does not update the grass Would have been so awesome if it would have worked... :/
                          Easy to use UMG Mini Map on the UE4 Marketplace.
                          Forum thread: https://forums.unrealengine.com/show...-Plug-and-Play

                          Comment


                            #14
                            Originally posted by DanielW View Post
                            Most of this has already been said but just for completeness:

                            DistanceToNearestSurface only works if you have 'Generate Mesh Distance Fields' project setting enabled (you have to restart editor after changing), and you are on DX11 hardware. This means Geforce 4xx and above, Radeon 6xxx and above.

                            DistanceToNearestSurface will always return around 0 if sampled on a shadow casting opaque surface, by definition. It's the distance to the nearest opaque surface. However only static mesh components + foliage + instanced SMC's actually have distance field reprsentations, so on something like Landscape you should get a non-zero value. On other meshes you have to disable either CastShadows or bAffectDistanceFieldLighting in order to sample the distance values of other meshes.

                            Going forward we can have landscape affect the global distance field, let me know if that would be useful.
                            That would be very useful indeed. For example, you could mask crashing waves on ocean planes and in reverse make effects for wet sand, erosion or what have you. Yes, pretty please with sugar and cream and jam and chocolate on top
                            escalation1985.com

                            Comment

                            Working...
                            X