3D游戏设计作业7:粒子系统

粒子系统

游戏截图:
在这里插入图片描述
1,作业要求

参考 http://i-remember.fr/en 这类网站,使用粒子流编程控制制作一些效果, 如“粒子光环”。

2,设计思路

1,建立对象

在Hierarchy界面建立一个空对象Main,右键点击,选择Effects-Particle System,生成对象Ring。
在这里插入图片描述
2,制备圆环

新建脚本MyRing.cs,利用脚本控制粒子的行为。
声明公有变量,方便我们通过外部设置粒子系统的属性。

    public ParticleSystem _particleSystem;      // 粒子系统对象。
    public int maxParticleNumber = 10000;       // 发射的最大粒子数
    public float pingPong = 0.05f;              // 游离范围
    public float particleSize = 0.05f;          // 粒子的大小
    public float maxRadius = 10, minRadius = 5; // 粒子旋转半径范围
    public float particleSpeed = 0.05f;         // 粒子运动的速度

声明私有变量,用来控制粒子的位置和颜色。

    private float[] particleAngles;         // 每个粒子的位置偏移角
    private float[] particleRadius;         // 每个粒子的运动半径
    private float time = 0;
    private ParticleSystem.Particle[] particleArray;
    public Color[] colors = { new Color(255, 255, 255), new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255), new Color(255, 255, 0), new Color(0, 255, 255), new Color(255, 0, 255), new Color(0, 0, 0) };
    private float colorTimeOut = 0;
    private Gradient gradient;              // 颜色控制器
    private GradientAlphaKey[] alphaKeys;
    private GradientColorKey[] colorKeys;

在start函数中初始化前面声明的属性。

    void Start()
    {
        // 初始化粒子数组、粒子角度数组、粒子半径数组
        _particleSystem = GetComponent<ParticleSystem>();
        particleArray = new ParticleSystem.Particle[maxParticleNumber];
        particleAngles = new float[maxParticleNumber];
        particleRadius = new float[maxParticleNumber];

        // 初始化粒子系统
        _particleSystem.startSpeed = 0;
        _particleSystem.loop = false;
        _particleSystem.maxParticles = maxParticleNumber;
        _particleSystem.Emit(maxParticleNumber);            // 发射粒子
        _particleSystem.GetParticles(particleArray);

        // 初始化各粒子位置
        for (int i = 0; i < maxParticleNumber; i++)
        {
            // 随机生成每个粒子距离中心的半径,同时希望粒子集中于平均半径附近
            float midRadius = (maxRadius + minRadius) / 2;
            float rate1 = Random.Range(1.0f, midRadius / minRadius);
            float rate2 = Random.Range(midRadius / maxRadius, 1.0f);
            particleRadius[i] = Random.Range(minRadius * rate1, maxRadius * rate2);
            // 设置粒子大小
            particleArray[i].startSize = particleSize;
            // 随机生成每个粒子的角度
            particleAngles[i] = Random.Range(0, 360);
            float rad = particleAngles[i] / 180 * Mathf.PI;
            particleArray[i].position = new Vector3(particleRadius[i] * Mathf.Cos(rad), particleRadius[i] * Mathf.Sin(rad), 0); // 放置粒子
        }

        _particleSystem.SetParticles(particleArray, particleArray.Length);

        // 初始化梯度颜色控制器
        alphaKeys = new GradientAlphaKey[5];
        alphaKeys[0].time = 0.0f; alphaKeys[0].alpha = 1.0f;
        alphaKeys[1].time = 0.4f; alphaKeys[1].alpha = 0.4f;
        alphaKeys[2].time = 0.6f; alphaKeys[2].alpha = 1.0f;
        alphaKeys[3].time = 0.9f; alphaKeys[3].alpha = 0.4f;
        alphaKeys[4].time = 1.0f; alphaKeys[4].alpha = 0.6f;
        colorKeys = new GradientColorKey[2];
        colorKeys[0].time = 0; colorKeys[0].color = Color.white;
        colorKeys[1].time = 1; colorKeys[1].color = Color.white;
        gradient = new Gradient();
        gradient.SetKeys(colorKeys, alphaKeys);
    }

3,旋转圆环

上面的代码生成的粒子已经是圆形了,只要在update里面限制住每个粒子的位置就行了,而粒子的旋转也是通过改变粒子的位置来实现的,而在这里就要将粒子分为两份,一份逆时针旋转,一份顺时针旋转。

    void Update()
    {
        for (int i = 0; i < particleArray.Length; i++)
        {
            // 更改粒子角度(一半在逆时针,一半在顺时针)
            particleAngles[i] = (particleAngles[i] + (i % 2 == 0 ? particleSpeed : 0 - particleSpeed) + 360) % 360;
            // 设置粒子位置
            float rad = particleAngles[i] / 180 * Mathf.PI;
            particleArray[i].position = new Vector3(particleRadius[i] * Mathf.Cos(rad), particleRadius[i] * Mathf.Sin(rad), 0);
        }
        _particleSystem.SetParticles(particleArray, particleArray.Length);
    }

令粒子有不同的速度,让它们看起来更灵动些。

particleAngles[i] = (particleAngles[i] + (i % 10 + 1) * (i % 2 == 0 ? particleSpeed : 0 - particleSpeed) + 360) % 360;

给粒子添加粒子波动效果,而且波动是是在一定范围内的。

time += Time.deltaTime;
particleRadius[i] += (Mathf.PingPong(time / minRadius / maxRadius, pingPong) - pingPong / 2);

更改粒子的颜色,并改变粒子的透明度。

