HW8: Particle System

Homework Requirements - Making Simple Particles
According to the requirements of reference resources, make a particle system
Use code control to make it have different effects in different scenarios
Reference blog:

Experimental procedure

First review the production process of the particle ocean:

  1. Add an empty object and add the parts of the particle system
  2. Simply adjust the particle material in the particle component (choose a default particle material, don't let it be empty)
  3. Write code programming, hang on to empty objects. And drag the particle system components into the public variables of the code.
  4. Add a color gradient component and manually modify the color

Particle Halo Maker

First add an empty object, and add the ParticleSystem, and then simply set the properties:

The properties of the emitter Render, the most important thing here is to select the material, otherwise there will be a bunch of small squares with default colors, and the visual effect may not be the best

Properties of Particle Halos

The properties of the particle halo

 ParticleSystem myparticleSystem
is first of all the particle system itself, which is the basis of particle programming.
ParticleSystem.Particle[] particleArray
is followed by the particle array, which saves the state of each particle. Here we need to specify a quantity count, which is the size of the array. I set it to 1000. Since
SingleParticle[] points are halos, the points of each particle
The positions definitely need to be sorted regularly, instead of running around randomly, so the state of each particle for the entire halo is recorded here, the two most important attributes are angle and radius, because each particle moves around the center point, so the movement The trajectory will have a radius, where will it move to? You need angles to record. A class SingleParticle defined by myself is used here, which will be explained later.
Gradient grad
color gradienter, the gradienter that comes with Unity, as long as the interval of color change is selected, there will be a gradual night color.
There are also some more complex variable attributes: particle size, inner and outer diameters of the halo, speed of rotation, direction of rotation, etc.
 

 The position property of the particle:

The area of ​​the xy plane where the particle is located is mainly estimated by angle and radius, and the xy coordinates are calculated by basic mathematical trigonometric functions. It defines some methods that can be called from the outside.

Particle system initialization

In the Start function, first set some basic properties of the particle system, such as the number of particles, etc., which need to be specified at the beginning and will not change.

For the assignment of the particle system, we can directly obtain the components mounted on the same object through the GetComponent method, but here we use manual dragging to assign values ​​to the public variables of the script.
Then there are various initializations to create a new array of particle positions. Since we set the number to 1000, the length of the array is 1000. In addition to this array, there is also a real particle array in the particle system. Set the size, maximum number of particles, The number of emitters emitted. After setting, you need to use the particle system to get it, so that you can actually set it successfully.
One thing worth noting here is that the old version usually uses ParticleSystem.startSize to set, but in the new version, you need to get ParticleSystem.main with a variable first, and then set the startSize and maxParticles of main.

Among them, the Init function is used as the initialization of the particle position:

Through the inner diameter and outer diameter, a section is divided in the middle, and the particles are randomly distributed in these two parts, and the angle is 360 degrees random.

Then there is a change every time Update:

Each update needs to loop through 1000 particles, and make a change to the state of each particle, such as angle color and so on. The specific operation is as follows

Divide 1000 particles into 10 layers by means of modulus, each layer rotates at a slightly different angle (equivalent to speed), and then divides the whole into 3 parts: the first part and the third part rotate in the same direction, The second part is the rotation in the other direction. The overall rotation effect is not too single. Of course, in the end, it is necessary to take the modulus of the increased angle, so as not to overflow 360 degrees, or less than 0 degrees. Finally, use the method in the class of particle position to calculate the xy coordinates.
Then there is the change of color. First, we need to use the gradienter to adjust the color. In fact, this step can still be adjusted by code.

This is the main part. You can see something like a progress bar. There are some points on the top and bottom. The upper point represents the gradient of transparency (which can be understood as a change in brightness), and the lower point is the choice of color. Adding points only needs to Click on the corresponding blank place, and to delete the point, you need to click on a point and press the delete button.
Then you need to change the color, you need to click the color bar below, and then select the appropriate color, after all the selection is complete, you can see the automatic gradient between the two color points. Transparency is similar.
After the adjustment, you can select the color through the code, from 0 to 1 is the first color to the last color.
So our idea is that not only the color changes with the angle, but also with the time, so we need to estimate the time, in the above code:

This part is for calculation. First, use real-time time, that is, the running time of the program to modulo a floating-point number (note that it is a modulo floating-point number, so that the result will be a floating-point number), and get a number in an interval. Here The modulus is 1.0, which means that the color change will run around in one second. If it is a number with a large modulus ratio, it needs to be normalized, that is, the modulus is divided by the number.
Then you need to add the normalized result of the current angle, and finally use a cyclic addition and subtraction to make the final result between 0 and 1. Use the Evaluate of the Gradient to get the corresponding color
. Finally, the most critical step is to set the calculated color and position to the particle itself. The SetParticles function is used here.

Demonstration effect

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_40552127/article/details/128239297