/// <summary>Internal use only.  Do not call this method</summary>
        public override void InternalUpdateCameraState(Vector3 worldUp, float deltaTime)
        {
            // Get the state from the camera
            if (m_Camera == null)
            {
                m_Camera = GetComponent <Camera>();
            }

            m_State                = CameraState.Default;
            m_State.RawPosition    = transform.position;
            m_State.RawOrientation = transform.rotation;
            m_State.ReferenceUp    = m_State.RawOrientation * Vector3.up;
            if (m_Camera != null)
            {
                m_State.Lens = LensSettings.FromCamera(m_Camera);
            }

            if (m_LookAt != null)
            {
                m_State.ReferenceLookAt = m_LookAt.transform.position;
                Vector3 dir = m_State.ReferenceLookAt - State.RawPosition;
                if (!dir.AlmostZero())
                {
                    m_State.ReferenceLookAt = m_State.RawPosition + Vector3.Project(
                        dir, State.RawOrientation * Vector3.forward);
                }
            }
            SetPositionBlendMethod(ref m_State, m_PositionBlending);
            InvokePostPipelineStageCallback(this, CinemachineCore.Stage.Finalize, ref m_State, deltaTime);
        }
        /// <summary>
        /// Create a camera state based on the current transform of this vcam
        /// </summary>
        /// <param name="worldUp">Current World Up direction, as provided by the brain</param>
        /// <param name="lens">Lens settings to serve as base, will be combined with lens from brain, if any</param>
        /// <returns></returns>
        protected CameraState PullStateFromVirtualCamera(Vector3 worldUp, LensSettings lens)
        {
            CameraState state = CameraState.Default;

            state.RawPosition    = transform.position;
            state.RawOrientation = transform.rotation;
            state.ReferenceUp    = worldUp;

            lens.Orthographic     = false;
            lens.IsPhysicalCamera = false;
            lens.SensorSize       = Vector2.one;

            CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(this);

            if (brain != null)
            {
                lens.Orthographic = brain.OutputCamera.orthographic;
                lens.SensorSize   = new Vector2(brain.OutputCamera.aspect, 1f);
#if UNITY_2018_2_OR_NEWER
                lens.IsPhysicalCamera = brain.OutputCamera.usePhysicalProperties;
                if (lens.IsPhysicalCamera)
                {
                    lens.SensorSize = brain.OutputCamera.sensorSize;
                }
#endif
            }
            state.Lens = lens;
            return(state);
        }
        /// <summary>
        /// Create a blend from one ICinemachineCamera to another,
        /// or to/from a point, if we can't do anything else
        /// </summary>
        private CinemachineBlend CreateBlend(
            ICinemachineCamera camA, ICinemachineCamera camB,
            AnimationCurve blendCurve, float duration,
            CinemachineBlend activeBlend)
        {
            //UnityEngine.Profiling.Profiler.BeginSample("CinemachineTrackedDolly.MutateCameraState");
            if (blendCurve == null || duration <= 0 || (camA == null && camB == null))
            {
                //UnityEngine.Profiling.Profiler.EndSample();
                return(null);
            }
            if (camA == null || activeBlend != null)
            {
                // Blend from the current camera position
                CameraState state = CameraState.Default;
                if (activeBlend != null)
                {
                    state = activeBlend.State;
                }
                else
                {
                    state.RawPosition    = transform.position;
                    state.RawOrientation = transform.rotation;
                    state.Lens           = LensSettings.FromCamera(OutputCamera);
                }
                camA = new StaticPointVirtualCamera(state, activeBlend == null ? "(none)" : "Mid-blend");
            }
            CinemachineBlend blend = new CinemachineBlend(camA, camB, blendCurve, duration, 0);

            //UnityEngine.Profiling.Profiler.EndSample();
            return(blend);
        }
示例#4
0
        private CameraState CalculateNewState(Vector3 worldUp, float deltaTime)
        {
            //UnityEngine.Profiling.Profiler.BeginSample("CinemachineFreeLook.CalculateNewState");
            CameraState state = PullStateFromVirtualCamera(worldUp, m_Lens);

            m_Lens = state.Lens;

            m_YAxisRecentering.DoRecentering(ref m_YAxis, deltaTime, 0.5f);

            // Blend from the appropriate rigs
            float t = GetYAxisValue();

            if (t > 0.5f)
            {
                if (mBlendA != null)
                {
                    mBlendA.TimeInBlend = (t - 0.5f) * 2f;
                    mBlendA.UpdateCameraState(worldUp, deltaTime);
                    state = mBlendA.State;
                }
            }
            else
            {
                if (mBlendB != null)
                {
                    mBlendB.TimeInBlend = t * 2f;
                    mBlendB.UpdateCameraState(worldUp, deltaTime);
                    state = mBlendB.State;
                }
            }
            //UnityEngine.Profiling.Profiler.EndSample();
            return(state);
        }
