123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using UnityEngine;
- using UnityEngine.Playables;
- using UnityEngine.Timeline;
- public class CurveControlMixerBehaviour : PlayableBehaviour
- {
- Renderer m_Renderer;
- float inputWeight = 0;
- public override void ProcessFrame(Playable playable, FrameData info, object playerData)
- {
- m_Renderer = playerData as Renderer;
- if (m_Renderer == null)
- return;
- int inputCount = playable.GetInputCount();
- for (int i = 0; i < inputCount; i++)
- {
- inputWeight = playable.GetInputWeight(i);
- if (inputWeight == 0)
- {
- continue;
- }
- ScriptPlayable<CurveControlBehaviour> inputPlayable = (ScriptPlayable<CurveControlBehaviour>)playable.GetInput(i);
- CurveControlBehaviour input = inputPlayable.GetBehaviour();
- if (!m_Renderer.sharedMaterial.HasProperty(input.valueName))
- {
- continue;
- }
- switch (input.m_curveControlType)
- {
- case CurveControlType.Start:
- // m_Renderer.sharedMaterial.SetFloat(input.valueName, input.defaultValue);
- SetValue(m_Renderer, input, input.defaultValue);
- break;
- case CurveControlType.Transition:
- Playable PlayableDir = playable.GetGraph().GetRootPlayable(0);
- float playtime = (float)PlayableDir.GetTime();
- float weight = Mathf.InverseLerp((float)input.Clip.start, (float)input.Clip.end, playtime);
- // m_Renderer.sharedMaterial.SetFloat(input.valueName, input.ac.Evaluate(weight));
- SetValue(m_Renderer, input, input.ac.Evaluate(weight));
- break;
- case CurveControlType.End:
- // m_Renderer.sharedMaterial.SetFloat(input.valueName, input.defaultValue);
- SetValue(m_Renderer, input, input.defaultValue);
- break;
- }
- }
- }
- public void SetValue(Renderer r, CurveControlBehaviour input, float v)
- {
- switch (input.mCurveValueType)
- {
- case CurveValueType.Float:
- r.sharedMaterial.SetFloat(input.valueName, v);
- break;
- case CurveValueType.Vector2:
- Vector2 v2 = Vector2.zero;
- if (input.index == 1 || input.index == 2)
- {
- v2 = r.sharedMaterial.GetTextureScale(input.valueName);
- if (input.index == 1)
- {
- v2.x = v;
- }
- else
- {
- v2.y = v;
- }
- r.sharedMaterial.SetTextureScale(input.valueName, v2);
- }
- else if (input.index == 3 || input.index == 4)
- {
- v2 = r.sharedMaterial.GetTextureOffset(input.valueName);
- if (input.index == 3)
- {
- v2.x = v;
- }
- else
- {
- v2.y = v;
- }
- r.sharedMaterial.SetTextureScale(input.valueName, v2);
- }
- break;
- case CurveValueType.Vector4:
- Vector4 v4 = r.sharedMaterial.GetVector(input.valueName);
- switch (input.index)
- {
- case 1:
- v4.x = v;
- break;
- case 2:
- v4.y = v;
- break;
- case 3:
- v4.z = v;
- break;
- case 4:
- v4.w = v;
- break;
- }
- r.sharedMaterial.SetVector(input.valueName, v4);
- break;
- }
- }
- public override void OnGraphStart(Playable playable)
- {
- base.OnGraphStart(playable);
- }
- public override void OnBehaviourPlay(Playable playable, FrameData info)
- {
- base.OnBehaviourPlay(playable, info);
- }
- public override void OnBehaviourPause(Playable playable, FrameData info)
- {
- base.OnBehaviourPause(playable, info);
- }
- }
|