Reputation: 358
I have an issue with an gameobject with material fade out & fade in in a Unity 3D Project.
I have a script on each asset with a function that returns the array of materials need to change. i use an auxiliary:
public float timeToDisappear = 2.0f;
void ChangeMaterialtoTransparent (Material mat) {...}
void ChangeMaterialtoFade (Material mat) {...}
void ChangeMaterialToOpaque(Material mat) {...}
Each fading object has a InvisibilityField field that returns the materials in its part. all InvisibilityField in an array.
when time for the material to fade comes, I claim its materials and use
void FadeOverTime( Material[] mats)
{
foreach (Material mat in mats)
{
ChangeMaterialtoFade (mat);
StartCoroutine(TurnInvisible(mats));
}
}
// code for TurnInvisible is at the end as its not the issue and it works fine
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) {
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 - sometimes 2 different parts with the same material 1 is revealed and 1 is not.
the print result of the print function clames that ALL materials have been lerped to 0.995.
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. // as a helper this is the invisibility code that works fine
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;
}
}
Upvotes: 2
Views: 1075
Reputation: 358
problem solved. the mess was caused because the materials were set to Fade rather than Transparent. I dont know what 's the diferent but it seemed to solve the issue
Upvotes: 1