internal static void DrawVirtualCameraBaseGizmos(CinemachineVirtualCameraBase vcam, GizmoType selectionType)
        {
            // Don't draw gizmos on hidden stuff
            if ((vcam.VirtualCameraGameObject.hideFlags & (HideFlags.HideInHierarchy | HideFlags.HideInInspector)) != 0)
            {
                return;
            }

            if (vcam.ParentCamera != null && (selectionType & GizmoType.Active) == 0)
            {
                return;
            }

            CameraState state = vcam.State;

            Gizmos.DrawIcon(state.FinalPosition, kGizmoFileName, true);

            DrawCameraFrustumGizmo(
                CinemachineCore.Instance.FindPotentialTargetBrain(vcam),
                state.Lens,
                Matrix4x4.TRS(
                    state.FinalPosition,
                    UnityQuaternionExtensions.Normalized(state.FinalOrientation), Vector3.one),
                CinemachineCore.Instance.IsLive(vcam)
                    ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour
                    : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour);
        }
Beispiel #2
0
        /// <summary>Applies the composer rules and orients the camera accordingly</summary>
        /// <param name="curState">The current camera state</param>
        /// <param name="statePrevFrame">The camera state on the previous frame (unused)</param>
        /// <param name="deltaTime">Used for calculating damping.  If less than
        /// or equal to zero, then target will snap to the center of the dead zone.</param>
        /// <returns>curState with RawOrientation applied</returns>
        public virtual CameraState MutateCameraState(
            CameraState curState, CameraState statePrevFrame, float deltaTime)
        {
            if (!IsValid || !curState.HasLookAt)
            {
                return(curState);
            }

            CameraState newState = curState;

            newState.ReferenceLookAt = GetTrackedPoint(ref newState);
            float targetDistance = (newState.ReferenceLookAt - newState.CorrectedPosition).magnitude;

            if (targetDistance < UnityVectorExtensions.Epsilon)
            {
                return(newState);  // navel-gazing, get outa here
            }
            // Calculate effective fov - fake it for ortho based on target distance
            float fov, fovH;

            if (newState.Lens.Orthographic)
            {
                fov  = Mathf.Rad2Deg * 2 * Mathf.Atan(newState.Lens.OrthographicSize / targetDistance);
                fovH = Mathf.Rad2Deg * 2 * Mathf.Atan(newState.Lens.Aspect * newState.Lens.OrthographicSize / targetDistance);
            }
            else
            {
                fov = newState.Lens.FieldOfView;
                double radHFOV = 2 * Math.Atan(Math.Tan(fov * Mathf.Deg2Rad / 2) * newState.Lens.Aspect);
                fovH = (float)(Mathf.Rad2Deg * radHFOV);
            }

            Rect softGuideFOV = ScreenToFOV(SoftGuideRect, fov, fovH, newState.Lens.Aspect);

            if (deltaTime <= 0)
            {
                // No damping, just snap to central bounds, skipping the soft zone
                Rect rect = new Rect(softGuideFOV.center, Vector2.zero); // Force to center
                newState.RawOrientation = PlaceWithinScreenBounds(
                    ref newState, rect, newState.RawOrientation, fov, fovH, 0);
            }
            else
            {
                // Start with previous frame's orientation (but with current up)
                Quaternion rigOrientation = Quaternion.LookRotation(
                    statePrevFrame.RawOrientation * Vector3.forward, newState.ReferenceUp);

                // First force the previous rotation into the hard bounds, no damping
                Rect hardGuideFOV = ScreenToFOV(HardGuideRect, fov, fovH, newState.Lens.Aspect);
                newState.RawOrientation = PlaceWithinScreenBounds(
                    ref newState, hardGuideFOV, rigOrientation, fov, fovH, 0);

                // Now move it through the soft zone, with damping
                newState.RawOrientation = PlaceWithinScreenBounds(
                    ref newState, softGuideFOV, newState.RawOrientation, fov, fovH, deltaTime);
            }
            newState.RawOrientation = UnityQuaternionExtensions.Normalized(newState.RawOrientation);
            return(newState);
        }
        /// <summary>Applies the composer rules and orients the camera accordingly</summary>
        /// <param name="curState">The current camera state</param>
        /// <param name="statePrevFrame">The camera state on the previous frame (unused)</param>
        /// <param name="deltaTime">Used for calculating damping.  If less than
        /// or equal to zero, then target will snap to the center of the dead zone.</param>
        /// <returns>curState with RawOrientation applied</returns>
        public CameraState MutateCameraState(
            CameraState curState, CameraState statePrevFrame, float deltaTime)
        {
            if (!IsValid || !curState.HasLookAt)
            {
                return(curState);
            }

            CameraState newState = curState;

            newState.ReferenceLookAt = GetTrackedPoint(newState.ReferenceLookAt);
            if ((newState.ReferenceLookAt - newState.CorrectedPosition).AlmostZero())
            {
                return(newState);  // navel-gazing, get outa here
            }
            CinemachineBrain brain  = CinemachineCore.Instance.FindPotentialTargetBrain(VirtualCamera);
            float            aspect = brain != null ? brain.OutputCamera.aspect : 1;
            float            fovH   = CameraUtilities.CalculateHorizontalFOV(newState.Lens.FieldOfView, aspect);

            Rect softGuideFOV = ScreenToFOV(SoftGuideRect, newState.Lens.FieldOfView, fovH, aspect);

            if (deltaTime <= 0)
            {
                // No damping, just snap to central bounds, skipping the soft zone
                Rect rect = new Rect(softGuideFOV.center, Vector2.zero); // Force to center
                newState.RawOrientation = PlaceWithinScreenBounds(
                    ref newState, rect, newState.RawOrientation, fovH, 0);
            }
            else
            {
                // Start with previous frame's orientation (but with current up)
                Quaternion rigOrientation = Quaternion.LookRotation(
                    statePrevFrame.RawOrientation * Vector3.forward, newState.ReferenceUp);

                // First force the previous rotation into the hard bounds, no damping
                Rect hardGuideFOV = ScreenToFOV(HardGuideRect, newState.Lens.FieldOfView, fovH, aspect);
                newState.RawOrientation = PlaceWithinScreenBounds(
                    ref newState, hardGuideFOV, rigOrientation, fovH, 0);

                // Now move it through the soft zone, with damping
                newState.RawOrientation = PlaceWithinScreenBounds(
                    ref newState, softGuideFOV, newState.RawOrientation, fovH, deltaTime);
            }
            newState.RawOrientation = UnityQuaternionExtensions.Normalized(newState.RawOrientation);
            return(newState);
        }
        internal static void DrawVirtualCameraGizmos(CinemachineVirtualCameraBase vcam, GizmoType selectionType)
        {
            // Don't draw gizmos on hidden stuff
            if ((vcam.VirtualCameraGameObject.hideFlags & (HideFlags.HideInHierarchy | HideFlags.HideInInspector)) != 0)
            {
                return;
            }

            Color originalGizmoColour = Gizmos.color;

            Gizmos.color = CinemachineCore.Instance.IsLive(vcam)
                ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour
                : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour;

            CameraState state = vcam.State;

            Gizmos.DrawIcon(state.FinalPosition, "Cinemachine/cm_logo_lg.png", true);

            float            aspect = 1;
            CinemachineBrain brain  = CinemachineCore.Instance.FindPotentialTargetBrain(vcam);

            if (brain != null)
            {
                aspect = brain.OutputCamera.aspect;
            }

            Matrix4x4 originalMatrix = Gizmos.matrix;

            Gizmos.matrix = Matrix4x4.TRS(
                state.FinalPosition,
                UnityQuaternionExtensions.Normalized(state.FinalOrientation), Vector3.one);
            Gizmos.DrawFrustum(
                Vector3.zero, state.Lens.FieldOfView,
                state.Lens.FarClipPlane, state.Lens.NearClipPlane, aspect);
            Gizmos.matrix = originalMatrix;
            Gizmos.color  = originalGizmoColour;
        }
