- Home /
Fading Materials
What I am looking to do is to fade one material into another on a gameObject, over a period of time. What I currently have is an array of materials which change one by one, each one slighly more similar to the desired material last than the last. Is there a way to dynamically change the alpha on a material, so that they can be crossfaded? If this can't be done on materials then would this be possible with textures instead?
Answer by StephanK · Jul 04, 2010 at 12:15 PM
I don't think this is possible with materials. But depending on how your materials are layed out and what shaders you are using you might be able to achieve the effect you are looking for. These are some of the possibilities:
- change the main color. (works only if both materials are using the same texture/shader)
- write a shader that is able to crossfade two (or more) materials. There is a shader on the wiki called skybox blended which shows how to do that. However this will only work if both desired materials use the same shader. (actually its only one material with different textures)
- fading out using alpha will only work if you are using transparent shaders.
Answer by Deikkan · Jul 04, 2010 at 01:37 PM
OK, I've built a shader using the Skybox Blended code, and applied it to a material called "Blend". The code for the shader is below.
Shader "Texture Blend" {
Properties { _Tint ("Tint Color", Color) = (.5, .5, .5, .5) _Blend ("Blend", Range(0.0,1.0)) = 0.5 _Tex ("Start Texture", 2D) = "white" {} _Tex2 ("End Texture", 2D) = "white" {}
}
SubShader { Tags { "Queue" = "Background" } Cull Off ZWrite On ZTest Always Fog { Mode Off } Lighting Off
Color [_Tint] Pass { SetTexture [_Tex] { combine texture } SetTexture [_Tex2] { constantColor (0,0,0,[_Blend]) combine texture lerp(constant) previous } SetTexture [_Tex2] { combine previous +- primary, previous * primary } } }
I want to change the 'Blend' value in a script, and the site said to use:
skyboxmaterial.SetFloat("_Blend", yourBlend)
However, when I try to call it in the script using:
Blend.SetFloat("_Blend", examplefloat);
I get the error that Blend (the material) has not been identified. Any help?
nvm, worked it out.
Decided to use renderer.material.shader = Shader.Find("Texture Blend"); and declare it that way.
Answer by ellens · Jul 09, 2012 at 03:25 PM
You can crossfade them with the Lerp method of materials. Just input the two materials you want to crossfade between:
http://docs.unity3d.com/Documentation/ScriptReference/Material.Lerp.html
Answer by ellens · Sep 06, 2012 at 08:14 AM
I have written a script and a custom shader for this purpose. See files and code here: http://www.sundh.com/blog/2012/09/real-time-blend-2-textures-in-unity/
Hope it helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220613063813im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vertex Alpha value doesn't update? 1 Answer
Material doesn't have property '_Color' 4 Answers
Fading multiple objects 0 Answers
Gun Barrel To Get Hot - Change _Tint 1 Answer
Fade stick... 0 Answers