- Home /
Change Material's Alpha
Hello,
I am trying to change a material's alpha on a game object via code. I want the alpha to fade in and out base on distance. I am using Unity 5, and I don't think this code works anymore.
alphaValue = Mathf.Lerp(renderer.material.color.a, 0F,
Time.deltaTime * fadeLerpConstant);
Any suggestions on how to achieve this?
Thanks,
John L
Answer by YoungDeveloper · Apr 27, 2015 at 06:37 PM
Hi, i am currently testing this and trying out things too. First of all alpha on materials set to opacity wont work, you have to set it transparent. Setting materials to transparent by default from optimization perspective would not be a good option. So to change that via code:
MeshRenderer renderer = gameObject.GetComponent<MeshRenderer>();
Material material = renderer.material;
material.SetFloat("_Mode", 4f);
//0f - opacity
//1f - cutout
//2f - fade
//3f - transparent
But this wont work out of the box (i wish it would). In one of the unity forum posts i saw that you have to set rest of the properties.
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
I am not shader expert, so if someone could elaborate what exactly these do and are of them even needed i would be thankful.
What comes to color itself, i got it working this way. I set these before rest of the properties, right after switching the mode.
Color32 col = renderer.material.GetColor("_Color");
col.a = 100;
renderer.material.SetColor("_Color", col);
Thanks!
That worked. Is there away to lerp or somehow do a fade-in or out that can be added?
Thanks again,
John L
After switching the "_$$anonymous$$ode" use coroutine or update to lerp. This is correct way to use lerp, otherwise it will be moving towards the point, but not to point.
private float alpha = 0;
private float lerp = 0;
private byte a;
private void Update(){
alpha = $$anonymous$$athf.Lerp(0f, 255f,lerp);
a = (byte)alpha;
//use this a
lerp += Time.deltaTime * 0.1f;
if(lerp >= 1f){
lerp = 0f;
}
}
Found extremely helpful article, thanks to Elwin.
I'd still suggest using floats than ints for mat.SetFloat("_$$anonymous$$ode", 3); otherwise it will have to force cast every time.
Edit: He sets render queue negative for opaque, but the docs say " Render queue number should be positive to work properly.", maybe it does count for this mode. Will do research on that.
I used this code from above to lerp fade an object, and it works in editor, but not on mobile, any ideas why?
I'm years late to the party but if memory serves these "_Color" names are names of uniform variables that certain shaders may or may not have. It's entirely possible that some shaders don't have these variables so this code is a bit hit or miss.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
Changing material of objects by the tag in different scenes 0 Answers