Example #1
0
 public void Init(Playable playable)
 {
     // Build our vcam registry for scrubbing updates
     mAllCamerasForScrubbing = new List <List <CinemachineVirtualCameraBase> >();
     for (int i = 0; i < playable.GetInputCount(); ++i)
     {
         var clip = (ScriptPlayable <CinemachineShotPlayable>)playable.GetInput(i);
         CinemachineShotPlayable shot = clip.GetBehaviour();
         if (shot != null && shot.IsValid)
         {
             var vcam      = shot.VirtualCamera;
             int nestLevel = 0;
             for (ICinemachineCamera p = vcam.ParentCamera; p != null; p = p.ParentCamera)
             {
                 ++nestLevel;
             }
             while (mAllCamerasForScrubbing.Count <= nestLevel)
             {
                 mAllCamerasForScrubbing.Add(new List <CinemachineVirtualCameraBase>());
             }
             if (mAllCamerasForScrubbing[nestLevel].IndexOf(vcam) < 0)
             {
                 mAllCamerasForScrubbing[nestLevel].Add(vcam);
             }
         }
     }
 }
Example #2
0
        public void Init(Playable playable)
        {
            // Build our vcam registry for scrubbing updates
            CachedObjects = new List <ClipObjects>(playable.GetInputCount());
            for (int i = 0; i < playable.GetInputCount(); ++i)
            {
                var cs = new ClipObjects
                {
                    Cameras = new List <List <CinemachineVirtualCameraBase> >(),
                };

                var clip = (ScriptPlayable <CinemachineShotPlayable>)playable.GetInput(i);
                CinemachineShotPlayable shot = clip.GetBehaviour();
                if (shot != null && shot.IsValid)
                {
                    var mainVcam = shot.VirtualCamera;
                    cs.Cameras.Add(new List <CinemachineVirtualCameraBase>());

                    // Add all child cameras
                    scratch.Clear();
                    mainVcam.GetComponentsInChildren(scratch);
                    for (int j = 0; j < scratch.Count; ++j)
                    {
                        var vcam = scratch[j];

                        int nestLevel = 0;
                        for (ICinemachineCamera p = vcam.ParentCamera;
                             p != null && p != (ICinemachineCamera)mainVcam; p = p.ParentCamera)
                        {
                            ++nestLevel;
                        }
                        while (cs.Cameras.Count <= nestLevel)
                        {
                            cs.Cameras.Add(new List <CinemachineVirtualCameraBase>());
                        }
                        cs.Cameras[nestLevel].Add(vcam);
                        cs.MaxDampTime = Mathf.Max(cs.MaxDampTime, vcam.GetMaxDampTime());
                    }
                }
                CachedObjects.Add(cs);
            }
        }
Example #3
0
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        base.ProcessFrame(playable, info, playerData);

        // Get the brain that this track controls.
        // Older versions of timeline sent the gameObject by mistake.
        GameObject go = playerData as GameObject;

        if (go == null)
        {
            mBrain = (CinemachineBrain)playerData;
        }
        else
        {
            mBrain = go.GetComponent <CinemachineBrain>();
        }
        if (mBrain == null)
        {
            return;
        }

        // Find which clips are active.  We can process a maximum of 2.
        // In the case that the weights don't add up to 1, the outgoing weight
        // will be calculated as the inverse of the incoming weight.
        int      activeInputs = 0;
        ClipInfo clipA        = new ClipInfo();
        ClipInfo clipB        = new ClipInfo();

        for (int i = 0; i < playable.GetInputCount(); ++i)
        {
            float weight = playable.GetInputWeight(i);
            var   clip   = (ScriptPlayable <CinemachineShotPlayable>)playable.GetInput(i);
            CinemachineShotPlayable shot = clip.GetBehaviour();
            if (shot != null && shot.IsValid &&
                playable.GetPlayState() == PlayState.Playing &&
                weight > 0)
            {
                clipA           = clipB;
                clipB.vcam      = shot.VirtualCamera;
                clipB.weight    = weight;
                clipB.localTime = clip.GetTime();
                clipB.duration  = clip.GetDuration();
                if (++activeInputs == 2)
                {
                    break;
                }
            }
        }

        // Figure out which clip is incoming
        bool incomingIsB = clipB.weight >= 1 || clipB.localTime < clipB.duration / 2;

        if (activeInputs == 2)
        {
            if (clipB.localTime < clipA.localTime)
            {
                incomingIsB = true;
            }
            else if (clipB.localTime > clipA.localTime)
            {
                incomingIsB = false;
            }
            else
            {
                incomingIsB = clipB.duration >= clipA.duration;
            }
        }

        // Override the Cinemachine brain with our results
        ICinemachineCamera camA = incomingIsB ? clipA.vcam : clipB.vcam;
        ICinemachineCamera camB = incomingIsB ? clipB.vcam : clipA.vcam;
        float camWeightB        = incomingIsB ? clipB.weight : 1 - clipB.weight;

        mBrainOverrideId = mBrain.SetCameraOverride(
            mBrainOverrideId, camA, camB, camWeightB, GetDeltaTime(info.deltaTime));
    }
