# Perlin noise- lightning bolt explaination

I've seen the source code of the Lightning Procedural Example, as posted below, but I can't understand it well and was wondering if someone could explain how the Perlin code scrambles the beam and if there was a way to simulate lightning simpler than this as an alternative?

``````    /*
This script is placed in public domain. The author takes no responsibility for any possible harm.
Contributed by Jonathan Czeck
*/
using UnityEngine;
using System.Collections;

public class LightningBolt : MonoBehaviour
{
public Transform target;
public int zigs = 100;
public float speed = 1f;
public float scale = 1f;
public Light startLight;
public Light endLight;

Perlin noise;
float oneOverZigs;

private Particle[] particles;

void Start()
{
oneOverZigs = 1f / (float)zigs;
particleEmitter.emit = false;

particleEmitter.Emit(zigs);
particles = particleEmitter.particles;
}

void Update ()
{
if (noise == null)
noise = new Perlin();

float timex = Time.time * speed * 0.1365143f;
float timey = Time.time * speed * 1.21688f;
float timez = Time.time * speed * 2.5564f;

for (int i=0; i < particles.Length; i++)
{
Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i);
Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z),
noise.Noise(timey + position.x, timey + position.y, timey + position.z),
noise.Noise(timez + position.x, timez + position.y, timez + position.z));
position += (offset * scale * ((float)i * oneOverZigs));

particles*.position = position;*
_particles*.color = Color.white;*_
<em>_particles*.energy = 1f;*_</em>
<em>_*}*_</em>
<em>_*particleEmitter.particles = particles;*_</em>
<em>_*if (particleEmitter.particleCount >= 2)*_</em>
<em>_*{*_</em>
<em>_*if (startLight)*_</em>
<em>_*startLight.transform.position = particles[0].position;*_</em>
<em>_*if (endLight)*_</em>
<em>_*endLight.transform.position = particles[particles.Length - 1].position;*_</em>
<em>_*}*_</em>
<em>_*}*_</em>
<em>_*}*_</em>
<em>_*```*_</em>``````

I can shed some light on how this script works, but I'm trying to look for a more effecient way of doing this myself. The beauty of the Perlin function is that it emulates patterns in nature, so it looks good when used with things like lightning. The down side is it's really slow. I'd appreciate anyone offering any alternatives they may have...

Anyway - to the code...

``````using UnityEngine;
using System.Collections;

public class LightningBolt : MonoBehaviour
{
public Transform target;
public int zigs = 100;
public float speed = 1f;
public float scale = 1f;
public Light startLight;
public Light endLight;

Perlin noise;
float oneOverZigs;

private Particle[] particles;

``````

/* *The start function simply makes the particle emitter put out as many particles as you *specify in the "zigs" variable. It also grabs the percentage each particle makes up of *the whole bolt, and saves a reference to each particle for use in the update function. */

``````    void Start()
{
oneOverZigs = 1f / (float)zigs;
particleEmitter.emit = false;

particleEmitter.Emit(zigs);
particles = particleEmitter.particles;
}

``````

/* * * * */

``````    void Update ()
{
if (noise == null)
noise = new Perlin();
// not quite sure what these are for - just for good luck, i suppose...
float timex = Time.time * speed * 0.1365143f;
float timey = Time.time * speed * 1.21688f;
float timez = Time.time * speed * 2.5564f;

``````

// loop through every particle, because we're manually setting every particle's position

``````        for (int i=0; i < particles.Length; i++)
{

``````

// this line basically lines the particles up in a straight line between the beginning point and the end point of the lightning bolt

``````            Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i);

``````

// now every particle is going to be offset by whatever value perlin returns

// this is why perlin is so expensive, because it's getting called 3 times for each and every particle in your lightning bolt

//each time it gets called, it's providing a number to offset this individual particle by, based on it's basic straight-line position, using the timex value in the mix to make sure it's not just a crazy random number, but that it makes a somewhat smooth line over time

``````            Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z),
noise.Noise(timey + position.x, timey + position.y, timey + position.z),
noise.Noise(timez + position.x, timez + position.y, timez + position.z));

``````

// now the position gets updated from the straight-line position to the perlin position

``````            position += (offset * scale * ((float)i * oneOverZigs));

// and the actual particle in question is now put in place.
particles*.position = position;*
_particles*.color = Color.white;*_
<em>_particles*.energy = 1f;*_</em>
<em>_*}*_</em>
<em>_*particleEmitter.particles = particles;*_</em>
<em>_*```*_</em>
<em>_*<p>// this just places lights at the start and end of the lightning bolt</p>*_</em>
<em>_*```*_</em>
<em>_*if (particleEmitter.particleCount >= 2)*_</em>
<em>_*{*_</em>
<em>_*if (startLight)*_</em>
<em>_*startLight.transform.position = particles[0].position;*_</em>
<em>_*if (endLight)*_</em>
<em>_*endLight.transform.position = particles[particles.Length - 1].position;*_</em>
<em>_*}*_</em>
<em>_*}*_</em>
<em>_*}*_</em>
<em>_*```*_</em>
<em>_*<p>Hope that helps.  As I said, I'm looking for a more efficient method myself, so I'll let you know if I make any progress.</p>*_</em>``````