Unity中利用材质自发光实现物体闪烁效果

版权声明:若有错误之处,欢迎指正!如有侵权,请联系删除! https://blog.csdn.net/qq_21397217/article/details/80967432

Unity中利用材质自发光实现物体闪烁效果

补充:这种方法有一点问题,在测试(Windows平台)的时候发现,要想在Build出来的游戏中实现闪烁效果,就必须在 Project 窗口中将源材质的自发光属性(Emission)启用,否则自发光效果就只能在编辑器模式中生效。
启用源材质的自发光效果后,将其亮度(Brightness)调整为0,物体看起来就和没有启用自发光时一样。

看到别的游戏里有物体高亮闪烁效果,但自己不会写Shader,就只想到用材质自发光来做一下,不知道有没有更好的办法!

原理比较简单,通过代码开启材质的自发光效果,然后不断地调整自发光的亮度即可。
首先要获取到材质对象实例 material,然后通过其进行其他操作:

  • 启用自发光效果的代码是 material.EnableKeyword("_EMISSION")
  • 关闭自发光效果的代码是 material.DisableKeyword("_EMISSION")
  • 设置自发光颜色和亮度的代码是 material.SetColor("_EmissionColor", Color.HSVToRGB(_h, _s, _v))
    • 其中的 _h、_s、_v参数分别代表颜色的色相、饱和度和亮度。
    • 获取颜色的色相、饱和度和亮度的代码为 Color.RGBToHSV(color, out _h, out _s, out _v)
  • 下面有完整的源代码

此方法实现的闪烁效果不能发出光晕,因为自发光的光晕必须经过烘焙才能生效,而烘焙是在运行前完成的,所以无法在运行时产生光晕效果;另外闪烁的最高亮度只能为1,不能像烘焙时那样将亮度设为大于1而产生HDR效果。

glinting

源代码

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;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_21397217/article/details/80967432
今日推荐