Example #4
0
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        base.ProcessFrame(playable, info, playerData);

        // Get the object that this track controls
        m_BrainOverrideStack = playerData as ICameraOverrideStack;
        if (m_BrainOverrideStack == null)
        {
            return;
        }

        // Find which clips are active.  We can process a maximum of 2.
        // In the case that the weights don't add up to 1, the outgoing weight
        // will be calculated as the inverse of the incoming weight.
        int   activeInputs = 0;
        int   clipIndexA   = -1;
        int   clipIndexB   = -1;
        bool  incomingIsA  = false;   // Assume that incoming clip is clip B
        float weightB      = 1;

        for (int i = 0; i < playable.GetInputCount(); ++i)
        {
            float weight = playable.GetInputWeight(i);
            var   clip   = (ScriptPlayable <CinemachineShotPlayable>)playable.GetInput(i);
            CinemachineShotPlayable shot = clip.GetBehaviour();
            if (shot != null && shot.IsValid &&
                playable.GetPlayState() == PlayState.Playing &&
                weight > 0)
            {
                clipIndexA = clipIndexB;
                clipIndexB = i;
                weightB    = weight;
                if (++activeInputs == 2)
                {
                    // Deduce which clip is incoming (timeline doesn't know)
                    var clipA = playable.GetInput(clipIndexA);
                    // Incoming has later start time (therefore earlier current time)
                    incomingIsA = clip.GetTime() >= clipA.GetTime();
                    // If same start time, longer clip is incoming
                    if (clip.GetTime() == clipA.GetTime())
                    {
                        incomingIsA = clip.GetDuration() < clipA.GetDuration();
                    }
                    break;
                }
            }
        }

        // Special case: check for only one clip that's fading out - it must be outgoing
        if (activeInputs == 1 && weightB < 1 &&
            playable.GetInput(clipIndexB).GetTime() > playable.GetInput(clipIndexB).GetDuration() / 2)
        {
            incomingIsA = true;
        }
        if (incomingIsA)
        {
            (clipIndexA, clipIndexB) = (clipIndexB, clipIndexA);
            weightB = 1 - weightB;
        }

        ICinemachineCamera camA = null;

        if (clipIndexA >= 0)
        {
            CinemachineShotPlayable shot
                 = ((ScriptPlayable <CinemachineShotPlayable>)playable.GetInput(clipIndexA)).GetBehaviour();
            camA = shot.VirtualCamera;
        }

        ICinemachineCamera camB = null;

        if (clipIndexB >= 0)
        {
            CinemachineShotPlayable shot
                 = ((ScriptPlayable <CinemachineShotPlayable>)playable.GetInput(clipIndexB)).GetBehaviour();
            camB = shot.VirtualCamera;
        }

        // Override the Cinemachine brain with our results
        m_BrainOverrideId = m_BrainOverrideStack.SetCameraOverride(
            m_BrainOverrideId, camA, camB, weightB, GetDeltaTime(info.deltaTime));

#if UNITY_EDITOR && UNITY_2019_2_OR_NEWER
        if (m_ScrubbingCacheHelper != null && TargetPositionCache.CacheMode != TargetPositionCache.Mode.Disabled)
        {
            bool isNewB = (m_ScrubbingCacheHelper.ActivePlayableA != clipIndexB &&
                           m_ScrubbingCacheHelper.ActivePlayableB != clipIndexB);

            m_ScrubbingCacheHelper.ActivePlayableA = clipIndexA;
            m_ScrubbingCacheHelper.ActivePlayableB = clipIndexB;
            if (clipIndexA >= 0)
            {
                m_ScrubbingCacheHelper.ScrubToHere(
                    (float)GetMasterPlayableDirector().time, clipIndexA, false,
                    (float)playable.GetInput(clipIndexA).GetTime(), m_BrainOverrideStack.DefaultWorldUp);
            }
            if (clipIndexB >= 0)
            {
                m_ScrubbingCacheHelper.ScrubToHere(
                    (float)GetMasterPlayableDirector().time, clipIndexB, isNewB && weightB > 0.99f,
                    (float)playable.GetInput(clipIndexB).GetTime(), m_BrainOverrideStack.DefaultWorldUp);
            }
        }
#endif
    }