Beispiel #5
0
        /// <summary>Intelligently blend the contents of two states.</summary>
        /// <param name="stateA">The first state, corresponding to t=0</param>
        /// <param name="stateB">The second state, corresponding to t=1</param>
        /// <param name="t">How much to interpolate.  Internally clamped to 0..1</param>
        /// <returns>Linearly interpolated CameraState</returns>
        public static CameraState Lerp(CameraState stateA, CameraState stateB, float t)
        {
            t = Mathf.Clamp01(t);
            float adjustedT = t;

            CameraState state = new CameraState();

            // Combine the blend hints intelligently
            if (((stateA.BlendHint & stateB.BlendHint) & BlendHintValue.NoPosition) != 0)
            {
                state.BlendHint |= BlendHintValue.NoPosition;
            }
            if (((stateA.BlendHint & stateB.BlendHint) & BlendHintValue.NoOrientation) != 0)
            {
                state.BlendHint |= BlendHintValue.NoOrientation;
            }
            if (((stateA.BlendHint & stateB.BlendHint) & BlendHintValue.NoLens) != 0)
            {
                state.BlendHint |= BlendHintValue.NoLens;
            }
            if (((stateA.BlendHint | stateB.BlendHint) & BlendHintValue.SphericalPositionBlend) != 0)
            {
                state.BlendHint |= BlendHintValue.SphericalPositionBlend;
            }
            if (((stateA.BlendHint | stateB.BlendHint) & BlendHintValue.CylindricalPositionBlend) != 0)
            {
                state.BlendHint |= BlendHintValue.CylindricalPositionBlend;
            }

            if (((stateA.BlendHint | stateB.BlendHint) & BlendHintValue.NoLens) == 0)
            {
                state.Lens = LensSettings.Lerp(stateA.Lens, stateB.Lens, t);
            }
            else if (((stateA.BlendHint & stateB.BlendHint) & BlendHintValue.NoLens) == 0)
            {
                if ((stateA.BlendHint & BlendHintValue.NoLens) != 0)
                {
                    state.Lens = stateB.Lens;
                }
                else
                {
                    state.Lens = stateA.Lens;
                }
            }
            state.ReferenceUp = Vector3.Slerp(stateA.ReferenceUp, stateB.ReferenceUp, t);
            state.ShotQuality = Mathf.Lerp(stateA.ShotQuality, stateB.ShotQuality, t);

            state.PositionCorrection = ApplyPosBlendHint(
                stateA.PositionCorrection, stateA.BlendHint,
                stateB.PositionCorrection, stateB.BlendHint,
                state.PositionCorrection,
                Vector3.Lerp(stateA.PositionCorrection, stateB.PositionCorrection, t));

            state.OrientationCorrection = ApplyRotBlendHint(
                stateA.OrientationCorrection, stateA.BlendHint,
                stateB.OrientationCorrection, stateB.BlendHint,
                state.OrientationCorrection,
                Quaternion.Slerp(stateA.OrientationCorrection, stateB.OrientationCorrection, t));

            // LookAt target
            if (!stateA.HasLookAt || !stateB.HasLookAt)
            {
                state.ReferenceLookAt = kNoPoint;
            }
            else
            {
                // Re-interpolate FOV to preserve target composition, if possible
                float fovA = stateA.Lens.FieldOfView;
                float fovB = stateB.Lens.FieldOfView;
                if (((stateA.BlendHint | stateB.BlendHint) & BlendHintValue.NoLens) == 0 &&
                    !state.Lens.Orthographic && !Mathf.Approximately(fovA, fovB))
                {
                    LensSettings lens = state.Lens;
                    lens.FieldOfView = InterpolateFOV(
                        fovA, fovB,
                        Mathf.Max((stateA.ReferenceLookAt - stateA.CorrectedPosition).magnitude, stateA.Lens.NearClipPlane),
                        Mathf.Max((stateB.ReferenceLookAt - stateB.CorrectedPosition).magnitude, stateB.Lens.NearClipPlane), t);
                    state.Lens = lens;

                    // Make sure we preserve the screen composition through FOV changes
                    adjustedT = Mathf.Abs((lens.FieldOfView - fovA) / (fovB - fovA));
                }

                // Linear interpolation of lookAt target point
                state.ReferenceLookAt = Vector3.Lerp(
                    stateA.ReferenceLookAt, stateB.ReferenceLookAt, adjustedT);
            }

            // Raw position
            state.RawPosition = ApplyPosBlendHint(
                stateA.RawPosition, stateA.BlendHint,
                stateB.RawPosition, stateB.BlendHint,
                state.RawPosition, state.InterpolatePosition(
                    stateA.RawPosition, stateA.ReferenceLookAt,
                    stateB.RawPosition, stateB.ReferenceLookAt,
                    t));


            // Interpolate the LookAt in Screen Space if requested
            if (state.HasLookAt &&
                ((stateA.BlendHint | stateB.BlendHint) & BlendHintValue.RadialAimBlend) != 0)
            {
                state.ReferenceLookAt = state.RawPosition + Vector3.Slerp(
                    stateA.ReferenceLookAt - state.RawPosition,
                    stateB.ReferenceLookAt - state.RawPosition, adjustedT);
            }

            // Clever orientation interpolation
            Quaternion newOrient = state.RawOrientation;

            if (((stateA.BlendHint | stateB.BlendHint) & BlendHintValue.NoOrientation) == 0)
            {
                Vector3 dirTarget = Vector3.zero;
                if (state.HasLookAt)//&& ((stateA.BlendHint | stateB.BlendHint) & BlendHintValue.RadialAimBlend) == 0)
                {
                    // If orientations are different, use LookAt to blend them
                    float angle = Quaternion.Angle(stateA.RawOrientation, stateB.RawOrientation);
                    if (angle > UnityVectorExtensions.Epsilon)
                    {
                        dirTarget = state.ReferenceLookAt - state.CorrectedPosition;
                    }
                }
                if (dirTarget.AlmostZero() ||
                    ((stateA.BlendHint | stateB.BlendHint) & BlendHintValue.IgnoreLookAtTarget) != 0)
                {
                    // Don't know what we're looking at - can only slerp
                    newOrient = UnityQuaternionExtensions.SlerpWithReferenceUp(
                        stateA.RawOrientation, stateB.RawOrientation, t, state.ReferenceUp);
                }
                else
                {
                    // Rotate while preserving our lookAt target
                    dirTarget = dirTarget.normalized;
                    if ((dirTarget - state.ReferenceUp).AlmostZero() ||
                        (dirTarget + state.ReferenceUp).AlmostZero())
                    {
                        // Looking up or down at the pole
                        newOrient = UnityQuaternionExtensions.SlerpWithReferenceUp(
                            stateA.RawOrientation, stateB.RawOrientation, t, state.ReferenceUp);
                    }
                    else
                    {
                        // Put the target in the center
                        newOrient = Quaternion.LookRotation(dirTarget, state.ReferenceUp);

                        // Blend the desired offsets from center
                        Vector2 deltaA = -stateA.RawOrientation.GetCameraRotationToTarget(
                            stateA.ReferenceLookAt - stateA.CorrectedPosition, stateA.ReferenceUp);
                        Vector2 deltaB = -stateB.RawOrientation.GetCameraRotationToTarget(
                            stateB.ReferenceLookAt - stateB.CorrectedPosition, stateB.ReferenceUp);
                        newOrient = newOrient.ApplyCameraRotation(
                            Vector2.Lerp(deltaA, deltaB, adjustedT), state.ReferenceUp);
                    }
                }
            }
            state.RawOrientation = ApplyRotBlendHint(
                stateA.RawOrientation, stateA.BlendHint,
                stateB.RawOrientation, stateB.BlendHint,
                state.RawOrientation, newOrient);

            // Accumulate the custom blendables and apply the weights
            for (int i = 0; i < stateA.NumCustomBlendables; ++i)
            {
                CustomBlendable b = stateA.GetCustomBlendable(i);
                b.m_Weight *= (1 - t);
                if (b.m_Weight > UnityVectorExtensions.Epsilon)
                {
                    state.AddCustomBlendable(b);
                }
            }
            for (int i = 0; i < stateB.NumCustomBlendables; ++i)
            {
                CustomBlendable b = stateB.GetCustomBlendable(i);
                b.m_Weight *= t;
                if (b.m_Weight > UnityVectorExtensions.Epsilon)
                {
                    state.AddCustomBlendable(b);
                }
            }
            return(state);
        }
