CurveControlMixerBehaviour.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. public class CurveControlMixerBehaviour : PlayableBehaviour
  6. {
  7. Renderer m_Renderer;
  8. float inputWeight = 0;
  9. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  10. {
  11. m_Renderer = playerData as Renderer;
  12. if (m_Renderer == null)
  13. return;
  14. int inputCount = playable.GetInputCount();
  15. for (int i = 0; i < inputCount; i++)
  16. {
  17. inputWeight = playable.GetInputWeight(i);
  18. if (inputWeight == 0)
  19. {
  20. continue;
  21. }
  22. ScriptPlayable<CurveControlBehaviour> inputPlayable = (ScriptPlayable<CurveControlBehaviour>)playable.GetInput(i);
  23. CurveControlBehaviour input = inputPlayable.GetBehaviour();
  24. if (!m_Renderer.sharedMaterial.HasProperty(input.valueName))
  25. {
  26. continue;
  27. }
  28. switch (input.m_curveControlType)
  29. {
  30. case CurveControlType.Start:
  31. // m_Renderer.sharedMaterial.SetFloat(input.valueName, input.defaultValue);
  32. SetValue(m_Renderer, input, input.defaultValue);
  33. break;
  34. case CurveControlType.Transition:
  35. Playable PlayableDir = playable.GetGraph().GetRootPlayable(0);
  36. float playtime = (float)PlayableDir.GetTime();
  37. float weight = Mathf.InverseLerp((float)input.Clip.start, (float)input.Clip.end, playtime);
  38. // m_Renderer.sharedMaterial.SetFloat(input.valueName, input.ac.Evaluate(weight));
  39. SetValue(m_Renderer, input, input.ac.Evaluate(weight));
  40. break;
  41. case CurveControlType.End:
  42. // m_Renderer.sharedMaterial.SetFloat(input.valueName, input.defaultValue);
  43. SetValue(m_Renderer, input, input.defaultValue);
  44. break;
  45. }
  46. }
  47. }
  48. public void SetValue(Renderer r, CurveControlBehaviour input, float v)
  49. {
  50. switch (input.mCurveValueType)
  51. {
  52. case CurveValueType.Float:
  53. r.sharedMaterial.SetFloat(input.valueName, v);
  54. break;
  55. case CurveValueType.Vector2:
  56. Vector2 v2 = Vector2.zero;
  57. if (input.index == 1 || input.index == 2)
  58. {
  59. v2 = r.sharedMaterial.GetTextureScale(input.valueName);
  60. if (input.index == 1)
  61. {
  62. v2.x = v;
  63. }
  64. else
  65. {
  66. v2.y = v;
  67. }
  68. r.sharedMaterial.SetTextureScale(input.valueName, v2);
  69. }
  70. else if (input.index == 3 || input.index == 4)
  71. {
  72. v2 = r.sharedMaterial.GetTextureOffset(input.valueName);
  73. if (input.index == 3)
  74. {
  75. v2.x = v;
  76. }
  77. else
  78. {
  79. v2.y = v;
  80. }
  81. r.sharedMaterial.SetTextureScale(input.valueName, v2);
  82. }
  83. break;
  84. case CurveValueType.Vector4:
  85. Vector4 v4 = r.sharedMaterial.GetVector(input.valueName);
  86. switch (input.index)
  87. {
  88. case 1:
  89. v4.x = v;
  90. break;
  91. case 2:
  92. v4.y = v;
  93. break;
  94. case 3:
  95. v4.z = v;
  96. break;
  97. case 4:
  98. v4.w = v;
  99. break;
  100. }
  101. r.sharedMaterial.SetVector(input.valueName, v4);
  102. break;
  103. }
  104. }
  105. public override void OnGraphStart(Playable playable)
  106. {
  107. base.OnGraphStart(playable);
  108. }
  109. public override void OnBehaviourPlay(Playable playable, FrameData info)
  110. {
  111. base.OnBehaviourPlay(playable, info);
  112. }
  113. public override void OnBehaviourPause(Playable playable, FrameData info)
  114. {
  115. base.OnBehaviourPause(playable, info);
  116. }
  117. }