Unity Mesh Renderer won't be completely transparent

拈花ヽ惹草 提交于 2019-12-01 14:17:59

Assuming your code is fine, the problem very likely is from your Material settings. This changed in Unity 5. To change the alpha of a Mesh Renderer, you must also change the Material's Rendering Mode from Opaque(default) to Fade.

Transparent Mode is also fine but will will not be completely transparent and will result to the problem in your question.

You can change the mode from script.

Property Name: _Mode

With Debug.Log(MyRenderer.material.GetFloat("_Mode"));, I got the follwing values:

0 = Opaque
1 = Cutout
2 = Fade
3 = Transparent

We can change the Rendering Mode to Fade with MyRenderer.material.SetFloat("_Mode",2);

There is a known problem when setting the Render Mode from script. You must also update all other properties as well to make the change take effect. Here is a complete way to change your Render Mode to Fade from script:

MyRenderer.material.SetFloat("_Mode", 2);
MyRenderer.material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
MyRenderer.material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
MyRenderer.material.SetInt("_ZWrite", 0);
MyRenderer.material.DisableKeyword("_ALPHATEST_ON");
MyRenderer.material.EnableKeyword("_ALPHABLEND_ON");
MyRenderer.material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
MyRenderer.material.renderQueue = 3000;

Finally, if the this is working for you then your script is not good. You can use the script below to fade in and fade out a Mesh Renderer.

public MeshRenderer MyRenderer;
bool fading = false;

void Fade(bool fadeIn, float duration)
{
    if (fading)
    {
        return;
    }
    fading = true;

    changeModeToFade();
    StartCoroutine(FadeTo(fadeIn, duration));
}

void changeModeToFade()
{
    MyRenderer.material.SetFloat("_Mode", 2);
    MyRenderer.material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    MyRenderer.material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    MyRenderer.material.SetInt("_ZWrite", 0);
    MyRenderer.material.DisableKeyword("_ALPHATEST_ON");
    MyRenderer.material.EnableKeyword("_ALPHABLEND_ON");
    MyRenderer.material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    MyRenderer.material.renderQueue = 3000;
}

IEnumerator FadeTo(bool fadeIn, float duration)
{
    //MyRenderer.material.
    float counter = 0f;

    //Set Values depending on if fadeIn or fadeOut
    float a, b;
    if (fadeIn)
    {
        a = 0;
        b = 1;
    }
    else
    {
        a = 1;
        b = 0;
    }


    //Enable MyRenderer component
    if (!MyRenderer.enabled)
        MyRenderer.enabled = true;

    //Get original Mesh Color
    Color meshColor = MyRenderer.material.color;


    //Do the actual fading
    while (counter < duration)
    {
        counter += Time.deltaTime;
        float alpha = Mathf.Lerp(a, b, counter / duration);
        Debug.Log(alpha);


        MyRenderer.material.color = new Color(meshColor.r, meshColor.g, meshColor.b, alpha);
        yield return null;
    }

    if (!fadeIn)
    {
        //Disable Mesh Renderer
        MyRenderer.enabled = false;
    }
    fading = false; //So that we can call this function next time
}

void Start()
{
    //Fade(true, 3f); //Fade In
    Fade(false, 3f);//Fade Out
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!