Beispiel #6
0
        /// <summary>Applies the composer rules and orients the camera accordingly</summary>
        /// <param name="curState">The current camera state</param>
        /// <param name="deltaTime">Used for calculating damping.  If less than
        /// zero, then target will snap to the center of the dead zone.</param>
        public override void MutateCameraState(ref CameraState curState, float deltaTime)
        {
            if (!IsValid || !curState.HasLookAt)
            {
                return;
            }

            // Correct the tracked point in the event that it's behind the camera
            // while the real target is in front
            if (!(TrackedPoint - curState.ReferenceLookAt).AlmostZero())
            {
                Vector3 mid       = Vector3.Lerp(curState.CorrectedPosition, curState.ReferenceLookAt, 0.5f);
                Vector3 toLookAt  = curState.ReferenceLookAt - mid;
                Vector3 toTracked = TrackedPoint - mid;
                if (Vector3.Dot(toLookAt, toTracked) < 0)
                {
                    float t = Vector3.Distance(curState.ReferenceLookAt, mid)
                              / Vector3.Distance(curState.ReferenceLookAt, TrackedPoint);
                    TrackedPoint = Vector3.Lerp(curState.ReferenceLookAt, TrackedPoint, t);
                }
            }

            float targetDistance = (TrackedPoint - curState.CorrectedPosition).magnitude;

            if (targetDistance < Epsilon)
            {
                if (deltaTime >= 0 && VirtualCamera.PreviousStateIsValid)
                {
                    curState.RawOrientation = m_CameraOrientationPrevFrame;
                }
                return;  // navel-gazing, get outa here
            }

            // Expensive FOV calculations
            mCache.UpdateCache(curState.Lens, SoftGuideRect, HardGuideRect, targetDistance);

            Quaternion rigOrientation = curState.RawOrientation;

            if (deltaTime < 0 || !VirtualCamera.PreviousStateIsValid)
            {
                // No damping, just snap to central bounds, skipping the soft zone
                rigOrientation = Quaternion.LookRotation(
                    rigOrientation * Vector3.forward, curState.ReferenceUp);
                Rect rect = mCache.mFovSoftGuideRect;
                if (m_CenterOnActivate)
                {
                    rect = new Rect(rect.center, Vector2.zero); // Force to center
                }
                RotateToScreenBounds(
                    ref curState, rect, curState.ReferenceLookAt,
                    ref rigOrientation, mCache.mFov, mCache.mFovH, -1);
            }
            else
            {
                // Start with previous frame's orientation (but with current up)
                Vector3 dir = m_LookAtPrevFrame - m_CameraPosPrevFrame;
                if (dir.AlmostZero())
                {
                    rigOrientation = Quaternion.LookRotation(
                        m_CameraOrientationPrevFrame * Vector3.forward, curState.ReferenceUp);
                }
                else
                {
                    dir            = Quaternion.Euler(curState.PositionDampingBypass) * dir;
                    rigOrientation = Quaternion.LookRotation(dir, curState.ReferenceUp);
                    rigOrientation = rigOrientation.ApplyCameraRotation(
                        -m_ScreenOffsetPrevFrame, curState.ReferenceUp);
                }

                // Move target through the soft zone, with damping
                RotateToScreenBounds(
                    ref curState, mCache.mFovSoftGuideRect, TrackedPoint,
                    ref rigOrientation, mCache.mFov, mCache.mFovH, deltaTime);

                // Force the actual target (not the lookahead one) into the hard bounds, no damping
                if (deltaTime < 0 || VirtualCamera.LookAtTargetAttachment > 1 - Epsilon)
                {
                    RotateToScreenBounds(
                        ref curState, mCache.mFovHardGuideRect, curState.ReferenceLookAt,
                        ref rigOrientation, mCache.mFov, mCache.mFovH, -1);
                }
            }

            m_CameraPosPrevFrame         = curState.CorrectedPosition;
            m_LookAtPrevFrame            = TrackedPoint;
            m_CameraOrientationPrevFrame = UnityQuaternionExtensions.Normalized(rigOrientation);
            m_ScreenOffsetPrevFrame      = m_CameraOrientationPrevFrame.GetCameraRotationToTarget(
                m_LookAtPrevFrame - curState.CorrectedPosition, curState.ReferenceUp);

            curState.RawOrientation = m_CameraOrientationPrevFrame;
        }
        /// <summary>Applies the composer rules and orients the camera accordingly</summary>
        /// <param name="curState">The current camera state</param>
        /// <param name="deltaTime">Used for calculating damping.  If less than
        /// zero, then target will snap to the center of the dead zone.</param>
        public override void MutateCameraState(ref CameraState curState, float deltaTime)
        {
            // Initialize the state for previous frame if appropriate
            if (deltaTime < 0)
            {
                m_Predictor.Reset();
            }

            if (!IsValid || !curState.HasLookAt)
            {
                return;
            }

            float targetDistance = (TrackedPoint - curState.CorrectedPosition).magnitude;

            if (targetDistance < Epsilon)
            {
                if (deltaTime >= 0)
                {
                    curState.RawOrientation = m_CameraOrientationPrevFrame;
                }
                return;  // navel-gazing, get outa here
            }

            float fov, fovH;

            if (curState.Lens.Orthographic)
            {
                // Calculate effective fov - fake it for ortho based on target distance
                fov  = Mathf.Rad2Deg * 2 * Mathf.Atan(curState.Lens.OrthographicSize / targetDistance);
                fovH = Mathf.Rad2Deg * 2 * Mathf.Atan(
                    curState.Lens.Aspect * curState.Lens.OrthographicSize / targetDistance);
            }
            else
            {
                fov = curState.Lens.FieldOfView;
                double radHFOV = 2 * Math.Atan(Math.Tan(fov * Mathf.Deg2Rad / 2) * curState.Lens.Aspect);
                fovH = (float)(Mathf.Rad2Deg * radHFOV);
            }

            Quaternion rigOrientation = curState.RawOrientation;
            Rect       softGuideFOV   = ScreenToFOV(SoftGuideRect, fov, fovH, curState.Lens.Aspect);

            if (deltaTime < 0)
            {
                // No damping, just snap to central bounds, skipping the soft zone
                Rect rect = new Rect(softGuideFOV.center, Vector2.zero); // Force to center
                RotateToScreenBounds(ref curState, rect, ref rigOrientation, fov, fovH, -1);
            }
            else
            {
                // Start with previous frame's orientation (but with current up)
                Vector3 dir = m_LookAtPrevFrame - (m_CameraPosPrevFrame + curState.PositionDampingBypass);
                if (dir.AlmostZero())
                {
                    rigOrientation = Quaternion.LookRotation(
                        m_CameraOrientationPrevFrame * Vector3.forward, curState.ReferenceUp);
                }
                else
                {
                    rigOrientation = Quaternion.LookRotation(dir, curState.ReferenceUp);
                    rigOrientation = rigOrientation.ApplyCameraRotation(
                        -m_ScreenOffsetPrevFrame, curState.ReferenceUp);
                }

                // First force the previous rotation into the hard bounds, no damping,
                // then Now move it through the soft zone, with damping
                Rect hardGuideFOV = ScreenToFOV(HardGuideRect, fov, fovH, curState.Lens.Aspect);
                if (!RotateToScreenBounds(ref curState, hardGuideFOV, ref rigOrientation, fov, fovH, -1))
                {
                    RotateToScreenBounds(ref curState, softGuideFOV, ref rigOrientation, fov, fovH, deltaTime);
                }
            }
            m_CameraPosPrevFrame         = curState.CorrectedPosition;
            m_LookAtPrevFrame            = TrackedPoint;
            m_CameraOrientationPrevFrame = UnityQuaternionExtensions.Normalized(rigOrientation);
            m_ScreenOffsetPrevFrame      = m_CameraOrientationPrevFrame.GetCameraRotationToTarget(
                m_LookAtPrevFrame - curState.CorrectedPosition, curState.ReferenceUp);

            curState.RawOrientation = m_CameraOrientationPrevFrame;
        }
