3D Computer Game Programming-Note 8
1、简单粒子制作
- 按参考资源要求,制作一个粒子系统
- 使用 3.3 节介绍,用代码控制使之在不同场景下效果不一样
【实现过程】
- 参照参考资源制作一个粒子系统
LightBall
,部分有改动。
首先新建一个 Particle System
并命名为 LightBall
作为该粒子系统的主体部分,基础参数如下:
简单解释一些重要参数:
Start Lifetime:粒子的生命周期,即粒子从发射到消失的时间,因为我们的目的是得到一个“持续散发光芒”的粒子系统,下一个粒子必须在现有粒子消失之前发射,尽管勾选了 Looping,但发射周期仍然有一个阈值,因此需要设置一个合适的生命周期来达到想要的效果;
Start Speed:粒子开始的速度,作为骨架部分粒子应保持不动,因此设置为 0;
Start Rotation:粒子开始的旋转方向,设置一个旋转角度区间以达到粒子系统光晕方向不断变化的效果;
Max Particles:单个周期内粒子系统发出的最大粒子数,对于 LightBall 来说,设置一个不太低的值即可。
调整粒子的发射速率为合适的值。作为骨架部分发射速率影响其闪烁效果,速率过高导致闪烁过于频繁甚至跳帧,频率过低则导致闪烁不连续:
![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037529-4f1c7db2-4e79-40ce-a764-0d3f7153f1cc.png#align=left&display=inline&height=174&margin=%5Bobject%20Object%5D&originHeight=174&originWidth=494&size=0&status=done&style=none&width=494)
选择 Shape
为 Box
,使粒子被约束在较小空间内,以达到光晕填充的效果:
![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037584-21099ecf-3b9a-4e8a-a855-61327f5a3b7d.png#align=left&display=inline&height=262&margin=%5Bobject%20Object%5D&originHeight=262&originWidth=498&size=0&status=done&style=none&width=498)
设置 Color Over Time
为从黑过渡到白再过渡到黑,达到光从暗到亮再渐渐消失的效果:
![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037537-5b45a855-a8c4-4dbd-a38e-bce0e1b7a884.png#align=left&display=inline&height=221&margin=%5Bobject%20Object%5D&originHeight=221&originWidth=445&size=0&status=done&style=none&width=445)
为粒子选择渲染材质——使用参考资源提供的素材:
![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037552-8f3ade52-8232-4abb-89d3-5f28eac7ea16.png#align=left&display=inline&height=472&margin=%5Bobject%20Object%5D&originHeight=472&originWidth=498&size=0&status=done&style=none&width=498)
勾选 Texture Sheet Animation
使纹理能以动画帧的形式呈现:
![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037521-7e899ef0-158d-4c52-8ee8-37b9b079ba27.png#align=left&display=inline&height=177&margin=%5Bobject%20Object%5D&originHeight=177&originWidth=500&size=0&status=done&style=none&width=500)
现在 LightBall
效果如下:
![](https://cdn.nlark.com/yuque/0/2020/gif/2589459/1606809037578-ec6c0408-513a-4de4-8e16-a4a74075871b.gif#align=left&display=inline&height=333&margin=%5Bobject%20Object%5D&originHeight=333&originWidth=546&size=0&status=done&style=none&width=546)
在 LightBall
下创建一个 Particle System
作为其子节点,命名为 StarLight
,作为装饰用的星光部分,基础参数如下:
![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037698-4bc290dd-6ffb-4e31-a9a6-f3945ef12b4f.png#align=left&display=inline&height=494&margin=%5Bobject%20Object%5D&originHeight=494&originWidth=502&size=0&status=done&style=none&width=502)
其他部分参数如下,大多与 LightBall
设置相同。其中,Shape
选择 Sphere
,使粒子朝各个方向自由发射。
![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037599-925f3993-92c4-4d1d-85c1-c1a7d24f3470.png#align=left&display=inline&height=68&margin=%5Bobject%20Object%5D&originHeight=68&originWidth=500&size=0&status=done&style=none&width=500)<br /> ![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037555-f0022e44-587c-4207-b9d9-b501d324a82b.png#align=left&display=inline&height=338&margin=%5Bobject%20Object%5D&originHeight=338&originWidth=501&size=0&status=done&style=none&width=501)<br /> ![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037734-80b19840-4f4d-492f-bdf9-3fc7c099419a.png#align=left&display=inline&height=48&margin=%5Bobject%20Object%5D&originHeight=48&originWidth=499&size=0&status=done&style=none&width=499)<br /> ![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037733-72213448-f20b-4d61-9ee9-54b9c19a08c5.png#align=left&display=inline&height=64&margin=%5Bobject%20Object%5D&originHeight=64&originWidth=498&size=0&status=done&style=none&width=498)<br /> ![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037475-6745d141-0a57-4c36-a87e-9b6389ef6889.png#align=left&display=inline&height=178&margin=%5Bobject%20Object%5D&originHeight=178&originWidth=503&size=0&status=done&style=none&width=503)<br /> ![](https://cdn.nlark.com/yuque/0/2020/png/2589459/1606809037554-3a7a9425-b0f2-40b2-a720-e7c0f0d6cb53.png#align=left&display=inline&height=472&margin=%5Bobject%20Object%5D&originHeight=472&originWidth=498&size=0&status=done&style=none&width=498)
现在 LightBall
效果如下:
![](https://cdn.nlark.com/yuque/0/2020/gif/2589459/1606809037723-53af9af5-c526-4217-ac87-784d99197c5c.gif#align=left&display=inline&height=333&margin=%5Bobject%20Object%5D&originHeight=333&originWidth=546&size=0&status=done&style=none&width=546)
编写
ColorGradient.cs
,使粒子系统在发射过程中呈现颜色渐变:public class ColorGradient : MonoBehaviour {
private ParticleSystem ball;
private float r, g, b;
private bool flag = true;
void Start() {
ball = this.GetComponent<ParticleSystem>();
r = 1.0f;
g = 0.3f;
b = 0.25f;
}
void Update() {
if (g >= 0.95f) {
flag = false;
}
else if (g <= 0.3f) {
flag = true;
}
if (flag) {
g += 0.001f;
}
else {
g -= 0.001f;
}
var main = ball.main;
main.startColor = new ParticleSystem.MinMaxGradient(new Color(r, g, b));
}
}
将上述脚本挂载到LightBall
和 StarLight
上,运行:
![](https://cdn.nlark.com/yuque/0/2020/gif/2589459/1606809037815-3b9e2333-bf8c-4364-a9cf-1c009355b04a.gif#align=left&display=inline&height=333&margin=%5Bobject%20Object%5D&originHeight=333&originWidth=546&size=0&status=done&style=none&width=546)
实现了粒子系统颜色在红黄色调间来回变化。
对上述粒子系统 LightBall
稍微改造并制成预制 Star
,结合天空盒可以创建一个星空场景:
“星星”由脚本控制在随机位置生成:
public class Sky : MonoBehaviour {
void Start() {
for (int i = 0; i < 100; ++i) {
ParticleSystem star = Instantiate(Resources.Load<ParticleSystem>("Prefabs/Star"), new Vector3(Random.Range(-14.0f, 14.0f), Random.Range(3.5f, 6.5f), 0), Quaternion.identity);
}
}
}
这说明粒子系统还有很多应用场景等待着我们去发现!