Beispiel #1
0
        public static bool SetupWorkPlane(Camera camera, Vector3 worldCenterPoint, Vector3 worldDirection, ref CSGPlane workPlane)
        {
            if (camera == null || !camera)
            {
                return(false);
            }

            if (camera.orthographic)
            {
                CSGGrid.ForceGrid = false;
                workPlane         = CSGGrid.CurrentWorkGridPlane;
                return(true);
            }

            var normal = worldDirection;

            /*
             * if (YMoveModeActive)
             * {
             *      var forward = camera.transform.forward;
             *      Vector3 tangent, binormal;
             *      GeometryUtility.CalculateTangents(normal, out tangent, out binormal);
             *      if (Mathf.Abs(Vector3.Dot(forward, tangent)) > Mathf.Abs(Vector3.Dot(forward, binormal)))
             *              normal = tangent;
             *      else
             *              normal = binormal;
             * }*/

            workPlane = new CSGPlane(GridUtility.CleanNormal(normal), worldCenterPoint);
            return(CSGGrid.SetForcedGrid(camera, workPlane));
        }
Beispiel #2
0
        public static bool SetupRayWorkPlane(Vector3 worldOrigin, Vector3 worldDirection, ref CSGPlane outWorkPlane)
        {
            var camera = Camera.current;

            if (camera == null || !camera)
            {
                return(false);
            }

            Vector3 tangent, normal;
            var     cameraBackwards      = -camera.transform.forward;
            var     closestAxisForward   = GeometryUtility.SnapToClosestAxis(cameraBackwards);
            var     closestAxisDirection = GeometryUtility.SnapToClosestAxis(worldDirection);

            if (Vector3.Dot(closestAxisForward, closestAxisDirection) != 0)
            {
                float dot1 = Mathf.Abs(Vector3.Dot(cameraBackwards, MathConstants.rightVector3));
                float dot2 = Mathf.Abs(Vector3.Dot(cameraBackwards, MathConstants.upVector3));
                float dot3 = Mathf.Abs(Vector3.Dot(cameraBackwards, MathConstants.forwardVector3));
                if (dot1 < dot2)
                {
                    if (dot1 < dot3)
                    {
                        tangent = MathConstants.rightVector3;
                    }
                    else
                    {
                        tangent = MathConstants.forwardVector3;
                    }
                }
                else
                {
                    if (dot2 < dot3)
                    {
                        tangent = MathConstants.upVector3;
                    }
                    else
                    {
                        tangent = MathConstants.forwardVector3;
                    }
                }
            }
            else
            {
                tangent = Vector3.Cross(worldDirection, closestAxisForward);
            }

            if (!camera.orthographic)
            {
                normal = Vector3.Cross(worldDirection, tangent);
            }
            else
            {
                normal = cameraBackwards;
            }

            outWorkPlane = new CSGPlane(GridUtility.CleanNormal(normal), worldOrigin);

            return(CSGGrid.SetForcedGrid(outWorkPlane));
        }
        public static Vector3 SnapPointToGrid(Vector3 point, CSGPlane plane, ref List <Vector3> snappingEdges, out CSGBrush snappedOnBrush, CSGBrush[] ignoreBrushes, bool ignoreAllBrushes = false)
        {
            snappedOnBrush = null;
            var toggleSnapping = SelectionUtility.IsSnappingToggled;
            var doSnapping     = RealtimeCSG.CSGSettings.SnapToGrid ^ toggleSnapping;

            var snappedPoint = point;

            if (doSnapping)
            {
                snappedPoint = snappedPoint + RealtimeCSG.Grid.ForceSnapDeltaToGrid(MathConstants.zeroVector3, snappedPoint);
                snappedPoint = RealtimeCSG.Grid.PointFromGridSpace(RealtimeCSG.Grid.CubeProject(RealtimeCSG.Grid.PlaneToGridSpace(plane), RealtimeCSG.Grid.PointToGridSpace(snappedPoint)));

                // snap twice to get rid of some tiny movements caused by the projection in depth
                snappedPoint = snappedPoint + RealtimeCSG.Grid.ForceSnapDeltaToGrid(MathConstants.zeroVector3, snappedPoint);
                snappedPoint = RealtimeCSG.Grid.PointFromGridSpace(RealtimeCSG.Grid.CubeProject(RealtimeCSG.Grid.PlaneToGridSpace(plane), RealtimeCSG.Grid.PointToGridSpace(snappedPoint)));
            }
            else
            {
                snappedPoint = GeometryUtility.ProjectPointOnPlane(plane, snappedPoint);
            }

            //GeometryUtility.ProjectPointOnPlane(plane, snappedPoint);
            if (doSnapping && !ignoreAllBrushes)
            {
                return(GridUtility.SnapToWorld(plane, point, snappedPoint, ref snappingEdges, out snappedOnBrush, ignoreBrushes));
            }

            return(snappedPoint);
        }
        public bool UpdateRadius(TexGen surfaceTexGen, Vector3 currentSurfacePoint)
        {
            var handleSize     = CSG_HandleUtility.GetHandleSize(this.RotateCenterPoint);
            var vectorToCenter = currentSurfacePoint - this.RotateCenterPoint;

            this.RotateRadius = vectorToCenter.magnitude;
            vectorToCenter.Normalize();

            var rotateCurrentAngle = 0.0f;

            if (!this.HaveRotateStartAngle)
            {
                if (this.RotateRadius > handleSize * GUIConstants.minRotateRadius)
                {
                    this.HaveRotateStartAngle    = true;
                    this.RotateOriginalAngle     = surfaceTexGen.RotationAngle;
                    this.RotateStartVector       = vectorToCenter;
                    this.RotateCurrentStartAngle = GeometryUtility.SignedAngle(this.RotateSurfaceTangent, vectorToCenter, this.RotateSurfaceNormal);
                }
                this.RotateRadius = Math.Max(this.RotateRadius, handleSize * GUIConstants.minRotateRadius);
            }
            else
            {
                rotateCurrentAngle = GeometryUtility.SignedAngle(this.RotateStartVector, vectorToCenter, this.RotateSurfaceNormal);
                var minSize        = handleSize * GUIConstants.minRotateRadius * 2;
                var radiusStepSize = minSize;
                this.RotateRadius = (Mathf.CeilToInt(((this.RotateRadius - minSize) / radiusStepSize) - 0.5f) * radiusStepSize) + minSize;
            }

            // snap texture coordinates in world/local space
            this.RotateCurrentSnappedAngle = GridUtility.SnappedAngle(this.RotateOriginalAngle + rotateCurrentAngle) - this.RotateOriginalAngle;

            return(this.HaveRotateStartAngle);
        }
Beispiel #5
0
        public static Vector3 SnapDeltaRelative(Camera camera, Vector3 worldDeltaMovement, bool snapToGridPlane = true)
        {
            UpdateGridOrientation(camera);
            if (gridOrientation == null)
            {
                return(worldDeltaMovement);
            }

            var worldPlane  = gridOrientation.gridWorkPlane;
            var scaleVector = gridOrientation.gridSnapScale;
            var snapVector  = gridOrientation.gridSnapVector;

            var gridLocalDeltaMovement = VectorToGridSpace(worldDeltaMovement);
            var gridLocalPlane         = PlaneToGridSpace(worldPlane);

            if (snapToGridPlane)
            {
                scaleVector.x *= (Mathf.Abs(gridLocalPlane.a) >= 1 - MathConstants.EqualityEpsilon) ? 0 : 1;
                scaleVector.y *= (Mathf.Abs(gridLocalPlane.b) >= 1 - MathConstants.EqualityEpsilon) ? 0 : 1;
                scaleVector.z *= (Mathf.Abs(gridLocalPlane.c) >= 1 - MathConstants.EqualityEpsilon) ? 0 : 1;
            }
            var snappedDeltaMovement = gridLocalDeltaMovement;

            if (snapToGridPlane)
            {
                snappedDeltaMovement = GeometryUtility.ProjectPointOnPlane(gridLocalPlane, snappedDeltaMovement);
            }
            snappedDeltaMovement = GridUtility.CleanPosition(snappedDeltaMovement);

            snappedDeltaMovement = SnapRoundPosition(snappedDeltaMovement, snapVector);

            if (snapToGridPlane)
            {
                snappedDeltaMovement = GeometryUtility.ProjectPointOnPlane(gridLocalPlane, snappedDeltaMovement);
            }
            snappedDeltaMovement = GridUtility.CleanPosition(snappedDeltaMovement);


            snappedDeltaMovement.x *= scaleVector.x;
            snappedDeltaMovement.y *= scaleVector.y;
            snappedDeltaMovement.z *= scaleVector.z;

            worldDeltaMovement = VectorFromGridSpace(snappedDeltaMovement);
            return(worldDeltaMovement);
        }
Beispiel #6
0
        public static void MoveControlMeshVertices(CSGBrush[] brushes, Vector3 offset)
        {
            for (int b = 0; b < brushes.Length; b++)
            {
                var controlMesh = brushes[b].ControlMesh;
                if (controlMesh == null ||
                    controlMesh.Vertices == null)
                {
                    continue;
                }

                var localOffset = brushes[b].transform.worldToLocalMatrix.MultiplyVector(offset);
                for (var p = 0; p < controlMesh.Vertices.Length; p++)
                {
                    controlMesh.Vertices[p] = GridUtility.CleanPosition(controlMesh.Vertices[p] + localOffset);
                }
            }
        }
Beispiel #7
0
        public static void MoveControlMeshVertices(CSGBrush brush, Vector3 offset)
        {
            if (!brush)
            {
                return;
            }

            var controlMesh = brush.ControlMesh;

            if (controlMesh == null ||
                controlMesh.Vertices == null)
            {
                return;
            }

            var localOffset = brush.transform.worldToLocalMatrix.MultiplyVector(offset);

            for (var p = 0; p < controlMesh.Vertices.Length; p++)
            {
                controlMesh.Vertices[p] = GridUtility.CleanPosition(controlMesh.Vertices[p] + localOffset);
            }
        }
        public static Vector3 SnapPointToGrid(Vector3 point, CSGPlane plane, ref List <Vector3> snappingEdges, out CSGBrush snappedOnBrush, CSGBrush[] ignoreBrushes, bool ignoreAllBrushes = false)
        {
            snappedOnBrush = null;
            // Note: relative snapping wouldn't make sense here since it's a single point that's being snapped and there is no relative movement
            var activeSnappingMode = RealtimeCSG.CSGSettings.ActiveSnappingMode;

            var snappedPoint = point;

            switch (activeSnappingMode)
            {
            case SnapMode.RelativeSnapping:     // TODO: fixme
            case SnapMode.GridSnapping:
            {
                snappedPoint = snappedPoint + RealtimeCSG.CSGGrid.ForceSnapDeltaToGrid(MathConstants.zeroVector3, snappedPoint);
                snappedPoint = RealtimeCSG.CSGGrid.PointFromGridSpace(RealtimeCSG.CSGGrid.CubeProject(RealtimeCSG.CSGGrid.PlaneToGridSpace(plane), RealtimeCSG.CSGGrid.PointToGridSpace(snappedPoint)));

                // snap twice to get rid of some tiny movements caused by the projection in depth
                snappedPoint = snappedPoint + RealtimeCSG.CSGGrid.ForceSnapDeltaToGrid(MathConstants.zeroVector3, snappedPoint);
                snappedPoint = RealtimeCSG.CSGGrid.PointFromGridSpace(RealtimeCSG.CSGGrid.CubeProject(RealtimeCSG.CSGGrid.PlaneToGridSpace(plane), RealtimeCSG.CSGGrid.PointToGridSpace(snappedPoint)));

                if (!ignoreAllBrushes)
                {
                    return(GridUtility.SnapToWorld(plane, point, snappedPoint, ref snappingEdges, out snappedOnBrush, ignoreBrushes));
                }

                return(snappedPoint);
            }

            default:
            case SnapMode.None:
            {
                snappedPoint = GeometryUtility.ProjectPointOnPlane(plane, snappedPoint);
                return(snappedPoint);
            }
            }
        }
