This sample shows an implementation of lightning rendering in XNA GS 2.0
The technique and math is based on a DirectX10 sample made by NVIDIA. Since the NVIDIA sample was made for DirectX10, I had to move most of the simulation on the CPU.
A GPU simulation can also be done, but it would need means of getting random numbers in the vertex shader. To do this, you can either use shader constants (though these give a limited range or numbers), or store them inside vertex textures. For this sample, I chose to stick to the CPU implementation, so the sample can be run by any PC owning a SM 2.0 video card.
The effect is very customizable. The LightningBolt class (looking back at this, I might have chosen a better name) takes as a constructor parameter an instance of LightningDescriptor. This LightningDescriptor contains a bunch of parameters that may be customized in order to obtain different shapes, appearances and behaviours for the lightning effects.
One of the most important parameters is the topology, which is a list of operations that will be recursively applied on the lightning segments to obtain the lightning shape. These operations may be either Jitter (which takes a point on the segment and displaces it), or JitterAndFork (which also creates a new segment starting from the displaced points, towards a random point near the original segment).
To see how these affect the lightning, please try and experiment with different settings.
The sample includes 4 examples of such usages: lightning, energy bolt, graffiti-like shape, and wormy-laser-shape .
When running the sample, use the following controls:
Cycle through examples: Gamepad A / Keyboard Space
Toggle Background: Gamepad B / Keyboard B
Freeze Animation: Gamepad Y / Keyboard F
Exit Sample : Gamepad Back / Keyboard Escape
The sample source code can be downloaded here: Lightning Sample.