示例#5
0
 protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
 {
     CinemachineFollowZoom.VcamExtraState extraState = base.GetExtraState <CinemachineFollowZoom.VcamExtraState>(vcam);
     if (!base.enabled || deltaTime < 0f)
     {
         extraState.m_previousFrameZoom = state.Lens.FieldOfView;
     }
     if (stage == CinemachineCore.Stage.Body)
     {
         float num   = Mathf.Max(this.m_Width, 0f);
         float value = 179f;
         float num2  = Vector3.Distance(state.CorrectedPosition, state.ReferenceLookAt);
         if (num2 > 0.0001f)
         {
             float min = num2 * 2f * Mathf.Tan(this.m_MinFOV * 0.0174532924f / 2f);
             float max = num2 * 2f * Mathf.Tan(this.m_MaxFOV * 0.0174532924f / 2f);
             num = Mathf.Clamp(num, min, max);
             if (deltaTime >= 0f && this.m_Damping > 0f)
             {
                 float num3 = num2 * 2f * Mathf.Tan(extraState.m_previousFrameZoom * 0.0174532924f / 2f);
                 float num4 = num - num3;
                 num4 = Damper.Damp(num4, this.m_Damping, deltaTime);
                 num  = num3 + num4;
             }
             value = 2f * Mathf.Atan(num / (2f * num2)) * 57.29578f;
         }
         LensSettings lens = state.Lens;
         lens.FieldOfView = (extraState.m_previousFrameZoom = Mathf.Clamp(value, this.m_MinFOV, this.m_MaxFOV));
         state.Lens       = lens;
     }
 }
        /// <summary>Construct a CameraState object from the Unity Camera</summary>
        public override void UpdateCameraState(Vector3 worldUp, float deltaTime)
        {
            // Get the state from the camera
            if (m_Camera == null)
            {
                m_Camera = GetComponent <Camera>();
            }

            m_State                = CameraState.Default;
            m_State.RawPosition    = transform.position;
            m_State.RawOrientation = transform.rotation;
            m_State.ReferenceUp    = m_State.RawOrientation * Vector3.up;
            if (m_Camera != null)
            {
                m_State.Lens = LensSettings.FromCamera(m_Camera);
            }

            if (m_LookAt != null)
            {
                m_State.ReferenceLookAt = m_LookAt.transform.position;
                Vector3 dir = m_State.ReferenceLookAt - State.RawPosition;
                if (!dir.AlmostZero())
                {
                    m_State.ReferenceLookAt = m_State.RawPosition + Vector3.Project(
                        dir, State.RawOrientation * Vector3.forward);
                }
            }
        }
