码迷,mamicode.com
首页 > 编程语言 > 详细

Unity 3D Game 粒子光环

时间:2019-10-01 14:28:07      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:keyword   RKE   i++   blank   alpha   游离   lse   max   tor   

介绍

参考 http://i-remember.fr/en 制作类似该网站效果
技术图片

基本步骤

创建粒子光环(空对象)

技术图片

将摄像机背景置为黑色

技术图片

创建粒子系统,配置参数

技术图片

新建 C# 脚本,命名为 ParticleRing

ParticleRing.cs

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class : MonoBehaviour
{
public class circleParticle
{
public float radius = 0.0f;
public float angle = 0.0f;
public float time = 0.0f;
public circleParticle(float radius, float angle, float time)
{
this.radius = radius;
this.angle = angle;
this.time = time;
}
}

public ParticleSystem particleSystem;
private ParticleSystem.Particle[] particlesArray; // 粒子数组
private circleParticle[] particleAttr; //粒子属性数组
public int particleSum = 10000; // 粒子数量
public float minRadius = 5.0f;
public float maxRadius = 10.0f;
public int Part = 2; // 将粒子们分为两部分
public float minSpeed = 0.09f;
public float maxSpeed = 0.12f;
public float speedLevelSum = 9; // 粒子速度分为九层
public float driftRange = 0.03f; // 游离范围

public Gradient colorGradient;

void Start()
{
particleAttr = new circleParticle[particleSum];
particlesArray = new ParticleSystem.Particle[particleSum];
particleSystem.maxParticles = particleSum;
particleSystem.Emit(particleSum);
particleSystem.GetParticles(particlesArray);

// 初始化梯度颜色控制器
GradientAlphaKey[] 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.9f;
GradientColorKey[] colorKeys = new GradientColorKey[2];
colorKeys[0].time = 0.0f; colorKeys[0].color = Color.white;
colorKeys[1].time = 1.0f; colorKeys[1].color = Color.white;
colorGradient.SetKeys(colorKeys, alphaKeys);


for (int i = 0; i < particleSum; i++)
{
// 随机产生角度
float randomAngle = Random.Range(0.0f, 360.0f);

// 随机产生每个粒子距离中心的半径,同时粒子要集中在平均半径附近
float midRadius = (maxRadius + minRadius) / 2;
float minRate = Random.Range(1.0f, midRadius / minRadius);
float maxRate = Random.Range(midRadius / maxRadius, 1.0f);
float randomRadius = Random.Range(minRadius * minRate, maxRadius * maxRate);

// 随机时间
float randomTime = Random.Range (0, 10f);

//粒子属性设置
particleAttr[i] = new circleParticle(randomRadius, randomAngle, randomTime);
particlesArray[i].position = new Vector3(randomRadius * Mathf.Cos(randomAngle), randomRadius * Mathf.Sin(randomAngle), 0.0f);
}
//设置粒子
particleSystem.SetParticles(particlesArray, particleSum);
}


void Update()
{
for (int i = 0; i < particleSum; i++)
{
// 速度方向分为两部分,一部分顺时针,一部分逆时针
float speed = (i % Part == 0) ? Random.Range(minSpeed, maxSpeed) : - 2 * Random.Range(minSpeed, maxSpeed);
// 给不同的粒子速度加权,分为 9 层
float weightedSpeed = (i % speedLevelSum + 1) * speed;

// 更新角度
particleAttr[i].angle += Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; // 此处粒子的速度与半径的平方根成正比
particleAttr[i].angle = particleAttr[i].angle % 360;
float radian = particleAttr[i].angle / 180 * Mathf.PI;
// 更新半径
particleAttr [i].time += Time.deltaTime;
particleAttr [i].radius += Mathf.PingPong(particleAttr [i].time / minRadius / maxRadius, driftRange) - driftRange / 2.0f;
// 更新位置
particlesArray[i].position = new Vector3(particleAttr [i].radius * Mathf.Cos(radian), particleAttr [i].radius * Mathf.Sin(radian), 0f);

particlesArray[i].color = colorGradient.Evaluate(particleAttr[i].angle / 360.0f);
}
particleSystem.SetParticles(particlesArray, particleSum);
}
}

效果图

技术图片

鼠标悬停在粒子光环内部效果

修改 circleParticle