Beispiel #8
0
        /// <summary>Intelligently blend the contents of two states.</summary>
        /// <param name="stateA">The first state, corresponding to t=0</param>
        /// <param name="stateB">The second state, corresponding to t=1</param>
        /// <param name="t">PHow much to interpolate.  Internally clamped to 0..1</param>
        /// <returns>Linearly interpolated CameraState</returns>
        public static CameraState Lerp(CameraState stateA, CameraState stateB, float t)
        {
            t = Mathf.Clamp01(t);
            float adjustedT = t;

            CameraState state = new CameraState();

            state.Lens        = LensSettings.Lerp(stateA.Lens, stateB.Lens, t);
            state.ReferenceUp = Vector3.Slerp(stateA.ReferenceUp, stateB.ReferenceUp, t);
            state.RawPosition = Vector3.Lerp(stateA.RawPosition, stateB.RawPosition, t);

            Vector3 dirTarget = Vector3.zero;

            if (!stateA.HasLookAt || !stateB.HasLookAt)
            {
                state.ReferenceLookAt = kNoPoint;   // can't interpolate if undefined
            }
            else
            {
                // Re-interpolate FOV to preserve target composition, if possible
                float fovA = stateA.Lens.FieldOfView;
                float fovB = stateB.Lens.FieldOfView;
                if (!Mathf.Approximately(fovA, fovB))
                {
                    LensSettings lens = state.Lens;
                    lens.FieldOfView = state.InterpolateFOV(
                        fovA, fovB,
                        (stateA.ReferenceLookAt - stateA.RawPosition).magnitude,
                        (stateB.ReferenceLookAt - stateB.RawPosition).magnitude, t);
                    state.Lens = lens;

                    // Make sure we preserve the screen composition through FOV changes
                    adjustedT = Mathf.Abs((lens.FieldOfView - fovA) / (fovB - fovA));
                }

                // Spherical linear interpolation about RawPosition
                state.ReferenceLookAt = state.RawPosition + Vector3.Slerp(
                    stateA.ReferenceLookAt - state.RawPosition,
                    stateB.ReferenceLookAt - state.RawPosition, adjustedT);
                dirTarget = state.ReferenceLookAt - state.RawPosition;
            }

            // Clever orientation interpolation
            if (dirTarget.AlmostZero())
            {
                // Don't know what we're looking at - can only slerp
                state.RawOrientation = UnityQuaternionExtensions.SlerpWithReferenceUp(
                    stateA.RawOrientation, stateB.RawOrientation, t, state.ReferenceUp);
            }
            else
            {
                // Rotate while preserving our lookAt target
                dirTarget = dirTarget.normalized;
                if ((dirTarget - state.ReferenceUp).AlmostZero() ||
                    (dirTarget + state.ReferenceUp).AlmostZero())
                {
                    // Looking up or down at the pole
                    state.RawOrientation = UnityQuaternionExtensions.SlerpWithReferenceUp(
                        stateA.RawOrientation, stateB.RawOrientation, t, state.ReferenceUp);
                }
                else
                {
                    // Put the target in the center
                    state.RawOrientation = Quaternion.LookRotation(dirTarget, state.ReferenceUp);

                    // Blend the desired offsets from center
                    Vector2 deltaA = -stateA.RawOrientation.GetCameraRotationToTarget(
                        stateA.ReferenceLookAt - stateA.RawPosition, stateA.ReferenceUp);
                    Vector2 deltaB = -stateB.RawOrientation.GetCameraRotationToTarget(
                        stateB.ReferenceLookAt - stateB.RawPosition, stateB.ReferenceUp);
                    state.RawOrientation = state.RawOrientation.ApplyCameraRotation(
                        Vector2.Lerp(deltaA, deltaB, adjustedT), state.ReferenceUp);
                }
            }

            state.ShotQuality        = Mathf.Lerp(stateA.ShotQuality, stateB.ShotQuality, t);
            state.PositionCorrection = Vector3.Lerp(
                stateA.PositionCorrection, stateB.PositionCorrection, t);
            // GML todo: is this right?  Can it introduce a roll?
            state.OrientationCorrection = Quaternion.Slerp(
                stateA.OrientationCorrection, stateB.OrientationCorrection, t);
            return(state);
        }
        /// <summary>Applies the composer rules and orients the camera accordingly</summary>
        /// <param name="curState">The current camera state</param>
        /// <param name="deltaTime">Used for calculating damping.  If less than
        /// zero, then target will snap to the center of the dead zone.</param>
        public override void MutateCameraState(ref CameraState curState, float deltaTime)
        {
            // Initialize the state for previous frame if appropriate
            if (deltaTime < 0)
            {
                m_Predictor.Reset();
            }

            if (!IsValid || !curState.HasLookAt)
            {
                return;
            }

            float targetDistance = (TrackedPoint - curState.CorrectedPosition).magnitude;

            if (targetDistance < Epsilon)
            {
                if (deltaTime >= 0)
                {
                    curState.RawOrientation = m_CameraOrientationPrevFrame;
                }
                return;  // navel-gazing, get outa here
            }

            // Expensive FOV calculations
            mCache.UpdateCache(curState.Lens, SoftGuideRect, HardGuideRect, targetDistance);

            Quaternion rigOrientation = curState.RawOrientation;

            if (deltaTime < 0)
            {
                // No damping, just snap to central bounds, skipping the soft zone
                Rect rect = mCache.mFovSoftGuideRect;
                if (m_CenterOnActivate)
                {
                    rect = new Rect(rect.center, Vector2.zero); // Force to center
                }
                RotateToScreenBounds(ref curState, rect, ref rigOrientation, mCache.mFov, mCache.mFovH, -1);
            }
            else
            {
                // Start with previous frame's orientation (but with current up)
                Vector3 dir = m_LookAtPrevFrame - (m_CameraPosPrevFrame + curState.PositionDampingBypass);
                if (dir.AlmostZero())
                {
                    rigOrientation = Quaternion.LookRotation(
                        m_CameraOrientationPrevFrame * Vector3.forward, curState.ReferenceUp);
                }
                else
                {
                    rigOrientation = Quaternion.LookRotation(dir, curState.ReferenceUp);
                    rigOrientation = rigOrientation.ApplyCameraRotation(
                        -m_ScreenOffsetPrevFrame, curState.ReferenceUp);
                }

                // First force the previous rotation into the hard bounds, no damping,
                // then Now move it through the soft zone, with damping
                if (!RotateToScreenBounds(ref curState, mCache.mFovHardGuideRect, ref rigOrientation, mCache.mFov, mCache.mFovH, -1))
                {
                    RotateToScreenBounds(ref curState, mCache.mFovSoftGuideRect, ref rigOrientation, mCache.mFov, mCache.mFovH, deltaTime);
                }
            }
            m_CameraPosPrevFrame         = curState.CorrectedPosition;
            m_LookAtPrevFrame            = TrackedPoint;
            m_CameraOrientationPrevFrame = UnityQuaternionExtensions.Normalized(rigOrientation);
            m_ScreenOffsetPrevFrame      = m_CameraOrientationPrevFrame.GetCameraRotationToTarget(
                m_LookAtPrevFrame - curState.CorrectedPosition, curState.ReferenceUp);

            curState.RawOrientation = m_CameraOrientationPrevFrame;
        }