示例#7
0
        /// <summary>
        /// Linearly blends the fields of two LensSettings and returns the result
        /// </summary>
        /// <param name="lensA">The LensSettings to blend from</param>
        /// <param name="lensB">The LensSettings to blend to</param>
        /// <param name="t">The interpolation value. Internally clamped to the range [0,1]</param>
        /// <returns>Interpolated settings</returns>
        public static LensSettings Lerp(LensSettings lensA, LensSettings lensB, float t)
        {
            t = Mathf.Clamp01(t);
            LensSettings blendedLens = new LensSettings();

            blendedLens.FarClipPlane     = Mathf.Lerp(lensA.FarClipPlane, lensB.FarClipPlane, t);
            blendedLens.NearClipPlane    = Mathf.Lerp(lensA.NearClipPlane, lensB.NearClipPlane, t);
            blendedLens.FieldOfView      = Mathf.Lerp(lensA.FieldOfView, lensB.FieldOfView, t);
            blendedLens.OrthographicSize = Mathf.Lerp(lensA.OrthographicSize, lensB.OrthographicSize, t);
            blendedLens.Dutch            = Mathf.Lerp(lensA.Dutch, lensB.Dutch, t);
            blendedLens.Orthographic     = lensA.Orthographic && lensB.Orthographic;
            blendedLens.IsPhysicalCamera = lensA.IsPhysicalCamera || lensB.IsPhysicalCamera;
            blendedLens.SensorSize       = Vector2.Lerp(lensA.SensorSize, lensB.SensorSize, t);
            blendedLens.LensShift        = Vector2.Lerp(lensA.LensShift, lensB.LensShift, t);

#if CINEMACHINE_HDRP
            blendedLens.Iso            = Mathf.RoundToInt(Mathf.Lerp((float)lensA.Iso, (float)lensB.Iso, t));
            blendedLens.ShutterSpeed   = Mathf.Lerp(lensA.ShutterSpeed, lensB.ShutterSpeed, t);
            blendedLens.Aperture       = Mathf.Lerp(lensA.Aperture, lensB.Aperture, t);
            blendedLens.BladeCount     = Mathf.RoundToInt(Mathf.Lerp(lensA.BladeCount, lensB.BladeCount, t));;
            blendedLens.Curvature      = Vector2.Lerp(lensA.Curvature, lensB.Curvature, t);
            blendedLens.BarrelClipping = Mathf.Lerp(lensA.BarrelClipping, lensB.BarrelClipping, t);
            blendedLens.Anamorphism    = Mathf.Lerp(lensA.Anamorphism, lensB.Anamorphism, t);
#endif
            return(blendedLens);
        }
        private CameraState CalculateNewState(Vector3 worldUp, float deltaTime)
        {
            CameraState state = PullStateFromVirtualCamera(worldUp, m_Lens);

            m_Lens = state.Lens;

            m_YAxisRecentering.DoRecentering(ref m_YAxis, deltaTime, 0.5f);

            // Blend from the appropriate rigs
            float t = GetYAxisValue();

            if (t > 0.5f)
            {
                if (mBlendA != null)
                {
                    mBlendA.TimeInBlend = (t - 0.5f) * 2f;
                    mBlendA.UpdateCameraState(worldUp, deltaTime);
                    state = mBlendA.State;
                }
            }
            else
            {
                if (mBlendB != null)
                {
                    mBlendB.TimeInBlend = t * 2f;
                    mBlendB.UpdateCameraState(worldUp, deltaTime);
                    state = mBlendB.State;
                }
            }
            return(state);
        }
        public override void MutateCameraState(ref CameraState curState, float deltaTime)
        {
            CinemachineTargetGroup targetGroup = this.TargetGroup;

            if (targetGroup == null)
            {
                base.MutateCameraState(ref curState, deltaTime);
                return;
            }
            if (!this.IsValid || !curState.HasLookAt)
            {
                this.m_prevTargetHeight = 0f;
                return;
            }
            curState.ReferenceLookAt = this.GetLookAtPointAndSetTrackedPoint(targetGroup.transform.position);
            Vector3 v         = base.TrackedPoint - curState.RawPosition;
            float   magnitude = v.magnitude;

            if (magnitude < 0.0001f)
            {
                return;
            }
            Vector3 vector      = v.AlmostZero() ? Vector3.forward : v.normalized;
            Bounds  boundingBox = targetGroup.BoundingBox;

            this.m_lastBoundsMatrix = Matrix4x4.TRS(boundingBox.center - vector * boundingBox.extents.magnitude, Quaternion.LookRotation(vector, curState.ReferenceUp), Vector3.one);
            this.m_LastBounds       = targetGroup.GetViewSpaceBoundingBox(this.m_lastBoundsMatrix);
            float   num = this.GetTargetHeight(this.m_LastBounds);
            Vector3 a   = this.m_lastBoundsMatrix.MultiplyPoint3x4(this.m_LastBounds.center);

            if (deltaTime >= 0f)
            {
                float num2 = num - this.m_prevTargetHeight;
                num2 = Damper.Damp(num2, this.m_FrameDamping, deltaTime);
                num  = this.m_prevTargetHeight + num2;
            }
            this.m_prevTargetHeight = num;
            if (!curState.Lens.Orthographic && this.m_AdjustmentMode != CinemachineGroupComposer.AdjustmentMode.ZoomOnly)
            {
                float fieldOfView = curState.Lens.FieldOfView;
                float num3        = num / (2f * Mathf.Tan(fieldOfView * 0.0174532924f / 2f)) + this.m_LastBounds.extents.z;
                num3 = Mathf.Clamp(num3, magnitude - this.m_MaxDollyIn, magnitude + this.m_MaxDollyOut);
                num3 = Mathf.Clamp(num3, this.m_MinimumDistance, this.m_MaximumDistance);
                curState.PositionCorrection += a - vector * num3 - curState.RawPosition;
            }
            if (curState.Lens.Orthographic || this.m_AdjustmentMode != CinemachineGroupComposer.AdjustmentMode.DollyOnly)
            {
                float num4  = (base.TrackedPoint - curState.CorrectedPosition).magnitude - this.m_LastBounds.extents.z;
                float value = 179f;
                if (num4 > 0.0001f)
                {
                    value = 2f * Mathf.Atan(num / (2f * num4)) * 57.29578f;
                }
                LensSettings lens = curState.Lens;
                lens.FieldOfView      = Mathf.Clamp(value, this.m_MinimumFOV, this.m_MaximumFOV);
                lens.OrthographicSize = Mathf.Clamp(num / 2f, this.m_MinimumOrthoSize, this.m_MaximumOrthoSize);
                curState.Lens         = lens;
            }
            base.MutateCameraState(ref curState, deltaTime);
        }
        /// <summary>Internal use only.  Called by CinemachineCore at designated update time
        /// so the vcam can position itself and track its targets.  All 3 child rigs are updated,
        /// and a blend calculated, depending on the value of the Y axis.</summary>
        /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
        /// <param name="deltaTime">Delta time for time-based effects (ignore if less than 0)</param>
        override public void InternalUpdateCameraState(Vector3 worldUp, float deltaTime)
        {
            // Initialize the camera state, in case the game object got moved in the editor
            m_State = PullStateFromVirtualCamera(worldUp, ref m_Lens);
            m_Rigs[(int)RigID.Top].m_Lens.SnapshotCameraReadOnlyProperties(ref m_Lens);
            m_Rigs[(int)RigID.Bottom].m_Lens.SnapshotCameraReadOnlyProperties(ref m_Lens);

            // Update our axes
            bool activeCam = PreviousStateIsValid || CinemachineCore.Instance.IsLive(this);

            if (activeCam && deltaTime >= 0)
            {
                if (m_VerticalAxis.Update(deltaTime))
                {
                    m_VerticalAxis.m_Recentering.CancelRecentering();
                }
                m_RadialAxis.Update(deltaTime);
            }
            m_VerticalAxis.m_Recentering.DoRecentering(ref m_VerticalAxis, deltaTime, 0.5f);

            // Blend the components
            if (mBlender == null)
            {
                mBlender = new ComponentBlender(this);
            }
            mBlender.Blend(GetVerticalAxisValue());

            // Blend the lens
            if (m_Rigs[mBlender.OtherRig].m_CustomLens)
            {
                m_State.Lens = LensSettings.Lerp(
                    m_State.Lens, m_Rigs[mBlender.OtherRig].m_Lens, mBlender.BlendAmount);
            }

            // Do our stuff
            SetReferenceLookAtTargetInState(ref m_State);
            InvokeComponentPipeline(ref m_State, worldUp, deltaTime);
            ApplyPositionBlendMethod(ref m_State, m_Transitions.m_BlendHint);

            // Restore the components
            mBlender.Restore();

            // Push the raw position back to the game object's transform, so it
            // moves along with the camera.
            if (!UserIsDragging)
            {
                if (Follow != null)
                {
                    transform.position = State.RawPosition;
                }
                if (LookAt != null)
                {
                    transform.rotation = State.RawOrientation;
                }
            }
            // Signal that it's all done
            InvokePostPipelineStageCallback(this, CinemachineCore.Stage.Finalize, ref m_State, deltaTime);
            PreviousStateIsValid = true;
        }
 public void Validate()
 {
     if (m_Lens.FieldOfView == 0)
     {
         m_Lens = LensSettings.Default;
     }
     m_Lens.Validate();
 }
