Home > Mobile >  How to change Material Rendering Mode to Fade by script?
How to change Material Rendering Mode to Fade by script?

Time:05-20

The material rendering mode is now set by default to opaque. I want to change it to fade.

So far i did :

if (g.GetComponent<SkinnedMeshRenderer>() != null)
                {
                    Material[] mats = g.GetComponent<SkinnedMeshRenderer>().materials;
                }

The material is at index 0 there is only one material but i'm not sure how to access the rendering mode of the material and how to change it to fade.

Material

What i tried so far :

I created a new public static class :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class MaterialUtils
{
    public enum BlendMode
    {
        Opaque,
        Cutout,
        Fade,
        Transparent
    }

    public static void SetupBlendMode(Material material, BlendMode blendMode)
    {
        switch (blendMode)
        {
            case BlendMode.Transparent:
                material.SetOverrideTag("RenderType", "Transparent");
                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 = (int)UnityEngine.Rendering.RenderQueue.Transparent;
                material.SetFloat("_Mode", 3.0f);
                break;
            case BlendMode.Opaque:
                material.SetOverrideTag("RenderType", "");
                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;
                material.SetFloat("_Mode", 0.0f);
                break;
            default:
                Debug.LogWarning("Warning: BlendMode: "   blendMode   " is not yet implemented!");
                break;
        }
    }
}

Then back to the editor script :

Inside OnGUI :

if (GUILayout.Button("Start"))
        {
            allChildren = new List<Transform>();

            foreach (Transform g in transform.GetComponentsInChildren<Transform>())
            {
                var level = ObjectLevel(g);
                names.Add(" Level "   level.ToString());

                if (g.GetComponent<SkinnedMeshRenderer>() != null)
                {
                    Material[] mats = g.GetComponent<SkinnedMeshRenderer>().sharedMaterials;
                    Material mat = mats[0];
                    MaterialUtils.SetupBlendMode(mat, isOpaque ? MaterialUtils.BlendMode.Fade : MaterialUtils.BlendMode.Fade);
                    mats[0] = mat;
                    allChildren.Add(g);
                }
            }
        }

but i'm getting warning in the editor :

Warning: BlendMode: Fade is not yet implemented!

On the line :

MaterialUtils.SetupBlendMode(mat, isOpaque ? MaterialUtils.BlendMode.Fade : MaterialUtils.BlendMode.Opaque);

The rendering mode is still opaque.

CodePudding user response:

Simply set float of _Mode property:

renderer.material.SetFloat("_Mode", 1)

E.g code:

private MeshRenderer renderer;
void Start() => renderer = GetComponent<MeshRenderer>();
void Update()
{
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        renderer.material.SetFloat("_Mode", 0); // Opaque
    }
    if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        renderer.material.SetFloat("_Mode", 1); // Cutout
    }
    if (Input.GetKeyDown(KeyCode.Alpha3))
    {
        renderer.material.SetFloat("_Mode", 2); // Fade
    }
    if (Input.GetKeyDown(KeyCode.Alpha4))
    {
        renderer.material.SetFloat("_Mode", 3); // Transparent
    }
}
  • Related