Beispiel #9
0
        public static Vector3 SnapDeltaToRayGrid(Camera camera, Ray worldRay, Vector3 worldDeltaMovement, Vector3[] worldPoints, bool snapToSelf = false)
        {
            UpdateGridOrientation(camera);
            if (gridOrientation == null || worldPoints == null || worldPoints.Length == 0)
            {
                return(worldDeltaMovement);
            }

            var snapVector  = gridOrientation.gridSnapVector;
            var scaleVector = gridOrientation.gridSnapScale;

            var localDeltaMovement = VectorToGridSpace(worldDeltaMovement);
            var localLineDir       = VectorToGridSpace(worldRay.direction);
            var localLineOrg       = PointToGridSpace(worldRay.origin);

            scaleVector.x *= ((Mathf.Abs(localLineDir.y) >= 1 - MathConstants.EqualityEpsilon) || (Mathf.Abs(localLineDir.z) >= 1 - MathConstants.EqualityEpsilon)) ? 0 : 1;
            scaleVector.y *= ((Mathf.Abs(localLineDir.x) >= 1 - MathConstants.EqualityEpsilon) || (Mathf.Abs(localLineDir.z) >= 1 - MathConstants.EqualityEpsilon)) ? 0 : 1;
            scaleVector.z *= ((Mathf.Abs(localLineDir.x) >= 1 - MathConstants.EqualityEpsilon) || (Mathf.Abs(localLineDir.y) >= 1 - MathConstants.EqualityEpsilon)) ? 0 : 1;

            var snappedDeltaMovement = localDeltaMovement;

            if (Mathf.Abs(scaleVector.x) < MathConstants.EqualityEpsilon)
            {
                snappedDeltaMovement.x = 0;
            }
            if (Mathf.Abs(scaleVector.y) < MathConstants.EqualityEpsilon)
            {
                snappedDeltaMovement.y = 0;
            }
            if (Mathf.Abs(scaleVector.z) < MathConstants.EqualityEpsilon)
            {
                snappedDeltaMovement.z = 0;
            }

            Vector3[] localPoints;
            if (worldPoints.Length > 1)
            {
                var bounds = new AABB();
                bounds.Reset();
                for (int i = 0; i < worldPoints.Length; i++)
                {
                    var localPoint = GeometryUtility.ProjectPointOnInfiniteLine(PointToGridSpace(worldPoints[i]), localLineOrg, localLineDir);
                    bounds.Extend(localPoint);
                }
                localPoints = bounds.GetCorners();
            }
            else
            {
                localPoints = new Vector3[] { GeometryUtility.ProjectPointOnInfiniteLine(PointToGridSpace(worldPoints[0]), localLineOrg, localLineDir) };
            }

            for (int i = 0; i < localPoints.Length; i++)
            {
                var oldPoint = localPoints[i];
                var newPoint = GeometryUtility.ProjectPointOnInfiniteLine(oldPoint + localDeltaMovement, localLineOrg, localLineDir);

                var snappedNewPoint = SnapRoundPosition(newPoint, snapVector);

                snappedNewPoint = GridUtility.CleanPosition(GeometryUtility.ProjectPointOnInfiniteLine(snappedNewPoint, localLineOrg, localLineDir));

                var foundDeltaMovement = (snappedNewPoint - oldPoint);

                foundDeltaMovement.x *= scaleVector.x;
                foundDeltaMovement.y *= scaleVector.y;
                foundDeltaMovement.z *= scaleVector.z;

                if (i == 0 || Math.Abs(foundDeltaMovement.x) < Mathf.Abs(snappedDeltaMovement.x))
                {
                    snappedDeltaMovement.x = foundDeltaMovement.x;
                }
                if (i == 0 || Math.Abs(foundDeltaMovement.y) < Mathf.Abs(snappedDeltaMovement.y))
                {
                    snappedDeltaMovement.y = foundDeltaMovement.y;
                }
                if (i == 0 || Math.Abs(foundDeltaMovement.z) < Mathf.Abs(snappedDeltaMovement.z))
                {
                    snappedDeltaMovement.z = foundDeltaMovement.z;
                }
            }

            if (snapToSelf)
            {
                var snapDelta = (snappedDeltaMovement - localDeltaMovement);
                if (Mathf.Abs(snapDelta.x) > Mathf.Abs(localDeltaMovement.x))
                {
                    snappedDeltaMovement.x = 0;
                }
                if (Mathf.Abs(snapDelta.y) > Mathf.Abs(localDeltaMovement.y))
                {
                    snappedDeltaMovement.y = 0;
                }
                if (Mathf.Abs(snapDelta.z) > Mathf.Abs(localDeltaMovement.z))
                {
                    snappedDeltaMovement.z = 0;
                }
            }

            worldDeltaMovement = VectorFromGridSpace(snappedDeltaMovement);
            return(worldDeltaMovement);
        }
Beispiel #10
0
        public static Vector3 SnapDeltaToGrid(Camera camera, Vector3 worldDeltaMovement, Vector3[] worldPoints, bool snapToGridPlane = true, bool snapToSelf = false)
        {
            UpdateGridOrientation(camera);
            if (gridOrientation == null || worldPoints == null || worldPoints.Length == 0)
            {
                return(worldDeltaMovement);
            }

            var worldPlane  = gridOrientation.gridWorkPlane;
            var scaleVector = gridOrientation.gridSnapScale;
            var snapVector  = gridOrientation.gridSnapVector;

            var gridLocalDeltaMovement = VectorToGridSpace(worldDeltaMovement);
            var gridLocalPlane         = PlaneToGridSpace(worldPlane);

            if (snapToGridPlane)
            {
                scaleVector.x *= (Mathf.Abs(gridLocalPlane.a) >= 1 - MathConstants.EqualityEpsilon) ? 0 : 1;
                scaleVector.y *= (Mathf.Abs(gridLocalPlane.b) >= 1 - MathConstants.EqualityEpsilon) ? 0 : 1;
                scaleVector.z *= (Mathf.Abs(gridLocalPlane.c) >= 1 - MathConstants.EqualityEpsilon) ? 0 : 1;
            }
            var snappedDeltaMovement = gridLocalDeltaMovement;

            if (Mathf.Abs(scaleVector.x) < MathConstants.EqualityEpsilon)
            {
                snappedDeltaMovement.x = 0;
            }
            if (Mathf.Abs(scaleVector.y) < MathConstants.EqualityEpsilon)
            {
                snappedDeltaMovement.y = 0;
            }
            if (Mathf.Abs(scaleVector.z) < MathConstants.EqualityEpsilon)
            {
                snappedDeltaMovement.z = 0;
            }

            Vector3[] gridLocalPoints;
            if (worldPoints.Length > 1)
            {
                var bounds = new AABB();
                bounds.Reset();
                for (int i = 0; i < worldPoints.Length; i++)
                {
                    Vector3 localPoint = PointToGridSpace(worldPoints[i]);
                    if (snapToGridPlane)
                    {
                        localPoint = GeometryUtility.ProjectPointOnPlane(gridLocalPlane, localPoint);
                    }
                    if (float.IsNaN(localPoint.x) || float.IsNaN(localPoint.y) || float.IsNaN(localPoint.z) ||
                        float.IsInfinity(localPoint.x) || float.IsInfinity(localPoint.y) || float.IsInfinity(localPoint.z))
                    {
                        continue;
                    }
                    bounds.Extend(localPoint);
                }
                gridLocalPoints = bounds.GetCorners();
            }
            else
            {
                var     localGridSpacePoint = PointToGridSpace(worldPoints[0]);
                Vector3 projectedPoint      = localGridSpacePoint;
                if (snapToGridPlane)
                {
                    projectedPoint = GeometryUtility.ProjectPointOnPlane(gridLocalPlane, localGridSpacePoint);
                }

                if (float.IsNaN(projectedPoint.x) || float.IsNaN(projectedPoint.y) || float.IsNaN(projectedPoint.z) ||
                    float.IsInfinity(projectedPoint.x) || float.IsInfinity(projectedPoint.y) || float.IsInfinity(projectedPoint.z))
                {
                    gridLocalPoints = new Vector3[0] {
                    }
                }
                ;
                else
                {
                    gridLocalPoints = new Vector3[] { projectedPoint }
                };
            }

            for (int i = 0; i < gridLocalPoints.Length; i++)
            {
                var oldPoint = gridLocalPoints[i];
                var newPoint = gridLocalPoints[i] + gridLocalDeltaMovement;
                if (snapToGridPlane)
                {
                    newPoint = GeometryUtility.ProjectPointOnPlane(gridLocalPlane, newPoint);
                }
                newPoint = GridUtility.CleanPosition(newPoint);

                var snappedNewPoint = SnapRoundPosition(newPoint, snapVector);

                if (snapToGridPlane)
                {
                    snappedNewPoint = GeometryUtility.ProjectPointOnPlane(gridLocalPlane, snappedNewPoint);
                }
                snappedNewPoint = GridUtility.CleanPosition(snappedNewPoint);

                var foundDeltaMovement = (snappedNewPoint - oldPoint);

                foundDeltaMovement.x *= scaleVector.x;
                foundDeltaMovement.y *= scaleVector.y;
                foundDeltaMovement.z *= scaleVector.z;

                if (i == 0 || Math.Abs(foundDeltaMovement.x) < Mathf.Abs(snappedDeltaMovement.x))
                {
                    snappedDeltaMovement.x = foundDeltaMovement.x;
                }
                if (i == 0 || Math.Abs(foundDeltaMovement.y) < Mathf.Abs(snappedDeltaMovement.y))
                {
                    snappedDeltaMovement.y = foundDeltaMovement.y;
                }
                if (i == 0 || Math.Abs(foundDeltaMovement.z) < Mathf.Abs(snappedDeltaMovement.z))
                {
                    snappedDeltaMovement.z = foundDeltaMovement.z;
                }
            }

            if (snapToSelf)
            {
                var snapDelta = (snappedDeltaMovement - gridLocalDeltaMovement);
                if (Mathf.Abs(snapDelta.x) > Mathf.Abs(gridLocalDeltaMovement.x))
                {
                    snappedDeltaMovement.x = 0;
                }
                if (Mathf.Abs(snapDelta.y) > Mathf.Abs(gridLocalDeltaMovement.y))
                {
                    snappedDeltaMovement.y = 0;
                }
                if (Mathf.Abs(snapDelta.z) > Mathf.Abs(gridLocalDeltaMovement.z))
                {
                    snappedDeltaMovement.z = 0;
                }
            }

            worldDeltaMovement = VectorFromGridSpace(snappedDeltaMovement);
            return(worldDeltaMovement);
        }