示例#12
0
        /// <summary>
        /// Snapshot the properties that are read-only in the Camera
        /// </summary>
        /// <param name="lens">The LensSettings from which we will take the info</param>
        public void SnapshotCameraReadOnlyProperties(ref LensSettings lens)
        {
            Orthographic = lens.Orthographic;
            SensorSize   = lens.SensorSize;
#if UNITY_2018_2_OR_NEWER
            IsPhysicalCamera = lens.IsPhysicalCamera;
#endif
        }
        float AdjustCameraDepthAndLensForGroupFraming(
            CinemachineTargetGroup group, float targetZ, 
            ref CameraState curState, float deltaTime)
        {
            float cameraOffset = 0;

            // Get the bounding box from that POV in view space, and find its height
            Bounds bounds = group.BoundingBox;
            Vector3 fwd = curState.RawOrientation * Vector3.forward;
            m_lastBoundsMatrix = Matrix4x4.TRS(
                    bounds.center - (fwd * bounds.extents.magnitude),
                    curState.RawOrientation, Vector3.one);
            m_LastBounds = group.GetViewSpaceBoundingBox(m_lastBoundsMatrix);
            float targetHeight = GetTargetHeight(m_LastBounds);

            // Apply damping
            if (deltaTime >= 0)
            {
                float delta = targetHeight - m_prevTargetHeight;
                delta = Damper.Damp(delta, m_ZDamping, deltaTime);
                targetHeight = m_prevTargetHeight + delta;
            }
            m_prevTargetHeight = targetHeight;

            // Move the camera
            if (!curState.Lens.Orthographic && m_AdjustmentMode != AdjustmentMode.ZoomOnly)
            {
                // What distance would be needed to get the target height, at the current FOV
                float desiredDistance 
                    = targetHeight / (2f * Mathf.Tan(curState.Lens.FieldOfView * Mathf.Deg2Rad / 2f));

                // target the near surface of the bounding box
                desiredDistance += m_LastBounds.extents.z;

                // Clamp to respect min/max distance settings
                desiredDistance = Mathf.Clamp(
                        desiredDistance, targetZ - m_MaxDollyIn, targetZ + m_MaxDollyOut);
                desiredDistance = Mathf.Clamp(desiredDistance, m_MinimumDistance, m_MaximumDistance);

                // Apply
                cameraOffset += desiredDistance - targetZ;
            }

            // Apply zoom
            if (curState.Lens.Orthographic || m_AdjustmentMode != AdjustmentMode.DollyOnly)
            {
                float nearBoundsDistance = (targetZ + cameraOffset) - m_LastBounds.extents.z;
                float currentFOV = 179;
                if (nearBoundsDistance > Epsilon)
                    currentFOV = 2f * Mathf.Atan(targetHeight / (2 * nearBoundsDistance)) * Mathf.Rad2Deg;

                LensSettings lens = curState.Lens;
                lens.FieldOfView = Mathf.Clamp(currentFOV, m_MinimumFOV, m_MaximumFOV);
                lens.OrthographicSize = Mathf.Clamp(targetHeight / 2, m_MinimumOrthoSize, m_MaximumOrthoSize);
                curState.Lens = lens;
            }
            return -cameraOffset;
        }
        private CameraState CalculateNewState(Vector3 worldUp, float deltaTime)
        {
            // Initialize the camera state, in case the game object got moved in the editor
            CameraState state = PullStateFromVirtualCamera(worldUp, m_Lens);

            m_Lens = state.Lens;

            Transform lookAtTarget = LookAt;

            if (lookAtTarget != mCachedLookAtTarget)
            {
                mCachedLookAtTarget     = lookAtTarget;
                mCachedLookAtTargetVcam = null;
                if (lookAtTarget != null)
                {
                    mCachedLookAtTargetVcam = lookAtTarget.GetComponent <CinemachineVirtualCameraBase>();
                }
            }
            if (lookAtTarget != null)
            {
                if (mCachedLookAtTargetVcam != null)
                {
                    state.ReferenceLookAt = mCachedLookAtTargetVcam.State.FinalPosition;
                }
                else
                {
                    state.ReferenceLookAt = lookAtTarget.position;
                }
            }

            // Update the state by invoking the component pipeline
            CinemachineCore.Stage curStage = CinemachineCore.Stage.Body;
            UpdateComponentPipeline(); // avoid GetComponentPipeline() here because of GC
            bool hasAim = false;

            if (m_ComponentPipeline != null)
            {
                for (int i = 0; i < m_ComponentPipeline.Length; ++i)
                {
                    m_ComponentPipeline[i].PrePipelineMutateCameraState(ref state);
                    if (m_ComponentPipeline[i].Stage == CinemachineCore.Stage.Aim)
                    {
                        hasAim = true;
                    }
                }
                for (int i = 0; i < m_ComponentPipeline.Length; ++i)
                {
                    curStage = AdvancePipelineStage(
                        ref state, deltaTime, curStage,
                        (int)m_ComponentPipeline[i].Stage, hasAim);
                    m_ComponentPipeline[i].MutateCameraState(ref state, deltaTime);
                }
            }
            AdvancePipelineStage(
                ref state, deltaTime, curStage,
                (int)CinemachineCore.Stage.Finalize + 1, hasAim);
            return(state);
        }