Beispiel #10
0
        public static CameraState Lerp(CameraState stateA, CameraState stateB, float t)
        {
            t = Mathf.Clamp01(t);
            float       t2     = t;
            CameraState result = default(CameraState);

            result.Lens                  = LensSettings.Lerp(stateA.Lens, stateB.Lens, t);
            result.ReferenceUp           = Vector3.Slerp(stateA.ReferenceUp, stateB.ReferenceUp, t);
            result.RawPosition           = Vector3.Lerp(stateA.RawPosition, stateB.RawPosition, t);
            result.ShotQuality           = Mathf.Lerp(stateA.ShotQuality, stateB.ShotQuality, t);
            result.PositionCorrection    = Vector3.Lerp(stateA.PositionCorrection, stateB.PositionCorrection, t);
            result.OrientationCorrection = Quaternion.Slerp(stateA.OrientationCorrection, stateB.OrientationCorrection, t);
            Vector3 vector = Vector3.zero;

            if (!stateA.HasLookAt || !stateB.HasLookAt)
            {
                result.ReferenceLookAt = CameraState.kNoPoint;
            }
            else
            {
                float fieldOfView  = stateA.Lens.FieldOfView;
                float fieldOfView2 = stateB.Lens.FieldOfView;
                if (!result.Lens.Orthographic && !Mathf.Approximately(fieldOfView, fieldOfView2))
                {
                    LensSettings lens = result.Lens;
                    lens.FieldOfView = result.InterpolateFOV(fieldOfView, fieldOfView2, Mathf.Max((stateA.ReferenceLookAt - stateA.CorrectedPosition).magnitude, stateA.Lens.NearClipPlane), Mathf.Max((stateB.ReferenceLookAt - stateB.CorrectedPosition).magnitude, stateB.Lens.NearClipPlane), t);
                    result.Lens      = lens;
                    t2 = Mathf.Abs((lens.FieldOfView - fieldOfView) / (fieldOfView2 - fieldOfView));
                }
                result.ReferenceLookAt = Vector3.Lerp(stateA.ReferenceLookAt, stateB.ReferenceLookAt, t2);
                float num = Quaternion.Angle(stateA.RawOrientation, stateB.RawOrientation);
                if (num > 0.0001f)
                {
                    vector = result.ReferenceLookAt - result.CorrectedPosition;
                }
            }
            if (vector.AlmostZero())
            {
                result.RawOrientation = UnityQuaternionExtensions.SlerpWithReferenceUp(stateA.RawOrientation, stateB.RawOrientation, t, result.ReferenceUp);
            }
            else
            {
                vector = vector.normalized;
                if ((vector - result.ReferenceUp).AlmostZero() || (vector + result.ReferenceUp).AlmostZero())
                {
                    result.RawOrientation = UnityQuaternionExtensions.SlerpWithReferenceUp(stateA.RawOrientation, stateB.RawOrientation, t, result.ReferenceUp);
                }
                else
                {
                    result.RawOrientation = Quaternion.LookRotation(vector, result.ReferenceUp);
                    Vector2 a = -stateA.RawOrientation.GetCameraRotationToTarget(stateA.ReferenceLookAt - stateA.CorrectedPosition, stateA.ReferenceUp);
                    Vector2 b = -stateB.RawOrientation.GetCameraRotationToTarget(stateB.ReferenceLookAt - stateB.CorrectedPosition, stateB.ReferenceUp);
                    result.RawOrientation = result.RawOrientation.ApplyCameraRotation(Vector2.Lerp(a, b, t2), result.ReferenceUp);
                }
            }
            for (int i = 0; i < stateA.NumCustomBlendables; i++)
            {
                CameraState.CustomBlendable customBlendable = stateA.GetCustomBlendable(i);
                customBlendable.m_Weight *= 1f - t;
                if (customBlendable.m_Weight > 0.0001f)
                {
                    result.AddCustomBlendable(customBlendable);
                }
            }
            for (int j = 0; j < stateB.NumCustomBlendables; j++)
            {
                CameraState.CustomBlendable customBlendable2 = stateB.GetCustomBlendable(j);
                customBlendable2.m_Weight *= t;
                if (customBlendable2.m_Weight > 0.0001f)
                {
                    result.AddCustomBlendable(customBlendable2);
                }
            }
            return(result);
        }
