Unity 通過代碼簡單實現文理的灰化顯示
阿新 • • 發佈:2017-11-13
http gre date ria public xtu src 線上 bsp
1.可以用於紋理的處理,也可用於模型顯示的處理(比如某件準備或者服飾未獲取的時候,灰化顯示)
線上對比圖:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TextureGrey : MonoBehaviour { [SerializeField] UITexture tex = null; Texture2D tex2D = null; Vector3 grey = new Vector3(0.299f, 0.587f, 0.114f); //灰化Shader中的灰度值,具體含義目前不是太清楚 void Start() { tex2D = tex.mainTexture as Texture2D; } void Update() { if (Input.GetKeyUp(KeyCode.A)) { //灰化 Vector3 tempV3 = Vector3.zero; if (null != tex2D) { Color[] cols = tex2D.GetPixels(); for (int i = 0, iMax = cols.Length; i < iMax; ++i) { tempV3.Set(cols[i].r, cols[i].g, cols[i].b); float dot = Vector3.Dot(tempV3, grey); cols[i] = new Color(dot, dot, dot, cols[i].a); } tex2D = new Texture2D(tex2D.width, tex2D.height, TextureFormat.ARGB32, false); tex2D.SetPixels(cols); tex2D.Apply(false); //實際上傳改變後的像素數據到顯卡上 tex.mainTexture = tex2D as Texture; } } } }
Unity 通過代碼簡單實現文理的灰化顯示