Beispiel #11
0
        public static Vector3 SnapToWorld(Camera camera, CSGPlane snapPlane, Vector3 unsnappedPosition, Vector3 snappedPosition, ref List <Vector3> snappingEdges, out CSGBrush snappedOnBrush, CSGBrush[] ignoreBrushes = null)
        {
            snappedOnBrush = null;

            test_points[0] = unsnappedPosition;
            test_points[1] = snappedPosition;
            worldIntersections.Clear();

            for (int i = 0; i < test_points.Length; i++)
            {
                var test_point2D = CameraUtility.WorldToGUIPoint(test_points[i]);
                LegacyBrushIntersection intersection;
                if (SceneQueryUtility.FindWorldIntersection(camera, test_point2D, out intersection))
                {
                    if (intersection.brush &&
                        intersection.brush.ControlMesh != null)
                    {
                        intersection.worldIntersection = GeometryUtility.ProjectPointOnPlane(snapPlane, intersection.worldIntersection);

                        worldIntersections.Add(intersection);
                    }
                }
            }

            var     old_difference           = snappedPosition - unsnappedPosition;
            var     old_difference_magnitude = old_difference.magnitude * 1.5f;
            Vector3 newSnappedPoint          = snappedPosition;

            CSGPlane?snappingPlane = snapPlane;

            for (int i = 0; i < worldIntersections.Count; i++)
            {
                if (ignoreBrushes != null &&
                    ArrayUtility.Contains(ignoreBrushes, worldIntersections[i].brush))
                {
                    continue;
                }

                List <Vector3> outEdgePoints;
                Vector3        outPosition;
                if (GridUtility.SnapToVertices(worldIntersections[i].brush,
                                               snappingPlane, unsnappedPosition,
                                               out outEdgePoints,
                                               out outPosition))
                {
                    var new_difference           = outPosition - unsnappedPosition;
                    var new_difference_magnitude = new_difference.magnitude;

                    if (new_difference_magnitude <= old_difference_magnitude + MathConstants.EqualityEpsilon)
                    {
                        old_difference_magnitude = new_difference_magnitude;
                        newSnappedPoint          = outPosition;
                        snappingEdges            = outEdgePoints;
                        snappedOnBrush           = worldIntersections[i].brush;
                    }
                }
                if (GridUtility.SnapToEdge(camera,
                                           worldIntersections[i].brush, snappingPlane ?? worldIntersections[i].worldPlane,
                                           worldIntersections[i].worldIntersection,
                                           out outEdgePoints,
                                           out outPosition))
                {
                    var new_difference           = outPosition - unsnappedPosition;
                    var new_difference_magnitude = new_difference.magnitude * 1.1f;

                    if (new_difference_magnitude <= old_difference_magnitude + MathConstants.EqualityEpsilon)
                    {
                        old_difference_magnitude = new_difference_magnitude;
                        newSnappedPoint          = outPosition;
                        snappingEdges            = outEdgePoints;
                        snappedOnBrush           = worldIntersections[i].brush;
                    }
                }
            }

            //snappingEdges = FindAllEdgesThatTouchPoint(snappedOnBrush, newSnappedPoint);
            return(newSnappedPoint);
        }
Beispiel #12
0
        public static Vector3 SnapToWorld(CSGPlane snapPlane, Vector3 unsnappedPosition, Vector3 snappedPosition, ref List <Vector3> snappingEdges, out CSGBrush snappedOnBrush, CSGBrush[] ignoreBrushes = null)
        {
            snappedOnBrush = null;

            var intersections = new BrushIntersection[0];
            var test_points   = new Vector3[] { unsnappedPosition, snappedPosition };
            BrushIntersection intersection;

            for (int i = 0; i < test_points.Length; i++)
            {
                var test_point2D = HandleUtility.WorldToGUIPoint(test_points[i]);
                if (SceneQueryUtility.FindWorldIntersection(test_point2D, out intersection, MathConstants.GrowBrushFactor))
                {
                    if (intersection.brush &&
                        intersection.brush.ControlMesh != null)
                    {
                        intersection.worldIntersection = GeometryUtility.ProjectPointOnPlane(snapPlane, intersection.worldIntersection);
                        ArrayUtility.Add(ref intersections, intersection);
                    }
                }
            }

            var     old_difference           = snappedPosition - unsnappedPosition;
            var     old_difference_magnitude = old_difference.magnitude * 1.5f;
            Vector3 newSnappedPoint          = snappedPosition;

            CSGPlane?snappingPlane = snapPlane;

            for (int i = 0; i < intersections.Length; i++)
            {
                if (ignoreBrushes != null &&
                    ArrayUtility.Contains(ignoreBrushes, intersections[i].brush))
                {
                    continue;
                }
                List <Vector3> outEdgePoints;
                Vector3        outPosition;
                if (GridUtility.SnapToVertices(intersections[i].brush,
                                               snappingPlane, unsnappedPosition,
                                               out outEdgePoints,
                                               out outPosition))
                {
                    var new_difference           = outPosition - unsnappedPosition;
                    var new_difference_magnitude = new_difference.magnitude;

                    if (new_difference_magnitude <= old_difference_magnitude + MathConstants.EqualityEpsilon)
                    {
                        old_difference_magnitude = new_difference_magnitude;
                        newSnappedPoint          = outPosition;
                        snappingEdges            = outEdgePoints;
                        snappedOnBrush           = intersections[i].brush;
                    }
                }
                if (GridUtility.SnapToEdge(intersections[i].brush, snappingPlane ?? intersections[i].plane,
                                           intersections[i].worldIntersection,
                                           out outEdgePoints,
                                           out outPosition))
                {
                    var new_difference           = outPosition - unsnappedPosition;
                    var new_difference_magnitude = new_difference.magnitude * 1.1f;

                    if (new_difference_magnitude <= old_difference_magnitude + MathConstants.EqualityEpsilon)
                    {
                        old_difference_magnitude = new_difference_magnitude;
                        newSnappedPoint          = outPosition;
                        snappingEdges            = outEdgePoints;
                        snappedOnBrush           = intersections[i].brush;
                    }
                }
            }

            //snappingEdges = FindAllEdgesThatTouchPoint(snappedOnBrush, newSnappedPoint);
            return(newSnappedPoint);
        }
