Unity3D——粒子系统

来源:互联网 发布:md5解密算法 编辑:程序博客网 时间:2024/05/18 13:44

这次笔者实现了类似http://i-remember.fr/en网站的效果


下面就直接讲编写步骤


1. 设置主摄像机的背景为全黑,并且将Clear Flags改为Solid Color,即可实现全黑背景



2. 创建空对象Halo,在其下再创建两个空对象


在空对象中各添加一个粒子系统,Add Component->Effects->ParticleSystem,设置参数如下图所示(两个粒子系统参数相同)



3. 先构建一个保存粒子属性的对象

public class Particle {    public float radius { get; set; }    public float angle { get; set; }    public Particle(float radius = 0F, float angle = 0F) {        this.radius = radius; // 半径          this.angle = angle;  // 角度      }}


4. 然后先编写outer的脚本。首先声明一些相关的参数,作用如注释所示

private ParticleSystem _particleSystem; // 粒子系统private ParticleSystem.Particle[] _particleArray; // 粒子数组private Particle[] _particles; // 粒子属性数组private float _minRadius = 2F; // 粒子最小半径private float _maxRadius = 4.5F; // 粒子最大半径private int _haloCount = 5000; // 粒子最大数量

在Start方法中,初始化参数,获取粒子系统

// 初始化参数_particleSystem = this.GetComponent<ParticleSystem>();_particleArray = new ParticleSystem.Particle[_haloCount];_particles = new Particle[_haloCount];_particleSystem.maxParticles = _haloCount;_particleSystem.Emit(_haloCount);_particleSystem.GetParticles(_particleArray);

初始化每个粒子的属性,主要就是角度和半径,为了更加的视觉效果,我让粒子集中在中部的位置,并设置较大的半径(毕竟这是外圈嘛~)

// 设置粒子属性for (int i = 0; i < _haloCount; ++i) {    //设置粒子半径,尽量集中在中部,设置半径较大    float midRadius = (_maxRadius + _minRadius) / 2;    float shiftMinRadius = Random.Range(1, midRadius / _minRadius);    float shiftMaxRadius = Random.Range(midRadius / _maxRadius, 1);    float radius = Random.Range(_minRadius * shiftMinRadius * 1.1F, _maxRadius * shiftMaxRadius * 1.1F);    //设置粒子角度      float angle = Random.Range(0, Mathf.PI * 2);    // 新建一个粒子属性对象    _particles[i] = new Particle(radius, angle);    // 使用参数方程 x = cos(t), y = sin(t) 计算粒子位置,其中t是角度    _particleArray[i].position = new Vector3(radius * Mathf.Cos(angle), radius * Mathf.Sin(angle), 0);}// 设置粒子系统_particleSystem.SetParticles(_particleArray, _particleArray.Length);

然后就构建好一个静态的初始的粒子系统,运行看看



颜色有点不对啊……


这里我发现还没有设置粒子的material



我设置了material



可以看到



表明初始化成功。


5. 现在要让粒子旋转。这里我参照了师兄的想法,利用方程x =cos(t), y = sin(t)来计算粒子的位置。这部分代码在Update方法中实现,更新方法与初始化时候的方法相同

// 更新粒子属性数组以实现顺时针旋转for (int i = 0; i < _haloCount; ++i) {    _particles[i].angle -= Random.Range(0, 1F / 360);    _particleArray[i].position = new Vector3(_particles[i].radius * Mathf.Cos(_particles[i].angle),        _particles[i].radius * Mathf.Sin(_particles[i].angle), 0);}// 更新设置粒子系统_particleSystem.SetParticles(_particleArray, _particleArray.Length);

然后就可以看到动态效果了



6. 然后编写inner的脚本。这两个脚本功能大致一样,我只是设置旋转的方向不同

// 更新粒子属性数组以实现逆时针旋转for (int i = 0; i < _haloCount; ++i) {    _particles[i].angle += Random.Range(0, 1F / 360);    _particleArray[i].position = new Vector3(_particles[i].radius * Mathf.Cos(_particles[i].angle),        _particles[i].radius * Mathf.Sin(_particles[i].angle), 0);}// 更新设置粒子系统_particleSystem.SetParticles(_particleArray, _particleArray.Length);

并且,由于这是内圈,因此我在初始化的时候设置了较小的半径(之前是乘以1.1,现在是乘以0.9)

//设置粒子半径,尽量集中在中部,设置半径较大float midRadius = (_maxRadius + _minRadius) / 2;float shiftMinRadius = Random.Range(1, midRadius / _minRadius);float shiftMaxRadius = Random.Range(midRadius / _maxRadius, 1);float radius = Random.Range(_minRadius * shiftMinRadius * 0.9F, _maxRadius * shiftMaxRadius * 0.9F);


至此,所有代码编写完成,可以看到内外两圈反方向旋转的效果



0 0