Lerp frm opacity does't reveal material
i have an issue with an gameobject with material fade out & fade in
i have a script on each asset with a function that returns the array of materials need to change. i use an auxiliary:
public List<Material> TransparentMaterials;
public float timeToDisappear = 2.0f;
void ChangeMaterialtoTransparent (Material mat) {
mat.SetFloat("_Mode", 3f);
mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
mat.EnableKeyword("_ALPHABLEND_ON");
mat.renderQueue = 3000;
}
void ChangeMaterialtoFade (Material mat) {
mat.SetFloat("_Mode", 2);
mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
mat.SetInt("_ZWrite", 0);
mat.DisableKeyword("_ALPHATEST_ON");
mat.EnableKeyword("_ALPHABLEND_ON");
mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
mat.renderQueue = 3000;
}
void ChangeMaterialToOpaque(Material mat)
{
mat.SetFloat("_Mode", 0);
mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
mat.SetInt("_ZWrite", 1);
mat.DisableKeyword("_ALPHATEST_ON");
mat.DisableKeyword("_ALPHABLEND_ON");
mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
mat.renderQueue = -1;
}
when time for the material to fade comes, i clame its materials and use
void FadeOverTime( Material[] mats)
{
foreach (Material mat in mats)
{
if(!_transparentMaterialsNames.Contains(mat.name.Substring(0,5)))
ChangeMaterialtoFade (mat);
StartCoroutine(TurnInvisible(mats));
}
}
IEnumerator TurnInvisible(Material[] mats)
{
float startTime = Time.time;
while (Time.time < startTime + timeToDisappear)
{
foreach (Material m in mats)
{
float tmp = Mathf.Lerp(1.0f,0.0f,(Time.time - startTime)/timeToDisappear);
FadeColorTolevel(m,tmp);
}
yield return null;
}
}
and all is good.
however when trying to lerp it back
IEnumerator ShowAndWait(Material[] mats,int index)
{
yield return StartCoroutine(Show(mats, index));
foreach (Material mat in mats) {
if(!_transparentMaterialsNames.Contains(mat.name.Substring(0,5)));
ChangeMaterialToOpaque (mat);
}
}
IEnumerator Show(Material[] mats, int index)
{
float startTime = Time.time;
InvisibilityField VF = _visibilityComponents [index];
while (Time.time < startTime + timeToDisappear)
{
foreach (Material m in mats)
{
float target = VF.GetOpacityLevel(m);
float tmp = Mathf.Lerp(0.0f,target,(Time.time - startTime)/timeToDisappear);
FadeColorTolevel(m,tmp);
print (m.name + "opacity is " + tmp + " as for " + (Time.time - startTime)/timeToDisappear);
}
yield return null;
}
}
void FadeColorTolevel(Material mat, float lvl)
{
Color Col = mat.color;
Col.a = lvl;
mat.color = Col;
}
only reveals some materials (its 1 model with multimaterial) wile other remain invisible
the print result of the print function clames that ALL materials have been lerped to 0.995.
the effect is that when the line ChangeMaterialToOpaque (mat);
kickes in, some materials are fully visible while other just blink back into visibility as they have skipped all the lerp process..
the fade in function is almost identical to the fade out function that works so i don't know what am i doing wrong.
i use Unity 5.2.0f3.