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
- Unity3D——粒子系统
- Unity3D笔记第九天——粒子系统
- Unity3D粒子系统——实现粒子播放完成之后自动销毁
- Unity3D -- 图形 -- 粒子系统
- Unity3D ParticleSystem粒子系统
- Unity3D 粒子系统
- Unity3D-粒子系统
- Unity3D粒子系统
- unity3D 粒子系统详解
- Unity3D ParticleSystem粒子系统 【初级】
- unity3D粒子系统基础属性
- Unity3D:粒子系统Particle System
- unity3D粒子系统--Particle System
- Unity3D(五)粒子系统
- [Unity3D]Unity3D游戏开发之粒子系统
- Unity3D学习——粒子光环
- Unity3D——粒子系统被2D的NGUI或者UGUI遮挡的问题
- Unity粒子系统 Unity3D ParticleSystem粒子系统(3.5版)
- ArchLinux调节屏幕亮度
- 树莓派3B驱动Kinect for windows(OpenNI)
- 写博客写博客
- Java异常打印输出中常见方法的分析
- Autoencoder
- Unity3D——粒子系统
- 解决Glide加载网络圆角图片先显示完整图片的问题
- 自动化测试框架设计
- 知名蓝牙芯片原厂及其发展应用史(含蓝牙V1.0-V5.0介绍)
- 通用链表/栈/队列-算法学习笔记十六
- 乱谈精灵宝可梦Go(Pokemon Go)
- AJAX初学
- OpenCV-霍夫变换
- Java工具类——jdbc