Unity中利用材質自發光實現物體閃爍效果,供大家參考,具體內容如下
補充:這種方法有一點問題,在測試(Windows平臺)的時候發現,要想在Build出來的游戲中實現閃爍效果,就必須在 Project 窗口中將源材質的自發光屬性(Emission)啟用,否則自發光效果就只能在編輯器模式中生效。
啟用源材質的自發光效果后,將其亮度(Brightness)調整為0,物體看起來就和沒有啟用自發光時一樣。
看到別的游戲里有物體高亮閃爍效果,但自己不會寫Shader,就只想到用材質自發光來做一下,不知道有沒有更好的辦法!
原理比較簡單,通過代碼開啟材質的自發光效果,然后不斷地調整自發光的亮度即可。
首先要獲取到材質對象實例 material,然后通過其進行其他操作:
啟用自發光效果的代碼是 material.EnableKeyword("_EMISSION")
關閉自發光效果的代碼是 material.DisableKeyword("_EMISSION")
設置自發光顏色和亮度的代碼是 material.SetColor("_EmissionColor", Color.HSVToRGB(_h, _s, _v))
下面有完整的源代碼
此方法實現的閃爍效果不能發出光暈,因為自發光的光暈必須經過烘焙才能生效,而烘焙是在運行前完成的,所以無法在運行時產生光暈效果;另外閃爍的最高亮度只能為1,不能像烘焙時那樣將亮度設為大于1而產生HDR效果。

源代碼
using System.Collections;using UnityEngine;public class Glinting : MonoBehaviour{ /// <summary> /// 閃爍顏色 /// </summary> public Color color = new Color(1, 0, 1, 1); /// <summary> /// 最低發光亮度,取值范圍[0,1],需小于最高發光亮度。 /// </summary> [Range(0.0f, 1.0f)] public float minBrightness = 0.0f; /// <summary> /// 最高發光亮度,取值范圍[0,1],需大于最低發光亮度。 /// </summary> [Range(0.0f, 1)] public float maxBrightness = 0.5f; /// <summary> /// 閃爍頻率,取值范圍[0.2,30.0]。 /// </summary> [Range(0.2f, 30.0f)] public float rate = 1; [Tooltip("勾選此項則啟動時自動開始閃爍")] [SerializeField] private bool _autoStart = false; private float _h, _s, _v; // 色調,飽和度,亮度 private float _deltaBrightness; // 最低最高亮度差 private Renderer _renderer; private Material _material; private readonly string _keyword = "_EMISSION"; private readonly string _colorName = "_EmissionColor"; private Coroutine _glinting; private void Start() { _renderer = gameObject.GetComponent<Renderer>(); _material = _renderer.material; if (_autoStart) { StartGlinting(); } } /// <summary> /// 校驗數據,并保證運行時的修改能夠得到應用。 /// 該方法只在編輯器模式中生效?。。?/// </summary> private void OnValidate() { // 限制亮度范圍 if (minBrightness < 0 || minBrightness > 1) { minBrightness = 0.0f; Debug.LogError("最低亮度超出取值范圍[0, 1],已重置為0。"); } if (maxBrightness < 0 || maxBrightness > 1) { maxBrightness = 1.0f; Debug.LogError("最高亮度超出取值范圍[0, 1],已重置為1。"); } if (minBrightness >= maxBrightness) { minBrightness = 0.0f; maxBrightness = 1.0f; Debug.LogError("最低亮度[MinBrightness]必須低于最高亮度[MaxBrightness],已分別重置為0/1!"); } // 限制閃爍頻率 if (rate < 0.2f || rate > 30.0f) { rate = 1; Debug.LogError("閃爍頻率超出取值范圍[0.2, 30.0],已重置為1.0。"); } // 更新亮度差 _deltaBrightness = maxBrightness - minBrightness; // 更新顏色 // 注意不能使用 _v ,否則在運行時修改參數會導致亮度突變 float tempV = 0; Color.RGBToHSV(color, out _h, out _s, out tempV); } /// <summary> /// 開始閃爍。 /// </summary> public void StartGlinting() { _material.EnableKeyword(_keyword); if (_glinting != null) { StopCoroutine(_glinting); } _glinting = StartCoroutine(IEGlinting()); } /// <summary> /// 停止閃爍。 /// </summary> public void StopGlinting() { _material.DisableKeyword(_keyword); if (_glinting != null) { StopCoroutine(_glinting); } } /// <summary> /// 控制自發光強度。 /// </summary> /// <returns></returns> private IEnumerator IEGlinting() { Color.RGBToHSV(color, out _h, out _s, out _v); _v = minBrightness; _deltaBrightness = maxBrightness - minBrightness; bool increase = true; while (true) { if (increase) { _v += _deltaBrightness * Time.deltaTime * rate; increase = _v <= maxBrightness; } else { _v -= _deltaBrightness * Time.deltaTime * rate; increase = _v <= minBrightness; } _material.SetColor(_colorName, Color.HSVToRGB(_h, _s, _v)); //_renderer.UpdateGIMaterials(); yield return null; } }}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答