Exemple #1
0
        private float GetEffectiveDeltaTime()
        {
            OverrideStackFrame activeOverride = GetActiveOverride();
            float deltaTime = (Application.isPlaying || SoloCamera != null) ? Time.fixedDeltaTime : 0;

            if (activeOverride != null)
            {
                deltaTime = activeOverride.deltaTime;
            }
            return(deltaTime);
        }
Exemple #2
0
        /// Get the override and move it to the top of the stack
        private OverrideStackFrame GetOverrideFrame(int id)
        {
            foreach (OverrideStackFrame o in mOverrideStack)
            {
                if (o.id == id)
                {
                    return(o);
                }
            }
            OverrideStackFrame ovr = new OverrideStackFrame();

            ovr.id = id;
            mOverrideStack.Insert(0, ovr);
            return(ovr);
        }
        private float GetEffectiveDeltaTime(bool fixedDelta)
        {
            OverrideStackFrame activeOverride = GetActiveOverride();

            if (activeOverride != null)
            {
                return(activeOverride.deltaTime);
            }
            if (m_IgnoreTimeScale)
            {
                return((Application.isPlaying || SoloCamera != null)
                    ? (fixedDelta ? Time.fixedUnscaledDeltaTime : Time.unscaledDeltaTime) : 0);
            }
            return((Application.isPlaying || SoloCamera != null)
                ? (fixedDelta ? Time.fixedDeltaTime : Time.deltaTime) : 0);
        }
        /// Get the override if it's present, else insert it
        private OverrideStackFrame GetOverrideFrame(int id)
        {
            int count = mOverrideStack.Count;

            for (int i = 0; i < count; ++i)
            {
                if (mOverrideStack[i].id == id)
                {
                    return(mOverrideStack[i]);
                }
            }
            OverrideStackFrame ovr = new OverrideStackFrame();

            ovr.id = id;
            mOverrideStack.Insert(0, ovr);
            return(ovr);
        }
        private float GetEffectiveDeltaTime(bool fixedDelta)
        {
            if (SoloCamera != null)
            {
                return(Time.unscaledDeltaTime);
            }
            OverrideStackFrame activeOverride = GetActiveOverride();

            if (activeOverride != null)
            {
                return(activeOverride.Expired ? -1 : activeOverride.deltaTime);
            }
            if (!Application.isPlaying)
            {
                return(-1);
            }
            if (m_IgnoreTimeScale)
            {
                return(fixedDelta ? Time.fixedDeltaTime : Time.unscaledDeltaTime);
            }
            return(fixedDelta ? Time.fixedDeltaTime * Time.timeScale : Time.deltaTime);
        }
