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效果。
源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145using 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; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是朴实雪碧最近收集整理的关于Unity利用材质自发光实现物体闪烁的全部内容,更多相关Unity利用材质自发光实现物体闪烁内容请搜索靠谱客的其他文章。
发表评论 取消回复