LightControlMixerBehaviour.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. public class LightControlMixerBehaviour : PlayableBehaviour
  6. {
  7. Color m_DefaultColor;
  8. float m_DefaultIntensity;
  9. float m_DefaultBounceIntensity;
  10. float m_DefaultRange;
  11. Light m_TrackBinding;
  12. bool m_FirstFrameHappened;
  13. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  14. {
  15. m_TrackBinding = playerData as Light;
  16. if (m_TrackBinding == null)
  17. return;
  18. if (!m_FirstFrameHappened)
  19. {
  20. m_DefaultColor = m_TrackBinding.color;
  21. m_DefaultIntensity = m_TrackBinding.intensity;
  22. m_DefaultBounceIntensity = m_TrackBinding.bounceIntensity;
  23. m_DefaultRange = m_TrackBinding.range;
  24. m_FirstFrameHappened = true;
  25. }
  26. int inputCount = playable.GetInputCount ();
  27. Color blendedColor = Color.clear;
  28. float blendedIntensity = 0f;
  29. float blendedBounceIntensity = 0f;
  30. float blendedRange = 0f;
  31. float totalWeight = 0f;
  32. float greatestWeight = 0f;
  33. int currentInputs = 0;
  34. for (int i = 0; i < inputCount; i++)
  35. {
  36. float inputWeight = playable.GetInputWeight(i);
  37. ScriptPlayable<LightControlBehaviour> inputPlayable = (ScriptPlayable<LightControlBehaviour>)playable.GetInput(i);
  38. LightControlBehaviour input = inputPlayable.GetBehaviour ();
  39. blendedColor += input.color * inputWeight;
  40. blendedIntensity += input.intensity * inputWeight;
  41. blendedBounceIntensity += input.bounceIntensity * inputWeight;
  42. blendedRange += input.range * inputWeight;
  43. totalWeight += inputWeight;
  44. if (inputWeight > greatestWeight)
  45. {
  46. greatestWeight = inputWeight;
  47. }
  48. if (!Mathf.Approximately (inputWeight, 0f))
  49. currentInputs++;
  50. }
  51. m_TrackBinding.color = blendedColor + m_DefaultColor * (1f - totalWeight);
  52. m_TrackBinding.intensity = blendedIntensity + m_DefaultIntensity * (1f - totalWeight);
  53. m_TrackBinding.bounceIntensity = blendedBounceIntensity + m_DefaultBounceIntensity * (1f - totalWeight);
  54. m_TrackBinding.range = blendedRange + m_DefaultRange * (1f - totalWeight);
  55. }
  56. public override void OnPlayableDestroy (Playable playable)
  57. {
  58. m_FirstFrameHappened = false;
  59. if(m_TrackBinding == null)
  60. return;
  61. m_TrackBinding.color = m_DefaultColor;
  62. m_TrackBinding.intensity = m_DefaultIntensity;
  63. m_TrackBinding.bounceIntensity = m_DefaultBounceIntensity;
  64. m_TrackBinding.range = m_DefaultRange;
  65. }
  66. }