Beispiel #1
0
        /// <summary>
        /// Sets the specified <see cref="Transform"/> to match the position and
        /// rotation of the <see cref="SceneView"/> camera, and returns the scene view
        /// camera's lens settings.
        /// </summary>
        /// <param name="sceneObject">The <see cref="Transform"/> to match with the
        /// <see cref="SceneView"/> camera.</param>
        /// <returns>A <see cref="LensSettings"/> representing the scene view camera's lens</returns>
        public static LensSettings MatchSceneViewCamera(Transform sceneObject)
        {
            var lens = LensSettings.Default;

            // Take initial settings from the GameView camera, because we don't want to override
            // things like ortho vs perspective - we just want position and FOV
            var brain = GetOrCreateBrain();

            if (brain != null && brain.OutputCamera != null)
            {
                lens = LensSettings.FromCamera(brain.OutputCamera);
            }

            if (SceneView.lastActiveSceneView != null)
            {
                var src = SceneView.lastActiveSceneView.camera;
                sceneObject.SetPositionAndRotation(src.transform.position, src.transform.rotation);
                if (lens.Orthographic == src.orthographic)
                {
                    if (src.orthographic)
                    {
                        lens.OrthographicSize = src.orthographicSize;
                    }
                    else
                    {
                        lens.FieldOfView = src.fieldOfView;
                    }
                }
            }
            return(lens);
        }
    // Start is called before the first frame update
    void Start()
    {
        vcam = FindObjectOfType <CinemachineVirtualCamera>();

        //SETTERS
        myRigidBody    = GetComponent <Rigidbody2D>();
        myBodyCollider = GetComponent <PolygonCollider2D>();
        myAnimator     = GetComponent <Animator>();
        gasAnimator    = turning_gas.GetComponent <Animator>();

        gasAnimator.SetBool("turning", false);

        myRender  = GetComponent <SpriteRenderer>();
        isAlive   = true;
        ShipSpeed = InitialShipSpeed;

        int playerID = PlayerPrefs.GetInt("PlayerID", 1);

        myAnimator.SetInteger("PlayerID", playerID);
        myRender.sprite = sprites[playerID - 1];

        //ROTATION SETTERS
        times_rotated        = 0;
        m_Lens               = vcam.m_Lens;
        startingZoom         = m_Lens.OrthographicSize;
        myRigidBody.velocity = new Vector3(0, 0, 0);
    }
Beispiel #3
0
        protected void GetLensSettings(ref LensSettings lensSettings)
        {
            if (!FieldOfView.IsNone)
            {
                FieldOfView.Value = lensSettings.FieldOfView;
            }

            if (!OrthographicSize.IsNone)
            {
                OrthographicSize.Value = lensSettings.OrthographicSize;
            }

            if (!NearClipPlane.IsNone)
            {
                NearClipPlane.Value = lensSettings.NearClipPlane;
            }

            if (!FarClipPlane.IsNone)
            {
                FarClipPlane.Value = lensSettings.FarClipPlane;
            }

            if (!Dutch.IsNone)
            {
                Dutch.Value = lensSettings.Dutch;
            }
        }
        /// <summary>
        /// Create a Virtual Camera, with components
        /// </summary>
        static CinemachineVirtualCamera InternalCreateVirtualCamera(
            string name, bool selectIt, params Type[] components)
        {
            // Create a new virtual camera
            var        brain = CreateCameraBrainIfAbsent();
            GameObject go    = InspectorUtility.CreateGameObject(
                GenerateUniqueObjectName(typeof(CinemachineVirtualCamera), name),
                typeof(CinemachineVirtualCamera));

            if (SceneView.lastActiveSceneView != null)
            {
                go.transform.position = SceneView.lastActiveSceneView.pivot;
            }
            Undo.RegisterCreatedObjectUndo(go, "create " + name);
            CinemachineVirtualCamera vcam = go.GetComponent <CinemachineVirtualCamera>();
            GameObject componentOwner     = vcam.GetComponentOwner().gameObject;

            foreach (Type t in components)
            {
                Undo.AddComponent(componentOwner, t);
            }
            vcam.InvalidateComponentPipeline();
            if (brain != null && brain.OutputCamera != null)
            {
                vcam.m_Lens = LensSettings.FromCamera(brain.OutputCamera);
            }
            if (selectIt)
            {
                Selection.activeObject = go;
            }
            return(vcam);
        }
        /// <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);
                }
            }
        }
