首页 > 其他分享 >Unity动态修改材质球RenderingMode属性

Unity动态修改材质球RenderingMode属性

时间:2022-10-21 14:23:33浏览次数:56  
标签:UnityEngine DisableKeyword RenderingMode material SetInt Rendering Unity 材质

Material四个模式

 

 动态修改代码

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

public class CanvasPos : MonoBehaviour
{
    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;
        }
    }
}

调用代码如下

 private Material toumingsb1, toumingsb2;

void Start(){ 
Material[] tou = hit.transform.GetComponent<MeshRenderer>().materials;
                    toumingsb1 = tou[0];

                    toumingsb2 = tou[1];
                    CanvasPos.SetMaterialRenderingMode(toumingsb1, CanvasPos.RenderingMode.Transparent);
                    CanvasPos.SetMaterialRenderingMode(toumingsb2, CanvasPos.RenderingMode.Fade);
}

 

本次就这么多了,需要会在补充的,感谢阅读

 

标签:UnityEngine,DisableKeyword,RenderingMode,material,SetInt,Rendering,Unity,材质
From: https://www.cnblogs.com/qq2351194611/p/16813270.html

相关文章