public Vector3 SnapExtents1D(Extents1D currentExtents, Vector3 currentPosition, Vector3 slideOrigin, Vector3 slideDirection, float snappingStep, out SnapResult1D snapResult) { snapResult = SnapResult1D.None; var offsetPos = currentPosition - slideOrigin; var offsetDistance = SnappingUtility.WorldPointToDistance(offsetPos, slideDirection); var currDistance = offsetDistance - this.startOffset; var movedExtents = currentExtents + offsetDistance; var snappedExtents = movedExtents; snappedExtents.min = SnappingUtility.SnapValue(movedExtents.min, snappingStep); snappedExtents.max = SnappingUtility.SnapValue(movedExtents.max, snappingStep); var snappedExtentsOffset = snappedExtents - movedExtents; var snappedPivot = SnappingUtility.SnapValue(currDistance, snappingStep) - currDistance; if (!Snapping.BoundsSnappingActive && !Snapping.PivotSnappingActive) { return(currentPosition); } var abs_pivot = Snapping.PivotSnappingActive ? SnappingUtility.Quantize(Mathf.Abs(snappedPivot)) : float.PositiveInfinity; var abs_min_extents = Snapping.BoundsSnappingActive ? SnappingUtility.Quantize(Mathf.Abs(snappedExtentsOffset.min)) : float.PositiveInfinity; var abs_max_extents = Snapping.BoundsSnappingActive ? SnappingUtility.Quantize(Mathf.Abs(snappedExtentsOffset.max)) : float.PositiveInfinity; var snappedOffsetDistance = (abs_pivot < abs_min_extents && abs_pivot < abs_max_extents) ? snappedPivot : ((abs_min_extents < abs_max_extents) ? snappedExtentsOffset.min : snappedExtentsOffset.max); if (abs_min_extents <= abs_max_extents && abs_min_extents <= abs_pivot) { snapResult |= SnapResult1D.Min; } if (abs_max_extents <= abs_min_extents && abs_max_extents <= abs_pivot) { snapResult |= SnapResult1D.Max; } if (abs_pivot <= abs_min_extents && abs_pivot <= abs_max_extents) { snapResult |= SnapResult1D.Pivot; } min = slideOrigin + SnappingUtility.DistanceToWorldPoint(snappedExtents.min, slideDirection); max = slideOrigin + SnappingUtility.DistanceToWorldPoint(snappedExtents.max, slideDirection); var newOffset = offsetDistance + snappedOffsetDistance; if (Mathf.Abs(snappedOffsetDistance) > Mathf.Abs(offsetDistance)) { newOffset = 0; } var snappedDistance = SnappingUtility.DistanceToWorldPoint(newOffset, slideDirection); var snappedPosition = (snappedDistance + slideOrigin); return(snappedPosition); }
public static Extents1D GetExtentsOfPointArray(Vector3[] points, Vector3 direction, Vector3 origin) { var min = float.PositiveInfinity; var max = float.NegativeInfinity; for (var i = 0; i < points.Length; i++) { var distance = SnappingUtility.WorldPointToDistance(points[i], direction, origin); min = Mathf.Min(min, distance); max = Mathf.Max(max, distance); } return(new Extents1D(min, max)); }
static Extents1D GetExtentsOfPointArray(Matrix4x4 matrix, Vector3[] points, Vector3 slideOrigin, Vector3 slideDirection) { var transformedDirection = matrix.MultiplyVector(slideDirection); var transformedOrigin = matrix.MultiplyPoint(slideOrigin); var min = float.PositiveInfinity; var max = float.NegativeInfinity; for (var i = 0; i < points.Length; i++) { var distance = SnappingUtility.WorldPointToDistance(points[i], transformedDirection, transformedOrigin); min = Mathf.Min(min, distance); max = Mathf.Max(max, distance); } return(new Extents1D(min, max)); }
public void Initialize(Vector2 currentMousePosition, Vector3 slideOrigin, Vector3 slideDirection, float snappingStep, Axis axis) { this.slideDirection = slideDirection; this.snappingStep = snappingStep; this.slideOrigin = SceneHandleUtility.ProjectPointRay(Grid.ActiveGrid.Center, slideOrigin, slideDirection); this.slideExtents.min = this.slideExtents.max = 0; this.snappedPosition = this.slideOrigin; this.slidePosition = this.slideOrigin; this.slideOffset = slideOrigin - this.slideOrigin; this.startOffset = SnappingUtility.WorldPointToDistance(this.slidePosition - slideOrigin, slideDirection); this.startMousePosition = currentMousePosition; this.slideAxis = axis; this.snapResult = SnapResult1D.None; this.min = slideOrigin + SnappingUtility.DistanceToWorldPoint(slideExtents.min, slideDirection); this.max = slideOrigin + SnappingUtility.DistanceToWorldPoint(slideExtents.max, slideDirection); }
public static (float, float) SnapCustom(List <Vector3> customSnapPoints, float currentPosition, Vector3 slideDirection, float minPointSnap, List <float> customDistances) { if (customSnapPoints.Count == 0) { return(float.PositiveInfinity, float.PositiveInfinity); } float smallest_abs_distance = float.PositiveInfinity; float smallest_distance = float.PositiveInfinity; customDistances.Clear(); for (int i = 0; i < customSnapPoints.Count; i++) { var snappedPoint = SnappingUtility.WorldPointToDistance(customSnapPoints[i], slideDirection); // Determine the offset between the current position and the point we want to snap against var snappedPointOffset = currentPosition - snappedPoint; var quantized_distance = SnappingUtility.Quantize(snappedPointOffset); var abs_distance = Mathf.Abs(quantized_distance); customDistances.Add(quantized_distance); // Use the smallest distance as the best snap distance if (smallest_abs_distance > abs_distance) { smallest_abs_distance = abs_distance; smallest_distance = snappedPointOffset; } } if (float.IsInfinity(smallest_abs_distance) || smallest_abs_distance > minPointSnap) { return(float.PositiveInfinity, float.PositiveInfinity); } return(smallest_abs_distance, smallest_distance); }
public Vector3 SnapExtents1D(Vector3 currentPosition) { this.snapResult = SnapResult1D.None; var boundsActive = Snapping.BoundsSnappingActive; var pivotActive = Snapping.PivotSnappingActive; // Get custom snapping positions along the ray var haveCustomSnapping = Snapping.GetCustomSnappingPoints(this.slideOffset + slidePosition, this.slideDirection, 0, s_CustomSnapPoints); if (!boundsActive && !pivotActive && !haveCustomSnapping) { return(currentPosition); } const float kMinPointSnap = 0.25f; float minPointSnap = !(boundsActive || pivotActive) ? kMinPointSnap : float.PositiveInfinity; // Offset to snapped position relative to the unsnapped position // (used to determine which snap value is closest to unsnapped position) // Smallest value is used float snappedOffsetDistance = float.PositiveInfinity; float snappedOffsetAbsDistance = float.PositiveInfinity; var deltaToOrigin = currentPosition - this.slideOrigin; var distanceToOrigin = SnappingUtility.WorldPointToDistance(deltaToOrigin, this.slideDirection); var quantized_min_extents = float.PositiveInfinity; var quantized_max_extents = float.PositiveInfinity; var snappedExtents = Extents1D.empty; if (boundsActive) { (float abs_distance, float snappedOffset, float quantized_min, float quantized_max) = Snapping.SnapBounds(this.slideExtents + distanceToOrigin, this.snappingStep); quantized_min_extents = quantized_min; quantized_max_extents = quantized_max; snappedExtents.min = this.slideExtents.min + distanceToOrigin + Mathf.Abs(quantized_min_extents); snappedExtents.max = this.slideExtents.min + distanceToOrigin + Mathf.Abs(quantized_max_extents); if (snappedOffsetAbsDistance > abs_distance) { snappedOffsetAbsDistance = abs_distance; snappedOffsetDistance = snappedOffset; } } var quantized_pivot = float.PositiveInfinity; if (pivotActive) { (float abs_distance, float snappedOffset, float quantized) = Snapping.SnapPoint(this.startOffset + distanceToOrigin, this.snappingStep); quantized_pivot = quantized; if (snappedOffsetAbsDistance > abs_distance) { snappedOffsetAbsDistance = abs_distance; snappedOffsetDistance = snappedOffset; } } if (haveCustomSnapping) { (float abs_distance, float snappedOffset) = Snapping.SnapCustom(s_CustomSnapPoints, this.startOffset + distanceToOrigin, this.slideDirection, minPointSnap, s_CustomDistances); if (snappedOffsetAbsDistance > abs_distance) { snappedOffsetAbsDistance = abs_distance; snappedOffsetDistance = snappedOffset; } } // If we didn't actually snap, just return the actual unsnapped position if (float.IsInfinity(snappedOffsetDistance)) { return(currentPosition); } // Snap against drag start position if (Mathf.Abs(snappedOffsetDistance) > Mathf.Abs(distanceToOrigin)) { snappedOffsetDistance = distanceToOrigin; } var quantizedDistance = SnappingUtility.Quantize(snappedOffsetDistance); // Figure out what kind of snapping visualization to show, this needs to be done afterwards since // while we're snapping each type of snap can override the next one. // Yet at the same time it's possible to snap with multiple snap-types at the same time. if (boundsActive) { if (quantizedDistance == quantized_min_extents) { this.snapResult |= SnapResult1D.Min; } if (quantizedDistance == quantized_max_extents) { this.snapResult |= SnapResult1D.Max; } min = this.slideOrigin + SnappingUtility.DistanceToWorldPoint(snappedExtents.min, this.slideDirection); max = this.slideOrigin + SnappingUtility.DistanceToWorldPoint(snappedExtents.max, this.slideDirection); } if (pivotActive) { if (quantizedDistance == quantized_pivot) { this.snapResult |= SnapResult1D.Pivot; } } if (haveCustomSnapping) { Snapping.SendCustomSnappedEvents(quantizedDistance, s_CustomDistances, 0); } // Calculate the new position based on the snapped offset var newOffset = distanceToOrigin - snappedOffsetDistance; var snappedDistance = SnappingUtility.DistanceToWorldPoint(newOffset, this.slideDirection); var snappedPosition = (snappedDistance + this.slideOrigin); return(snappedPosition); }