Exemple #1
0
    public void OnSceneGUI(SceneView scnview)
    {
        if (EditorApplication.isPlayingOrWillChangePlaymode)
        {
            return;                                                             // don't snap stuff in play mode
        }
        Event e = Event.current;

        if (e.Equals(Event.KeyboardEvent(AXIS_CONSTRAINT_KEY)))
        {
            toggleAxisConstraint = true;
        }

        if (e.type == EventType.KeyUp)
        {
            toggleAxisConstraint = false;
            bool used = true;
            switch (e.keyCode)
            {
            case KeyCode.Equals:
                IncreaseGridSize();
                break;

            case KeyCode.Minus:
                DecreaseGridSize();
                break;

            case KeyCode.LeftBracket:
                if (VerifyMenuNudgePerspective())
                {
                    MenuNudgePerspectiveBackward();
                }
                break;

            case KeyCode.RightBracket:
                if (VerifyMenuNudgePerspective())
                {
                    MenuNudgePerspectiveForward();
                }
                break;

            case KeyCode.Alpha0:
                if (VerifyMenuNudgePerspective())
                {
                    MenuNudgePerspectiveReset();
                }
                break;

            case KeyCode.Backslash:
                CyclePerspective();
                break;

            default:
                used = false;
                break;
            }

            if (used)
            {
                e.Use();
            }
        }

        Camera cam = Camera.current;

        ortho = cam.orthographic && IsRounded(scnview.rotation.eulerAngles.normalized);

        if (fullGrid)
        {
            pivot = lockGrid || Selection.activeTransform == null ? pivot : Selection.activeTransform.position;
        }
        else
        {
            Vector3 sceneViewPlanePivot = pivot;

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

            // the only time a locked grid should ever move is if it's pivot is out
            // of the camera's frustum.
            if ((lockGrid && !cam.InFrustum(pivot)) || !lockGrid)
            {
                if (plane.Raycast(ray, out dist))
                {
                    sceneViewPlanePivot = ray.GetPoint(Mathf.Min(dist, planeGridDrawDistance / 2f));
                }
                else
                {
                    sceneViewPlanePivot = ray.GetPoint(Mathf.Min(cam.farClipPlane / 2f, planeGridDrawDistance / 2f));
                }
            }

            if (lockGrid)
            {
                pivot = InverseAxisMask(sceneViewPlanePivot, renderPlane) + AxisMask(pivot, renderPlane);
            }
            else
            {
                pivot = Selection.activeTransform == null ? pivot : Selection.activeTransform.position;

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

                        #if DEBUG
            pivotGo.transform.position             = pivot;
            scenePivotGo.transform.position        = SceneView.lastActiveSceneView.pivot;
            scenePlaneIntercept.transform.position = sceneViewPlanePivot;

            Handles.Label(scenePivotGo.transform.position, "SceneView Pivot: " + scenePivotGo.transform.position.ToString());
            Handles.Label(scenePlaneIntercept.transform.position, "\nPivot Plane Intercept: " + scenePlaneIntercept.transform.position.ToString());
            Handles.Label(pivot, "ProGrids Pivot: " + pivot.ToString());
                        #endif
        }


        if (drawGrid)
        {
            if (ortho)
            {
                // ortho don't care about pivots
                DrawGridOrthographic(cam);
            }
            else
            {
                                #if PROFILE_TIMES
                profiler.LogStart("DrawGridPerspective");
                                #endif
                if (fullGrid)
                {
                    //  if perspective and 3d, use pivot like normal
                    pg_GridRenderer.DrawGridPerspective(cam, pivot, snapValue, new Color[3] {
                        gridColorX, gridColorY, gridColorZ
                    }, alphaBump);
                }
                else
                {
                    if ((renderPlane & Axis.X) == Axis.X)
                    {
                        planeGridDrawDistance = pg_GridRenderer.DrawPlane(cam, pivot + Vector3.right * offset, Vector3.up, Vector3.forward, snapValue, gridColorX, alphaBump);
                    }

                    if ((renderPlane & Axis.Y) == Axis.Y)
                    {
                        planeGridDrawDistance = pg_GridRenderer.DrawPlane(cam, pivot + Vector3.up * offset, Vector3.right, Vector3.forward, snapValue, gridColorY, alphaBump);
                    }

                    if ((renderPlane & Axis.Z) == Axis.Z)
                    {
                        planeGridDrawDistance = pg_GridRenderer.DrawPlane(cam, pivot + Vector3.forward * offset, Vector3.up, Vector3.right, snapValue, gridColorZ, alphaBump);
                    }
                }
                                #if PROFILE_TIMES
                profiler.LogFinish("DrawGridPerspective");
                                #endif
            }
        }

        if (e.type == EventType.ValidateCommand)
        {
            OnValidateCommand(Event.current.commandName);
            return;
        }

        // Always keep track of the selection
        if (!Selection.transforms.Contains(lastTransform))
        {
            if (Selection.activeTransform)
            {
                lastTransform = Selection.activeTransform;
                lastPosition  = Selection.activeTransform.position;
            }
        }

        if (!snapEnabled || GUIUtility.hotControl < 1)
        {
            return;
        }

        /**
         *	Snapping (for all the junk in PG, this method is literally the only code that actually affects anything).
         */
        if (Selection.activeTransform)
        {
            if (lastTransform.position != lastPosition)
            {
                Transform selected = lastTransform;

                Vector3 old  = selected.position;
                Vector3 mask = old - lastPosition;

                bool constraintsOn = toggleAxisConstraint ? !useAxisConstraints : useAxisConstraints;

                if (constraintsOn)
                {
                    // Bugger.SetKey("constraints", "on");
                    selected.position = pg_Util.SnapValue(old, mask, snapValue);
                }
                else
                {
                    // Bugger.SetKey("constraints", "off");
                    selected.position = pg_Util.SnapValue(old, snapValue);
                }

                Vector3 offset = selected.position - old;

                OffsetTransforms(Selection.transforms, selected, offset);

                lastPosition = selected.position;
            }
        }
    }
Exemple #2
0
    public void OnSceneGUI(SceneView scnview)
    {
        if (EditorApplication.isPlayingOrWillChangePlaymode)
        {
            return;                                                             // don't snap stuff in play mode
        }
        Event e = Event.current;

        if (e.Equals(Event.KeyboardEvent(AXIS_CONSTRAINT_KEY)))
        {
            toggleAxisConstraint = true;
        }

        if (e.Equals(Event.KeyboardEvent(TEMP_DISABLE_KEY)))
        {
            toggleTempSnap = true;
        }

        if (e.type == EventType.KeyUp)
        {
            toggleAxisConstraint = false;
            toggleTempSnap       = false;
            bool used = true;

            switch (e.keyCode)
            {
            case KeyCode.Equals:
                IncreaseGridSize();
                break;

            case KeyCode.Minus:
                DecreaseGridSize();
                break;

            case KeyCode.LeftBracket:
                if (VerifyMenuNudgePerspective())
                {
                    MenuNudgePerspectiveBackward();
                }
                break;

            case KeyCode.RightBracket:
                if (VerifyMenuNudgePerspective())
                {
                    MenuNudgePerspectiveForward();
                }
                break;

            case KeyCode.Alpha0:
                if (VerifyMenuNudgePerspective())
                {
                    MenuNudgePerspectiveReset();
                }
                break;

            case KeyCode.Backslash:
                CyclePerspective();
                break;

            default:
                used = false;
                break;
            }

            if (used)
            {
                e.Use();
            }
        }

        Camera cam = Camera.current;

        ortho = cam.orthographic && IsRounded(scnview.rotation.eulerAngles.normalized);

        camDir = pg_Util.CeilFloor(pivot - cam.transform.position);

        if (ortho && !prevOrtho)
        {
            pg_GridRenderer.Destroy();
        }

        prevOrtho = ortho;

        float camDistance = Vector3.Distance(cam.transform.position, lastPivot);                // distance from camera to pivot

        if (fullGrid)
        {
            pivot = lockGrid || Selection.activeTransform == null ? pivot : Selection.activeTransform.position;
        }
        else
        {
            Vector3 sceneViewPlanePivot = pivot;

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

            // the only time a locked grid should ever move is if it's pivot is out
            // of the camera's frustum.
            if ((lockGrid && !cam.InFrustum(pivot)) || !lockGrid)
            {
                if (plane.Raycast(ray, out dist))
                {
                    sceneViewPlanePivot = ray.GetPoint(Mathf.Min(dist, planeGridDrawDistance / 2f));
                }
                else
                {
                    sceneViewPlanePivot = ray.GetPoint(Mathf.Min(cam.farClipPlane / 2f, planeGridDrawDistance / 2f));
                }
            }

            if (lockGrid)
            {
                pivot = InverseAxisMask(sceneViewPlanePivot, renderPlane) + AxisMask(pivot, renderPlane);
            }
            else
            {
                pivot = Selection.activeTransform == null ? pivot : Selection.activeTransform.position;

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

        if (drawGrid)
        {
            if (ortho)
            {
                // ortho don't care about pivots
                DrawGridOrthographic(cam);
            }
            else
            {
                                #if PROFILE_TIMES
                profiler.LogStart("DrawGridPerspective");
                                #endif

                if (gridRepaint || pivot != lastPivot || Mathf.Abs(camDistance - lastDistance) > lastDistance / 2 || camDir != prevCamDir)
                {
                    prevCamDir   = camDir;
                    gridRepaint  = false;
                    lastPivot    = pivot;
                    lastDistance = camDistance;

                    if (fullGrid)
                    {
                        //  if perspective and 3d, use pivot like normal
                        pg_GridRenderer.DrawGridPerspective(cam, pivot, snapValue, new Color[3] {
                            gridColorX, gridColorY, gridColorZ
                        }, alphaBump);
                    }
                    else
                    {
                        if ((renderPlane & Axis.X) == Axis.X)
                        {
                            planeGridDrawDistance = pg_GridRenderer.DrawPlane(cam, pivot + Vector3.right * offset, Vector3.up, Vector3.forward, snapValue, gridColorX, alphaBump);
                        }

                        if ((renderPlane & Axis.Y) == Axis.Y)
                        {
                            planeGridDrawDistance = pg_GridRenderer.DrawPlane(cam, pivot + Vector3.up * offset, Vector3.right, Vector3.forward, snapValue, gridColorY, alphaBump);
                        }

                        if ((renderPlane & Axis.Z) == Axis.Z)
                        {
                            planeGridDrawDistance = pg_GridRenderer.DrawPlane(cam, pivot + Vector3.forward * offset, Vector3.up, Vector3.right, snapValue, gridColorZ, alphaBump);
                        }
                    }
                }
                                #if PROFILE_TIMES
                profiler.LogFinish("DrawGridPerspective");
                                #endif
            }
        }

        // Always keep track of the selection
        if (!Selection.transforms.Contains(lastTransform))
        {
            if (Selection.activeTransform)
            {
                lastTransform = Selection.activeTransform;

                lastPosition = Selection.activeTransform.position;
                // lastRotation = Selection.activeTransform.localRotation.eulerAngles;
                lastScale = Selection.activeTransform.localScale;
            }
        }

        if (!snapEnabled || GUIUtility.hotControl < 1)
        {
            return;
        }

        // Bugger.SetKey("Toggle Snap Off", toggleTempSnap);

        /**
         *	Snapping (for all the junk in PG, this method is literally the only code that actually affects anything).
         */
        if (Selection.activeTransform)
        {
            if (!FuzzyEquals(lastTransform.position, lastPosition))
            {
                Transform selected = lastTransform;

                if (!toggleTempSnap)
                {
                    Vector3 old  = selected.position;
                    Vector3 mask = old - lastPosition;

                    bool constraintsOn = toggleAxisConstraint ? !useAxisConstraints : useAxisConstraints;

                    if (constraintsOn)
                    {
                        selected.position = pg_Util.SnapValue(old, mask, snapValue);
                    }
                    else
                    {
                        selected.position = pg_Util.SnapValue(old, snapValue);
                    }

                    Vector3 offset = selected.position - old;

                    OffsetTransforms(Selection.transforms, selected, offset);
                }

                lastPosition = selected.position;
            }

            if (!FuzzyEquals(lastTransform.localScale, lastScale) && scaleSnapEnabled)
            {
                if (!toggleTempSnap)
                {
                    Vector3 old  = lastTransform.localScale;
                    Vector3 mask = old - lastScale;

                    foreach (Transform t in Selection.transforms)
                    {
                        t.localScale = pg_Util.SnapValue(t.localScale, mask, snapValue);
                    }

                    lastScale = lastTransform.localScale;
                }
            }
        }
    }