示例#15
0
        private void ProcessActiveCamera(float deltaTime)
        {
            var activeCamera = ActiveVirtualCamera;

            if (activeCamera == null)
            {
                // No active virtal camera.  We create a state representing its position
                // and call the callback, but we don't actively set the transform or lens
                var state = CameraState.Default;
                state.RawPosition    = transform.position;
                state.RawOrientation = transform.rotation;
                state.Lens           = LensSettings.FromCamera(m_OutputCamera);
                state.BlendHint     |= CameraState.BlendHintValue.NoTransform | CameraState.BlendHintValue.NoLens;
                PushStateToUnityCamera(SoloCamera != null ? SoloCamera.State : state);
            }
            else
            {
                // Has the current camera changed this frame?
                if (mActiveCameraPreviousFrameGameObject == null)
                {
                    mActiveCameraPreviousFrame = null; // object was deleted
                }
                if (activeCamera != mActiveCameraPreviousFrame)
                {
                    // Notify incoming camera of transition
                    activeCamera.OnTransitionFromCamera(
                        mActiveCameraPreviousFrame, DefaultWorldUp, deltaTime);
                    if (m_CameraActivatedEvent != null)
                    {
                        m_CameraActivatedEvent.Invoke(activeCamera, mActiveCameraPreviousFrame);
                    }

                    // If we're cutting without a blend, send an event
                    if (!IsBlending || (mActiveCameraPreviousFrame != null &&
                                        !ActiveBlend.Uses(mActiveCameraPreviousFrame)))
                    {
                        if (m_CameraCutEvent != null)
                        {
                            m_CameraCutEvent.Invoke(this);
                        }
                        if (CinemachineCore.CameraCutEvent != null)
                        {
                            CinemachineCore.CameraCutEvent.Invoke(this);
                        }
                    }
                    // Re-update in case it's inactive
                    activeCamera.UpdateCameraState(DefaultWorldUp, deltaTime);
                }
                // Apply the vcam state to the Unity camera
                PushStateToUnityCamera(
                    SoloCamera != null ? SoloCamera.State : mCurrentLiveCameras.State);
            }
            mActiveCameraPreviousFrame = activeCamera;
            mActiveCameraPreviousFrameGameObject
                = activeCamera == null ? null : activeCamera.VirtualCameraGameObject;
        }
