This time to complete is the white circle effect shown on the http://i-remember.fr/en website.
First, let's take a look at its effect:
first, the website effect shows
second, create particles
Setting related parameters
Turn the camera background tone black
Third, write the script
1. Create a new script: ParticleRotate.cs and drag it to the paticle system.
2. Create an array of particles, initialize. At the same time we need to record the initial radius for each particle's initial angle. Consider the subsequent possibility that each particle will have more properties, so write a class to manage the particle properties
As we know, the angle and radius need to be randomly generated to represent a specific position of a particle.
//create particle system, particle array, number of particles, declare radius of particle rings PublicParticlesystem Particlesystem;PrivateParticlesystem.particle[] Particlesarray;PrivateParticleclass[] particleattr;//Particle attribute array Public intParticlenum =10000; Public classParticleclass { Public floatRadiu =0.0F Public floatAngle =0.0F Public Particleclass(floatRadiu_,floatAngle_) {Radiu = Radiu_; angle = Angle_; } }voidStart () {Particlesarray =NewParticlesystem.particle[particlenum]; Particlesystem.maxparticles = Particlenum; Particlesystem.emit (Particlenum); Particlesystem.getparticles (Particlesarray); for(inti =0; i < Particlenum; i++) {///corresponding initialization operation, set the radius for each particle, angle}//Set particleParticlesystem.setparticles (Particlesarray, particlenum); }
3, first consider the maximum radius of the Maxradius minimum radius of Minradius in the interval randomly generated for each particle radius
float randomAngle = Random.Range(0.0f360.0f); float randomRadius = Random.Range(minRadius, maxRadius); new particleClass(randomRadius, randomAngle); new0.0f);
The effect is as follows:
4, OK next to let it move up ~ ~ So the question came ~ How to move.
My idea is to divide this pile of particles into twoparts, the first part clockwise, and the second part counterclockwise. Just change its angle inside the update function.
public int part =2;void Update () {//set to two-part particles, part clockwise, partially counterclockwise. for (int i =0; i < particlenum; i++){if (i%2==0) Particleattr[i]. Angle+ = (i% part +1) * Speed;else Particleattr[i]. Angle-= (i% part +1) * Speed;Particleattr[i]. Angle= Particleattr[i]. Angle% the;Float rad = Particleattr[i]. Angle/ the* MATHF. PI;Particlesarray[i]. Position= new Vector3 (Particleattr[i]. Radiu* MATHF. Cos(RAD), Particleattr[i]. Radiu* MATHF. Sin(RAD),0F;} Particlesystem. Setparticles(Particlesarray, Particlenum);}
The effect of moving:
5, well, I have to admit that the effect of moving is too strange. A closer look will find. The resulting radius must be in the [Minradius, Maxradius] interval of the correct, but also more concentrated in the middle of an area, so as to achieve the effect of the site.
This is also the most interesting place I think.
First thought of the solution : normal distribution.
However, only Python has a corresponding normal distribution library. Writing a normal distribution with C # is too much trouble.
And then I found box-muller .
Portal: http://baike.baidu.com/view/1710258.htm
Here's how:
If there are two independent random numbers U1 and U2 within the (0,1] domain,
You can use either of the following two equations to calculate the random number Z for a normal distribution:
z = R * cos (θ) or z = R * sin (θ)
Wherein,R = sqrt ( -2 * ln (U2)) && θ= 2 *π* U1
The normal value z has an average equal to 0 and a standard deviation equal to 1, you can use the following equation to map Z to a statistic with a mean of M, standard deviation to SD x:x = m + (Z * SD)
The method is then written to the for loop.
A perfect method for generating random particles box-muller float sita =2* Mathf.pi *Random.Range(0,1); float R = mathf.Sqrt(-2* MATHF.Log(Random.Range(0,1))); float Z = R * MATHF.Cos(SITA); float Randomradius = (Minradius + Maxradius)/2+ Z *2.5F Print (Randomradius);Debug.Log("R ="+ R);Debug.Log(Randomradius);
What's the problem? R is infinite, waiting to change the value in log to 0.7,
running slowly does not say. 10,000 particles unity ran for nearly a minute before running out. Scared I thought it blew.
And the effect is very bad.
Well, there's nothing I can do. And then saw the great God this piece of code implemented.
// 随机产生每个粒子距离中心的半径,同时粒子要集中在平均半径附近 float2; float minRate = Random.Range(1.0f, midRadius / minRadius); float1.0f); float randomRadius = Random.Range(minRadius * minRate, maxRadius * maxRate);
A very clever use of three random functions to achieve the concentration of particles.
A random number is generated in an interval equivalent to [[Minradius, Midradius], [Midradius, Maxradius]]. Naturally, the random number in the middle will be more concentrated.
It's amazing.
6. After adding this part of the code to my Code. The effect of the implementation is very good.
This completes the experiment:
The following is the complete code:
usingUnityengine;usingSystem.Collections; Public classparticlerotate:monobehaviour{ Public classParticleclass { Public floatRadiu =0.0F Public floatAngle =0.0F Public Particleclass(floatRadiu_,floatAngle_) {Radiu = Radiu_; angle = Angle_; } }//create particle system, particle array, number of particles, declare radius of particle rings PublicParticlesystem Particlesystem;PrivateParticlesystem.particle[] Particlesarray;PrivateParticleclass[] particleattr;//Particle attribute array Public intParticlenum =10000; Public floatMinradius =5.0F Public floatMaxradius =10.0F Public intPart =2; Public floatSpeed =0.1FvoidStart () {particleattr =NewParticleclass[particlenum]; Particlesarray =NewParticlesystem.particle[particlenum]; Particlesystem.maxparticles = Particlenum; Particlesystem.emit (Particlenum); Particlesystem.getparticles (Particlesarray); for(inti =0; i < Particlenum; i++) {///corresponding initialization operation, set the radius for each particle, angle //Generate a random angle floatRandomangle = Random.range (0.0F360.0f);//randomly generates the radius of each particle from the center, while the particles are concentrated around the average radius floatMidradius = (Maxradius + Minradius)/2;floatMinrate = Random.range (1.0F, Midradius/minradius);floatMaxrate = Random.range (Midradius/maxradius,1.0f);floatRandomradius = Random.range (Minradius * minrate, Maxradius * maxrate); ////A perfect method for generating random particles box-muller //float sita = 2 * mathf.pi * random.range (0, 1); //float R = mathf.sqrt ( -2 * MATHF.LOG (0.7f)); //float Z = R * Mathf.cos (SITA); //float Randomradius = (Minradius + Maxradius)/2 + Z * 2.5f; //print (Randomradius); //debug.log ("R =" + R); //debug.log (Randomradius); //Particle property settingsParticleattr[i] =NewParticleclass (Randomradius, Randomangle); Particlesarray[i].position =NewVector3 (Randomradius * Mathf.cos (randomangle), Randomradius * Mathf.sin (Randomangle),0.0f); }//Set particleParticlesystem.setparticles (Particlesarray, particlenum); }voidUpdate () {//Set to two parts of the particle, part clockwise, part counterclockwise. for(inti =0; i < Particlenum; i++) {ifI2==0) Particleattr[i].angle + = (i% part +1) * SPEED;ElseParticleattr[i].angle-= (i% part +1) * SPEED;//Reset position according to new angleParticleattr[i].angle = particleattr[i].angle% the;floatrad = Particleattr[i].angle/ the* MATHF.PI; Particlesarray[i].position =NewVector3 (Particleattr[i].radiu * Mathf.cos (RAD), Particleattr[i].radiu * Mathf.sin (RAD),0f); } particlesystem.setparticles (Particlesarray, particlenum); }}
Unity-3d particle aperture effect