Beispiel #6
0
 public void SetTransitionsAndLens(
     CinemachineVirtualCameraBase.TransitionParams transitions, LensSettings lens)
 {
     blend_hint       = transitions.m_BlendHint.ToString();
     inherit_position = transitions.m_InheritPosition;
     mode_overwrite   = lens.ModeOverride.ToString();
 }
    void Update()
    {
        float targetFOV = DetermineTargetFOV();

        targetSettings.FieldOfView = targetFOV;
        vCam.m_Lens = LensSettings.Lerp(vCam.m_Lens, targetSettings, zoomRate);
    }
Beispiel #8
0
        /// <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);
        }
        protected void SetLensSettings(ref LensSettings lensSettings)
        {
            if (!FieldOfView.IsNone)
            {
                lensSettings.FieldOfView = FieldOfView.Value;
            }

            if (!OrthographicSize.IsNone)
            {
                lensSettings.OrthographicSize = OrthographicSize.Value;
            }

            if (!NearClipPlane.IsNone)
            {
                lensSettings.NearClipPlane = NearClipPlane.Value;
            }

            if (!FarClipPlane.IsNone)
            {
                lensSettings.FarClipPlane = FarClipPlane.Value;
            }

            if (!Dutch.IsNone)
            {
                lensSettings.Dutch = Dutch.Value;
            }
        }
Beispiel #10
0
 public static void SetVcamFromSceneView(CinemachineVirtualCamera vcam)
 {
     if (SceneView.lastActiveSceneView != null)
     {
         vcam.transform.position = SceneView.lastActiveSceneView.camera.transform.position;
         vcam.transform.rotation = SceneView.lastActiveSceneView.camera.transform.rotation;
         vcam.m_Lens             = LensSettings.FromCamera(SceneView.lastActiveSceneView.camera);
     }
 }
 private static void DrawBrainGizmos(CinemachineBrain brain, GizmoType drawType)
 {
     if (brain.OutputCamera != null && brain.m_ShowCameraFrustum)
     {
         DrawCameraFrustumGizmo(
             brain, LensSettings.FromCamera(brain.OutputCamera),
             brain.transform.localToWorldMatrix,
             Color.white); // GML why is this color hardcoded?
     }
 }
 public static void SetVcamFromSceneView(CinemachineVirtualCamera vcam)
 {
     if (SceneView.lastActiveSceneView != null)
     {
         vcam.transform.position = SceneView.lastActiveSceneView.camera.transform.position;
         vcam.transform.rotation = SceneView.lastActiveSceneView.camera.transform.rotation;
         var lens = LensSettings.FromCamera(SceneView.lastActiveSceneView.camera);
         // Don't grab these
         lens.NearClipPlane = LensSettings.Default.NearClipPlane;
         lens.FarClipPlane  = LensSettings.Default.FarClipPlane;
         vcam.m_Lens        = lens;
     }
 }
