Shadows alpha clipping doesn't work in build

Can someone confirm if it is a bug. ( Submitted report if so IN-25757)

Alpha clipped meshes have bugged shadows in builds only on unity 2022.2. I tried changing all settings possible with no success. On other hand issue doesn’t appear in unity 2021.3.

You can also see flickering between correct clipped shadow and non clipped when moving camera around.

It works perfectly in the editor but doesn’t in the build. Replicated in empty project on different unity versions.

Unity Editor 2021.3 and Build

Unity Editor 2022.2 and Build

2 Likes

Just now I encountered the same problem. I am using Unity 2022.2.19 with HDRP 14.0.7.

In the editor the alpha clipped shadows (of an HDRP/Lit material) look correct, whereas in the built player they are unclipped:

Correct, alpha-clipped shadows in editor.
Wrong, unclipped shadows in player.

Has this bug been confirmed? How can I find it in the issue tracker? If needed, I could provide more details.

Yes it has been confirmed : Unity Issue Tracker - Shadows alpha clipping is ignored and shadows rendered without details in Player

I’m using latest 2023.1 and it doesn’t seem to occur anymore.

Also when I tested in 2022 I found out that LitTessellation instead of Lit can remove the bug, but that must be more costly as even if Tessellation is disabled extra calculations are done ( I think? ).

3 Likes

Indeed, it does not occur in 2023.1.0b16 for me too.

Though I hope that it will either get fixed in 2022.2 soon, or that there exists at least a workaround, because I cannot imagine that most HDRP projects can do without any alpha-clipped shadows.

2 Likes

Yip I get this too - 2022.2.12 + HDRP.

i see something similar before in URP.
in that case if the Light Atlas Resolution was too low it breaks clipping in the shadows

give it a try

A bit more info - probably not useful, but who knows:

We have 3 different tree types and some bushes in the scene that I’m testing.
As stated before, the shadows render correctly in editor view and in play mode in the editor (game view).

The interesting new piece of information that I have, is that in the build, it is only one of the tree types that cause the bad shadows. The other trees and the bushes are fine.

Also - the bad shadows (ignored clipping) are not consistent. It flickers to and from correct rendering based on slightly different camera positions.

Now regarding the “bad tree” - I have failed to find any differences between this tree and the others, regarding the shader, mesh renderer settings, texture import settings, etc. Although it is a different material with different textures, it is still the same shader used by all the other trees (a Lit shader graph).

1 Like

After a little experimentation, I found a workaround, though please check whether it also works for you:

My tree model that suffers from the bug is made of a single mesh, but it uses two different materials: One for the trunk vertices and another for the leaf vertices.

I have found that the bug seems to occur only if both materials are using the same shader, but exactly one of the two materials has alpha clipping enabled. So the workaround is to either assign different shaders to the two materials, or to enable alpha clipping on both materials.

My guess is that this also applies to situations where your model uses even more than two materials, but I do not know for sure.

By the way, this workaround also fixes the camera-position-dependent flickering you mentioned.

3 Likes

This does not just affect meshes that use two different materials, the leaves on this fern are just a single mesh with just the leaf material and the shadows still ignore the alpha clipping.

Is there seriously no fix planned for the 2022 LTS? That’s what the issue tracker says, so I guess anybody using the extremely niche feature of vegetation can scrap their project now if it in any way depends on 2022 features or suck up these crap shadows. My bad for assuming basic features like shadows would be supported in an LTS release, silly me.

Does anybody have more ideas for a workaround? I already tried increasing the shadow atlas size for punctual lights as far as it will go, to no effect.

I have no work-around currently - but it somehow only affects a particular tree out of the 10 or so tree/bush variations we have. Once I know what the difference is (if any), I might be able to post a work-around.

1 Like

Looks like transmission mask, could be something to look closer at.
when Unity first added transmission mask, we found the internal Unity default in unity core without any data for transmission mask sort of looked like that

suggest check the vertex tangents.

@Amplify_David @mgeorgedeveloper_1

Thanks for the help you two, I really appreciate it!

I tried using a transmission mask on the material, made no difference unfortunately. What did make a difference was separating the leaf mesh into its own fbx though, suddenly the alpha clipping works. I don’t understand why though, I haven’t changed the mesh in any way and used the exact same export settings in blender and the same import settings in unity as I did for the larger fbx which the leaves were a part of.

1 Like

Okay I did a stupid amount of testing and finally found the culprit. I missed one option on the model import settings, that being lightmap UV generation. Any model that has that turned on will not work with alpha clipping, even if no lightmaps are baked or the mesh renderer in question is set to receive GI from light probes.

My take on this is that there is a design oversight in the HDRP lit shader that uses the second UV channel in some way for alpha clipping, even if that channel is occupied by lightmap UVs. This is not even the first time I encountered something like this, very similar problem with detail normal maps on the fabric shader, but there you can manually fix it since you have access to the UV mask.

1 Like

HDRP uses two UVs
UV1 = static lightmaps unity_LightmapST
UV2 = dynamic lightmaps unity_DynamicLightmapST

Yes I’m aware of that, I wasn’t quite clear with the naming. I meant UV1 when I said second UV channel, UV0 being the first one. I’m not using realtime GI so I’m not sure if UV2 also has an effect on it.

is it looking good now? can we see it?

The left shadow is what it looks like, this screenshot is taken from the build.

I’m afraid it’s not as simple as I thought yesterday, the behavior is abhorrently inconsistent. I did manage to get my fern casting alpha clipped shadows in the build, and that stopped being the case after I enabled lightmap UV generation on the isolated fern model. So I thought that was the cause, but disabling lightmap generation on the model after that did not get them working again, it looks like that model is permanently broken after that. Even undoing the change in version control and re-exporting it from blender made no difference, and creating a new file with just the leaves only works sometimes. It seems to be essentially random whether an exported model will work with alpha clipping, the only guarantee is that it definitely will not work if there is more than a single mesh at the root of the model.

Does anyone have more ideas for a workaround? This is killing my project.

Lit Tessellation has tons of problems and is not scalable if you have lots of foliage, also does not support LOD animated crossfading at all which looks horrible.

I was able to find what was causing this in my project using 2022.3.4f1. Problem appears to occur with objects that have materials with alpha clipping and static batching on. A work around is to disable static batching on these objects, provided it doesn’t affect your project’s performance. I will try a 2023 version to see if this still happens.

1 Like