示例#16
0
        /// <summary>
        /// Linearly blends the fields of two <see cref="LensSettings"/> and returns the result
        /// </summary>
        /// <param name="lensA">The <see cref="LensSettings"/> to blend from</param>
        /// <param name="lensB">The <see cref="LensSettings"/> to blend to</param>
        /// <param name="t">The interpolation value. Internally clamped to the range [0,1]</param>
        /// <returns>Interpolated settings</returns>
        public static LensSettings Lerp(LensSettings lensA, LensSettings lensB, float t)
        {
            t = Mathf.Clamp01(t);
            LensSettings blendedLens = new LensSettings();

            blendedLens.FarClipPlane  = Mathf.Lerp(lensA.FarClipPlane, lensB.FarClipPlane, t);
            blendedLens.NearClipPlane = Mathf.Lerp(lensA.NearClipPlane, lensB.NearClipPlane, t);
            blendedLens.FieldOfView   = Mathf.Lerp(lensA.FieldOfView, lensB.FieldOfView, t);
            blendedLens.Dutch         = Mathf.Lerp(lensA.Dutch, lensB.Dutch, t);
            return(blendedLens);
        }
 /// <summary>
 /// Snapshot the properties that are read-only in the Camera
 /// </summary>
 /// <param name="lens">The LensSettings from which we will take the info</param>
 public void SnapshotCameraReadOnlyProperties(ref LensSettings lens)
 {
     if (ModeOverride == OverrideModes.None)
     {
         m_OrthoFromCamera    = lens.Orthographic;
         m_SensorSize         = lens.m_SensorSize;
         m_PhysicalFromCamera = lens.IsPhysicalCamera;
     }
     if (!IsPhysicalCamera)
     {
         LensShift = Vector2.zero;
     }
 }
示例#18
0
 public static LensSettings Lerp(LensSettings lensA, LensSettings lensB, float t)
 {
     t = Mathf.Clamp01(t);
     return(new LensSettings
     {
         FarClipPlane = Mathf.Lerp(lensA.FarClipPlane, lensB.FarClipPlane, t),
         NearClipPlane = Mathf.Lerp(lensA.NearClipPlane, lensB.NearClipPlane, t),
         FieldOfView = Mathf.Lerp(lensA.FieldOfView, lensB.FieldOfView, t),
         OrthographicSize = Mathf.Lerp(lensA.OrthographicSize, lensB.OrthographicSize, t),
         Dutch = Mathf.Lerp(lensA.Dutch, lensB.Dutch, t),
         Aspect = Mathf.Lerp(lensA.Aspect, lensB.Aspect, t),
         Orthographic = (lensA.Orthographic && lensB.Orthographic)
     });
 }
示例#19
0
        /// <summary>
        /// Linearly blends the fields of two LensSettings and returns the result
        /// </summary>
        /// <param name="lensA">The LensSettings to blend from</param>
        /// <param name="lensB">The LensSettings to blend to</param>
        /// <param name="t">The interpolation value. Internally clamped to the range [0,1]</param>
        /// <returns>Interpolated settings</returns>
        public static LensSettings Lerp(LensSettings lensA, LensSettings lensB, float t)
        {
            t = Mathf.Clamp01(t);
            LensSettings blendedLens = new LensSettings();

            blendedLens.FarClipPlane     = Mathf.Lerp(lensA.FarClipPlane, lensB.FarClipPlane, t);
            blendedLens.NearClipPlane    = Mathf.Lerp(lensA.NearClipPlane, lensB.NearClipPlane, t);
            blendedLens.FieldOfView      = Mathf.Lerp(lensA.FieldOfView, lensB.FieldOfView, t);
            blendedLens.OrthographicSize = Mathf.Lerp(lensA.OrthographicSize, lensB.OrthographicSize, t);
            blendedLens.Dutch            = Mathf.Lerp(lensA.Dutch, lensB.Dutch, t);
            blendedLens.Aspect           = Mathf.Lerp(lensA.Aspect, lensB.Aspect, t);
            blendedLens.Orthographic     = lensA.Orthographic && lensB.Orthographic;
            return(blendedLens);
        }
示例#20
0
        public static LensSettings FromCamera(Camera fromCamera)
        {
            LensSettings @default = LensSettings.Default;

            if (fromCamera != null)
            {
                @default.FieldOfView      = fromCamera.fieldOfView;
                @default.OrthographicSize = fromCamera.orthographicSize;
                @default.NearClipPlane    = fromCamera.nearClipPlane;
                @default.FarClipPlane     = fromCamera.farClipPlane;
                @default.Orthographic     = fromCamera.orthographic;
                @default.Aspect           = fromCamera.aspect;
            }
            return(@default);
        }
        /// <summary>
        /// Creates a new LensSettings, copying the values from the
        /// supplied Camera
        /// </summary>
        /// <param name="fromCamera">The Camera from which the FoV, near
        /// and far clip planes will be copied.</param>
        public static LensSettings FromCamera(Camera fromCamera)
        {
            LensSettings lens = Default;

            if (fromCamera != null)
            {
                lens.FieldOfView      = fromCamera.fieldOfView;
                lens.OrthographicSize = fromCamera.orthographicSize;
                lens.NearClipPlane    = fromCamera.nearClipPlane;
                lens.FarClipPlane     = fromCamera.farClipPlane;
                lens.Orthographic     = fromCamera.orthographic;
                lens.Aspect           = fromCamera.aspect;
            }
            return(lens);
        }
