PolygonLightFlicker.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using UnityEngine;
  2. using System.Collections;
  3. public class PolygonLightFlicker : MonoBehaviour
  4. {
  5. // Properties
  6. public string waveFunction = "sin"; // possible values: sin, tri(angle), sqr(square), saw(tooth), inv(verted sawtooth), noise (random)
  7. public float startValue = 0.0f; // start
  8. public float amplitude = 1.0f; // amplitude of the wave
  9. public float phase = 0.0f; // start point inside on wave cycle
  10. public float frequency = 0.5f; // cycle frequency per second
  11. // Keep a copy of the original color
  12. private Color originalColor;
  13. // Store the original color
  14. void Start (){
  15. originalColor = GetComponent<Light>().color;
  16. }
  17. void Update (){
  18. Light light = GetComponent<Light>();
  19. light.color = originalColor * (EvalWave());
  20. }
  21. float EvalWave (){
  22. float x = (Time.time + phase)*frequency;
  23. float y;
  24. x = x - Mathf.Floor(x); // normalized value (0..1)
  25. if (waveFunction=="sin") {
  26. y = Mathf.Sin(x*2*Mathf.PI);
  27. }
  28. else if (waveFunction=="tri") {
  29. if (x < 0.5f)
  30. y = 4.0f * x - 1.0f;
  31. else
  32. y = -4.0f * x + 3.0f;
  33. }
  34. else if (waveFunction=="sqr") {
  35. if (x < 0.5f)
  36. y = 1.0f;
  37. else
  38. y = -1.0f;
  39. }
  40. else if (waveFunction=="saw") {
  41. y = x;
  42. }
  43. else if (waveFunction=="inv") {
  44. y = 1.0f - x;
  45. }
  46. else if (waveFunction=="noise") {
  47. y = 1 - (Random.value*2);
  48. }
  49. else {
  50. y = 1.0f;
  51. }
  52. return (y*amplitude)+startValue;
  53. }
  54. }