Bootstrap

Unity代码设置"Standard (Specular setup)"的渲染模式(RenderingMode)


需要在程序代码中设置材质"Standard (Specular setup)"的渲染模式,则编写如下代码:

    public enum RenderingMode 

    {
        Opaque,
        Cutout,
        Fade,
        Transparent,
    }

    public static void SetMaterialRenderingMode (Material material, RenderingMode renderingMode)
    {
        switch (renderingMode) {
        case RenderingMode.Opaque:
            material.SetInt ("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            material.SetInt ("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
            material.SetInt ("_ZWrite", 1);
            material.DisableKeyword ("_ALPHATEST_ON");
            material.DisableKeyword ("_ALPHABLEND_ON");
            material.DisableKeyword ("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = -1;
            break;
        case RenderingMode.Cutout:
            material.SetInt ("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            material.SetInt ("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
            material.SetInt ("_ZWrite", 1);
            material.EnableKeyword ("_ALPHATEST_ON");
            material.DisableKeyword ("_ALPHABLEND_ON");
            material.DisableKeyword ("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = 2450;
            break;
        case RenderingMode.Fade:
            material.SetInt ("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            material.SetInt ("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            material.SetInt ("_ZWrite", 0);
            material.DisableKeyword ("_ALPHATEST_ON");
            material.EnableKeyword ("_ALPHABLEND_ON");
            material.DisableKeyword ("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = 3000;
            break;
        case RenderingMode.Transparent:
            material.SetInt ("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            material.SetInt ("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            material.SetInt ("_ZWrite", 0);
            material.DisableKeyword ("_ALPHATEST_ON");
            material.DisableKeyword ("_ALPHABLEND_ON");
            material.EnableKeyword ("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = 3000;
            break;
        }

    }

执行代码:

Material currentMaterial = new Material(Shader.Find("Standard (Specular setup)"));

SetMaterialRenderingMode (currentMaterial, RenderingMode.Cutout);


悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;