Exemplo n.º 1
0
        public static Transform FindParentToAssignTo(BrushIntersection intersection)
        {
            if (intersection.brush == null ||
                SelectionUtility.IsPrefab(intersection.brush.gameObject))
            {
                var lastModel = SelectionUtility.LastUsedModel;
                if (lastModel == null ||
                    SelectionUtility.IsPrefab(lastModel.gameObject))
                {
                    return(null);
                }

                return(lastModel.transform);
            }

            var hoverParent = intersection.brush.transform.parent;
            var iterator    = hoverParent;

            while (iterator != null)
            {
                var node = iterator.GetComponent <CSGNode>();
                if (node != null)
                {
                    hoverParent = node.transform;
                }
                iterator = iterator.transform.parent;
            }
            return(hoverParent);
        }
Exemplo n.º 2
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 = CameraUtility.WorldToGUIPoint(test_points[i]);
                if (SceneQueryUtility.FindWorldIntersection(test_point2D, out intersection))
                {
                    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);
        }