添加 originRadius ,用以记录光环收缩前当前粒子的旋转半径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class circleParticle
{
public float radius = 0.0f;
public float angle = 0.0f;
public float time = 0.0f;
public float originRadius = 0.0f;
public circleParticle(float radius, float angle, float time)
{
this.radius = radius;
this.angle = angle;
this.time = time;
this.originRadius = radius;
}
}

修改 update()

判断鼠标与光环的关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 鼠标到光环中心的距离
float mouseToCenter = Mathf.Sqrt (Mathf.Pow((Input.mousePosition.x - Screen.width / 2), 2f) + Mathf.Pow((Input.mousePosition.y - Screen.height / 2), 2f));
// 鼠标在光环内部
bool mouseInRing = false;
// 此处认为满足此条件,即是在光环内部
if (mouseToCenter < Screen.height / 4) {
mouseInRing = true;
if (mouseInRingTime <= mouseInRingTimeLimit) { // 在粒子光环里面的累计时间每一帧加一,并不会大过限定值
mouseInRingTime++;
}
} else { // 鼠标移出粒子光环
mouseInRing = false;
if (mouseInRingTime > 0)
mouseInRingTime--;
}

根据鼠标与光环关系进行的行为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 当鼠标在粒子光环里当时间累积为零
if (mouseInRingTime == 0) {
particleAttr [i].originRadius = particleAttr[i].radius;
}

// 鼠标进入粒子光环
if (mouseInRing)
{
particleAttr[i].angle -= 1 / 3 * Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; // 此处粒子的速度与半径的平方根成正比
particleAttr[i].angle = particleAttr[i].angle % 360;
radian = particleAttr[i].angle / 180 * Mathf.PI;
if (mouseInRingTime < mouseInRingTimeLimit) {
particleAttr [i].radius = minRadius + (particleAttr [i].radius - minRadius) * (particleAttr [i].radius / maxRadius);
}
}
// 鼠标在粒子光环外
if (!mouseInRing && mouseInRingTime > 0)
{
particleAttr[i].angle += mouseInRingTime / 5 * Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; // 此处粒子的速度与半径的平方根成正比
particleAttr[i].angle = particleAttr[i].angle % 360;
radian = particleAttr[i].angle / 180 * Mathf.PI;

particleAttr [i].radius += (particleAttr [i].originRadius - particleAttr [i].radius) / Mathf.Sqrt(mouseInRingTime) * (particleAttr [i].radius / maxRadius);
}

