Advertisement
fivearchers

Lighting FX Using Curves and Gradients for Unity3D

Oct 22nd, 2013
243
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None
  1. using UnityEngine;
  2. using System.Collections;
  3. //Light FX Script for Unity3D - using curves and a gradient your light will change over time
  4. //  eg: Use a quick declining curve, and gradient from bright to dark for an explosion
  5. //  eg: Make nice random curves for a torch flicker
  6.  
  7. // Quentin Preik 2013/10/22
  8. // @fivearchers
  9.  
  10. public class LightCurve : MonoBehaviour {
  11.     public AnimationCurve intensityCurve;   //Used for intensity over time - cover 0-1 on both axis
  12.     public float intensityMin;              //Base intensity   
  13.     public float intensityMax;              //Max intensity - so using, 2 and 10 - at the bottom of a curve intensity
  14.                                                 //would be 2 and at the top it would be 10
  15.    
  16.     public Gradient colorCurve;             //Gradient to color the light over time
  17.     public AnimationCurve rangeCurve;       //Curve to change the range of the light over time
  18.     public float rangeMin;                  //Minimum range
  19.     public float rangeMax;                  //Maximum range
  20.     public float curveSpeed;                //How fast to go through the curves and gradient - 1=1s, 2=0.5s, 0.5=2s
  21.     public float curvePosition;             //Where to start on the curve 0-1, 0.5 would start halfway
  22.     public bool loop;                       //Whether to loop through the curve
  23.     public bool dieWhenDone;                //If you're not looping should the object destroy itself when it's done
  24.     private Light myLight;                 
  25.     // Use this for initialization
  26.     void Start () {
  27.         myLight=gameObject.light;
  28.     }
  29.    
  30.     // Update is called once per frame
  31.     void Update () {
  32.         if (curvePosition!=1f||!loop){
  33.             curvePosition+=curveSpeed*Time.deltaTime;
  34.             if (curvePosition>1f){
  35.                 if (dieWhenDone){
  36.                     Destroy(gameObject);
  37.                     return;
  38.                 }
  39.                 if (!loop){
  40.                     curvePosition=1f;  
  41.                 }
  42.                 else {
  43.                     curvePosition=1f-curvePosition;
  44.                 }
  45.             }
  46.             light.intensity=intensityMin+(intensityMax-intensityMin)*intensityCurve.Evaluate(curvePosition);
  47.             light.color=colorCurve.Evaluate(curvePosition);
  48.             light.range=rangeMin+(rangeMax-rangeMin)*rangeCurve.Evaluate(curvePosition);
  49.         }
  50.     }
  51. }
Advertisement
RAW Paste Data Copied
Advertisement