colorKeys[0].time = 0;
colorKeys[1].time = 1;
colorKeys[0].color = colors[(int)colorTimeOut % colors.Length];
colorKeys[1].color = colors[(int)colorTimeOut % colors.Length];
gradient.SetKeys(colorKeys, alphaKeys);
particleArray[i].startColor = gradient.Evaluate(particleAngles[i] / 360.0f);

4,清晰圆环

在unity的粒子系统里,只要粒子太小粒子的颜色就暗淡下来,所以这个圆环很暗。为了清晰圆环,我们使用Glow11插件,将Glow11脚本增加到摄像机的Inspector面板中。并把粒子系统中Renderer处的Material改为Default-Material。

3,完整代码

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

public class MyRing : MonoBehaviour
{
    public ParticleSystem _particleSystem;      // 粒子系统对象。
    public int maxParticleNumber = 10000;       // 发射的最大粒子数
    public float pingPong = 0.05f;              // 游离范围
    public float particleSize = 0.05f;          // 粒子的大小
    public float maxRadius = 10, minRadius = 5; // 粒子旋转半径范围
    public float particleSpeed = 0.05f;         // 粒子运动的速度

    private float[] particleAngles;         // 每个粒子的位置偏移角
    private float[] particleRadius;         // 每个粒子的运动半径
    private float time = 0;
    private ParticleSystem.Particle[] particleArray;
    public Color[] colors = { new Color(255, 255, 255), new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255), new Color(255, 255, 0), new Color(0, 255, 255), new Color(255, 0, 255), new Color(0, 0, 0) };
    private float colorTimeOut = 0;
    private Gradient gradient;              // 颜色控制器
    private GradientAlphaKey[] alphaKeys;
    private GradientColorKey[] colorKeys;

    // Use this for initialization
    void Start()
    {
        // 初始化粒子数组、粒子角度数组、粒子半径数组
        _particleSystem = GetComponent<ParticleSystem>();
        particleArray = new ParticleSystem.Particle[maxParticleNumber];
        particleAngles = new float[maxParticleNumber];
        particleRadius = new float[maxParticleNumber];

        // 初始化粒子系统
        _particleSystem.startSpeed = 0;
        _particleSystem.loop = false;
        _particleSystem.maxParticles = maxParticleNumber;
        _particleSystem.Emit(maxParticleNumber);            // 发射粒子
        _particleSystem.GetParticles(particleArray);

        // 初始化各粒子位置
        for (int i = 0; i < maxParticleNumber; i++)
        {
            // 随机生成每个粒子距离中心的半径,同时希望粒子集中于平均半径附近
            float midRadius = (maxRadius + minRadius) / 2;
            float rate1 = Random.Range(1.0f, midRadius / minRadius);
            float rate2 = Random.Range(midRadius / maxRadius, 1.0f);
            particleRadius[i] = Random.Range(minRadius * rate1, maxRadius * rate2);
            // 设置粒子大小
            particleArray[i].startSize = particleSize;
            // 随机生成每个粒子的角度
            particleAngles[i] = Random.Range(0, 360);
            float rad = particleAngles[i] / 180 * Mathf.PI;
            particleArray[i].position = new Vector3(particleRadius[i] * Mathf.Cos(rad), particleRadius[i] * Mathf.Sin(rad), 0); // 放置粒子
        }

        _particleSystem.SetParticles(particleArray, particleArray.Length);

        // 初始化梯度颜色控制器
        alphaKeys = new GradientAlphaKey[5];
        alphaKeys[0].time = 0.0f; alphaKeys[0].alpha = 1.0f;
        alphaKeys[1].time = 0.4f; alphaKeys[1].alpha = 0.4f;
        alphaKeys[2].time = 0.6f; alphaKeys[2].alpha = 1.0f;
        alphaKeys[3].time = 0.9f; alphaKeys[3].alpha = 0.4f;
        alphaKeys[4].time = 1.0f; alphaKeys[4].alpha = 0.6f;
        colorKeys = new GradientColorKey[2];
        colorKeys[0].time = 0; colorKeys[0].color = Color.white;
        colorKeys[1].time = 1; colorKeys[1].color = Color.white;
        gradient = new Gradient();
        gradient.SetKeys(colorKeys, alphaKeys);
    }

    // Update is called once per frame
    void Update()
    {
        colorTimeOut += Time.deltaTime;
        for (int i = 0; i < particleArray.Length; i++)
        {
            time += Time.deltaTime;
            // 更改粒子角度(一半在逆时针,一半在顺时针,而且速度有10个层级)
            particleAngles[i] = (particleAngles[i] + (i % 10 + 1) * (i % 2 == 0 ? particleSpeed : 0 - particleSpeed) + 360) % 360;
            // 更改粒子半径(造成粒子波动效果,而且波动是在一定范围内的)
            particleRadius[i] += (Mathf.PingPong(time / minRadius / maxRadius, pingPong) - pingPong / 2);
            // 更改粒子颜色
            // particleArray[i].startColor = colors[(int)colorTimeOut % colors.Length];
            colorKeys[0].time = 0;
            colorKeys[1].time = 1;
            colorKeys[0].color = colors[(int)colorTimeOut % colors.Length];
            colorKeys[1].color = colors[(int)colorTimeOut % colors.Length];
            gradient.SetKeys(colorKeys, alphaKeys);
            particleArray[i].startColor = gradient.Evaluate(particleAngles[i] / 360.0f);
            // 设置粒子位置
            float rad = particleAngles[i] / 180 * Mathf.PI;
            particleArray[i].position = new Vector3(particleRadius[i] * Mathf.Cos(rad), particleRadius[i] * Mathf.Sin(rad), 0);
        }
        _particleSystem.SetParticles(particleArray, particleArray.Length);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值