获取高清渲染管线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;
}
}