- Home /
ARCore – not changing alpha on material after changing BlendMode through code.
Anyone got any ideas why in ARCore I’m not seeing a smooth fade of the material after I change the material blend mode? I’ve tested the following code and in the editor/Game mode it’s working fine. Also the fade routine is working fine in ARCore if I set the material blend mode to “standard>fade” (or transparent) in the Material Inspector, then the routine works fine and I get a nice fade in ARCore and the editor.
However, I need to switch modes on the fly as I have multiple items to fade out and I can’t get Unity to play well with them (z-sorting issues). So the best logic I can find is to change the blend mode whilst fading in/out, then set to “Standard > Opaque” once done. BUT…. This does not appear to be working in ARCore. I see the modes are changing, as you can see z-buffering sorting issues come and go as it switches between modes, but the fading is no longer happening.
Any ideas?
Thanks in advance for any help
void Start()
{
myRenderer = GetComponent<Renderer>();
solidColor = myRenderer.material.color;
fadedColor = new Color(solidColor.r, solidColor.g, solidColor.b, 0f);
// myRenderer.material.renderQueue = 4001;
Debug.Log(myRenderer.material.renderQueue);
material = myRenderer.material;
}
public void startFading()
{
// go = true;
Debug.Log("startFading - bb-working");
if (!fading)
{
StartCoroutine(Fade());
}
}
IEnumerator Fade()
{
material.SetFloat("_Mode", 2);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
Debug.Log("coruntine started for skin");
fading = true;
Color fromColor = faded ? fadedColor : solidColor;
Color toColor = faded ? solidColor : fadedColor;
for (var t = 0f; t < fadeTime; t += Time.deltaTime)
{
Debug.Log("fading skin");
//currentColor = Color.Lerp(fromColor, toColor, t / fadeTime);
myRenderer.material.color = Color.Lerp(fromColor, toColor, t / fadeTime);
yield return null;
}
if (toColor == solidColor)
{
material.SetFloat("_Mode", 0);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
}
fading = false;
faded = !faded;
}
Answer by Richard3575 · Oct 19, 2018 at 02:06 PM
Right, Solved it! So you have to read the fine print. “Material of that type in your Assets. The material must be used in a scene” https://docs.unity3d.com/Manual/MaterialsAccessingViaScript.html (part 2 at the bottom)
So the issue was that it ran in the editor fine, but when built it stripped out all unused materials and I did not have a material using the Transparent shade in, and therefore it didn’t work. All I did was include a object with a “standard > transparent” material on in the scene and it works!!!
Your answer
Follow this Question
Related Questions
How to Add Render Queue property in Inspector to LightWeight Shader ? 1 Answer
How to edit a shader to make it show sprites it's on without lighting? 0 Answers
Seamless Water Material on a Plane using Shader Graph 3 Answers
My mesh didn’t appear in Unity 0 Answers
Sequential shaders. Pass the output color of a shader to the next one. 0 Answers