Beispiel #13
0
        public static bool HandleSceneKeyUp(IEditMode tool, bool checkForTextEditing = true)
        {
            if (EditorGUIUtility.editingTextField && checkForTextEditing)
            {
                return(false);
            }

            if (Keys.MakeSelectedPassThroughKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                OperationsUtility.SetPassThroughOnSelected(); return(true);
            }
            if (Keys.MakeSelectedAdditiveKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                OperationsUtility.ModifyOperationsOnSelected(CSGOperationType.Additive); return(true);
            }
            if (Keys.MakeSelectedSubtractiveKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                OperationsUtility.ModifyOperationsOnSelected(CSGOperationType.Subtractive); return(true);
            }
            if (Keys.MakeSelectedIntersectingKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                OperationsUtility.ModifyOperationsOnSelected(CSGOperationType.Intersecting); return(true);
            }

            if (Keys.ToggleSelectedObjectVisibilityKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                SelectionUtility.ToggleSelectedObjectVisibility(); return(true);
            }
            if (Keys.QuickHideSelectedObjectsKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                SelectionUtility.HideSelectedObjects(); return(true);
            }
            if (Keys.QuickHideUnselectedObjectsKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                SelectionUtility.HideUnselectedObjects(); return(true);
            }
            if (Keys.UnHideAllObjectsKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                SelectionUtility.UnHideAll(); return(true);
            }
            if (Keys.CancelActionKey.IsKeyPressed())
            {
                SelectionUtility.DeselectAll(); return(true);
            }

            if (Keys.HalfGridSizeKey.IsKeyPressed())
            {
                GridUtility.HalfGridSize(); return(true);
            }
            if (Keys.DoubleGridSizeKey.IsKeyPressed())
            {
                GridUtility.DoubleGridSize(); return(true);
            }
            if (Keys.ToggleShowGridKey.IsKeyPressed())
            {
                GridUtility.ToggleShowGrid(); return(true);
            }
            if (Keys.ToggleSnappingKey.IsKeyPressed())
            {
                GridUtility.ToggleSnapToGrid(); return(true);
            }
            return(false);
        }
        static void OnBottomBarGUI(SceneView sceneView, Rect barSize)
        {
            //if (Event.current.type == EventType.Layout)
            //	return;

            var  snapMode          = RealtimeCSG.CSGSettings.SnapMode;
            var  uniformGrid       = RealtimeCSG.CSGSettings.UniformGrid;
            var  moveSnapVector    = RealtimeCSG.CSGSettings.SnapVector;
            var  rotationSnap      = RealtimeCSG.CSGSettings.SnapRotation;
            var  scaleSnap         = RealtimeCSG.CSGSettings.SnapScale;
            var  showGrid          = RealtimeCSG.CSGSettings.GridVisible;
            var  lockAxisX         = RealtimeCSG.CSGSettings.LockAxisX;
            var  lockAxisY         = RealtimeCSG.CSGSettings.LockAxisY;
            var  lockAxisZ         = RealtimeCSG.CSGSettings.LockAxisZ;
            var  distanceUnit      = RealtimeCSG.CSGSettings.DistanceUnit;
            var  helperSurfaces    = RealtimeCSG.CSGSettings.VisibleHelperSurfaces;
            var  showWireframe     = RealtimeCSG.CSGSettings.IsWireframeShown(sceneView);
            var  skin              = CSG_GUIStyleUtility.Skin;
            var  updateSurfaces    = false;
            bool wireframeModified = false;

            var viewWidth = sceneView.position.width;

            float layoutHeight = barSize.height;
            float layoutX      = 6.0f;

            bool modified = false;

            GUI.changed = false;
            {
                currentRect.width  = 27;
                currentRect.y      = 0;
                currentRect.height = layoutHeight - currentRect.y;
                currentRect.y     += barSize.y;
                currentRect.x      = layoutX;
                layoutX           += currentRect.width;

                #region "Grid" button
                if (showGrid)
                {
                    showGrid = GUI.Toggle(currentRect, showGrid, skin.gridIconOn, EditorStyles.toolbarButton);
                }
                else
                {
                    showGrid = GUI.Toggle(currentRect, showGrid, skin.gridIcon, EditorStyles.toolbarButton);
                }
                //(x:6.00, y:0.00, width:27.00, height:18.00)
                TooltipUtility.SetToolTip(showGridTooltip, currentRect);
                #endregion

                if (viewWidth >= 800)
                {
                    layoutX += 6;                     //(x:33.00, y:0.00, width:6.00, height:6.00)
                }
                var prevBackgroundColor   = GUI.backgroundColor;
                var lockedBackgroundColor = skin.lockedBackgroundColor;
                if (lockAxisX)
                {
                    GUI.backgroundColor = lockedBackgroundColor;
                }

                #region "X" lock button
                currentRect.width  = 17;
                currentRect.y      = 0;
                currentRect.height = layoutHeight - currentRect.y;
                currentRect.y     += barSize.y;
                currentRect.x      = layoutX;
                layoutX           += currentRect.width;

                lockAxisX = !GUI.Toggle(currentRect, !lockAxisX, xLabel, skin.xToolbarButton);
                //(x:39.00, y:0.00, width:17.00, height:18.00)
                if (lockAxisX)
                {
                    TooltipUtility.SetToolTip(xTooltipOn, currentRect);
                }
                else
                {
                    TooltipUtility.SetToolTip(xTooltipOff, currentRect);
                }
                GUI.backgroundColor = prevBackgroundColor;
                #endregion

                #region "Y" lock button
                currentRect.x = layoutX;
                layoutX      += currentRect.width;

                if (lockAxisY)
                {
                    GUI.backgroundColor = lockedBackgroundColor;
                }
                lockAxisY = !GUI.Toggle(currentRect, !lockAxisY, yLabel, skin.yToolbarButton);
                //(x:56.00, y:0.00, width:17.00, height:18.00)
                if (lockAxisY)
                {
                    TooltipUtility.SetToolTip(yTooltipOn, currentRect);
                }
                else
                {
                    TooltipUtility.SetToolTip(yTooltipOff, currentRect);
                }
                GUI.backgroundColor = prevBackgroundColor;
                #endregion

                #region "Z" lock button
                currentRect.x = layoutX;
                layoutX      += currentRect.width;

                if (lockAxisZ)
                {
                    GUI.backgroundColor = lockedBackgroundColor;
                }
                lockAxisZ = !GUI.Toggle(currentRect, !lockAxisZ, zLabel, skin.zToolbarButton);
                //(x:56.00, y:0.00, width:17.00, height:18.00)
                if (lockAxisZ)
                {
                    TooltipUtility.SetToolTip(zTooltipOn, currentRect);
                }
                else
                {
                    TooltipUtility.SetToolTip(zTooltipOff, currentRect);
                }
                GUI.backgroundColor = prevBackgroundColor;
                #endregion
            }
            modified = GUI.changed || modified;

            if (viewWidth >= 800)
            {
                layoutX += 6;                 // (x:91.00, y:0.00, width:6.00, height:6.00)
            }
            #region "SnapMode" button
            GUI.changed = false;
            {
                currentRect.width  = 27;
                currentRect.y      = 0;
                currentRect.height = layoutHeight - currentRect.y;
                currentRect.y     += barSize.y;
                currentRect.x      = layoutX;
                layoutX           += currentRect.width;


                switch (snapMode)
                {
                case SnapMode.GridSnapping:
                {
                    var newValue = GUI.Toggle(currentRect, snapMode == SnapMode.GridSnapping, CSG_GUIStyleUtility.Skin.gridSnapIconOn, EditorStyles.toolbarButton);
                    if (GUI.changed)
                    {
                        snapMode = newValue ? SnapMode.GridSnapping : SnapMode.RelativeSnapping;
                    }
                    //(x:97.00, y:0.00, width:27.00, height:18.00)
                    TooltipUtility.SetToolTip(gridSnapModeTooltip, currentRect);
                    break;
                }

                case SnapMode.RelativeSnapping:
                {
                    var newValue = GUI.Toggle(currentRect, snapMode == SnapMode.RelativeSnapping, CSG_GUIStyleUtility.Skin.relSnapIconOn, EditorStyles.toolbarButton);
                    if (GUI.changed)
                    {
                        snapMode = newValue ? SnapMode.RelativeSnapping : SnapMode.None;
                    }
                    //(x:97.00, y:0.00, width:27.00, height:18.00)
                    TooltipUtility.SetToolTip(relativeSnapModeTooltip, currentRect);
                    break;
                }

                default:
                case SnapMode.None:
                {
                    var newValue = GUI.Toggle(currentRect, snapMode != SnapMode.None, CSG_GUIStyleUtility.Skin.noSnapIconOn, EditorStyles.toolbarButton);
                    if (GUI.changed)
                    {
                        snapMode = newValue ? SnapMode.GridSnapping : SnapMode.None;
                    }
                    //(x:97.00, y:0.00, width:27.00, height:18.00)
                    TooltipUtility.SetToolTip(noSnappingModeTooltip, currentRect);
                    break;
                }
                }
            }
            modified = GUI.changed || modified;
            #endregion

            if (viewWidth >= 460)
            {
                if (snapMode != SnapMode.None)
                {
                    #region "Position" label
                    if (viewWidth >= 500)
                    {
                        if (viewWidth >= 865)
                        {
                            currentRect.width  = 44;
                            currentRect.y      = 1;
                            currentRect.height = layoutHeight - currentRect.y;
                            currentRect.y     += barSize.y;
                            currentRect.x      = layoutX;
                            layoutX           += currentRect.width;

                            uniformGrid = GUI.Toggle(currentRect, uniformGrid, positionLargeLabel, miniTextStyle);
                            //(x:128.00, y:2.00, width:44.00, height:16.00)

                            TooltipUtility.SetToolTip(positionTooltip, currentRect);
                        }
                        else
                        {
                            currentRect.width  = 22;
                            currentRect.y      = 1;
                            currentRect.height = layoutHeight - currentRect.y;
                            currentRect.y     += barSize.y;
                            currentRect.x      = layoutX;
                            layoutX           += currentRect.width;

                            uniformGrid = GUI.Toggle(currentRect, uniformGrid, positionSmallLabel, miniTextStyle);
                            //(x:127.00, y:2.00, width:22.00, height:16.00)

                            TooltipUtility.SetToolTip(positionTooltip, currentRect);
                        }
                    }
                    #endregion

                    layoutX += 2;

                    #region "Position" field
                    if (uniformGrid || viewWidth < 515)
                    {
                        EditorGUI.showMixedValue = !(moveSnapVector.x == moveSnapVector.y && moveSnapVector.x == moveSnapVector.z);
                        GUI.changed = false;
                        {
                            currentRect.width  = 70;
                            currentRect.y      = 3;
                            currentRect.height = layoutHeight - (currentRect.y - 1);
                            currentRect.y     += barSize.y;
                            currentRect.x      = layoutX;
                            layoutX           += currentRect.width;

                            moveSnapVector.x = Units.DistanceUnitToUnity(distanceUnit, EditorGUI.DoubleField(currentRect, Units.UnityToDistanceUnit(distanceUnit, moveSnapVector.x), textInputStyle));                            //, MinSnapWidth, MaxSnapWidth));
                            //(x:176.00, y:3.00, width:70.00, height:16.00)
                        }
                        if (GUI.changed)
                        {
                            modified         = true;
                            moveSnapVector.y = moveSnapVector.x;
                            moveSnapVector.z = moveSnapVector.x;
                        }
                        EditorGUI.showMixedValue = false;
                    }
                    else
                    {
                        GUI.changed = false;
                        {
                            currentRect.width  = 70;
                            currentRect.y      = 3;
                            currentRect.height = layoutHeight - (currentRect.y - 1);
                            currentRect.y     += barSize.y;
                            currentRect.x      = layoutX;
                            layoutX           += currentRect.width;
                            layoutX++;

                            moveSnapVector.x = Units.DistanceUnitToUnity(distanceUnit, EditorGUI.DoubleField(currentRect, Units.UnityToDistanceUnit(distanceUnit, moveSnapVector.x), textInputStyle));                            //, MinSnapWidth, MaxSnapWidth));
                            //(x:175.00, y:3.00, width:70.00, height:16.00)


                            currentRect.x = layoutX;
                            layoutX      += currentRect.width;
                            layoutX++;

                            moveSnapVector.y = Units.DistanceUnitToUnity(distanceUnit, EditorGUI.DoubleField(currentRect, Units.UnityToDistanceUnit(distanceUnit, moveSnapVector.y), textInputStyle));                            //, MinSnapWidth, MaxSnapWidth));
                            //(x:247.00, y:3.00, width:70.00, height:16.00)


                            currentRect.x = layoutX;
                            layoutX      += currentRect.width;

                            moveSnapVector.z = Units.DistanceUnitToUnity(distanceUnit, EditorGUI.DoubleField(currentRect, Units.UnityToDistanceUnit(distanceUnit, moveSnapVector.z), textInputStyle));                            //, MinSnapWidth, MaxSnapWidth));
                            //(x:319.00, y:3.00, width:70.00, height:16.00)
                        }
                        modified = GUI.changed || modified;
                    }
                    #endregion

                    layoutX++;

                    #region "Position" Unit
                    DistanceUnit nextUnit = Units.CycleToNextUnit(distanceUnit);
                    GUIContent   unitText = Units.GetUnitGUIContent(distanceUnit);

                    currentRect.width  = 22;
                    currentRect.y      = 2;
                    currentRect.height = layoutHeight - currentRect.y;
                    currentRect.y     += barSize.y;
                    currentRect.x      = layoutX;
                    layoutX           += currentRect.width;

                    if (GUI.Button(currentRect, unitText, miniTextStyle))                    //(x:393.00, y:2.00, width:13.00, height:16.00)
                    {
                        distanceUnit = nextUnit;
                        modified     = true;
                    }
                    #endregion

                    layoutX += 2;

                    #region "Position" +/-
                    if (viewWidth >= 700)
                    {
                        currentRect.width  = 19;
                        currentRect.y      = 2;
                        currentRect.height = layoutHeight - (currentRect.y + 1);
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        if (GUI.Button(currentRect, positionPlusLabel, EditorStyles.miniButtonLeft))
                        {
                            GridUtility.DoubleGridSize(); moveSnapVector = RealtimeCSG.CSGSettings.SnapVector;
                        }
                        //(x:410.00, y:2.00, width:19.00, height:15.00)
                        TooltipUtility.SetToolTip(positionPlusTooltip, currentRect);

                        currentRect.width  = 17;
                        currentRect.y      = 2;
                        currentRect.height = layoutHeight - (currentRect.y + 1);
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        if (GUI.Button(currentRect, positionMinusLabel, EditorStyles.miniButtonRight))
                        {
                            GridUtility.HalfGridSize(); moveSnapVector = RealtimeCSG.CSGSettings.SnapVector;
                        }
                        //(x:429.00, y:2.00, width:17.00, height:15.00)
                        TooltipUtility.SetToolTip(positionMinnusTooltip, currentRect);
                    }
                    #endregion

                    layoutX += 2;

                    #region "Angle" label
                    if (viewWidth >= 750)
                    {
                        if (viewWidth >= 865)
                        {
                            currentRect.width  = 31;
                            currentRect.y      = 1;
                            currentRect.height = layoutHeight - currentRect.y;
                            currentRect.y     += barSize.y;
                            currentRect.x      = layoutX;
                            layoutX           += currentRect.width;

                            GUI.Label(currentRect, angleLargeLabel, miniTextStyle);
                            //(x:450.00, y:2.00, width:31.00, height:16.00)
                        }
                        else
                        {
                            currentRect.width  = 22;
                            currentRect.y      = 1;
                            currentRect.height = layoutHeight - currentRect.y;
                            currentRect.y     += barSize.y;
                            currentRect.x      = layoutX;
                            layoutX           += currentRect.width;

                            GUI.Label(currentRect, angleSmallLabel, miniTextStyle);
                            //(x:355.00, y:2.00, width:22.00, height:16.00)
                        }
                        TooltipUtility.SetToolTip(angleTooltip, currentRect);
                    }
                    #endregion

                    layoutX += 2;

                    #region "Angle" field
                    GUI.changed = false;
                    {
                        currentRect.width  = 70;
                        currentRect.y      = 3;
                        currentRect.height = layoutHeight - (currentRect.y - 1);
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        rotationSnap = EditorGUI.FloatField(currentRect, rotationSnap, textInputStyle);                        //, MinSnapWidth, MaxSnapWidth);
                        //(x:486.00, y:3.00, width:70.00, height:16.00)
                        if (viewWidth <= 750)
                        {
                            TooltipUtility.SetToolTip(angleTooltip, currentRect);
                        }
                    }
                    modified = GUI.changed || modified;
                    #endregion

                    layoutX++;

                    #region "Angle" Unit
                    if (viewWidth >= 370)
                    {
                        currentRect.width  = 14;
                        currentRect.y      = 1;
                        currentRect.height = layoutHeight - currentRect.y;
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        GUI.Label(currentRect, angleUnitLabel, miniTextStyle);
                    }
                    #endregion

                    layoutX += 2;

                    #region "Angle" +/-
                    if (viewWidth >= 700)
                    {
                        currentRect.width  = 19;
                        currentRect.y      = 1;
                        currentRect.height = layoutHeight - (currentRect.y + 1);
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        if (GUI.Button(currentRect, anglePlusLabel, EditorStyles.miniButtonLeft))
                        {
                            rotationSnap *= 2.0f; modified = true;
                        }
                        //(x:573.00, y:2.00, width:19.00, height:15.00)
                        TooltipUtility.SetToolTip(anglePlusTooltip, currentRect);


                        currentRect.width  = 17;
                        currentRect.y      = 1;
                        currentRect.height = layoutHeight - (currentRect.y + 1);
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        if (GUI.Button(currentRect, angleMinusLabel, EditorStyles.miniButtonRight))
                        {
                            rotationSnap /= 2.0f; modified = true;
                        }
                        //(x:592.00, y:2.00, width:17.00, height:15.00)
                        TooltipUtility.SetToolTip(angleMinnusTooltip, currentRect);
                    }
                    #endregion

                    layoutX += 2;

                    #region "Scale" label
                    if (viewWidth >= 750)
                    {
                        if (viewWidth >= 865)
                        {
                            currentRect.width  = 31;
                            currentRect.y      = 1;
                            currentRect.height = layoutHeight - currentRect.y;
                            currentRect.y     += barSize.y;
                            currentRect.x      = layoutX;
                            layoutX           += currentRect.width;

                            GUI.Label(currentRect, scaleLargeLabel, miniTextStyle);
                            //(x:613.00, y:2.00, width:31.00, height:16.00)
                        }
                        else
                        {
                            currentRect.width  = 19;
                            currentRect.y      = 1;
                            currentRect.height = layoutHeight - currentRect.y;
                            currentRect.y     += barSize.y;
                            currentRect.x      = layoutX;
                            layoutX           += currentRect.width;

                            GUI.Label(currentRect, scaleSmallLabel, miniTextStyle);
                            //(x:495.00, y:2.00, width:19.00, height:16.00)
                        }
                        TooltipUtility.SetToolTip(scaleTooltip, currentRect);
                    }
                    #endregion

                    layoutX += 2;

                    #region "Scale" field
                    GUI.changed = false;
                    {
                        currentRect.width  = 70;
                        currentRect.y      = 3;
                        currentRect.height = layoutHeight - (currentRect.y - 1);
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        scaleSnap = EditorGUI.FloatField(currentRect, scaleSnap, textInputStyle);                        //, MinSnapWidth, MaxSnapWidth);
                        //(x:648.00, y:3.00, width:70.00, height:16.00)
                        if (viewWidth <= 750)
                        {
                            TooltipUtility.SetToolTip(scaleTooltip, currentRect);
                        }
                    }
                    modified = GUI.changed || modified;
                    #endregion

                    layoutX++;

                    #region "Scale" Unit
                    if (viewWidth >= 370)
                    {
                        currentRect.width  = 15;
                        currentRect.y      = 1;
                        currentRect.height = layoutHeight - currentRect.y;
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        GUI.Label(currentRect, scaleUnitLabel, miniTextStyle);
                        //(x:722.00, y:2.00, width:15.00, height:16.00)
                    }
                    #endregion

                    layoutX += 2;

                    #region "Scale" +/-
                    if (viewWidth >= 700)
                    {
                        currentRect.width  = 19;
                        currentRect.y      = 2;
                        currentRect.height = layoutHeight - (currentRect.y + 1);
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        if (GUI.Button(currentRect, scalePlusLabel, EditorStyles.miniButtonLeft))
                        {
                            scaleSnap *= 10.0f; modified = true;
                        }
                        //(x:741.00, y:2.00, width:19.00, height:15.00)
                        TooltipUtility.SetToolTip(scalePlusTooltip, currentRect);


                        currentRect.width  = 17;
                        currentRect.y      = 2;
                        currentRect.height = layoutHeight - (currentRect.y + 1);
                        currentRect.y     += barSize.y;
                        currentRect.x      = layoutX;
                        layoutX           += currentRect.width;

                        if (GUI.Button(currentRect, scaleMinusLabel, EditorStyles.miniButtonRight))
                        {
                            scaleSnap /= 10.0f; modified = true;
                        }
                        //(x:760.00, y:2.00, width:17.00, height:15.00)
                        TooltipUtility.SetToolTip(scaleMinnusTooltip, currentRect);
                    }
                    #endregion
                }
            }


            var prevLayoutX = layoutX;

            layoutX = viewWidth;


            #region "Rebuild"
            currentRect.width  = 27;
            currentRect.y      = 0;
            currentRect.height = layoutHeight - currentRect.y;
            currentRect.y     += barSize.y;
            layoutX           -= currentRect.width;
            currentRect.x      = layoutX;

            if (GUI.Button(currentRect, CSG_GUIStyleUtility.Skin.rebuildIcon, EditorStyles.toolbarButton))
            {
                Debug.Log("Starting complete rebuild");

                var text = new System.Text.StringBuilder();

                MaterialUtility.ResetMaterialTypeLookup();

                InternalCSGModelManager.skipCheckForChanges = true;
                RealtimeCSG.CSGSettings.Reload();
                UnityCompilerDefineManager.UpdateUnityDefines();

                InternalCSGModelManager.registerTime          = 0.0;
                InternalCSGModelManager.validateTime          = 0.0;
                InternalCSGModelManager.hierarchyValidateTime = 0.0;
                InternalCSGModelManager.updateHierarchyTime   = 0.0;

                var startTime = EditorApplication.timeSinceStartup;
                InternalCSGModelManager.ForceRebuildAll();
                InternalCSGModelManager.OnHierarchyModified();
                var hierarchy_update_endTime = EditorApplication.timeSinceStartup;
                text.AppendFormat(CultureInfo.InvariantCulture, "Full hierarchy rebuild in {0:F} ms. ", (hierarchy_update_endTime - startTime) * 1000);


                NativeMethodBindings.RebuildAll();
                var csg_endTime = EditorApplication.timeSinceStartup;
                text.AppendFormat(CultureInfo.InvariantCulture, "Full CSG rebuild done in {0:F} ms. ", (csg_endTime - hierarchy_update_endTime) * 1000);

                InternalCSGModelManager.RemoveForcedUpdates();                 // we already did this in rebuild all
                InternalCSGModelManager.UpdateMeshes(text, forceUpdate: true);

                updateSurfaces = true;
                UpdateLoop.ResetUpdateRoutine();
                RealtimeCSG.CSGSettings.Save();
                InternalCSGModelManager.skipCheckForChanges = false;

                var scenes = new HashSet <UnityEngine.SceneManagement.Scene>();
                foreach (var model in InternalCSGModelManager.Models)
                {
                    scenes.Add(model.gameObject.scene);
                }

                text.AppendFormat(CultureInfo.InvariantCulture, "{0} brushes. ", Foundation.CSGManager.TreeBrushCount);

                Debug.Log(text.ToString());
            }
            //(x:1442.00, y:0.00, width:27.00, height:18.00)
            TooltipUtility.SetToolTip(rebuildTooltip, currentRect);
            #endregion

            if (viewWidth >= 800)
            {
                layoutX -= 6;                 //(x:1436.00, y:0.00, width:6.00, height:6.00)
            }
            #region "Helper Surface Flags" Mask
            if (viewWidth >= 250)
            {
                GUI.changed = false;
                {
                    prevLayoutX += 8;                      // extra space
                    prevLayoutX += 26;                     // width of "Show wireframe" button

                    currentRect.width = Mathf.Max(20, Mathf.Min(165, (viewWidth - prevLayoutX - currentRect.width)));

                    currentRect.y      = 0;
                    currentRect.height = layoutHeight - currentRect.y;
                    currentRect.y     += barSize.y;
                    layoutX           -= currentRect.width;
                    currentRect.x      = layoutX;

                    SurfaceVisibilityPopup.Button(sceneView, currentRect);

                    //(x:1267.00, y:2.00, width:165.00, height:16.00)
                    TooltipUtility.SetToolTip(helperSurfacesTooltip, currentRect);
                }
                if (GUI.changed)
                {
                    updateSurfaces = true;
                    modified       = true;
                }
            }
            #endregion

            #region "Show wireframe" button
            GUI.changed        = false;
            currentRect.width  = 26;
            currentRect.y      = 0;
            currentRect.height = layoutHeight - currentRect.y;
            currentRect.y     += barSize.y;
            layoutX           -= currentRect.width;
            currentRect.x      = layoutX;

            if (showWireframe)
            {
                showWireframe = GUI.Toggle(currentRect, showWireframe, CSG_GUIStyleUtility.Skin.wireframe, EditorStyles.toolbarButton);
                //(x:1237.00, y:0.00, width:26.00, height:18.00)
            }
            else
            {
                showWireframe = GUI.Toggle(currentRect, showWireframe, CSG_GUIStyleUtility.Skin.wireframeOn, EditorStyles.toolbarButton);
                //(x:1237.00, y:0.00, width:26.00, height:18.00)
            }
            TooltipUtility.SetToolTip(showWireframeTooltip, currentRect);
            if (GUI.changed)
            {
                wireframeModified = true;
                modified          = true;
            }
            #endregion



            #region Capture mouse clicks in empty space
            var mousePoint = Event.current.mousePosition;
            int controlID  = GUIUtility.GetControlID(BottomBarEditorOverlayHash, FocusType.Passive, barSize);
            switch (Event.current.GetTypeForControl(controlID))
            {
            case EventType.MouseDown:       { if (barSize.Contains(mousePoint))
                                              {
                                                  GUIUtility.hotControl = controlID; GUIUtility.keyboardControl = controlID; Event.current.Use();
                                              }
                                              break; }

            case EventType.MouseMove:       { if (barSize.Contains(mousePoint))
                                              {
                                                  Event.current.Use();
                                              }
                                              break; }

            case EventType.MouseUp:         { if (GUIUtility.hotControl == controlID)
                                              {
                                                  GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; Event.current.Use();
                                              }
                                              break; }

            case EventType.MouseDrag:       { if (GUIUtility.hotControl == controlID)
                                              {
                                                  Event.current.Use();
                                              }
                                              break; }

            case EventType.ScrollWheel: { if (barSize.Contains(mousePoint))
                                          {
                                              Event.current.Use();
                                          }
                                          break; }
            }
            #endregion



            #region Store modified values
            rotationSnap     = Mathf.Max(1.0f, Mathf.Abs((360 + (rotationSnap % 360))) % 360);
            moveSnapVector.x = Mathf.Max(1.0f / 1024.0f, moveSnapVector.x);
            moveSnapVector.y = Mathf.Max(1.0f / 1024.0f, moveSnapVector.y);
            moveSnapVector.z = Mathf.Max(1.0f / 1024.0f, moveSnapVector.z);

            scaleSnap = Mathf.Max(MathConstants.MinimumScale, scaleSnap);

            RealtimeCSG.CSGSettings.SnapMode     = snapMode;
            RealtimeCSG.CSGSettings.SnapVector   = moveSnapVector;
            RealtimeCSG.CSGSettings.SnapRotation = rotationSnap;
            RealtimeCSG.CSGSettings.SnapScale    = scaleSnap;
            RealtimeCSG.CSGSettings.UniformGrid  = uniformGrid;
//			RealtimeCSG.Settings.SnapVertex					= vertexSnap;
            RealtimeCSG.CSGSettings.GridVisible           = showGrid;
            RealtimeCSG.CSGSettings.LockAxisX             = lockAxisX;
            RealtimeCSG.CSGSettings.LockAxisY             = lockAxisY;
            RealtimeCSG.CSGSettings.LockAxisZ             = lockAxisZ;
            RealtimeCSG.CSGSettings.DistanceUnit          = distanceUnit;
            RealtimeCSG.CSGSettings.VisibleHelperSurfaces = helperSurfaces;

            if (wireframeModified)
            {
                RealtimeCSG.CSGSettings.SetWireframeShown(sceneView, showWireframe);
            }

            if (updateSurfaces)
            {
                MeshInstanceManager.UpdateHelperSurfaceVisibility(force: true);
            }

            if (modified)
            {
                GUI.changed = true;
                RealtimeCSG.CSGSettings.UpdateSnapSettings();
                RealtimeCSG.CSGSettings.Save();
                CSG_EditorGUIUtility.RepaintAll();
            }
            #endregion
        }
