April 04, 2009

Soft Particles

Particles are used all the time in games. Many effects can be produced from smoke and fire to stars and energy fields. Particles are typically semi-transparent planar surfaces. They can be drawn to the screen as sprites or textured quads. When these particles intersect with other geometry in the scene they often cause unnatural hard edges. The technique presented here is an attempt to eliminate such visually jarring edges.

The edges are caused by discarding portions of the particle textures that fail z-testing. The traditional z-test is an all or nothing operation. We need a gradual z-test that allows us to blend the alpha channel of the texture over some range. In order to implement our own z-testing we need access to the depth buffer. There are two options, use DX10 to access the depth buffer or create our own depth buffer. My current game project uses deferred shading so I have created my own depth buffer for this technique.

Like traditional rendering pipelines we will need to render the particles after other scene geometry. Once we have our depth buffer ready we then need to create our own z-buffering. In the particle pixel shader we need to compare the position of the pixel we are about to draw and the depth of other geometry already at that pixel. For this example let's assume linear depth values from zero at the near plane to far at the far plane. We subtract pixel depth from z-depth. If the value is greater than a preset threshold than we do nothing. If the value is negative than the traditional z-test will fail and the pixel will be discarded. And if the difference falls within zero and our threshold then we alter the pixel.

The whole goal of soft particles is to remove any noticable edges. Blending the particle as depth comparisons go from threshold to zero can be done many ways. A simple linear blend will greatly reduce the hard edges, but can still be very noticable. An exponential function is able to smooth out the changes across the blending range much better.

I have created a small video demonstration to compare standard and soft particles in my game project.



For more information on this wonderful technique please see the NVIDIA paper Soft Particles by Tristan Lorach.

No comments:

Post a Comment