LightFlicker.js 1.5 KB

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