Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Persona · Dec 04, 2010 at 02:57 AM · procedurallightningexplain

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[i].position = position;
         particles[i].color = Color.white;
         particles[i].energy = 1f;
     }

     particleEmitter.particles = particles;

     if (particleEmitter.particleCount >= 2)
     {
         if (startLight)
             startLight.transform.position = particles[0].position;
         if (endLight)
             endLight.transform.position = particles[particles.Length - 1].position;
     }
 }   

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by chief1234 · Dec 07, 2010 at 01:21 AM

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[i].position = position; particles[i].color = Color.white; particles[i].energy = 1f; }

     particleEmitter.particles = particles;

// this just places lights at the start and end of the lightning bolt

        if (particleEmitter.particleCount >= 2)
        {
            if (startLight)
                startLight.transform.position = particles[0].position;
            if (endLight)
                endLight.transform.position = particles[particles.Length - 1].position;
        }
    }   
}

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Skyrim's Sparks 0 Answers

Translating tutorial pseudocode to use LineRenderer 0 Answers

Run-time lightmap generating for procedural terrain 1 Answer

sharedMesh - what is the mesh shared with? 1 Answer

Infinite/procedural multiplayer world in Unity possible? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges