Unity-控制HDR渲染管线下HDRColor调色板

获取高清渲染管线HDR 材质球上的Emissive Color 值 并变换自发光强度

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

//渲染管线HDR
//材质球选取渲染shader 为 HDRenderPipeline/Lit
//需要配合相机后处理插件 Progress value
//此脚本可控制 HDR/Lit 材质球上的 Emissive Inputs 上面的HDR 自发光强度

    public Color color = new Color(61 / 255f, 226 / 255f, 131 / 255, 2);

    [Tooltip("最低发光亮度,取值范围[0,1],需小于最高发光亮度0")]
    [Range(0.0f, 1.0f)]
    public float minBrightness = 0.0f;

    [Tooltip("最高发光亮度,取值范围[0,1],需大于最低发光亮度。")]
    [Range(0.0f, 5)]
    public float maxBrightness = 0.5f;

    [Tooltip("闪烁频率,取值范围[0.2,30.0]。")]
    [Range(0.2f, 30.0f)]
    public float rate = 1;
    //是否闪烁
    [HideInInspector]
    public bool isGlinting = false;

    [Tooltip("勾选此项则启动时自动开始闪烁")]
    [SerializeField]
    public bool _autoStart = false;

    private float _h, _s, _v;           // 色调,饱和度,亮度
    private float _deltaBrightness;     // 最低最高亮度差
    private Renderer _renderer;

    private Material[] _materials;

    private readonly string _keyword = "_UnlitColor";
    private readonly string _colorName = "_EmissiveColor";

    private Coroutine _glinting;

    void OnEnable()
    {
        _renderer = gameObject.GetComponent<MeshRenderer>();

        _materials = _renderer.materials;

        if (_autoStart)
        {
            StartGlinting();
        }

    }

    private void StartGlinting()
    {
        isGlinting = true;
        if (_materials != null)
        {

            if (_materials.Length > 0)
            {
                for (int i = 0; i < _materials.Length; i++)
                {
                    _materials[i].EnableKeyword(_keyword);
                }

                if (_glinting != null)
                {
                    StopCoroutine(_glinting);
                }
                _glinting = StartCoroutine(IEGlinting());
            }
        }

    }

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

            for (int i = 0; i < _materials.Length; i++)
            {
                _materials[i].SetColor(_colorName, Color.HSVToRGB(_h, _s, _v));
            }
            yield return null;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值