- Home /
Transparency not working in Windows build
I'm having some trouble with a mesh that is rendering with alpha fine in my viewport not being rendered with alpha when I run it in my Windows build.
I have a skinned character that I'm using for an enemy that is using the standard shader for rendering. When it is killed, I play a fall over animation and then want it to slowly fade out. I'm doing this by changing the shader's color's alpha each Update():
if (!init)
{
init = true;
//Switch from opaque to transparent
Material m = meshRend.materials[0];
m.SetFloat("_Mode", 2);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0);
m.DisableKeyword("_ALPHATEST_ON");
m.EnableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000;
return false;
}
elapsedTime += Time.deltaTime;
float val = Mathf.Max(1 - (elapsedTime / waitTime), 0);
meshRend.material.color = new Color(1, 1, 1, val);
This works fine when I'm running in the Unity editor window, but when I'm running my windows build, the alpha fade doesn't happen (although the Z test does appear to be disabled once my init section is run).
I read in another thread that this might be due to Unity not compiling shaders with options that are not be used. I tried including the Standard shader in the list of shaders in Project Settings/Graphics/Always Included Shaders (as per one suggestion), but that just caused Unity to freeze during the build. I also tried creating a standard shader material that rendered with transparency, set it's alpha to .5 and assigned it to a dummy cube in my scene (to force Unity to render using a Standard shader with alpha). However, while the cube did render with transparency, my character is still not fading out.
Answer by kitfox-com · Sep 06, 2017 at 12:54 AM
I figured it out. FIrst I created a new Shader Variant Collecton object in my project, then I edited it to add an entry for a standard shader with a ForwardBase DIRECTIONAL_ALPHA_ON. Then I editied my Project Settings/Graphics/ Shader Preloading to include a reference to my Shader Variant Collection in the Preloaded Shaders array.
The Unity docs go into more details:
https://docs.unity3d.com/Manual/OptimizingShaderLoadTime.html
Hi, I'm having a similar issue and read your solution with interest. Problem is, I couldn't figure out how to add the DIRECTIONAL_ALPHA_ON keyword. I can add the Standard shader to the variant collection, but when I click in the inspector to add a keyword, it won't list the one I need. How did you manage it? Is there any place where I can enter the keyword manually? Thanks!
I had this exact problem too and the shader variant popup window where you select keywords is absolutely awful. You are unable to scroll down the long list of keywords or even type something in. I did however find a solution to manually put in the "DIRECTIONAL_ALPHA_ON".
So in the shader variant window I selected the only one I could see, DIRECTIONAL_ALPHATEST_ON. This isn't the one we need but we can override it. Then I clicked on the shader variant collection asset in the unity project window and opened the file location. There opened the .shadervariants file with my preferred text editor.
Here I saw the "DIRECTIONAL_ALPHATEST_ON" that I entered in the shader variant window and I replaced that with the "DIRECTIONAL_ALPHA_ON."
This solved my problem but Unity should really really update their shader variant window since this was pretty ridiculous.
Wow, it's been a long time since I've worked with Unity. But I finally saw your response, and I was able to edit the file without incident. Sadly, the transparency issue persists. Perhaps I need to explicitly add the shader variant to the material?
I tried creating a new SVC and Add Standard-ForwadBase DIRECTIONAL_ALPHA_ON with my text editor, but it still doesn't work. The Project Setting window can not track the standard variant either, that's really weird and confusing. Where can I learn more about when to use "DIRECTIONAL_ALPHA_ON"?
Answer by Morph12345 · Dec 03, 2017 at 09:41 PM
Hi, I have the same issue (i want to make an object transparent when the player is hidden, like a roof when the player is in the house).
I use the same code as you from "Material m = ..." to "...queue = 3000".
I tried to create a new Shader Variant Collection, I had theses keyword :
ForwardBase DIRECTIONAL_ALPHATEST_ON
ForwardBase DIRECTIONAL_ALPHABEND_ON
ForwardBase DIRECTIONAL_ALPHAPREMULTIPLY_ON
And then I save it and added it in the graphic settings as the doc says.
But the problem is still there.
Did you remenber if you did anything else ? or maybe how to try to debug this.
Thanks :)
Just guessing here, but I'd suggest removing those extra DIRECTIONAL_ALPHA* words you added to your Shader Variant Collection and just add a single DIRECTIONAL_ALPHA_ON. You could also just turn the active flag off on an object if you just want the roof to vanish.
Answer by foxes · Nov 28, 2018 at 11:35 PM
In version 2017.4.16 it also does not work in the Editor.
Your answer
Follow this Question
Related Questions
Alpha as Illumination AND Transparency? 0 Answers
Render transparent material without double occlusion 2 Answers
Add transparency to this shader? 0 Answers
Sorting issues within single transparent mesh 1 Answer
Decal Alpha Blend 0 Answers