unity+改变材质颜色与不透明度


源文件:

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

public class ModelTransparencyHelper : MonoBehaviour
{
    /*
    /// <summary>
    /// 从不透明到透明的过渡时间
    /// </summary>
    public float TransitonTime=0.3f;*/

    /// <summary>
    /// 透明的程度
    /// </summary>
    public float TransparenceValue = 0.01f;

    /// <summary>
    /// 原材质球组的主颜色值
    /// </summary>
    private List<Color> m_colors=new List<Color>();

    /// <summary>
    /// 原材质球组的引用
    /// </summary>
    private List<Material> m_Materials=new List<Material>();
    
    void Start () {
        try
        {
            if (m_Materials==null|| m_colors == null)
            {
                m_Materials=new List<Material>();
                m_colors=new List<Color>();
            }

            if (GetComponent<MeshRenderer>() != null)
            {
                m_Materials = GetComponent<MeshRenderer>().materials.ToList();
                for (int i = 0; i < m_Materials.Count; i++)
                {
                    m_colors.Add(m_Materials[i].color);
                }
            }
            //测试使用
            ToTransparence();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
		
	}

    /// <summary>
    /// 从不透明到透明
    /// </summary>
    /// <returns></returns>
    public bool ToTransparence()
    {
        try
        {
            if (m_Materials.Count<1)
            {
                return false;
            }

            for (int i = 0; i < m_Materials.Count; i++)
            {
                m_Materials[i].SetFloat("_Mode", 3);
                m_Materials[i].SetInt("_SrcBlend",(int)UnityEngine.Rendering.BlendMode.One);
                m_Materials[i].SetInt("_DstBlend",(int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                m_Materials[i].SetInt("_ZWrite",0);
                m_Materials[i].DisableKeyword("_ALPHATEST_ON");
                m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
                m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
                m_Materials[i].renderQueue = 500;
                m_Materials[i].color = new Color(m_colors[i].r,m_colors[i].g,m_colors[i].b,TransparenceValue);
            }
            print("try ToTransparence!");
            return true;

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    /// <summary>
    /// 在物体半透明时控制物体透明度
    /// </summary>
    public bool ControlTran(float t)
    {
        try
        {
            if (m_Materials.Count < 1)
            {
                return false;
            }

            if (t > 0.05 && t < 0.1)
            {

            }
            else
            {
                for (int i = 0; i < m_Materials.Count; i++)
                {
                    m_Materials[i].SetFloat("_Mode", 3);
                    m_Materials[i].SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    m_Materials[i].SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    m_Materials[i].SetInt("_ZWrite", 0);
                    m_Materials[i].DisableKeyword("_ALPHATEST_ON");
                    m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
                    m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
                    m_Materials[i].renderQueue = 500;
                    m_Materials[i].color = new Color(m_colors[i].r, m_colors[i].g, m_colors[i].b, t);
                }
            }
           // print("try ToTransparence!");
            return true;

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        
    }

    /// <summary>
    /// 从透明到不透明
    /// </summary>
    /// <returns></returns>
    public bool ToOpaque()
    {
        try
        {
            if (m_Materials.Count < 1)
            {
                return false;
            }

            for (int i = 0; i < m_Materials.Count; i++)
            {
                m_Materials[i].SetFloat("_Mode", 0);

                m_Materials[i].SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                m_Materials[i].SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                m_Materials[i].SetInt("_ZWrite", 1);
                m_Materials[i].DisableKeyword("_ALPHATEST_ON");
                m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
                m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
                m_Materials[i].renderQueue = -1;

                m_Materials[i].color = new Color(m_colors[i].r, m_colors[i].g, m_colors[i].b, m_colors[i].a);
                
            }
            print("try ToOpaque!");
            return true;

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
    private void Update()
    {
        //if (Input.GetMouseButtonDown(0))
        //{
        //    ToTransparence();
        //}
    }
}

  修改(要求一开始透明):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;
public class ARModeTransparency : MonoBehaviour {
    public List<Material> m_Materials = new List<Material>();
    public List<Color> m_colors = new List<Color>();
    // Use this for initialization
    void Start ()
    {
        try
        {
            if (m_Materials == null || m_colors == null)
            {
                m_Materials = new List<Material>();
                m_colors = new List<Color>();
            }

            if (GetComponent<MeshRenderer>() != null)
            {
                m_Materials = GetComponent<MeshRenderer>().materials.ToList();
                for (int i = 0; i < m_Materials.Count; i++)
                {
                    m_colors.Add(m_Materials[i].color);
                }
            }
            InitStateMode();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
    private void InitStateMode()
    {
        for (int i = 0; i < m_Materials.Count; i++)
        {
            m_Materials[i].SetFloat("_Mode",1);

            m_Materials[i].SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            m_Materials[i].SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            m_Materials[i].SetInt("_ZWrite", 0);
            m_Materials[i].DisableKeyword("_ALPHATEST_ON");
            m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
            m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
            m_Materials[i].renderQueue = 500;//渲染层级
            m_Materials[i].color = new Color(m_colors[i].r, m_colors[i].g, m_colors[i].b, 0);
            Debug.LogError("000  " + m_Materials[i].color.a);
        }
    }
    public void controllerTrans(float inputvalue)
    {
        if (inputvalue >= 0 && inputvalue <= 0.15)
        {
            for (int i = 0; i < m_Materials.Count; i++)
            {
                m_Materials[i].SetFloat("_Mode",1);
                m_Materials[i].SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                m_Materials[i].SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                m_Materials[i].SetInt("_ZWrite", 0);
                m_Materials[i].DisableKeyword("_ALPHATEST_ON");
                m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
                m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
                m_Materials[i].renderQueue = 500;
                m_Materials[i].color = new Color(m_colors[i].r, m_colors[i].g, m_colors[i].b, inputvalue);
                Debug.LogError("000  " + m_Materials[i].color.a);
            }
        }
        else
        {

            for (int i = 0; i < m_Materials.Count; i++)
            {
                m_Materials[i].SetFloat("_Mode", 3);
                m_Materials[i].SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                m_Materials[i].SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                m_Materials[i].SetInt("_ZWrite", 0);
                m_Materials[i].DisableKeyword("_ALPHATEST_ON");
                m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
                m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
                m_Materials[i].renderQueue = 3000;
                m_Materials[i].color = new Color(m_colors[i].r, m_colors[i].g, m_colors[i].b, inputvalue);
                Debug.LogError("000  " + m_Materials[i].color.a);
            }
        }
    }
	// Update is called once per frame
	void Update () {
		
	}
}

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM