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); }
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); }