Beispiel #13
0
        /// <summary>
        /// Used to convert a viewport point to a ray for a camera at a position and
        /// orientation different from where it is presently.
        /// </summary>
        /// <param name="aspect">Camera aspect ratio</param>
        /// <param name="withLens">The lens to apply to the camera when finding the viewport point</param>
        /// <param name="viewportPoint">The viewport point to use when creating the <see cref="Ray"/></param>
        /// <param name="fromPosition">The position to generate the ray from</param>
        /// <param name="withOrientation">The orientation of the camera used to generate the ray from</param>
        /// <returns>The ray for the camera at the specified <paramref name="fromPosition"/>
        /// and <paramref name="withOrientation"/>. </returns>
        public static Ray GetRayFromViewportAtPoint(
            float aspect, LensSettings withLens, Vector3 viewportPoint,
            Vector3 fromPosition, Quaternion withOrientation)
        {
            Matrix4x4 persp = Matrix4x4.Perspective(
                withLens.FieldOfView, aspect, withLens.NearClipPlane, withLens.FarClipPlane);

            viewportPoint.z = 0.1f;
            //Transform into clip space before passing through the inverse perspective matrix
            viewportPoint *= 2f;
            viewportPoint -= Vector3.one;
            Vector3 result = persp.inverse.MultiplyPoint(viewportPoint).normalized;

            result.z *= -1f;

            return(new Ray(fromPosition, withOrientation * result));
        }
        /// <summary>Callback to preform the zoom adjustment</summary>
        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;
            }

            // 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;
            }
        }
        internal static void DrawCameraFrustumGizmo(
            CinemachineBrain brain, LensSettings lens,
            Matrix4x4 transform, Color color)
        {
            float aspect = 1;
            var   ortho  = false;

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

            var originalMatrix      = Gizmos.matrix;
            var originalGizmoColour = Gizmos.color;

            Gizmos.color  = color;
            Gizmos.matrix = transform;
            if (ortho)
            {
                var size = new Vector3(
                    aspect * lens.OrthographicSize * 2,
                    lens.OrthographicSize * 2,
                    lens.NearClipPlane + lens.FarClipPlane);
                Gizmos.DrawWireCube(
                    new Vector3(0, 0, size.z / 2 + lens.NearClipPlane), size);
            }
            else
            {
                Gizmos.DrawFrustum(
                    Vector3.zero, lens.FieldOfView,
                    lens.FarClipPlane, lens.NearClipPlane, aspect);
            }

            Gizmos.matrix = originalMatrix;
            Gizmos.color  = originalGizmoColour;
        }
Beispiel #16
0
        /// <summary>
        /// Used to find the viewport point for the specified <see cref="Camera"/> at the specified
        /// position and orientation
        /// </summary>
        /// <param name="aspect">Camera aspect ratio</param>
        /// <param name="withLens">The lens to apply to the camera when finding the viewport point</param>
        /// <param name="worldPoint">The world point to find the viewport position of</param>
        /// <param name="fromPosition">Camera position</param>
        /// <param name="withOrientation">Camera orientation</param>
        /// <returns>The viewport space position of <paramref name="worldPoint"/></returns>
        public static Vector3 GetViewportPointFromWorldPoint(
            float aspect, LensSettings withLens, Vector3 worldPoint,
            Vector3 fromPosition, Quaternion withOrientation)
        {
            // If the world point is behind the camera, rotate it to bring it to the nearest
            // edge of the front hemisphere
            Vector3 fwd   = withOrientation * Vector3.forward;
            Vector3 dir   = worldPoint - fromPosition;
            float   angle = Vector3.Angle(fwd, dir);

            if (angle > 89)
            {
                Vector3 axis = Vector3.Cross(dir, fwd);
                if (axis.AlmostZero())
                {
                    axis = withOrientation * Vector3.up;
                }
                Quaternion q = Quaternion.AngleAxis(angle - 89, axis.normalized);
                worldPoint = (q * (worldPoint - fromPosition)) + fromPosition;
            }

            Matrix4x4 persp = Matrix4x4.Perspective(
                withLens.FieldOfView, aspect, withLens.NearClipPlane, withLens.FarClipPlane);
            Vector3 localPoint    = (withOrientation * (worldPoint - fromPosition)) + fromPosition;
            Vector4 viewportPoint = persp.MultiplyPoint(localPoint);

            if (Mathf.Abs(viewportPoint.z) > UnityVectorExtensions.Epsilon)
            {
                viewportPoint /= -viewportPoint.z;
            }

            viewportPoint += Vector4.one;
            viewportPoint *= 0.5f;

            return(viewportPoint);
        }