示例#22
0
        /// <summary>
        /// Linearly blends the fields of two LensSettings and returns the result
        /// </summary>
        /// <param name="lensA">The LensSettings to blend from</param>
        /// <param name="lensB">The LensSettings to blend to</param>
        /// <param name="t">The interpolation value. Internally clamped to the range [0,1]</param>
        /// <returns>Interpolated settings</returns>
        public static LensSettings Lerp(LensSettings lensA, LensSettings lensB, float t)
        {
            t = Mathf.Clamp01(t);
            LensSettings blendedLens = new LensSettings();

            blendedLens.FarClipPlane     = Mathf.Lerp(lensA.FarClipPlane, lensB.FarClipPlane, t);
            blendedLens.NearClipPlane    = Mathf.Lerp(lensA.NearClipPlane, lensB.NearClipPlane, t);
            blendedLens.FieldOfView      = Mathf.Lerp(lensA.FieldOfView, lensB.FieldOfView, t);
            blendedLens.OrthographicSize = Mathf.Lerp(lensA.OrthographicSize, lensB.OrthographicSize, t);
            blendedLens.Dutch            = Mathf.Lerp(lensA.Dutch, lensB.Dutch, t);
            blendedLens.Orthographic     = lensA.Orthographic && lensB.Orthographic;
            blendedLens.IsPhysicalCamera = lensA.IsPhysicalCamera || lensB.IsPhysicalCamera;
            blendedLens.SensorSize       = Vector2.Lerp(lensA.SensorSize, lensB.SensorSize, t);
            blendedLens.LensShift        = Vector2.Lerp(lensA.LensShift, lensB.LensShift, t);
            return(blendedLens);
        }
示例#23
0
            public void UpdateCache(
                LensSettings lens, Rect softGuide, Rect hardGuide, float targetDistance)
            {
                bool recalculate = mAspect != lens.Aspect ||
                                   softGuide != mSoftGuideRect || hardGuide != mHardGuideRect;

                if (lens.Orthographic)
                {
                    float orthoOverDistance = Mathf.Abs(lens.OrthographicSize / targetDistance);
                    if (mOrthoSizeOverDistance == 0 ||
                        Mathf.Abs(orthoOverDistance - mOrthoSizeOverDistance) / mOrthoSizeOverDistance
                        > mOrthoSizeOverDistance * 0.01f)
                    {
                        recalculate = true;
                    }
                    if (recalculate)
                    {
                        // Calculate effective fov - fake it for ortho based on target distance
                        mFov  = Mathf.Rad2Deg * 2 * Mathf.Atan(orthoOverDistance);
                        mFovH = Mathf.Rad2Deg * 2 * Mathf.Atan(lens.Aspect * orthoOverDistance);
                        mOrthoSizeOverDistance = orthoOverDistance;
                    }
                }
                else
                {
                    var verticalFOV = lens.FieldOfView;
                    if (mFov != verticalFOV)
                    {
                        recalculate = true;
                    }
                    if (recalculate)
                    {
                        mFov = verticalFOV;
                        double radHFOV = 2 * Math.Atan(Math.Tan(mFov * Mathf.Deg2Rad / 2) * lens.Aspect);
                        mFovH = (float)(Mathf.Rad2Deg * radHFOV);
                        mOrthoSizeOverDistance = 0;
                    }
                }
                if (recalculate)
                {
                    mFovSoftGuideRect = ScreenToFOV(softGuide, mFov, mFovH, lens.Aspect);
                    mSoftGuideRect    = softGuide;
                    mFovHardGuideRect = ScreenToFOV(hardGuide, mFov, mFovH, lens.Aspect);
                    mHardGuideRect    = hardGuide;
                    mAspect           = lens.Aspect;
                }
            }
示例#24
0
        /// <summary>
        /// Creates a new LensSettings, copying the values from the
        /// supplied Camera
        /// </summary>
        /// <param name="fromCamera">The Camera from which the FoV, near
        /// and far clip planes will be copied.</param>
        public static LensSettings FromCamera(Camera fromCamera)
        {
            LensSettings lens = Default;

            if (fromCamera != null)
            {
                lens.FieldOfView      = fromCamera.fieldOfView;
                lens.OrthographicSize = fromCamera.orthographicSize;
                lens.NearClipPlane    = fromCamera.nearClipPlane;
                lens.FarClipPlane     = fromCamera.farClipPlane;
#if UNITY_2018_2_OR_NEWER
                lens.LensShift = fromCamera.lensShift;
#endif
                lens.SnapshotCameraReadOnlyProperties(fromCamera);
            }
            return(lens);
        }
    static int get_m_Lens(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            Cinemachine.CinemachineVirtualCamera obj = (Cinemachine.CinemachineVirtualCamera)o;
            Cinemachine.LensSettings             ret = obj.m_Lens;
            ToLua.PushValue(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o, "attempt to index m_Lens on a nil value"));
        }
    }
    static int set_m_Lens(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            Cinemachine.CinemachineVirtualCamera obj  = (Cinemachine.CinemachineVirtualCamera)o;
            Cinemachine.LensSettings             arg0 = StackTraits <Cinemachine.LensSettings> .Check(L, 2);

            obj.m_Lens = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o, "attempt to index m_Lens on a nil value"));
        }
    }