Beispiel #11
0
        /// <summary>Intelligently blend the contents of two states.</summary>
        /// <param name="stateA">The first state, corresponding to t=0</param>
        /// <param name="stateB">The second state, corresponding to t=1</param>
        /// <param name="t">How much to interpolate.  Internally clamped to 0..1</param>
        /// <returns>Linearly interpolated CameraState</returns>
        public static CameraState Lerp(CameraState stateA, CameraState stateB, float t)
        {
            t = Mathf.Clamp01(t);
            float adjustedT = t;

            CameraState state = new CameraState();

            state.Lens        = LensSettings.Lerp(stateA.Lens, stateB.Lens, t);
            state.ReferenceUp = Vector3.Slerp(stateA.ReferenceUp, stateB.ReferenceUp, t);
            state.RawPosition = Vector3.Lerp(stateA.RawPosition, stateB.RawPosition, t);

            state.ShotQuality        = Mathf.Lerp(stateA.ShotQuality, stateB.ShotQuality, t);
            state.PositionCorrection = Vector3.Lerp(
                stateA.PositionCorrection, stateB.PositionCorrection, t);
            // GML todo: is this right?  Can it introduce a roll?
            state.OrientationCorrection = Quaternion.Slerp(
                stateA.OrientationCorrection, stateB.OrientationCorrection, t);

            Vector3 dirTarget = Vector3.zero;

            if (!stateA.HasLookAt || !stateB.HasLookAt)
            {
                state.ReferenceLookAt = kNoPoint;   // can't interpolate if undefined
            }
            else
            {
                // Re-interpolate FOV to preserve target composition, if possible
                float fovA = stateA.Lens.FieldOfView;
                float fovB = stateB.Lens.FieldOfView;
                if (!state.Lens.Orthographic && !Mathf.Approximately(fovA, fovB))
                {
                    LensSettings lens = state.Lens;
                    lens.FieldOfView = state.InterpolateFOV(
                        fovA, fovB,
                        Mathf.Max((stateA.ReferenceLookAt - stateA.CorrectedPosition).magnitude, stateA.Lens.NearClipPlane),
                        Mathf.Max((stateB.ReferenceLookAt - stateB.CorrectedPosition).magnitude, stateB.Lens.NearClipPlane), t);
                    state.Lens = lens;

                    // Make sure we preserve the screen composition through FOV changes
                    adjustedT = Mathf.Abs((lens.FieldOfView - fovA) / (fovB - fovA));
                }

                // Linear interpolation of lookAt target point
                state.ReferenceLookAt = Vector3.Lerp(
                    stateA.ReferenceLookAt, stateB.ReferenceLookAt, adjustedT);

                // If orientations are different, use LookAt to blend them
                float angle = Quaternion.Angle(stateA.RawOrientation, stateB.RawOrientation);
                if (angle > UnityVectorExtensions.Epsilon)
                {
                    dirTarget = state.ReferenceLookAt - state.CorrectedPosition;
                }
            }

            // Clever orientation interpolation
            if (dirTarget.AlmostZero())
            {
                // Don't know what we're looking at - can only slerp
                state.RawOrientation = UnityQuaternionExtensions.SlerpWithReferenceUp(
                    stateA.RawOrientation, stateB.RawOrientation, t, state.ReferenceUp);
            }
            else
            {
                // Rotate while preserving our lookAt target
                dirTarget = dirTarget.normalized;
                if ((dirTarget - state.ReferenceUp).AlmostZero() ||
                    (dirTarget + state.ReferenceUp).AlmostZero())
                {
                    // Looking up or down at the pole
                    state.RawOrientation = UnityQuaternionExtensions.SlerpWithReferenceUp(
                        stateA.RawOrientation, stateB.RawOrientation, t, state.ReferenceUp);
                }
                else
                {
                    // Put the target in the center
                    state.RawOrientation = Quaternion.LookRotation(dirTarget, state.ReferenceUp);

                    // Blend the desired offsets from center
                    Vector2 deltaA = -stateA.RawOrientation.GetCameraRotationToTarget(
                        stateA.ReferenceLookAt - stateA.CorrectedPosition, stateA.ReferenceUp);
                    Vector2 deltaB = -stateB.RawOrientation.GetCameraRotationToTarget(
                        stateB.ReferenceLookAt - stateB.CorrectedPosition, stateB.ReferenceUp);
                    state.RawOrientation = state.RawOrientation.ApplyCameraRotation(
                        Vector2.Lerp(deltaA, deltaB, adjustedT), state.ReferenceUp);
                }
            }

            // Accumulate the custom blendables and apply the weights
            for (int i = 0; i < stateA.NumCustomBlendables; ++i)
            {
                CustomBlendable b = stateA.GetCustomBlendable(i);
                b.m_Weight *= (1 - t);
                if (b.m_Weight > UnityVectorExtensions.Epsilon)
                {
                    state.AddCustomBlendable(b);
                }
            }
            for (int i = 0; i < stateB.NumCustomBlendables; ++i)
            {
                CustomBlendable b = stateB.GetCustomBlendable(i);
                b.m_Weight *= t;
                if (b.m_Weight > UnityVectorExtensions.Epsilon)
                {
                    state.AddCustomBlendable(b);
                }
            }
            return(state);
        }
        /// <summary>Applies the composer rules and orients the camera accordingly</summary>
        /// <param name="curState">The current camera state</param>
        /// <param name="deltaTime">Used for calculating damping.  If less than
        /// or equal to zero, then target will snap to the center of the dead zone.</param>
        public virtual void MutateCameraState(ref CameraState curState, float deltaTime)
        {
            // Initialize the state for previous frame if appropriate
            if (deltaTime <= 0)
            {
                m_CameraOrientationPrevFrame = curState.RawOrientation;
            }

            if (!IsValid || !curState.HasLookAt)
            {
                return;
            }

            curState.ReferenceLookAt = GetTrackedPoint(curState.ReferenceLookAt);
            float targetDistance = (curState.ReferenceLookAt - curState.CorrectedPosition).magnitude;

            if (targetDistance < UnityVectorExtensions.Epsilon)
            {
                return;  // navel-gazing, get outa here
            }
            //UnityEngine.Profiling.Profiler.BeginSample("CinemachineComposer.MutateCameraState");
            float fov, fovH;

            if (curState.Lens.Orthographic)
            {
                // Calculate effective fov - fake it for ortho based on target distance
                fov  = Mathf.Rad2Deg * 2 * Mathf.Atan(curState.Lens.OrthographicSize / targetDistance);
                fovH = Mathf.Rad2Deg * 2 * Mathf.Atan(
                    curState.Lens.Aspect * curState.Lens.OrthographicSize / targetDistance);
            }
            else
            {
                fov = curState.Lens.FieldOfView;
                double radHFOV = 2 * Math.Atan(Math.Tan(fov * Mathf.Deg2Rad / 2) * curState.Lens.Aspect);
                fovH = (float)(Mathf.Rad2Deg * radHFOV);
            }

            Rect softGuideFOV = ScreenToFOV(SoftGuideRect, fov, fovH, curState.Lens.Aspect);

            if (deltaTime <= 0)
            {
                // No damping, just snap to central bounds, skipping the soft zone
                Rect rect = new Rect(softGuideFOV.center, Vector2.zero); // Force to center
                curState.RawOrientation = RotateToScreenBounds(
                    ref curState, rect, curState.RawOrientation, fov, fovH, 0);
            }
            else
            {
                // Start with previous frame's orientation (but with current up)
                Quaternion rigOrientation = Quaternion.LookRotation(
                    m_CameraOrientationPrevFrame * Vector3.forward, curState.ReferenceUp);

                // First force the previous rotation into the hard bounds, no damping
                Rect hardGuideFOV = ScreenToFOV(HardGuideRect, fov, fovH, curState.Lens.Aspect);
                curState.RawOrientation = RotateToScreenBounds(
                    ref curState, hardGuideFOV, rigOrientation, fov, fovH, 0);

                // Now move it through the soft zone, with damping
                curState.RawOrientation = RotateToScreenBounds(
                    ref curState, softGuideFOV, curState.RawOrientation, fov, fovH, deltaTime);
            }
            curState.RawOrientation             = m_CameraOrientationPrevFrame
                                                = UnityQuaternionExtensions.Normalized(curState.RawOrientation);
            //UnityEngine.Profiling.Profiler.EndSample();
        }