MatControlMixerBehaviour.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. public class MatControlMixerBehaviour : PlayableBehaviour
  6. {
  7. Color m_DefaultColor;
  8. Color m_DefaultEmissionColor;
  9. Renderer m_TrackBinding;
  10. bool init = false;
  11. float inputWeight = 0;
  12. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  13. {
  14. m_TrackBinding = playerData as Renderer;
  15. if (m_TrackBinding == null)
  16. return;
  17. if (!init)
  18. {
  19. init = true;
  20. if (Application.isPlaying)
  21. {
  22. // 复制材质球
  23. m_DefaultColor = m_TrackBinding.material.color;
  24. m_DefaultEmissionColor = GetEmissionColor(m_TrackBinding);
  25. }
  26. }
  27. int inputCount = playable.GetInputCount();
  28. for (int i = 0; i < inputCount; i++)
  29. {
  30. inputWeight = playable.GetInputWeight(i);
  31. if (inputWeight == 0)
  32. continue;
  33. // float blend = (float)playable.GetTime();
  34. // Debug.Log("cur weight = " + inputWeight);
  35. ScriptPlayable<MatControlBehaviour> inputPlayable = (ScriptPlayable<MatControlBehaviour>)playable.GetInput(i);
  36. MatControlBehaviour input = inputPlayable.GetBehaviour();
  37. // Debug.Log(input.Clip.start + " " + input.Clip.end);
  38. if (input.isContinued)
  39. {
  40. if (Application.isPlaying)
  41. {
  42. m_TrackBinding.sharedMaterial.color = input.color;
  43. m_DefaultColor = input.color;
  44. }
  45. continue;
  46. }
  47. Playable PlayableDir = playable.GetGraph().GetRootPlayable(0);
  48. float playtime = (float)PlayableDir.GetTime();
  49. float colorWeight = Mathf.InverseLerp((float)input.Clip.start, (float)input.Clip.end, playtime);
  50. // Debug.Log("colorWeight = " + colorWeight);
  51. if (Application.isPlaying)
  52. {
  53. // PlayableDir.Pause();
  54. m_TrackBinding.sharedMaterial.color = Color.Lerp(m_DefaultColor, input.color, colorWeight);
  55. if (input.isEmission)
  56. {
  57. Color c = Color.Lerp(m_DefaultEmissionColor, input.Emission, colorWeight);
  58. SetEmissionColor(m_TrackBinding, c);
  59. }
  60. }
  61. }
  62. }
  63. public Color GetEmissionColor(Renderer r)
  64. {
  65. if (r.sharedMaterial.IsKeywordEnabled("_EMISSION"))
  66. {
  67. return r.sharedMaterial.GetColor("_EmissionColor");
  68. }
  69. return Color.clear;
  70. }
  71. public void SetEmissionColor(Renderer r, Color c)
  72. {
  73. if (r.sharedMaterial.IsKeywordEnabled("_EMISSION"))
  74. {
  75. r.sharedMaterial.SetColor("_EmissionColor", c);
  76. }
  77. }
  78. public override void OnPlayableDestroy(Playable playable)
  79. {
  80. ResetColor();
  81. base.OnPlayableDestroy(playable);
  82. }
  83. public override void OnGraphStop(Playable playable)
  84. {
  85. ResetColor();
  86. base.OnGraphStop(playable);
  87. }
  88. public override void OnGraphStart(Playable playable)
  89. {
  90. ResetColor();
  91. base.OnGraphStart(playable);
  92. }
  93. public override void OnBehaviourPlay(Playable playable, FrameData info)
  94. {
  95. ResetColor();
  96. base.OnBehaviourPlay(playable, info);
  97. }
  98. public override void OnBehaviourPause(Playable playable, FrameData info)
  99. {
  100. ResetColor();
  101. base.OnBehaviourPause(playable, info);
  102. }
  103. public void ResetColor()
  104. {
  105. if (m_TrackBinding != null)
  106. {
  107. if (Application.isPlaying)
  108. {
  109. m_TrackBinding.sharedMaterial.color = m_DefaultColor;
  110. if (m_TrackBinding.sharedMaterial.IsKeywordEnabled("_EMISSION"))
  111. {
  112. m_TrackBinding.sharedMaterial.SetColor("_EmissionColor", m_DefaultEmissionColor);
  113. }
  114. }
  115. }
  116. }
  117. }