Exemple #6
0
        /// Get the next active blend on the stack.  Used when an override blends in from nothing.
        private OverrideStackFrame GetNextActiveFrame(int overrideId)
        {
            bool pastMine = false;

            foreach (OverrideStackFrame o in mOverrideStack)
            {
                if (o.id == overrideId)
                {
                    pastMine = true;
                }
                else if (o.Active && pastMine)
                {
                    return(o);
                }
            }
            // Create a frame representing the non-override state (gameplay)
            OverrideStackFrame ovr = new OverrideStackFrame();

            ovr.camera = TopCameraFromPriorityQueue();
            ovr.blend  = mActiveBlend;
            return(ovr);
        }
        private void ProcessActiveCamera(float deltaTime)
        {
            // This condition should never occur, but let's be defensive
            if (!isActiveAndEnabled)
            {
                mActiveCameraPreviousFrame   = null;
                mOutgoingCameraPreviousFrame = null;
                mPreviousFrameWasOverride    = false;
                return;
            }

            //UnityEngine.Profiling.Profiler.BeginSample("CinemachineBrain.ProcessActiveCamera");

            OverrideStackFrame activeOverride = GetActiveOverride();
            ICinemachineCamera activeCamera   = ActiveVirtualCamera;

            if (activeCamera == null)
            {
                mOutgoingCameraPreviousFrame = null;
            }
            else
            {
                // If there is an override, we kill the in-game blend
                if (activeOverride != null)
                {
                    mActiveBlend = null;
                }
                CinemachineBlend activeBlend = ActiveBlend;

                // Check for unexpected deletion of the cached mActiveCameraPreviousFrame
                if (mActiveCameraPreviousFrame != null && mActiveCameraPreviousFrame.VirtualCameraGameObject == null)
                {
                    mActiveCameraPreviousFrame = null;
                }

                // Are we transitioning cameras?
                if (mActiveCameraPreviousFrame != activeCamera)
                {
                    // Do we need to create a game-play blend?
                    if (mActiveCameraPreviousFrame != null &&
                        !mPreviousFrameWasOverride &&
                        activeOverride == null &&
                        deltaTime >= 0)
                    {
                        // Create a blend (will be null if a cut)
                        activeBlend = CreateBlend(
                            mActiveCameraPreviousFrame, activeCamera,
                            LookupBlend(mActiveCameraPreviousFrame, activeCamera),
                            mActiveBlend, deltaTime);
                    }
                    // Need this check because Timeline override sometimes inverts outgoing and incoming
                    if (activeCamera != mOutgoingCameraPreviousFrame)
                    {
                        // Notify incoming camera of transition
                        activeCamera.OnTransitionFromCamera(mActiveCameraPreviousFrame, DefaultWorldUp, deltaTime);

                        // If the incoming camera is disabled, then we must assume
                        // that it has not been updated properly
                        if (!activeCamera.VirtualCameraGameObject.activeInHierarchy &&
                            (activeBlend == null || !activeBlend.Uses(activeCamera)))
                        {
                            activeCamera.InternalUpdateCameraState(DefaultWorldUp, -1);
                        }
                        if (m_CameraActivatedEvent != null)
                        {
                            m_CameraActivatedEvent.Invoke(activeCamera);
                        }
                    }
                    // If we're cutting without a blend, or no active cameras
                    // were active last frame, send an event
                    if (activeBlend == null ||
                        (activeBlend.CamA != mActiveCameraPreviousFrame &&
                         activeBlend.CamB != mActiveCameraPreviousFrame &&
                         activeBlend.CamA != mOutgoingCameraPreviousFrame &&
                         activeBlend.CamB != mOutgoingCameraPreviousFrame))
                    {
                        if (m_CameraCutEvent != null)
                        {
                            m_CameraCutEvent.Invoke(this);
                        }
                    }
                }

                // Advance the current blend (if any)
                if (activeBlend != null)
                {
                    if (activeOverride == null)
                    {
                        activeBlend.TimeInBlend += (deltaTime >= 0)
                            ? deltaTime : activeBlend.Duration;
                    }
                    if (activeBlend.IsComplete)
                    {
                        activeBlend = null;
                    }
                }
                if (activeOverride == null)
                {
                    mActiveBlend = activeBlend;
                }

                // Apply the result to the Unity camera
                CameraState state = activeCamera.State;
                if (activeBlend != null)
                {
                    state = activeBlend.State;
                }
                PushStateToUnityCamera(state, activeCamera);

                mOutgoingCameraPreviousFrame = null;
                if (activeBlend != null)
                {
                    mOutgoingCameraPreviousFrame = activeBlend.CamB;
                }
            }

            mActiveCameraPreviousFrame = activeCamera;
            mPreviousFrameWasOverride  = activeOverride != null;

            if (mPreviousFrameWasOverride)
            {
                // Hack: Because we don't know whether blending in or out... grrr...
                if (activeOverride.blend != null)
                {
                    if (activeOverride.blend.BlendWeight < 0.5f)
                    {
                        mActiveCameraPreviousFrame   = activeOverride.blend.CamA;
                        mOutgoingCameraPreviousFrame = activeOverride.blend.CamB;
                    }
                    else
                    {
                        mActiveCameraPreviousFrame   = activeOverride.blend.CamB;
                        mOutgoingCameraPreviousFrame = activeOverride.blend.CamA;
                    }
                }
            }
            //UnityEngine.Profiling.Profiler.EndSample();
        }
        /// <summary>
        /// This API is specifically for Timeline.  Do not use it.
        /// Override the current camera and current blend.  This setting will trump
        /// any in-game logic that sets virtual camera priorities and Enabled states.
        /// This is the main API for the timeline.
        /// </summary>
        /// <param name="overrideId">Id to represent a specific client.  An internal
        /// stack is maintained, with the most recent non-empty override taking precenence.
        /// This id must be > 0.  If you pass -1, a new id will be created, and returned.
        /// Use that id for subsequent calls.  Don't forget to
        /// call ReleaseCameraOverride after all overriding is finished, to
        /// free the OverideStack resources.</param>
        /// <param name="camA"> The camera to set, corresponding to weight=0</param>
        /// <param name="camB"> The camera to set, corresponding to weight=1</param>
        /// <param name="weightB">The blend weight.  0=camA, 1=camB</param>
        /// <param name="deltaTime">override for deltaTime.  Should be Time.FixedDelta for
        /// time-based calculations to be included, -1 otherwise</param>
        /// <returns>The oiverride ID.  Don't forget to call ReleaseCameraOverride
        /// after all overriding is finished, to free the OverideStack resources.</returns>
        internal int SetCameraOverride(
            int overrideId,
            ICinemachineCamera camA, ICinemachineCamera camB,
            float weightB, float deltaTime)
        {
            //UnityEngine.Profiling.Profiler.BeginSample("CinemachineBrain.SetCameraOverride");
            if (overrideId < 0)
            {
                overrideId = mNextOverrideId++;
            }

            OverrideStackFrame ovr = GetOverrideFrame(overrideId);

            ovr.camera         = null;
            ovr.deltaTime      = deltaTime;
            ovr.timeOfOverride = Time.realtimeSinceStartup;
            if (camA != null || camB != null)
            {
                if (weightB <= UnityVectorExtensions.Epsilon)
                {
                    ovr.blend = null;
                    if (camA != null)
                    {
                        ovr.camera = camA; // no blend
                    }
                }
                else if (weightB >= (1f - UnityVectorExtensions.Epsilon))
                {
                    ovr.blend = null;
                    if (camB != null)
                    {
                        ovr.camera = camB; // no blend
                    }
                }
                else
                {
                    // We have a blend.  If one of the supplied cameras is null,
                    // we use the current active virtual camera (blending in/out of game logic),
                    // If we do have a null camera, make sure it's the 'from' camera.
                    // Timeline does not distinguish between from and to cams, but we do.
                    if (camB == null)
                    {
                        // Swap them
                        ICinemachineCamera c = camB;
                        camB    = camA;
                        camA    = c;
                        weightB = 1f - weightB;
                    }

                    // Are we blending with something in progress?
                    if (camA == null)
                    {
                        OverrideStackFrame frame = GetNextActiveFrame(overrideId);
                        if (frame.blend != null)
                        {
                            camA = new BlendSourceVirtualCamera(frame.blend, deltaTime);
                        }
                        else
                        {
                            camA = frame.camera != null ? frame.camera : camB;
                        }
                    }

                    // Create the override blend
                    if (ovr.blend == null)
                    {
                        ovr.blend = new CinemachineBlend(
                            camA, camB, AnimationCurve.Linear(0, 0, 1, 1), 1, weightB);
                    }
                    ovr.blend.CamA        = camA;
                    ovr.blend.CamB        = camB;
                    ovr.blend.TimeInBlend = weightB;
                    ovr.camera            = camB;
                }
            }
            //UnityEngine.Profiling.Profiler.EndSample();
            return(overrideId);
        }
