How it works...

The vertex shader receives the particle's initial velocity (VertexInitVel) and start time (VertexBirthTime) in its two input attributes. The Time variable stores the amount of time that has elapsed since the beginning of the animation. The Transp output variable is the overall transparency of the particle.

In the main function of the vertex shader, we start by determining the particle's age (t) as the current simulation time minus the particle's birth time. The following if statement determines whether the particle is alive yet. If the particle's age is greater than zero, the particle is alive, otherwise, the particle has yet to be born. In the latter case, the position is set to the camera's origin and the particle is rendered fully transparent. We do the same thing if the particle's age is greater than its lifetime.

If the particle is alive, the particle's position (pos) is determined using the kinematic equation described previously. The cameraPos vertex position is determined by offsetting the particle's position using the offsets array. We transform the position into camera coordinates (using MV), and add the offset for the current vertex using gl_VertexID as the index. 

gl_VertexID is a built-in variable in GLSL that takes on the index of the vertex of the current instance.  In this case, since we are using six vertices per particle, gl_VertexID will be between 0 and 5.

By applying the offsets in camera coordinates, we gain a quality that is often desired in particle systems. The particle's quad will always face the camera. This effect, called billboarding, gives the particles the illusion that they are solid shapes rather than just flat quads.

We scale the offset value by ParticleSize to set the size of the particle. The transparency is determined by linearly interpolating based on the particle's age:

Transp = mix( 1, 0, t / ParticleLifetime );

When the particle is born it is fully opaque, and linearly becomes transparent as it ages. The value of Transp is 1.0 at birth and 0.0 at the end of the particle's lifetime.

In the fragment shader, we color the fragment with the result of value of a texture lookup. Before finishing, we multiply the alpha value of the final color by the variable Transp, in order to scale the overall transparency of the particle based on the particle's age (as determined in the vertex shader).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.125.219