完整代码

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
145
146
147
148
149
150
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class : MonoBehaviour
{
public class circleParticle
{
public float radius = 0.0f;
public float angle = 0.0f;
public float time = 0.0f;
public float originRadius = 0.0f;
public circleParticle(float radius, float angle, float time)
{
this.radius = radius;
this.angle = angle;
this.time = time;
this.originRadius = radius;
}
}

public ParticleSystem particleSystem;
private ParticleSystem.Particle[] particlesArray; // 粒子数组
private circleParticle[] particleAttr; //粒子属性数组
public int particleSum = 10000; // 粒子数量
public float minRadius = 5.0f;
public float maxRadius = 10.0f;
public int Part = 2; // 将粒子们分为两部分
public float minSpeed = 0.09f;
public float maxSpeed = 0.12f;
public float speedLevelSum = 9; // 粒子速度分为九层
public float driftRange = 0.03f; // 游离范围
public int mouseInRingTime;
public int mouseInRingTimeLimit;

public Gradient colorGradient;

void Start()
{
particleAttr = new circleParticle[particleSum];
particlesArray = new ParticleSystem.Particle[particleSum];
particleSystem.maxParticles = particleSum;
particleSystem.Emit(particleSum);
particleSystem.GetParticles(particlesArray);

// 初始化梯度颜色控制器
GradientAlphaKey[] 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.9f;
GradientColorKey[] colorKeys = new GradientColorKey[2];
colorKeys[0].time = 0.0f; colorKeys[0].color = Color.white;
colorKeys[1].time = 1.0f; colorKeys[1].color = Color.white;
colorGradient.SetKeys(colorKeys, alphaKeys);

mouseInRingTime = 0;
mouseInRingTimeLimit = 30;

for (int i = 0; i < particleSum; i++)
{
// 随机产生角度
float randomAngle = Random.Range(0.0f, 360.0f);

// 随机产生每个粒子距离中心的半径,同时粒子要集中在平均半径附近
float midRadius = (maxRadius + minRadius) / 2;
float minRate = Random.Range(1.0f, midRadius / minRadius);
float maxRate = Random.Range(midRadius / maxRadius, 1.0f);
float randomRadius = Random.Range(minRadius * minRate, maxRadius * maxRate);

// 随机时间
float randomTime = Random.Range (0, 10f);

//粒子属性设置
particleAttr[i] = new circleParticle(randomRadius, randomAngle, randomTime);
particlesArray[i].position = new Vector3(randomRadius * Mathf.Cos(randomAngle), randomRadius * Mathf.Sin(randomAngle), 0.0f);
}
//设置粒子
particleSystem.SetParticles(particlesArray, particleSum);
}


void Update()
{
// 鼠标到光环中心的距离
float mouseToCenter = Mathf.Sqrt (Mathf.Pow((Input.mousePosition.x - Screen.width / 2), 2f) + Mathf.Pow((Input.mousePosition.y - Screen.height / 2), 2f));
// 鼠标在光环内部
bool mouseInRing = false;
// 此处认为满足此条件,即是在光环内部
if (mouseToCenter < Screen.height / 4) {
mouseInRing = true;
if (mouseInRingTime <= mouseInRingTimeLimit) { // 在粒子光环里面的累计时间每一帧加一,并不会大过限定值
mouseInRingTime++;
}
} else { // 鼠标移出粒子光环
mouseInRing = false;
if (mouseInRingTime > 0)
mouseInRingTime--;
}


for (int i = 0; i < particleSum; i++)
{
// 速度方向分为两部分,一部分顺时针,一部分逆时针
float speed = (i % Part == 0) ? Random.Range(minSpeed, maxSpeed) : - 2 * Random.Range(minSpeed, maxSpeed);
// 给不同的粒子速度加权,分为 9 层
float weightedSpeed = (i % speedLevelSum + 1) * speed;

// 更新角度
particleAttr[i].angle += Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; // 此处粒子的速度与半径的平方根成正比
particleAttr[i].angle = particleAttr[i].angle % 360;
float radian = particleAttr[i].angle / 180 * Mathf.PI;
// 更新半径
particleAttr [i].time += Time.deltaTime;
particleAttr [i].radius += Mathf.PingPong(particleAttr [i].time / minRadius / maxRadius, driftRange) - driftRange / 2.0f;

// 当鼠标在粒子光环里当时间累积为零
if (mouseInRingTime == 0) {
particleAttr [i].originRadius = particleAttr[i].radius;
}

// 鼠标进入粒子光环
if (mouseInRing)
{
particleAttr[i].angle -= 1 / 3 * Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; // 此处粒子的速度与半径的平方根成正比
particleAttr[i].angle = particleAttr[i].angle % 360;
radian = particleAttr[i].angle / 180 * Mathf.PI;
if (mouseInRingTime < mouseInRingTimeLimit) {
particleAttr [i].radius = minRadius + (particleAttr [i].radius - minRadius) * (particleAttr [i].radius / maxRadius);
}
}
// 鼠标在粒子光环外
if (!mouseInRing && mouseInRingTime > 0)
{
particleAttr[i].angle += mouseInRingTime / 5 * Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; // 此处粒子的速度与半径的平方根成正比
particleAttr[i].angle = particleAttr[i].angle % 360;
radian = particleAttr[i].angle / 180 * Mathf.PI;

particleAttr [i].radius += (particleAttr [i].originRadius - particleAttr [i].radius) / Mathf.Sqrt(mouseInRingTime) * (particleAttr [i].radius / maxRadius);
}

// 更新位置
particlesArray[i].position = new Vector3(particleAttr [i].radius * Mathf.Cos(radian), particleAttr [i].radius * Mathf.Sin(radian), 0f);

particlesArray[i].color = colorGradient.Evaluate(particleAttr[i].angle / 360.0f);
}
particleSystem.SetParticles(particlesArray, particleSum);
}
}

最终效果

技术图片

引用

Unity3D学习笔记(9)—— 粒子光环
Unity-3D 粒子光圈效果

原文:大专栏  Unity 3D Game 粒子光环


Unity 3D Game 粒子光环

标签:keyword   RKE   i++   blank   alpha   游离   lse   max   tor   

原文地址:https://www.cnblogs.com/petewell/p/11615054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!