示例#27
0
        /// <summary>
        /// Create a camera state based on the current transform of this vcam
        /// </summary>
        /// <param name="worldUp">Current World Up direction, as provided by the brain</param>
        /// <param name="lens">Lens settings to serve as base, will be combined with lens from brain, if any</param>
        /// <returns></returns>
        protected CameraState PullStateFromVirtualCamera(Vector3 worldUp, ref LensSettings lens)
        {
            CameraState state = CameraState.Default;

            state.RawPosition    = TargetPositionCache.GetTargetPosition(transform);
            state.RawOrientation = TargetPositionCache.GetTargetRotation(transform);
            state.ReferenceUp    = worldUp;

            CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(this);

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

            state.Lens = lens;
            return(state);
        }
        /// <summary>Callback to to the camera confining</summary>
        protected override void PostPipelineStageCallback(
            CinemachineVirtualCameraBase vcam,
            CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
        {
            // Move the body before the Aim is calculated
            if (stage == CinemachineCore.Stage.Body)
            {
                UpdateBounds();

                if (vcam.Follow != null)
                {
                    vcam.Follow.position = GetFinalPosition();
                }

                LensSettings newLensSettings = state.Lens;
                newLensSettings.OrthographicSize = GetFinalOrthographicSize();
                state.Lens = newLensSettings;
            }
        }
        protected override void PostPipelineStageCallback(
            CinemachineVirtualCameraBase vcam,
            CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
        {
            VcamExtraState extra = GetExtraState <VcamExtraState>(vcam);

            if (!enabled || deltaTime < 0)
            {
                extra.m_previousFrameZoom = state.Lens.FieldOfView;
            }
            if (enabled)
            {
                // Set the zoom after the body has been positioned, but before the aim,
                // so that composer can compose using the updated fov.
                if (stage == CinemachineCore.Stage.Body)
                {
                    // Try to reproduce the target width
                    float targetWidth = Mathf.Max(m_Width, 0);
                    float fov         = 179f;
                    float d           = Vector3.Distance(state.CorrectedPosition, state.ReferenceLookAt);
                    if (d > UnityVectorExtensions.Epsilon)
                    {
                        // Clamp targetWidth to FOV min/max
                        float minW = d * 2f * Mathf.Tan(m_MinFOV * Mathf.Deg2Rad / 2f);
                        float maxW = d * 2f * Mathf.Tan(m_MaxFOV * Mathf.Deg2Rad / 2f);
                        targetWidth = Mathf.Clamp(targetWidth, minW, maxW);

                        // Apply damping
                        if (deltaTime >= 0 && m_Damping > 0)
                        {
                            float currentWidth = d * 2f * Mathf.Tan(extra.m_previousFrameZoom * Mathf.Deg2Rad / 2f);
                            float delta        = targetWidth - currentWidth;
                            delta       = Damper.Damp(delta, m_Damping, deltaTime);
                            targetWidth = currentWidth + delta;
                        }
                        fov = 2f * Mathf.Atan(targetWidth / (2 * d)) * Mathf.Rad2Deg;
                    }
                    LensSettings lens = state.Lens;
                    lens.FieldOfView = extra.m_previousFrameZoom = Mathf.Clamp(fov, m_MinFOV, m_MaxFOV);
                    state.Lens       = lens;
                }
            }
        }
示例#30
0
        /// <summary>
        /// Creates a new LensSettings, copying the values from the
        /// supplied Camera
        /// </summary>
        /// <param name="fromCamera">The Camera from which the FoV, near
        /// and far clip planes will be copied.</param>
        public static LensSettings FromCamera(Camera fromCamera)
        {
            LensSettings lens = Default;

            if (fromCamera != null)
            {
                lens.FieldOfView  = fromCamera.fieldOfView;
                lens.SensorSize   = new Vector2(fromCamera.aspect, 1f);
                lens.Orthographic = fromCamera.orthographic;
#if UNITY_2018_2_OR_NEWER
                lens.IsPhysicalCamera = fromCamera.usePhysicalProperties;
                lens.SensorSize       = fromCamera.sensorSize;
                lens.LensShift        = fromCamera.lensShift;
#endif
                lens.OrthographicSize = fromCamera.orthographicSize;
                lens.NearClipPlane    = fromCamera.nearClipPlane;
                lens.FarClipPlane     = fromCamera.farClipPlane;
            }
            return(lens);
        }