Beispiel #15
0
        static void OnGUIContents(bool isSceneGUI, ObjectEditBrushTool tool)
        {
            CommonGUI.StartToolGUI();

            var filteredSelection   = CSGBrushEditorManager.FilteredSelection;
            var defaultMoveOffset   = CSGSettings.DefaultMoveOffset;
            var defaultRotateOffset = CSGSettings.DefaultRotateOffset;
            var displayNewCenter    = GridUtility.CleanPosition((Tools.pivotRotation == PivotRotation.Local) ?
                                                                tool.LocalSpacePivotCenter :
                                                                tool.WorldSpacePivotCenter);

            GUILayout.BeginVertical(GUIStyleUtility.ContentEmpty);
            {
                GUILayout.BeginHorizontal(GUIStyleUtility.ContentEmpty);
                {
                    HandleCSGOperations(isSceneGUI, tool, filteredSelection);
                    GUILayout.BeginVertical(GUIStyleUtility.ContentEmpty);
                    {
                        EditorGUI.BeginDisabledGroup(!tool.HaveSelection);
                        {
                            if (Tools.current == Tool.Move)
                            {
                                EditorGUI.BeginDisabledGroup(defaultMoveOffset.sqrMagnitude < MathConstants.EqualityEpsilonSqr);
                                {
                                    if (GUILayout.Button(MoveByOffsetContent))
                                    {
                                        tool.MoveByOffset(RealtimeCSG.CSGSettings.DefaultMoveOffset);
                                    }
                                    TooltipUtility.SetToolTip(MoveByOffsetTooltip);
                                    if (GUILayout.Button(CloneMoveByOffsetContent))
                                    {
                                        tool.CloneMoveByOffset(RealtimeCSG.CSGSettings.DefaultMoveOffset);
                                    }
                                    TooltipUtility.SetToolTip(CloneMoveByOffsetTooltip);
                                }
                                EditorGUI.EndDisabledGroup();
                            }
                            else
                            if (Tools.current == Tool.Rotate)
                            {
                                EditorGUI.BeginDisabledGroup(defaultMoveOffset.sqrMagnitude < MathConstants.EqualityEpsilonSqr);
                                {
                                    if (GUILayout.Button(RotateByOffsetContent))
                                    {
                                        tool.RotateByOffset(Quaternion.Euler(RealtimeCSG.CSGSettings.DefaultRotateOffset));
                                    }
                                    TooltipUtility.SetToolTip(RotateByOffsetTooltip);
                                    if (GUILayout.Button(CloneRotateByOffsetContent))
                                    {
                                        tool.CloneRotateByOffset(Quaternion.Euler(RealtimeCSG.CSGSettings.DefaultRotateOffset));
                                    }
                                    TooltipUtility.SetToolTip(CloneRotateByOffsetTooltip);
                                }
                                EditorGUI.EndDisabledGroup();
                                if (GUILayout.Button(RecenterPivotContent))
                                {
                                    tool.RecenterPivot();
                                }
                                TooltipUtility.SetToolTip(RecenterPivotTooltip);
                            }
                        }
                        EditorGUI.EndDisabledGroup();
                    }
                    GUILayout.EndVertical();
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(5);
                if (Tools.current == Tool.Move)
                {
                    var doubleFieldOptions = isSceneGUI ? MaxWidth150 : GUIStyleUtility.ContentEmpty;
                    EditorGUI.BeginDisabledGroup(!tool.HaveSelection);
                    {
                        EditorGUI.BeginChangeCheck();
                        {
                            GUILayout.Label("Offset");
                            defaultMoveOffset = GUIStyleUtility.DistanceVector3Field(defaultMoveOffset, false, doubleFieldOptions);
                        }
                        if (EditorGUI.EndChangeCheck())
                        {
                            RealtimeCSG.CSGSettings.DefaultMoveOffset = defaultMoveOffset;
                            RealtimeCSG.CSGSettings.Save();
                        }
                    }
                    EditorGUI.EndDisabledGroup();
                }
                else
                if (Tools.current == Tool.Rotate)
                {
                    var doubleFieldOptions = isSceneGUI ? MaxWidth150 : GUIStyleUtility.ContentEmpty;
                    EditorGUI.BeginDisabledGroup(Tools.pivotMode == PivotMode.Center || !tool.HaveSelection);
                    {
                        EditorGUI.BeginChangeCheck();
                        {
                            GUILayout.Label("Offset");
                            defaultRotateOffset = GUIStyleUtility.EulerDegreeField(defaultRotateOffset);
                        }
                        if (EditorGUI.EndChangeCheck())
                        {
                            RealtimeCSG.CSGSettings.DefaultRotateOffset = defaultRotateOffset;
                            RealtimeCSG.CSGSettings.Save();
                        }

                        EditorGUI.BeginChangeCheck();
                        {
                            GUILayout.Label("Pivot");
                            displayNewCenter = GUIStyleUtility.DistanceVector3Field(displayNewCenter, false, doubleFieldOptions);
                            TooltipUtility.SetToolTip(PivotVectorTooltip);
                        }
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (Tools.pivotRotation == PivotRotation.Local)
                            {
                                tool.LocalSpacePivotCenter = displayNewCenter;
                            }
                            else
                            {
                                tool.WorldSpacePivotCenter = displayNewCenter;
                            }
                        }
                    }
                    EditorGUI.EndDisabledGroup();
                }

                /*
                 * if (Tools.current != Tool.Rotate)
                 * {
                 *      if (!isSceneGUI || !SceneView.currentDrawingSceneView.camera.orthographic)
                 *      {
                 *              if (!isSceneGUI)
                 *              {
                 *                      GUILayout.Space(10);
                 *                      GUILayout.Label("Tips", EditorStyles.miniLabel);
                 *              }
                 *
                 *              GUILayout.BeginVertical(isSceneGUI ? GUI.skin.box : GUIStyle.none);
                 *              {
                 *                      GUILayout.Label(Keys.VerticalMoveMode.ToString() + " to drag brush up/down", EditorStyles.miniLabel);
                 *              }
                 *              GUILayout.EndVertical();
                 *      }
                 * }
                 */
            }
            GUILayout.EndVertical();
            EditorGUI.showMixedValue = false;
        }
