123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using UnityEngine;
- using UnityEngine.Playables;
- using UnityEngine.Timeline;
- public class MatControlMixerBehaviour : PlayableBehaviour
- {
- Color m_DefaultColor;
- Color m_DefaultEmissionColor;
- Renderer m_TrackBinding;
- bool init = false;
- float inputWeight = 0;
- public override void ProcessFrame(Playable playable, FrameData info, object playerData)
- {
- m_TrackBinding = playerData as Renderer;
- if (m_TrackBinding == null)
- return;
- if (!init)
- {
- init = true;
- if (Application.isPlaying)
- {
- // 复制材质球
- m_DefaultColor = m_TrackBinding.material.color;
- m_DefaultEmissionColor = GetEmissionColor(m_TrackBinding);
- }
-
- }
- int inputCount = playable.GetInputCount();
- for (int i = 0; i < inputCount; i++)
- {
- inputWeight = playable.GetInputWeight(i);
- if (inputWeight == 0)
- continue;
- // float blend = (float)playable.GetTime();
-
- // Debug.Log("cur weight = " + inputWeight);
- ScriptPlayable<MatControlBehaviour> inputPlayable = (ScriptPlayable<MatControlBehaviour>)playable.GetInput(i);
- MatControlBehaviour input = inputPlayable.GetBehaviour();
- // Debug.Log(input.Clip.start + " " + input.Clip.end);
- if (input.isContinued)
- {
- if (Application.isPlaying)
- {
- m_TrackBinding.sharedMaterial.color = input.color;
- m_DefaultColor = input.color;
- }
- continue;
- }
- Playable PlayableDir = playable.GetGraph().GetRootPlayable(0);
- float playtime = (float)PlayableDir.GetTime();
- float colorWeight = Mathf.InverseLerp((float)input.Clip.start, (float)input.Clip.end, playtime);
- // Debug.Log("colorWeight = " + colorWeight);
- if (Application.isPlaying)
- {
- // PlayableDir.Pause();
- m_TrackBinding.sharedMaterial.color = Color.Lerp(m_DefaultColor, input.color, colorWeight);
- if (input.isEmission)
- {
- Color c = Color.Lerp(m_DefaultEmissionColor, input.Emission, colorWeight);
- SetEmissionColor(m_TrackBinding, c);
- }
- }
- }
- }
- public Color GetEmissionColor(Renderer r)
- {
- if (r.sharedMaterial.IsKeywordEnabled("_EMISSION"))
- {
- return r.sharedMaterial.GetColor("_EmissionColor");
- }
- return Color.clear;
- }
- public void SetEmissionColor(Renderer r, Color c)
- {
- if (r.sharedMaterial.IsKeywordEnabled("_EMISSION"))
- {
- r.sharedMaterial.SetColor("_EmissionColor", c);
- }
- }
- public override void OnPlayableDestroy(Playable playable)
- {
- ResetColor();
- base.OnPlayableDestroy(playable);
- }
- public override void OnGraphStop(Playable playable)
- {
- ResetColor();
- base.OnGraphStop(playable);
- }
- public override void OnGraphStart(Playable playable)
- {
- ResetColor();
- base.OnGraphStart(playable);
- }
- public override void OnBehaviourPlay(Playable playable, FrameData info)
- {
- ResetColor();
- base.OnBehaviourPlay(playable, info);
- }
- public override void OnBehaviourPause(Playable playable, FrameData info)
- {
- ResetColor();
- base.OnBehaviourPause(playable, info);
- }
- public void ResetColor()
- {
- if (m_TrackBinding != null)
- {
- if (Application.isPlaying)
- {
- m_TrackBinding.sharedMaterial.color = m_DefaultColor;
- if (m_TrackBinding.sharedMaterial.IsKeywordEnabled("_EMISSION"))
- {
- m_TrackBinding.sharedMaterial.SetColor("_EmissionColor", m_DefaultEmissionColor);
- }
- }
- }
- }
- }
|