+4
Under review

Unity 5: Issues lightmapping with shaders containing Opacity Clip

Br00d 9 years ago updated by behealed 7 years ago 27
Hey there,

I have some images that make it pretty clear what is happening but I'm trying to get my universal shader to work with lightmapping. I had to take it apart piece by piece before I found that it was the opacity clip. The darker shapes are rendered with the standard Unity 5 shader, the lighter with a brand new SF shader.
Below are the lightmaps rendered:

Image 379

Image 380

Image 381

Image 382

I checked out the post here: http://shaderforge.userecho.com/topic/372545-alpha-clip-lightmap-generate-incorrect-shadows/ and followed the steps but got no where. This seems broken to me but it could just be Unity 5.
Under review
Odd - it should work. I'll have to look into this. Have you tried modifying the blending settings?
I can't edit the blend settings because being differed locks the source drop downs.
Does Unity's default shaders support this?
Because I can't find why this wouldn't work unless it's a Unity bug
Hey I ended up trying the cutout standard shader and it doesnt work. It is a unity bug. However fade and transparent do work, if you enabled opacity in differed it could offer a workaround until it's fixed? Auto sort is all locked too.. Anyways thank you.
Temporary, possible, fix!
1. Change all shaders to Forward.
2. Reassign all OpacityClip to Opacity.
3. Bake lightMaps.
4. Reassign all Opacty back to OpacityClip
5. Change all shaders back to Differed.
Bam! Lightmaps for clipped Differed objects.
I have upgraded to 5.1.1 and SFv1.16 but I'm still having issues with this.
Try removing the _cutoff property at the top of the shader code
Yeah that is another way around the problem. What if I want to make a shader with:
_DiffuseColor ("Diffuse Color", Color) = (1,1,1,1)
_DiffuseRGBA ("Diffuse (RGBA)", 2D) = "white" {}
_ColorRamp ("Color Ramp", 2D) = "white" {}
_CRampOverrideWhitefornooverride ("CRamp Override (White for no override)", Color) = (0.5,0.5,0.5,1)
_SpecularColor ("Specular Color", Color) = (1,1,1,1)
_SpecularRGBGlossA ("Specular (RGB Gloss A)", 2D) = "white" {}
_SpecularStrength ("Specular Strength", Range(0, 1)) = 1
_GlossStrength ("Gloss Strength", Range(0, 1)) = 1
_EmissiveColor ("Emissive Color", Color) = (1,1,1,1)
_EmissiveRGBFresnelA ("Emissive (RGB Fresnel A)", 2D) = "white" {}
_EmissiveStrength ("Emissive Strength", Range(0, 3)) = 0
_FresnelColor ("Fresnel Color", Color) = (1,1,1,1)
_FresnelStrength ("Fresnel Strength", Range(0, 3)) = 1
_FresnelThickness ("Fresnel Thickness", Range(0, 5)) = 1
_Normal ("Normal", 2D) = "bump" {}
_ReflectionCubemap ("Reflection Cubemap", Cube) = "_Skybox" {}
_ReflectionMask ("Reflection Mask", 2D) = "white" {}
_ReflectionStrength ("Reflection Strength", Range(0, 1)) = 0.2
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5

Is this just not possible?
I want to use this shader on meshes of grass with geopaint.
Differed, no cutoff... If I can't use alpha blending in differed rendering, how else can I get grass that looks like this but gives me a shadow map? With cutoff, the realtime shadowmap rendering gets a clean shadow but cant use it for the lightmap.. What is going on here? :(

You can't use alpha blending in deferred
Sorry, I meant opacity clip.
The issue is that the lightmapper won't bake shaders with cutoff. They will however bake with alpha blend. I do believe this to be a Unity issue.
I made a post on the Unity forums, http://forum.unity3d.com/threads/unity-5-lightmapping-with-shaderforges-opacity-clip-cutoff.335694/

I will keep this thread as up to date as possible in the event I learn anything new. Thanks Joachim!
Update:
Today I decided to start a new project and import some objects to a new test scene. This is Unity 5.1.1p1 running stock on the specular setup standard shader using cutout and a deferred rendering path. Is this evidence that Unity is ok in this instance?




Yeah. Again, I think this has to do with the _cutoff slider. Does it work properly if you remove it from the shader code, in the SF shader?
After making that change to the shader, lightmapping begins to work, but then I lose the opacity clip in the lightmap. That's what I was saying in my post with the first image of the grass. Here's what it looks like in the new project.




In other words, no. The light map is rendered however it is then not affected by the opacity clip as in the image seen above.

Have you found anything out about this? I'm still coming up empty handed...

I think you'll have to compare it to the code in Unity's shader in this case, to see what's different. Not sure what to do otherwise

First image is with no baked lights; Top and bottom cubes are lit using a standard shader and the middle two are brand new PBR shaders built with SF v1.25.

Baked


I'll prepare an example scene for you if you wish?

I finally got a ticket to my dev for this issue. He's stumped as well. So stumped that he learned how to write a shader and made one today. Then he combined the Unity standard cutout shader, a newly created PBR shader with opacity clip and his hand written shader in to a scene on a surface that is a checker board pattern of the standard shader and the shaderforge shader..

The results of baking the scene clearly show an issue in how the shader generated with shader forge is rendering the lightmapped texture. This is coming from the fact that both his newly generated shader and the Unity standard shader are displaying the correct information after being baked. However the SF PBR is severely broken. If it helps you, we even messed with the lightmap texture files and found that if we delete everything on the left side of the directional EXR file, the lightmap from the SF shader begins to be displayed correctly, but still contains errors. I have a unity package that can be extracted in to a new project so that you can see it for yourself. Inside the package is a readme with further information.


That package is available on dropbox here


Please take a look at that and let me know what you think. Below is a viewport preview of the scene.

I'll look into this first thing when I get back to updating SF. (Considering getting someone to help maintain SF, as I'm getting more and more busy with another project at the moment :( )

So I just discovered that SF doesn't support Directional Specular LMs. I suppose that my issue is both because of dir spec LMs and an issue apparent with opacity clip. I just also found this similar issue. http://shaderforge.userecho.com/topics/919-cutout-do-not-receive-self-shadows/


Is there anything that we can do to help you? Can we get in to contact to see whether or not we can figure this out together? At this point in our project, going backwards would be very expensive for the company and at the same time SF really needs this functionality.

The directional LM issue is likely a lot more involved than the opacity clip one. Opacity clip is likely a one-line fix, while the directional LM requires a lot of restructuring. The reason for this is that you need to run the BRDF twice, which the SF compiler is definitely not designed for.

Sorry for bumping an old thread but, I just got whammo'd by this issue. None of my alpha clipped shaders are making any shadows if I bake my scene. But if I attach unity's standard shader to my material, it gives several options. #1: it has a drop-down option to select "baked". And #2: it has another drop-down option to select "alpha clipped". If I select both those options, then bingo, I got baked shadows and everything appears to be properly alpha clipped. So it appears that this IS possible to do. Hopefully someone smarter than me can research this and post the code here, and maybe the dev could get it worked in if we hand him the code on a silver platter. :P

Hopefully is an understatement. We're about to launch Mekazoo and baking our terrain shadows would really get us the performance boost we need on consoles. Honestly we would pay for an expansion to the code base if it allowed us to guarantee a constant 30fps experience.

For now, my solution is I'm going to have to switch my objects to the standard shader, set the standard shader to "bake" and "alpha clip" mode (as I described above), and then bake the scene, and then switch the objects back to my SF shaders. I'm going to write a script to do this automatically for me. I don't know if this solution would work for you, but you might want to consider it.