Beispiel #16
0
        static public Vector3 FixPosition(Vector3 currentPosition, Matrix4x4 worldToLocalMatrix, Matrix4x4 localToWorldMatrix, Vector3 previousPosition, bool ignoreAxisLocking = false)
        {
            if (currentPosition == previousPosition)
            {
                return(currentPosition);
            }

            var pivotRotation = UnityEditor.Tools.pivotRotation;

            if (pivotRotation == UnityEditor.PivotRotation.Local)
            {
                previousPosition = worldToLocalMatrix.MultiplyPoint(previousPosition);
                currentPosition  = worldToLocalMatrix.MultiplyPoint(currentPosition);

                if (!ignoreAxisLocking)
                {
                    if (RealtimeCSG.CSGSettings.LockAxisX)
                    {
                        currentPosition.x = previousPosition.x;
                    }
                    if (RealtimeCSG.CSGSettings.LockAxisY)
                    {
                        currentPosition.y = previousPosition.y;
                    }
                    if (RealtimeCSG.CSGSettings.LockAxisZ)
                    {
                        currentPosition.z = previousPosition.z;
                    }
                }

                var doGridSnapping = RealtimeCSG.CSGSettings.GridSnapping;
                if (doGridSnapping)
                {
                    if (Mathf.Abs(currentPosition.x - previousPosition.x) < MathConstants.EqualityEpsilon)
                    {
                        currentPosition.x = previousPosition.x;
                    }
                    if (Mathf.Abs(currentPosition.y - previousPosition.y) < MathConstants.EqualityEpsilon)
                    {
                        currentPosition.y = previousPosition.y;
                    }
                    if (Mathf.Abs(currentPosition.z - previousPosition.z) < MathConstants.EqualityEpsilon)
                    {
                        currentPosition.z = previousPosition.z;
                    }

                    if (currentPosition.x != previousPosition.x)
                    {
                        currentPosition.x = Mathf.Round(currentPosition.x / RealtimeCSG.CSGSettings.SnapVector.x) * RealtimeCSG.CSGSettings.SnapVector.x;
                    }
                    if (currentPosition.y != previousPosition.y)
                    {
                        currentPosition.y = Mathf.Round(currentPosition.y / RealtimeCSG.CSGSettings.SnapVector.y) * RealtimeCSG.CSGSettings.SnapVector.y;
                    }
                    if (currentPosition.z != previousPosition.z)
                    {
                        currentPosition.z = Mathf.Round(currentPosition.z / RealtimeCSG.CSGSettings.SnapVector.z) * RealtimeCSG.CSGSettings.SnapVector.z;
                    }
                }

                currentPosition = localToWorldMatrix.MultiplyPoint(currentPosition);
            }
            else
            {
                if (!ignoreAxisLocking)
                {
                    if (RealtimeCSG.CSGSettings.LockAxisX)
                    {
                        currentPosition.x = previousPosition.x;
                    }
                    if (RealtimeCSG.CSGSettings.LockAxisY)
                    {
                        currentPosition.y = previousPosition.y;
                    }
                    if (RealtimeCSG.CSGSettings.LockAxisZ)
                    {
                        currentPosition.z = previousPosition.z;
                    }
                }

                if (RealtimeCSG.CSGSettings.GridSnapping)
                {
                    if (currentPosition.x != previousPosition.x)
                    {
                        currentPosition.x = Mathf.Round(currentPosition.x / RealtimeCSG.CSGSettings.SnapVector.x) * RealtimeCSG.CSGSettings.SnapVector.x;
                    }
                    if (currentPosition.y != previousPosition.y)
                    {
                        currentPosition.y = Mathf.Round(currentPosition.y / RealtimeCSG.CSGSettings.SnapVector.y) * RealtimeCSG.CSGSettings.SnapVector.y;
                    }
                    if (currentPosition.z != previousPosition.z)
                    {
                        currentPosition.z = Mathf.Round(currentPosition.z / RealtimeCSG.CSGSettings.SnapVector.z) * RealtimeCSG.CSGSettings.SnapVector.z;
                    }
                }
            }

            currentPosition = GridUtility.CleanPosition(currentPosition);
            return(currentPosition);
        }
        static void OnGUIContents(bool isSceneGUI, EditModePlace tool)
        {
            EditModeCommonGUI.StartToolGUI();

            var filteredSelection   = EditModeManager.FilteredSelection;
            var defaultMoveOffset   = CSGSettings.DefaultMoveOffset;
            var defaultRotateOffset = CSGSettings.DefaultRotateOffset;
            var displayNewCenter    = GridUtility.CleanPosition((Tools.pivotRotation == PivotRotation.Local) ?
                                                                tool.LocalSpacePivotCenter :
                                                                tool.WorldSpacePivotCenter);

            GUILayout.BeginVertical(CSG_GUIStyleUtility.ContentEmpty);
            {
                GUILayout.BeginHorizontal(CSG_GUIStyleUtility.ContentEmpty);
                {
                    ShowCSGOperations(isSceneGUI, tool, filteredSelection);
                    GUILayout.BeginVertical(CSG_GUIStyleUtility.ContentEmpty);
                    {
                        EditorGUI.BeginDisabledGroup(!tool.HaveSelection);
                        {
                            if (Tools.current == Tool.Move)
                            {
                                EditorGUI.BeginDisabledGroup(defaultMoveOffset.sqrMagnitude < MathConstants.EqualityEpsilonSqr);
                                {
                                    if (GUILayout.Button(MoveByOffsetContent))
                                    {
                                        tool.MoveByOffset(RealtimeCSG.CSGSettings.DefaultMoveOffset);
                                    }
                                    TooltipUtility.SetToolTip(MoveByOffsetTooltip);
                                    if (GUILayout.Button(CloneMoveByOffsetContent))
                                    {
                                        tool.CloneMoveByOffset(RealtimeCSG.CSGSettings.DefaultMoveOffset);
                                    }
                                    TooltipUtility.SetToolTip(CloneMoveByOffsetTooltip);
                                }
                                EditorGUI.EndDisabledGroup();
                            }
                            else
                            if (Tools.current == Tool.Rotate)
                            {
                                EditorGUI.BeginDisabledGroup(defaultMoveOffset.sqrMagnitude < MathConstants.EqualityEpsilonSqr);
                                {
                                    if (GUILayout.Button(RotateByOffsetContent))
                                    {
                                        tool.RotateByOffset(Quaternion.Euler(RealtimeCSG.CSGSettings.DefaultRotateOffset));
                                    }
                                    TooltipUtility.SetToolTip(RotateByOffsetTooltip);
                                    if (GUILayout.Button(CloneRotateByOffsetContent))
                                    {
                                        tool.CloneRotateByOffset(Quaternion.Euler(RealtimeCSG.CSGSettings.DefaultRotateOffset));
                                    }
                                    TooltipUtility.SetToolTip(CloneRotateByOffsetTooltip);
                                }
                                EditorGUI.EndDisabledGroup();
                                if (GUILayout.Button(RecenterPivotContent))
                                {
                                    tool.RecenterPivot();
                                }
                                TooltipUtility.SetToolTip(RecenterPivotTooltip);
                            }
                        }
                        EditorGUI.EndDisabledGroup();
                    }
                    GUILayout.EndVertical();
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(5);
                if (Tools.current == Tool.Move)
                {
                    var doubleFieldOptions = isSceneGUI ? MaxWidth150 : CSG_GUIStyleUtility.ContentEmpty;
                    EditorGUI.BeginDisabledGroup(!tool.HaveSelection);
                    {
                        EditorGUI.BeginChangeCheck();
                        {
                            GUILayout.Label(MoveOffsetContent);
                            defaultMoveOffset = CSG_EditorGUIUtility.DistanceVector3Field(defaultMoveOffset, false, doubleFieldOptions);
                        }
                        if (EditorGUI.EndChangeCheck())
                        {
                            RealtimeCSG.CSGSettings.DefaultMoveOffset = defaultMoveOffset;
                            RealtimeCSG.CSGSettings.Save();
                        }
                    }
                    EditorGUI.EndDisabledGroup();
                }
                else
                if (Tools.current == Tool.Rotate)
                {
                    var doubleFieldOptions = isSceneGUI ? MaxWidth150 : CSG_GUIStyleUtility.ContentEmpty;
                    EditorGUI.BeginDisabledGroup(Tools.pivotMode == PivotMode.Center || !tool.HaveSelection);
                    {
                        EditorGUI.BeginChangeCheck();
                        {
                            GUILayout.Label(RotationCenterContent);
                            defaultRotateOffset = CSG_EditorGUIUtility.EulerDegreeField(defaultRotateOffset);
                        }
                        if (EditorGUI.EndChangeCheck())
                        {
                            RealtimeCSG.CSGSettings.DefaultRotateOffset = defaultRotateOffset;
                            RealtimeCSG.CSGSettings.Save();
                        }

                        EditorGUI.BeginChangeCheck();
                        {
                            GUILayout.Label(PivotCenterContent);
                            displayNewCenter = CSG_EditorGUIUtility.DistanceVector3Field(displayNewCenter, false, doubleFieldOptions);
                            TooltipUtility.SetToolTip(PivotVectorTooltip);
                        }
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (Tools.pivotRotation == PivotRotation.Local)
                            {
                                tool.LocalSpacePivotCenter = displayNewCenter;
                            }
                            else
                            {
                                tool.WorldSpacePivotCenter = displayNewCenter;
                            }
                        }
                    }
                    EditorGUI.EndDisabledGroup();
                }
            }
            GUILayout.EndVertical();
            EditorGUI.showMixedValue = false;
        }
