纳金网

标题: 闪烁灯光脚本 [打印本页]

作者: 狂风大尉    时间: 2014-6-26 17:03
标题: 闪烁灯光脚本
  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent( typeof( Light ) )]
  4. public class flickeringLight : MonoBehaviour
  5. {
  6.     // Flickering Styles
  7.     public enum flickerinLightStyles { CampFire = 0, Fluorescent = 1 };
  8.     public flickerinLightStyles flickeringLightStyle = flickerinLightStyles.CampFire;
  9.     // Campfire Methods
  10.     public enum campfireMethods { Intensity = 0, Range = 1, Both = 2 };
  11.     public campfireMethods campfireMethod = campfireMethods.Intensity;
  12.     // Intensity Styles
  13.     public enum campfireIntesityStyles { Sine = 0, Random = 1 };
  14.     public campfireIntesityStyles campfireIntesityStyle = campfireIntesityStyles.Random;
  15.     // Range Styles
  16.     public enum campfireRangeStyles { Sine = 0, Random = 1 };
  17.     public campfireRangeStyles campfireRangeStyle = campfireRangeStyles.Random;
  18.     // Base Intensity Value
  19.     public float CampfireIntensityBaseValue = 0.5f;
  20.     // Intensity Flickering Power
  21.     public float CampfireIntensityFlickerValue = 0.1f;
  22.     // Base Range Value
  23.     public float CampfireRangeBaseValue = 10.0f;
  24.     // Range Flickering Power
  25.     public float CampfireRangeFlickerValue = 2.0f;
  26.    
  27.     // If Style is Sine
  28.     private float CampfireSineCycleIntensity = 0.0f;
  29.     private float CampfireSineCycleRange = 0.0f;
  30.     // "Glow" Speeds
  31.     public float CampfireSineCycleIntensitySpeed = 5.0f;
  32.     public float CampfireSineCycleRangeSpeed = 5.0f;
  33.     public float FluorescentFlickerMin = 0.4f;
  34.     public float FluorescentFlickerMax = 0.5f;
  35.     public float FluorescentFlicerPercent = 0.95f;
  36.     // NOT IMPLEMENTED YET !!!!
  37.     public bool FluorescentFlickerPlaySound = false;
  38.     public AudioClip FluorescentFlickerAudioClip;
  39.     // ------------------------


  40.         // Use this for initialization
  41.         void Start () {
  42.         
  43.         }
  44.         
  45.         // Update is called once per frame
  46.         void Update () {
  47.         switch( flickeringLightStyle )
  48.         {
  49.             // If Flickering Style is Campfire
  50.             case flickerinLightStyles.CampFire:
  51.                 // If campfire method is Intesity OR Both
  52.                 if( campfireMethod == campfireMethods.Intensity || campfireMethod == campfireMethods.Both )
  53.                 {
  54.                     // If Intensity style is Sine
  55.                     if( campfireIntesityStyle == campfireIntesityStyles.Sine )
  56.                     {
  57.                         // Cycle the Campfire angle
  58.                         CampfireSineCycleIntensity += CampfireSineCycleIntensitySpeed;
  59.                         if( CampfireSineCycleIntensity > 360.0f ) CampfireSineCycleIntensity = 0.0f;
  60.                         // Base + Values
  61.                         light.intensity = CampfireIntensityBaseValue + ( ( Mathf.Sin( CampfireSineCycleIntensity * Mathf.Deg2Rad ) * ( CampfireIntensityFlickerValue / 2.0f ) ) + ( CampfireIntensityFlickerValue / 2.0f ) );
  62.                     }
  63.                     else light.intensity = CampfireIntensityBaseValue + Random.Range( 0.0f, CampfireIntensityFlickerValue );
  64.                 }
  65.                 // If campfire method is Range OR Both
  66.                 if( campfireMethod == campfireMethods.Range || campfireMethod == campfireMethods.Both )
  67.                 {
  68.                     // If Range style is Sine
  69.                     if( campfireRangeStyle == campfireRangeStyles.Sine )
  70.                     {
  71.                         // Cycle the Campfire angle
  72.                         CampfireSineCycleRange += CampfireSineCycleRangeSpeed;
  73.                         if( CampfireSineCycleRange > 360.0f ) CampfireSineCycleRange = 0.0f;
  74.                         // Base + Values
  75.                         light.range = CampfireRangeBaseValue + ( ( Mathf.Sin( CampfireSineCycleRange * Mathf.Deg2Rad ) * ( CampfireSineCycleRange / 2.0f ) ) + ( CampfireSineCycleRange / 2.0f ) );
  76.                     }
  77.                     else light.range = CampfireRangeBaseValue + Random.Range( 0.0f, CampfireRangeFlickerValue );
  78.                 }
  79.                 break;
  80.             // If Flickering Style is Fluorescent
  81.             case flickerinLightStyles.Fluorescent:
  82.                 if( Random.Range( 0.0f, 1.0f ) > FluorescentFlicerPercent )
  83.                 {
  84.                     light.intensity = FluorescentFlickerMin;
  85.                     // Check Audio - NOT IMPLEMENTED YET
  86.                     if( FluorescentFlickerPlaySound )
  87.                     {
  88.                     }
  89.                 }
  90.                 else light.intensity = FluorescentFlickerMax;
  91.                 break;
  92.             default:
  93.                 // You should not be here.
  94.                 break;
  95.         }
  96.         
  97.         }
  98. }
复制代码

作者: HIDEOKOJIMA    时间: 2014-6-26 18:08
Thanks for this !
作者: hyui    时间: 2014-6-26 18:39
Good to know!
作者: Kadina    时间: 2014-6-27 07:44
感谢分享
作者: texfill    时间: 2014-6-27 09:53
Thanks for share !
作者: wucnj    时间: 2014-6-27 14:18
感谢分享!!!




欢迎光临 纳金网 (http://wwww.narkii.com/club/) Powered by Discuz! X2.5