Exemple #1
0
        void CalculateGridPlacement(SceneView view)
        {
            var cam = view.camera;

            bool wasOrtho = gridIsOrthographic;

            gridIsOrthographic = cam.orthographic && Snapping.IsRounded(view.rotation.eulerAngles.normalized);

            m_CameraDirection = Snapping.Sign(m_Pivot - cam.transform.position);

            if (wasOrtho != gridIsOrthographic)
            {
                m_DoGridRepaint = true;

                if (view == SceneView.lastActiveSceneView && gridIsOrthographic != menuIsOrtho)
                {
                    if (gridIsOrthographic)
                    {
                        m_savedAxis     = (Axis)EditorPrefs.GetInt(PreferenceKeys.GridAxis);
                        m_savedFullGrid = EditorPrefs.GetBool(PreferenceKeys.PerspGrid);
                    }
                    else
                    {
                        SetRenderPlane(m_savedAxis);
                        FullGridEnabled = m_savedFullGrid;
                    }
                    SetMenuIsExtended(menuOpen);
                }
            }

            if (gridIsOrthographic)
            {
                return;
            }

            if (FullGridEnabled)
            {
                m_Pivot = m_GridIsLocked || Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position;
            }
            else
            {
                Vector3 sceneViewPlanePivot = m_Pivot;

                Ray   ray   = new Ray(cam.transform.position, cam.transform.forward);
                Plane plane = new Plane(Vector3.up, m_Pivot);

                // the only time a locked grid should ever move is if it's m_Pivot is out
                // of the camera's frustum.
                if ((m_GridIsLocked && !cam.InFrustum(m_Pivot)) || !m_GridIsLocked || view != SceneView.lastActiveSceneView)
                {
                    float dist;

                    if (plane.Raycast(ray, out dist))
                    {
                        sceneViewPlanePivot = ray.GetPoint(Mathf.Min(dist, m_PlaneGridDrawDistance / 2f));
                    }
                    else
                    {
                        sceneViewPlanePivot = ray.GetPoint(Mathf.Min(cam.farClipPlane / 2f, m_PlaneGridDrawDistance / 2f));
                    }
                }

                if (m_GridIsLocked)
                {
                    m_Pivot = EnumExtension.InverseAxisMask(sceneViewPlanePivot, m_RenderPlane) + EnumExtension.AxisMask(m_Pivot, m_RenderPlane);
                }
                else
                {
                    m_Pivot = Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position;

                    if (Selection.activeTransform == null || !cam.InFrustum(m_Pivot))
                    {
                        m_Pivot = EnumExtension.InverseAxisMask(sceneViewPlanePivot, m_RenderPlane) + EnumExtension.AxisMask(Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position, m_RenderPlane);
                    }
                }
            }
        }
        void CalculateGridPlacement(SceneView view)
        {
            var cam = view.camera;

            bool wasOrtho = GridIsOrthographic;

            GridIsOrthographic = cam.orthographic && Snapping.IsRounded(view.rotation.eulerAngles.normalized);

            m_CameraDirection = Snapping.Sign(m_Pivot - cam.transform.position);

            if (GridIsOrthographic && !wasOrtho || GridIsOrthographic != menuIsOrtho)
            {
                OnSceneBecameOrtho(view == SceneView.lastActiveSceneView);
            }

            if (!GridIsOrthographic && wasOrtho)
            {
                OnSceneBecamePersp(view == SceneView.lastActiveSceneView);
            }

            if (GridIsOrthographic)
            {
                return;
            }

            if (FullGridEnabled)
            {
                m_Pivot = m_GridIsLocked || Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position;
            }
            else
            {
                Vector3 sceneViewPlanePivot = m_Pivot;

                Ray   ray   = new Ray(cam.transform.position, cam.transform.forward);
                Plane plane = new Plane(Vector3.up, m_Pivot);

                // the only time a locked grid should ever move is if it's m_Pivot is out
                // of the camera's frustum.
                if ((m_GridIsLocked && !cam.InFrustum(m_Pivot)) || !m_GridIsLocked || view != SceneView.lastActiveSceneView)
                {
                    float dist;

                    if (plane.Raycast(ray, out dist))
                    {
                        sceneViewPlanePivot = ray.GetPoint(Mathf.Min(dist, m_PlaneGridDrawDistance / 2f));
                    }
                    else
                    {
                        sceneViewPlanePivot = ray.GetPoint(Mathf.Min(cam.farClipPlane / 2f, m_PlaneGridDrawDistance / 2f));
                    }
                }

                if (m_GridIsLocked)
                {
                    m_Pivot = EnumExtension.InverseAxisMask(sceneViewPlanePivot, m_RenderPlane) + EnumExtension.AxisMask(m_Pivot, m_RenderPlane);
                }
                else
                {
                    m_Pivot = Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position;

                    if (Selection.activeTransform == null || !cam.InFrustum(m_Pivot))
                    {
                        m_Pivot = EnumExtension.InverseAxisMask(sceneViewPlanePivot, m_RenderPlane) + EnumExtension.AxisMask(Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position, m_RenderPlane);
                    }
                }
            }
        }