Beispiel #18
0
        static void OnBottomBarGUI(SceneView sceneView)
        {
            var  snapToGrid        = RealtimeCSG.CSGSettings.SnapToGrid;
            var  uniformGrid       = RealtimeCSG.CSGSettings.UniformGrid;
            var  moveSnapVector    = RealtimeCSG.CSGSettings.SnapVector;
            var  rotationSnap      = RealtimeCSG.CSGSettings.SnapRotation;
            var  scaleSnap         = RealtimeCSG.CSGSettings.SnapScale;
            var  showGrid          = RealtimeCSG.CSGSettings.GridVisible;
            var  lockAxisX         = RealtimeCSG.CSGSettings.LockAxisX;
            var  lockAxisY         = RealtimeCSG.CSGSettings.LockAxisY;
            var  lockAxisZ         = RealtimeCSG.CSGSettings.LockAxisZ;
            var  distanceUnit      = RealtimeCSG.CSGSettings.DistanceUnit;
            var  helperSurfaces    = RealtimeCSG.CSGSettings.VisibleHelperSurfaces;
            var  showWireframe     = RealtimeCSG.CSGSettings.IsWireframeShown(sceneView);
            var  updateSurfaces    = false;
            bool wireframeModified = false;

            var viewWidth = sceneView.position.width;

            bool modified = false;

            EditorGUILayout.BeginHorizontal(GUIStyleUtility.ContentEmpty);
            {
                EditorGUI.BeginChangeCheck();
                {
                    var skin = GUIStyleUtility.Skin;
                    if (showGrid)
                    {
                        showGrid = GUILayout.Toggle(showGrid, skin.gridIconOn, EditorStyles.toolbarButton);
                    }
                    else
                    {
                        showGrid = GUILayout.Toggle(showGrid, skin.gridIcon, EditorStyles.toolbarButton);
                    }
                    TooltipUtility.SetToolTip(showGridTooltip);

                    if (viewWidth >= 800)
                    {
                        EditorGUILayout.Space();
                    }

                    if (viewWidth >= 200)
                    {
                        var prevBackgroundColor   = GUI.backgroundColor;
                        var lockedBackgroundColor = skin.lockedBackgroundColor;
                        if (lockAxisX)
                        {
                            GUI.backgroundColor = lockedBackgroundColor;
                        }
                        lockAxisX = !GUILayout.Toggle(!lockAxisX, xLabel, skin.xToolbarButton);
                        if (lockAxisX)
                        {
                            TooltipUtility.SetToolTip(xTooltipOn);
                        }
                        else
                        {
                            TooltipUtility.SetToolTip(xTooltipOff);
                        }
                        GUI.backgroundColor = prevBackgroundColor;

                        if (lockAxisY)
                        {
                            GUI.backgroundColor = lockedBackgroundColor;
                        }
                        lockAxisY = !GUILayout.Toggle(!lockAxisY, yLabel, skin.yToolbarButton);
                        if (lockAxisY)
                        {
                            TooltipUtility.SetToolTip(yTooltipOn);
                        }
                        else
                        {
                            TooltipUtility.SetToolTip(yTooltipOff);
                        }
                        GUI.backgroundColor = prevBackgroundColor;

                        if (lockAxisZ)
                        {
                            GUI.backgroundColor = lockedBackgroundColor;
                        }
                        lockAxisZ = !GUILayout.Toggle(!lockAxisZ, zLabel, skin.zToolbarButton);
                        if (lockAxisZ)
                        {
                            TooltipUtility.SetToolTip(zTooltipOn);
                        }
                        else
                        {
                            TooltipUtility.SetToolTip(zTooltipOff);
                        }
                        GUI.backgroundColor = prevBackgroundColor;
                    }
                }
                modified = EditorGUI.EndChangeCheck() || modified;

                if (viewWidth >= 800)
                {
                    EditorGUILayout.Space();
                }

                EditorGUI.BeginChangeCheck();
                {
                    if (viewWidth >= 475)
                    {
                        if (snapToGrid && viewWidth >= 310)
                        {
                            snapToGrid = GUILayout.Toggle(snapToGrid, GUIStyleUtility.Skin.snappingIconOn, EditorStyles.toolbarButton);
                            TooltipUtility.SetToolTip(snapToGridTooltip);
                            if (viewWidth >= 865)
                            {
                                uniformGrid = GUILayout.Toggle(uniformGrid, positionLargeLabel, miniTextStyle);
                            }
                            else
                            {
                                uniformGrid = GUILayout.Toggle(uniformGrid, positionSmallLabel, miniTextStyle);
                            }
                            TooltipUtility.SetToolTip(positionTooltip);
                        }
                        else
                        {
                            snapToGrid = GUILayout.Toggle(snapToGrid, GUIStyleUtility.Skin.snappingIcon, EditorStyles.toolbarButton);
                            TooltipUtility.SetToolTip(snapToGridTooltip);
                        }
                    }
                }
                modified = EditorGUI.EndChangeCheck() || modified;
                if (viewWidth >= 310)
                {
                    if (snapToGrid)
                    {
                        if (uniformGrid || viewWidth < 515)
                        {
                            EditorGUI.showMixedValue = !(moveSnapVector.x == moveSnapVector.y && moveSnapVector.x == moveSnapVector.z);
                            EditorGUI.BeginChangeCheck();
                            {
                                moveSnapVector.x = Units.DistanceUnitToUnity(distanceUnit, EditorGUILayout.DoubleField(Units.UnityToDistanceUnit(distanceUnit, moveSnapVector.x), textInputStyle, MinSnapWidth, MaxSnapWidth));
                            }
                            if (EditorGUI.EndChangeCheck())
                            {
                                modified         = true;
                                moveSnapVector.y = moveSnapVector.x;
                                moveSnapVector.z = moveSnapVector.x;
                            }
                            EditorGUI.showMixedValue = false;
                        }
                        else
                        {
                            EditorGUI.BeginChangeCheck();
                            {
                                moveSnapVector.x = Units.DistanceUnitToUnity(distanceUnit, EditorGUILayout.DoubleField(Units.UnityToDistanceUnit(distanceUnit, moveSnapVector.x), textInputStyle, MinSnapWidth, MaxSnapWidth));
                                moveSnapVector.y = Units.DistanceUnitToUnity(distanceUnit, EditorGUILayout.DoubleField(Units.UnityToDistanceUnit(distanceUnit, moveSnapVector.y), textInputStyle, MinSnapWidth, MaxSnapWidth));
                                moveSnapVector.z = Units.DistanceUnitToUnity(distanceUnit, EditorGUILayout.DoubleField(Units.UnityToDistanceUnit(distanceUnit, moveSnapVector.z), textInputStyle, MinSnapWidth, MaxSnapWidth));
                            }
                            modified = EditorGUI.EndChangeCheck() || modified;
                        }
                        DistanceUnit nextUnit = Units.CycleToNextUnit(distanceUnit);
                        GUIContent   unitText = Units.GetUnitGUIContent(distanceUnit);
                        if (GUILayout.Button(unitText, miniTextStyle))
                        {
                            distanceUnit = nextUnit;
                            modified     = true;
                        }
                        if (viewWidth >= 700)
                        {
                            if (GUILayout.Button(positionPlusLabel, EditorStyles.miniButtonLeft))
                            {
                                GridUtility.DoubleGridSize(); moveSnapVector = RealtimeCSG.CSGSettings.SnapVector;
                            }
                            TooltipUtility.SetToolTip(positionPlusTooltip);
                            if (GUILayout.Button(positionMinusLabel, EditorStyles.miniButtonRight))
                            {
                                GridUtility.HalfGridSize(); moveSnapVector = RealtimeCSG.CSGSettings.SnapVector;
                            }
                            TooltipUtility.SetToolTip(positionMinnusTooltip);
                        }
                        if (viewWidth >= 750)
                        {
                            if (viewWidth >= 865)
                            {
                                GUILayout.Label(angleLargeLabel, miniTextStyle);
                            }
                            else
                            {
                                GUILayout.Label(angleSmallLabel, miniTextStyle);
                            }
                            TooltipUtility.SetToolTip(angleTooltip);
                        }
                        EditorGUI.BeginChangeCheck();
                        {
                            rotationSnap = EditorGUILayout.FloatField(rotationSnap, textInputStyle, MinSnapWidth, MaxSnapWidth);
                            if (viewWidth <= 750)
                            {
                                TooltipUtility.SetToolTip(angleTooltip);
                            }
                        }
                        modified = EditorGUI.EndChangeCheck() || modified;

                        if (viewWidth >= 370)
                        {
                            GUILayout.Label(angleUnitLabel, miniTextStyle);
                        }
                        if (viewWidth >= 700)
                        {
                            if (GUILayout.Button(anglePlusLabel, EditorStyles.miniButtonLeft))
                            {
                                rotationSnap *= 2.0f; modified = true;
                            }
                            TooltipUtility.SetToolTip(anglePlusTooltip);
                            if (GUILayout.Button(angleMinusLabel, EditorStyles.miniButtonRight))
                            {
                                rotationSnap /= 2.0f; modified = true;
                            }
                            TooltipUtility.SetToolTip(angleMinnusTooltip);
                        }

                        if (viewWidth >= 750)
                        {
                            if (viewWidth >= 865)
                            {
                                GUILayout.Label(scaleLargeLabel, miniTextStyle);
                            }
                            else
                            {
                                GUILayout.Label(scaleSmallLabel, miniTextStyle);
                            }
                            TooltipUtility.SetToolTip(scaleTooltip);
                        }
                        EditorGUI.BeginChangeCheck();
                        {
                            scaleSnap = EditorGUILayout.FloatField(scaleSnap, textInputStyle, MinSnapWidth, MaxSnapWidth);
                            if (viewWidth <= 750)
                            {
                                TooltipUtility.SetToolTip(scaleTooltip);
                            }
                        }
                        modified = EditorGUI.EndChangeCheck() || modified;
                        if (viewWidth >= 370)
                        {
                            GUILayout.Label(scaleUnitLabel, miniTextStyle);
                        }
                        if (viewWidth >= 700)
                        {
                            if (GUILayout.Button(scalePlusLabel, EditorStyles.miniButtonLeft))
                            {
                                scaleSnap *= 10.0f; modified = true;
                            }
                            TooltipUtility.SetToolTip(scalePlusTooltip);
                            if (GUILayout.Button(scaleMinusLabel, EditorStyles.miniButtonRight))
                            {
                                scaleSnap /= 10.0f; modified = true;
                            }
                            TooltipUtility.SetToolTip(scaleMinnusTooltip);
                        }
                    }
                }

                if (viewWidth >= 750)
                {
                    GUILayout.FlexibleSpace();
                }
                EditorGUI.BeginChangeCheck();
                if (showWireframe)
                {
                    showWireframe = GUILayout.Toggle(showWireframe, GUIStyleUtility.Skin.wireframe, EditorStyles.toolbarButton);
                }
                else
                {
                    showWireframe = GUILayout.Toggle(showWireframe, GUIStyleUtility.Skin.wireframeOn, EditorStyles.toolbarButton);
                }
                TooltipUtility.SetToolTip(showWireframeTooltip);
                if (EditorGUI.EndChangeCheck())
                {
                    wireframeModified = true;
                    modified          = true;
                }

                if (viewWidth >= 250)
                {
                    EditorGUI.BeginChangeCheck();
                    {
                        helperSurfaces = (HelperSurfaceFlags)EditorGUILayout.MaskField((int)helperSurfaces, helperSurfaceFlagStrings, EnumMaxWidth, EnumMinWidth);
                        TooltipUtility.SetToolTip(helperSurfacesTooltip);
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        updateSurfaces = true;
                        modified       = true;
                    }
                }

                if (viewWidth >= 800)
                {
                    EditorGUILayout.Space();
                }

                if (GUILayout.Button(GUIStyleUtility.Skin.rebuildIcon, EditorStyles.toolbarButton))
                {
                    Debug.Log("Starting complete rebuild");
                    InternalCSGModelManager.skipRefresh = true;
                    RealtimeCSG.CSGSettings.Reload();
                    SceneViewEventHandler.UpdateDefines();

                    var startTime = EditorApplication.timeSinceStartup;
                    InternalCSGModelManager.Rebuild();
                    InternalCSGModelManager.OnHierarchyModified();
                    var hierarchy_update_endTime = EditorApplication.timeSinceStartup;

                    CSGBindings.RebuildAll();
                    var csg_endTime = EditorApplication.timeSinceStartup;

                    InternalCSGModelManager.UpdateMeshes();
                    MeshInstanceManager.UpdateHelperSurfaceVisibility();
                    var meshupdate_endTime = EditorApplication.timeSinceStartup;

                    updateSurfaces = true;
                    SceneViewEventHandler.ResetUpdateRoutine();
                    RealtimeCSG.CSGSettings.Save();
                    InternalCSGModelManager.skipRefresh = false;
                    Debug.Log(string.Format(CultureInfo.InvariantCulture,
                                            "Full hierarchy rebuild in {0:F} ms. Full CSG rebuild done in {1:F} ms. Mesh update done in {2:F} ms.",
                                            (hierarchy_update_endTime - startTime) * 1000,
                                            (csg_endTime - hierarchy_update_endTime) * 1000,
                                            (meshupdate_endTime - csg_endTime) * 1000
                                            ));
                }
                TooltipUtility.SetToolTip(rebuildTooltip);
            }
            EditorGUILayout.EndHorizontal();

            var mousePoint  = Event.current.mousePosition;
            var currentArea = GUILayoutUtility.GetLastRect();
            int controlID   = GUIUtility.GetControlID(BottomBarEditorOverlayHash, FocusType.Passive, currentArea);

            switch (Event.current.GetTypeForControl(controlID))
            {
            case EventType.MouseDown:       { if (currentArea.Contains(mousePoint))
                                              {
                                                  GUIUtility.hotControl = controlID; GUIUtility.keyboardControl = controlID; Event.current.Use();
                                              }
                                              break; }

            case EventType.MouseMove:       { if (currentArea.Contains(mousePoint))
                                              {
                                                  Event.current.Use();
                                              }
                                              break; }

            case EventType.MouseUp:         { if (GUIUtility.hotControl == controlID)
                                              {
                                                  GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; Event.current.Use();
                                              }
                                              break; }

            case EventType.MouseDrag:       { if (GUIUtility.hotControl == controlID)
                                              {
                                                  Event.current.Use();
                                              }
                                              break; }

            case EventType.ScrollWheel: { if (currentArea.Contains(mousePoint))
                                          {
                                              Event.current.Use();
                                          }
                                          break; }
            }

            rotationSnap     = Mathf.Max(1.0f, Mathf.Abs((360 + (rotationSnap % 360))) % 360);
            moveSnapVector.x = Mathf.Max(1.0f / 1024.0f, moveSnapVector.x);
            moveSnapVector.y = Mathf.Max(1.0f / 1024.0f, moveSnapVector.y);
            moveSnapVector.z = Mathf.Max(1.0f / 1024.0f, moveSnapVector.z);

            scaleSnap = Mathf.Max(MathConstants.MinimumScale, scaleSnap);

            RealtimeCSG.CSGSettings.SnapToGrid   = snapToGrid;
            RealtimeCSG.CSGSettings.SnapVector   = moveSnapVector;
            RealtimeCSG.CSGSettings.SnapRotation = rotationSnap;
            RealtimeCSG.CSGSettings.SnapScale    = scaleSnap;
            RealtimeCSG.CSGSettings.UniformGrid  = uniformGrid;
//			RealtimeCSG.Settings.SnapVertex					= vertexSnap;
            RealtimeCSG.CSGSettings.GridVisible           = showGrid;
            RealtimeCSG.CSGSettings.LockAxisX             = lockAxisX;
            RealtimeCSG.CSGSettings.LockAxisY             = lockAxisY;
            RealtimeCSG.CSGSettings.LockAxisZ             = lockAxisZ;
            RealtimeCSG.CSGSettings.DistanceUnit          = distanceUnit;
            RealtimeCSG.CSGSettings.VisibleHelperSurfaces = helperSurfaces;

            if (wireframeModified)
            {
                RealtimeCSG.CSGSettings.SetWireframeShown(sceneView, showWireframe);
            }

            if (updateSurfaces)
            {
                MeshInstanceManager.UpdateHelperSurfaceVisibility();
            }

            if (modified)
            {
                GUI.changed = true;
                RealtimeCSG.CSGSettings.UpdateSnapSettings();
                RealtimeCSG.CSGSettings.Save();
                SceneView.RepaintAll();
            }
        }