Exemple #9
0
        private void ProcessActiveCamera(float deltaTime)
        {
            // This condition should never occur, but let's be defensive
            if ((OutputCamera == null) || !OutputCamera.isActiveAndEnabled)
            {
                mActiveCameraPreviousFrame   = null;
                mOutgoingCameraPreviousFrame = null;
                mPreviousFrameWasOverride    = false;
                return;
            }

            //UnityEngine.Profiling.Profiler.BeginSample("Cinemachine");
            OverrideStackFrame activeOverride = GetActiveOverride();
            ICinemachineCamera activeCamera   = ActiveVirtualCamera;

            if (activeCamera == null)
            {
                mOutgoingCameraPreviousFrame = null;
            }
            else
            {
                // If there is an override, we kill the in-game blend
                if (activeOverride != null)
                {
                    mActiveBlend = null;
                }
                CinemachineBlend activeBlend = ActiveBlend;

                // Are we transitioning cameras?
                if (mActiveCameraPreviousFrame != activeCamera)
                {
                    // Do we need to create a game-play blend?
                    if (mActiveCameraPreviousFrame != null &&
                        !mPreviousFrameWasOverride &&
                        activeOverride == null &&
                        deltaTime > 0)
                    {
                        // Create a blend (will be null if a cut)
                        activeBlend = CreateBlend(
                            mActiveCameraPreviousFrame, activeCamera,
                            LookupBlendCurve(mActiveCameraPreviousFrame, activeCamera),
                            mActiveBlend);
                    }
                    // Need this check because Timeline override sometimes inverts outgoing and incoming
                    if (activeCamera != mOutgoingCameraPreviousFrame)
                    {
                        // Notify incoming camera of transition
                        activeCamera.OnTransitionFromCamera(mActiveCameraPreviousFrame);
                        if (m_CameraActivatedEvent != null)
                        {
                            m_CameraActivatedEvent.Invoke();
                        }
                    }
                    // If we're cutting without a blend, or no active cameras
                    // were active last frame, send an event
                    if (activeBlend == null ||
                        (activeBlend.CamA != mActiveCameraPreviousFrame &&
                         activeBlend.CamB != mActiveCameraPreviousFrame &&
                         activeBlend.CamA != mOutgoingCameraPreviousFrame &&
                         activeBlend.CamB != mOutgoingCameraPreviousFrame))
                    {
                        if (m_CameraCutEvent != null)
                        {
                            m_CameraCutEvent.Invoke();
                        }
                    }
                }

                // Advance the current blend (if any)
                if (activeBlend != null)
                {
                    if (activeOverride == null)
                    {
                        activeBlend.TimeInBlend += (deltaTime > 0)
                            ? deltaTime : activeBlend.Duration;
                    }
                    if (activeBlend.IsComplete)
                    {
                        activeBlend = null;
                    }
                }
                if (activeOverride == null)
                {
                    mActiveBlend = activeBlend;
                }

                // Apply the result to the Unity camera
                if (!IsSuspended)
                {
                    CameraState state = activeCamera.State;
                    if (activeBlend != null)
                    {
                        state = activeBlend.State;
                    }
                    PushStateToUnityCamera(state, OutputCamera, activeCamera);
                }

                if (CoreDebuggerPostFrameUpdateCallback != null)
                {
                    CoreDebuggerPostFrameUpdateCallback();
                }

                mOutgoingCameraPreviousFrame = null;
                if (activeBlend != null)
                {
                    mOutgoingCameraPreviousFrame = activeBlend.CamB;
                }
            }
            mActiveCameraPreviousFrame = activeCamera;
            mPreviousFrameWasOverride  = (activeOverride != null);
            if (mPreviousFrameWasOverride)
            {
                // Because we don't know whether blending in or out... grrr...
                if (activeOverride.blend != null)
                {
                    if (activeOverride.blend.BlendWeight < 0.5f)
                    {
                        mActiveCameraPreviousFrame   = activeOverride.blend.CamA;
                        mOutgoingCameraPreviousFrame = activeOverride.blend.CamB;
                    }
                    else
                    {
                        mActiveCameraPreviousFrame   = activeOverride.blend.CamB;
                        mOutgoingCameraPreviousFrame = activeOverride.blend.CamA;
                    }
                }
            }
            //UnityEngine.Profiling.Profiler.EndSample();
        }