Unity3d--particle production hw8

Unity3d–particle production hw8

1. Assignment content

Simple particle production

  • Make a particle system according to the requirements of the reference resources, refer to the resources
  • Use the introduction in Section 3.3 and use code control to make it have different effects in different scenarios.

2. Configuration

  1. First create a new empty object in the scene.

Insert image description here

  1. Then, add a component "particle system" to this object.

Insert image description here

  1. Particle components are added through Component->Effects->Particle System

Insert image description here

  1. Because the requirement here is code implementation, there is no need to directly set up the particle system. So after creating it, you can start writing scripts.

Insert image description here

3. Code part

step one:

Create a ParticleData class to store the basic information of each particle, including position, time and angle, so that the movement of each particle can be better set.

public class ParticleData{
	public float radius = 0f, angle = 0f, time = 0f;
	public ParticleData(float radius, float angle, float time) {
		this.radius = radius;
		this.angle = angle;
		this.time = time;
	}
}

Step 2:

Define an array for storing particle systems, particle objects, and information. In addition, there is a color gradient. The former is used to set particle attributes and positions, and the latter is used to change particle color.

private ParticleSystem particlesys;  			// 粒子系统
	private ParticleSystem.Particle[] particleArr;  // 粒子数组  
	private ParticleData[] particles; 				// 粒子数据数组
	public Gradient colorGradient; 					// 粒子颜色渐变

Step three:

Set the parameters required for particle movement and make appropriate adjustments during operation.

public int count = 10000;       				// 粒子数量  
	public float size = 0.03f;      				// 粒子大小  
	public float minRad = 1f;  						// 最小半径  
	public float maxRad = 3f; 						// 最大半径  
	public bool clock = true;   					// 顺时针|逆时针  
	public float speed = 2f;        				// 速度
	public float pingPong = 0.002f;  				// 游离范围

Step four:

Initialize the position, angle and other information of each particle, and use a random function to make the particles random. At the same time, we refer to previous works here. We can use proportion to make the particles more concentrated in the center, but dispersed randomly, so that the sub-image will be more impressive without losing the randomness.

// 随机每个粒子距离中心的半径,通过比例调节粒子更加集中于中心,但会随机的分散开
			float midRadius = (maxRad + minRad) / 2;
			float minRate = Random.Range(1.0f, midRadius / minRad);
			float maxRate = Random.Range(midRadius / maxRad, 1.0f);
			float radius = Random.Range(minRad * minRate, maxRad * maxRate);

			// 随机每个粒子的角度  
			float angle = Random.Range(0.0f,360.0f);
			float theta = angle / 180 * Mathf.PI;

			// 随机每个粒子的游离起始时间  
			float time = Random.Range(0.0f, 360.0f);
			//给每个粒子赋值,并保存粒子数据
			particles[i] = new ParticleData(radius, angle, time);
			

Step five:

update function. The movement of particles is achieved by setting the position of the particles in each frame, which needs to be set according to different motion graphics. First, determine whether it is clockwise or counterclockwise. Clockwise means subtracting a value, and counterclockwise means adding a value. The formula of radian is used here. The arc length can be found through radian and radius, so as to obtain the distance of each movement ( speed).

if (clock)  // 顺时针旋转  
				particles[i].angle -= (i % tier + 1) * (speed / particles[i].radius / tier);
else            // 逆时针旋转  
        particles[i].angle += (i % tier + 1) * (speed / particles[i].radius / tier);

At the same time, adding and subtracting angles may cause the particle angle to exceed 360, so use the following command to ensure that the particle angle is between 0 and 360.

particles[i].angle = (360 + particles[i].angle) % 360.0f;

Step six:

After setting the angle, you need to specifically set the position of the particle. Here you need to use the angle information saved by the particle above. If it is a circular motion, according to mathematics, the distance between the particle and the far point is equal at this time, so the particle moves circular motion.

particleArr[i].position = new Vector3(particles[i].radius * Mathf.Cos(theta), 0f, particles[i].radius * Mathf.Sin(theta));

In addition to circular motion, there can also be other more interesting particle motions. For example, using the following command will cause the particles to move in a heart shape. The principle is that the image of this function is a heart shape. Specifically, the coordinates are ( 16 sin 2 x , 13 cosx − 5 cos 2 x − 2 cos 3 x − cos 4 x ) (16sin^2x, 13cosx - 5cos2x - 2cos3x- cos4x)(16sin2 x,13cosx5cos2x2cos3xcos 4 x ) , the function is a bit complicated, but it can be implemented by changing only one line. It is very simple.

particleArr[i].position = new Vector3(0.1f * particles[i].radius * 16 * Mathf.Sin(theta) * Mathf.Sin(theta) * Mathf.Sin(theta), 0.1f * particles[i].radius *(13 * Mathf.Cos(theta) - 5 * Mathf.Cos(theta * 2) - 2 * Mathf.Cos(theta * 3) - Mathf.Cos(4 * theta)), 0f);

Step 7:

In addition, some random beating of particles within the radius is also needed. Here, the built-in PingPong function is used, so that the particles are more random and more beautiful.

particles[i].radius += Mathf.PingPong(particles[i].time / minRad / maxRad, pingPong) - pingPong / 2.0f;

4. Game pictures, videos, and code addresses.

Insert image description here

Video website: https://www.bilibili.com/video/av75156279/

or hw8.mov

Code address: https://github.com/ouzj5/3dgame/tree/master/hw8

Guess you like

Origin blog.csdn.net/qq_40135006/article/details/102993492