- Home /
Material that can fade from opaque to transparent?
How can i make a shader that supports both opaque color and transparent color? I tried a lot of things such as:
Legacy shaders (makes it only transparent)
Transparent setting (makes it only transparent)
Fade setting (same as transparent
I'll respond in the morning if I get anything. It's really late here.
I'm not exactly sure what you want here. Transparency/alpha is separate from color value. So as long as the shader supports the alpha channel, you can make any color transparent or opaque depending on the alpha value you give it.
Answer by IsaiahKelly · May 17, 2015 at 07:13 AM
Any shader that supports the color alpha value can be either opaque or transparent. It just depends on the alpha value set. Here are two ways to fade a Material. The first method "AlphaFade()" just fades out the original color's alpha. While the "ColorFade()" method changes the entire color (including alpha), so you need to make sure the fade color's alpha is set to low or zero for a fade to happen with it.
Note: Material must support the alpha channel for this to work! e.g. Unity 5 standard shader must be set to Fade/Transparent. Legacy shader must be set to "Transparent".
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
[Range (0.1f, 1.0f)]
public float fadeSpeed = 1f; // How fast alpha value decreases.
public Color fadeColor = new Color (0, 0, 0, 0);
private Material m_Material; // Used to store material reference.
private Color m_Color; // Used to store color reference.
void Start ()
{
// Get reference to object's material.
m_Material = GetComponent <Renderer> ().material;
// Get material's starting color value.
m_Color = m_Material.color;
// Must use "StartCoroutine()" to execute
// methods with return type of "IEnumerator".
StartCoroutine (ColorFade ());
}
// This method fades only the alpha.
IEnumerator AlphaFade ()
{
// Alpha start value.
float alpha = 1.0f;
// Loop until aplha is below zero (completely invisalbe)
while (alpha > 0.0f)
{
// Reduce alpha by fadeSpeed amount.
alpha -= fadeSpeed * Time.deltaTime;
// Create a new color using original color RGB values combined
// with new alpha value. We have to do this because we can't
// change the alpha value of the original color directly.
m_Material.color = new Color (m_Color.r, m_Color.g, m_Color.b, alpha);
yield return null;
}
}
// This method fades from original color to "fadeColor"
IEnumerator ColorFade ()
{
// Lerp start value.
float change = 0.0f;
// Loop until lerp value is 1 (fully changed)
while (change < 1.0f)
{
// Reduce change value by fadeSpeed amount.
change += fadeSpeed * Time.deltaTime;
m_Material.color = Color.Lerp (m_Color, fadeColor, change);
yield return null;
}
}
}
But what shader type can be both solid and transparent?
Why is my fade shader still transparent when the alpha is set to 255?
You're title says "$$anonymous$$aterial that can fade from opaque to transparent?" and that's exactly what the code above does. I don't understand what you mean by "both solid and transparent". Are you trying to just make a portion of the material transparent?
Please let me know which version of Unity and shader you are using for this. Do you have any textures on it too?
The material isn't responding. I see what your code is, and it's pretty much the same as what I have. I am using standard shader, opaque and just plain grey.
Your answer
Follow this Question
Related Questions
Semitransparent GUI Buttons 3 Answers
How to make a GUI that isnt alpha transparent? 1 Answer
No transparency for EditorGUILayout.TextField 1 Answer
Transparent Textures 1 Answer
i can only see objects in game mode not edit mode, why? 0 Answers