Dynamic Shadow Problems when using Distance Field Shader Functions

I have a lot of grass foliage in my game but since it is an RTS game the grass has to be removeable during runtime when the player starts building stuff on top.

Just removing the instanced static mesh components works but has really big impacts on performance (I have a very large landscape) so I wanted to just do it within the shader.

It is actually really easy to do using mesh distance field inputs in the grass shader but now my dynamic shadows (which I definitly need) don’t work properly.

The first (and I think best approach) is using the “DistanceToNearestSurface” Node. The problem however is even though the grass gets removed in an adjustable radius, dynamic shadow rendering won’t work at all.

The second approach ist to use the “DistanceFieldGradient” Node. i can still get the grass removed and I still get shadows rendered but shadows of the foliage that have been removed are also rendered. So I now get these dark shadowed areas even so nothing is there to cast those shadows which looks even weirder than on the screenshots.

This image shows a before and after for each method and the shader functions used. The output of this function goes straight into the opacity mask slot. This is a normal foliage shader with Material Domain set to surface, Blend Mode to Masked and Shading Model to Two Sided Foliage.

If you know a better solution to achieve this effect I would gladly use this instead. I just need to be able to remove foliage under and slightly around buildings during runtime.

I haven’t been able to solve this yet but I think I’ve narrowed it down at least a little bit.

My first approach using the DistanceToNearestSurface node with no shadows at all seems to not have shadows because during the shadow pass this node does not work. However, when I use the “Shadow Pass Switch” I can just have the shadow pass use a predefined value like 1. This at least made it possible to have shadows with both approaches:

This makes it possible to have shadows again but it now behaves like my second approach which means I get shadows from grass that is invisible.

I’ve also tried to instead of masking the grass with an opacity mask to push it under the map with world position offset. This at least transfers this into the vertex shader but it doesn’t make a difference for this at all.

Maybe it is possible the somehow feed data from one pass to another without recalculating or something hacky like that? I feel like the solution is pretty close but I am still out of ideas how to fix this.

Ever find a solution to this issue? thx

You can use runtime virtual textures combined with vertex offset (as the initial poster sort of tried)

This video explains it well: Easy Grass Occlusion in UE4 - YouTube