Beispiel #17
0
 private void Awake()
 {
     vCam             = this.GetComponent <CinemachineVirtualCamera>();
     originalSettings = vCam.m_Lens;
     targetSettings   = originalSettings;
 }
Beispiel #18
0
        Rect GetCameraRect(Camera outputCamera, LensSettings lens)
        {
            Rect  cameraRect   = outputCamera.pixelRect;
            float screenHeight = cameraRect.height;
            float screenWidth  = cameraRect.width;

            float screenAspect = screenWidth / screenHeight;

            switch (outputCamera.gateFit)
            {
            case Camera.GateFitMode.Vertical:
                screenWidth          = screenHeight * lens.Aspect;
                cameraRect.position += new Vector2((cameraRect.width - screenWidth) * 0.5f, 0);
                break;

            case Camera.GateFitMode.Horizontal:
                screenHeight         = screenWidth / lens.Aspect;
                cameraRect.position += new Vector2(0, (cameraRect.height - screenHeight) * 0.5f);
                break;

            case Camera.GateFitMode.Overscan:
                if (screenAspect < lens.Aspect)
                {
                    screenHeight         = screenWidth / lens.Aspect;
                    cameraRect.position += new Vector2(0, (cameraRect.height - screenHeight) * 0.5f);
                }
                else
                {
                    screenWidth          = screenHeight * lens.Aspect;
                    cameraRect.position += new Vector2((cameraRect.width - screenWidth) * 0.5f, 0);
                }
                break;

            case Camera.GateFitMode.Fill:
                if (screenAspect > lens.Aspect)
                {
                    screenHeight         = screenWidth / lens.Aspect;
                    cameraRect.position += new Vector2(0, (cameraRect.height - screenHeight) * 0.5f);
                }
                else
                {
                    screenWidth          = screenHeight * lens.Aspect;
                    cameraRect.position += new Vector2((cameraRect.width - screenWidth) * 0.5f, 0);
                }
                break;

            case Camera.GateFitMode.None:
                break;
            }

            cameraRect = new Rect(cameraRect.position, new Vector2(screenWidth, screenHeight));

            // Invert Y
            float h = cameraRect.height;

            cameraRect.yMax = Screen.height - cameraRect.yMin;
            cameraRect.yMin = cameraRect.yMax - h;

            // Shift the guides along with the lens
            cameraRect.position += new Vector2(
                -screenWidth * lens.LensShift.x, screenHeight * lens.LensShift.y);

            return(cameraRect);
        }
        public void OnGUI_DrawGuides(bool isLive, Camera outputCamera, LensSettings lens, bool showHardGuides)
        {
            Rect  gateRect     = outputCamera.pixelRect;
            Rect  cameraRect   = gateRect;
            float screenWidth  = cameraRect.width;
            float screenHeight = screenWidth / lens.Aspect;

            cameraRect.yMax = Screen.height - gateRect.yMin;
            cameraRect.yMin = cameraRect.yMax - gateRect.height;

            // Shift the guides along with the lens
            cameraRect.position += new Vector2(
                -screenWidth * lens.LensShift.x, screenHeight * lens.LensShift.y);

            // Rotate the guides along with the dutch
            Matrix4x4 oldMatrix = GUI.matrix;

            GUI.matrix = Matrix4x4.Translate(cameraRect.min);
            GUIUtility.RotateAroundPivot(lens.Dutch, cameraRect.center);
            Color hardBarsColour = CinemachineSettings.ComposerSettings.HardBoundsOverlayColour;
            Color softBarsColour = CinemachineSettings.ComposerSettings.SoftBoundsOverlayColour;
            float overlayOpacity = CinemachineSettings.ComposerSettings.OverlayOpacity;

            if (!isLive)
            {
                softBarsColour  = CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour;
                hardBarsColour  = Color.Lerp(softBarsColour, Color.black, 0.5f);
                overlayOpacity /= 2;
            }
            hardBarsColour.a *= overlayOpacity;
            softBarsColour.a *= overlayOpacity;

            Rect  r              = showHardGuides ? GetHardGuide() : new Rect(-2, -2, 4, 4);
            float hardEdgeLeft   = r.xMin * screenWidth;
            float hardEdgeTop    = r.yMin * screenHeight;
            float hardEdgeRight  = r.xMax * screenWidth;
            float hardEdgeBottom = r.yMax * screenHeight;

            mDragBars[(int)DragBar.HardBarLineLeft]   = new Rect(hardEdgeLeft - kGuideBarWidthPx / 2f, 0f, kGuideBarWidthPx, screenHeight);
            mDragBars[(int)DragBar.HardBarLineTop]    = new Rect(0f, hardEdgeTop - kGuideBarWidthPx / 2f, screenWidth, kGuideBarWidthPx);
            mDragBars[(int)DragBar.HardBarLineRight]  = new Rect(hardEdgeRight - kGuideBarWidthPx / 2f, 0f, kGuideBarWidthPx, screenHeight);
            mDragBars[(int)DragBar.HardBarLineBottom] = new Rect(0f, hardEdgeBottom - kGuideBarWidthPx / 2f, screenWidth, kGuideBarWidthPx);

            r = GetSoftGuide();
            float softEdgeLeft   = r.xMin * screenWidth;
            float softEdgeTop    = r.yMin * screenHeight;
            float softEdgeRight  = r.xMax * screenWidth;
            float softEdgeBottom = r.yMax * screenHeight;

            mDragBars[(int)DragBar.SoftBarLineLeft]   = new Rect(softEdgeLeft - kGuideBarWidthPx / 2f, 0f, kGuideBarWidthPx, screenHeight);
            mDragBars[(int)DragBar.SoftBarLineTop]    = new Rect(0f, softEdgeTop - kGuideBarWidthPx / 2f, screenWidth, kGuideBarWidthPx);
            mDragBars[(int)DragBar.SoftBarLineRight]  = new Rect(softEdgeRight - kGuideBarWidthPx / 2f, 0f, kGuideBarWidthPx, screenHeight);
            mDragBars[(int)DragBar.SoftBarLineBottom] = new Rect(0f, softEdgeBottom - kGuideBarWidthPx / 2f, screenWidth, kGuideBarWidthPx);

            mDragBars[(int)DragBar.Center] = new Rect(softEdgeLeft, softEdgeTop, softEdgeRight - softEdgeLeft, softEdgeBottom - softEdgeTop);

            // Handle dragging bars
            if (isLive)
            {
                OnGuiHandleBarDragging(screenWidth, screenHeight);
            }

            // Draw the masks
            GUI.color = hardBarsColour;
            Rect hardBarLeft  = new Rect(0, hardEdgeTop, Mathf.Max(0, hardEdgeLeft), hardEdgeBottom - hardEdgeTop);
            Rect hardBarRight = new Rect(hardEdgeRight, hardEdgeTop,
                                         Mathf.Max(0, screenWidth - hardEdgeRight), hardEdgeBottom - hardEdgeTop);
            Rect hardBarTop = new Rect(Mathf.Min(0, hardEdgeLeft), 0,
                                       Mathf.Max(screenWidth, hardEdgeRight) - Mathf.Min(0, hardEdgeLeft), Mathf.Max(0, hardEdgeTop));
            Rect hardBarBottom = new Rect(Mathf.Min(0, hardEdgeLeft), hardEdgeBottom,
                                          Mathf.Max(screenWidth, hardEdgeRight) - Mathf.Min(0, hardEdgeLeft),
                                          Mathf.Max(0, screenHeight - hardEdgeBottom));

            GUI.DrawTexture(hardBarLeft, Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(hardBarTop, Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(hardBarRight, Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(hardBarBottom, Texture2D.whiteTexture, ScaleMode.StretchToFill);

            GUI.color = softBarsColour;
            Rect softBarLeft   = new Rect(hardEdgeLeft, softEdgeTop, softEdgeLeft - hardEdgeLeft, softEdgeBottom - softEdgeTop);
            Rect softBarTop    = new Rect(hardEdgeLeft, hardEdgeTop, hardEdgeRight - hardEdgeLeft, softEdgeTop - hardEdgeTop);
            Rect softBarRight  = new Rect(softEdgeRight, softEdgeTop, hardEdgeRight - softEdgeRight, softEdgeBottom - softEdgeTop);
            Rect softBarBottom = new Rect(hardEdgeLeft, softEdgeBottom, hardEdgeRight - hardEdgeLeft, hardEdgeBottom - softEdgeBottom);

            GUI.DrawTexture(softBarLeft, Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(softBarTop, Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(softBarRight, Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(softBarBottom, Texture2D.whiteTexture, ScaleMode.StretchToFill);

            // Draw the drag bars
            GUI.DrawTexture(mDragBars[(int)DragBar.SoftBarLineLeft], Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(mDragBars[(int)DragBar.SoftBarLineTop], Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(mDragBars[(int)DragBar.SoftBarLineRight], Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(mDragBars[(int)DragBar.SoftBarLineBottom], Texture2D.whiteTexture, ScaleMode.StretchToFill);

            GUI.color = hardBarsColour;
            GUI.DrawTexture(mDragBars[(int)DragBar.HardBarLineLeft], Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(mDragBars[(int)DragBar.HardBarLineTop], Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(mDragBars[(int)DragBar.HardBarLineRight], Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.DrawTexture(mDragBars[(int)DragBar.HardBarLineBottom], Texture2D.whiteTexture, ScaleMode.StretchToFill);

            GUI.matrix = oldMatrix;
        }
        /// <summary>Applies the composer rules and orients the camera accordingly</summary>
        /// <param name="state">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)
        {
            // Can't do anything without a group to look at
            CinemachineTargetGroup group = TargetGroup;

            if (group == null)
            {
                base.MutateCameraState(ref curState, deltaTime);
                return;
            }

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

            curState.ReferenceLookAt = GetLookAtPointAndSetTrackedPoint(group.transform.position);
            Vector3 currentOffset   = TrackedPoint - curState.RawPosition;
            float   currentDistance = currentOffset.magnitude;

            if (currentDistance < Epsilon)
            {
                return;  // navel-gazing, get outa here
            }
            //UnityEngine.Profiling.Profiler.BeginSample("CinemachineGroupComposer.MutateCameraState");

            // Get the camera axis
            Vector3 fwd = currentOffset.AlmostZero() ? Vector3.forward : currentOffset.normalized;

            // Get the bounding box from that POV in view space, and find its width
            Bounds bounds = group.BoundingBox;

            m_lastBoundsMatrix = Matrix4x4.TRS(
                bounds.center - (fwd * bounds.extents.magnitude),
                Quaternion.LookRotation(fwd, curState.ReferenceUp), Vector3.one);
            m_LastBounds = group.GetViewSpaceBoundingBox(m_lastBoundsMatrix);
            float   targetHeight = GetTargetHeight(m_LastBounds);
            Vector3 targetPos    = m_lastBoundsMatrix.MultiplyPoint3x4(m_LastBounds.center);

            // Apply damping
            if (deltaTime >= 0)
            {
                float delta = targetHeight - m_prevTargetHeight;
                delta        = Damper.Damp(delta, m_FrameDamping, 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 currentFOV     = curState.Lens.FieldOfView;
                float targetDistance = targetHeight / (2f * Mathf.Tan(currentFOV * Mathf.Deg2Rad / 2f));

                // target the near surface of the bounding box
                float cameraDistance = targetDistance + m_LastBounds.extents.z;

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

                // Apply
                curState.PositionCorrection += targetPos - fwd * cameraDistance - curState.RawPosition;
            }

            // Apply zoom
            if (curState.Lens.Orthographic || m_AdjustmentMode != AdjustmentMode.DollyOnly)
            {
                float nearBoundsDistance = (TrackedPoint - curState.CorrectedPosition).magnitude
                                           - 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;
            }

            // Now compose normally
            base.MutateCameraState(ref curState, deltaTime);
            //UnityEngine.Profiling.Profiler.EndSample();
        }
Beispiel #21
0
        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);
        }