public Vector3 GetPointClosestToPoint(Vector3 point, bool includeMidEdgePoints) { if (includeMidEdgePoints) { List <Vector3> points = GetCenterAndCornerPoints(); points.AddRange(GetMidEdgePoints()); return(Vector3Extensions.GetClosestPointToPoint(points, point)); } else { return(Vector3Extensions.GetClosestPointToPoint(GetCenterAndCornerPoints(), point)); } }
public Vector3 GetSnapDestinationPointClosestToCursorPickPoint() { if (IsValid) { if (_type == SnapSurfaceType.GridCell) { List <Vector3> snapDestinationPoints = _surfaceQuad.GetCenterAndCornerPoints(); snapDestinationPoints.AddRange(_surfaceQuad.GetMidEdgePoints()); return(Vector3Extensions.GetClosestPointToPoint(snapDestinationPoints, _mouseCursorPickPoint)); } else { XZOrientedQuad3D gridCellQuad = _objectBoxSnapSurfaceGrid.GetCellFromPoint(_mouseCursorPickPoint).Quad; List <Vector3> snapDestinationPoints = gridCellQuad.GetCenterAndCornerPoints(); snapDestinationPoints.AddRange(gridCellQuad.GetMidEdgePoints()); return(Vector3Extensions.GetClosestPointToPoint(snapDestinationPoints, _mouseCursorPickPoint)); } } return(Vector3.zero); }
public void UpdateForMouseMovement() { if (_state == State.Inactive) { return; } if (MouseButtonStates.Instance.IsMouseButtonDown(MouseButton.Left)) { _state = State.Snap; } else { _state = State.SelectPivot; } if (_state == State.SelectPivot && _selectedParents.Count != 0) { Camera camera = SceneViewCamera.Camera; Vector2 mousePos = Event.current.InvMousePos(camera); _isPivotAvailable = false; float minDistanceSq = float.MaxValue; foreach (var parent in _selectedParents) { if (parent == null) { continue; } OrientedBox worldOOBB = parent.GetHierarchyWorldOrientedBox(); if (worldOOBB.IsValid()) { List <Vector3> centerAndCorners = worldOOBB.GetCenterAndCornerPoints(); List <Vector2> oobbScreenPts = Vector2Extensions.GetScreenPoints(centerAndCorners, camera); for (int ptIndex = 0; ptIndex < centerAndCorners.Count; ++ptIndex) { Vector3 worldPt = centerAndCorners[ptIndex]; Vector2 screenPt = oobbScreenPts[ptIndex]; float distSq = (mousePos - screenPt).sqrMagnitude; if (distSq < minDistanceSq) { minDistanceSq = distSq; _pivot = worldPt; _isPivotAvailable = true; } } } } } else if (_state == State.Snap && _isPivotAvailable) { GameObjectExtensions.RecordObjectTransformsForUndo(_selectedParents); MouseCursorRayHit cursorHit = MouseCursor.Instance.GetCursorRayHitForGridCell(); if (cursorHit.WasACellHit) { Camera camera = SceneViewCamera.Camera; Vector2 mousePos = Event.current.InvMousePos(camera); GridCellRayHit cellRayHit = cursorHit.GridCellRayHit; Vector3 snapDestination = Vector3Extensions.GetClosestPointToPoint(cellRayHit.HitCell.Quad.GetCenterAndCornerPoints(), cellRayHit.HitPoint); Vector3 moveVector = snapDestination - _pivot; foreach (var parent in _selectedParents) { if (parent != null) { parent.transform.position += moveVector; } } _pivot = snapDestination; ObjectSelection.Get().ObjectSelectionTransformGizmoSystem.OnObjectSelectionUpdated(); } } }
public Vector3 GetPointClosestToPoint(Vector3 point) { return(Vector3Extensions.GetClosestPointToPoint(GetPoints(), point)); }
public void UpdateForMouseMovement() { if (!_isActive) { return; } if (MouseButtonStates.Instance.IsMouseButtonDown(MouseButton.Left)) { _state = ObjectVertexSnapSessionState.SnapToDestination; } else { _state = ObjectVertexSnapSessionState.SelectSourceVertex; } if (_state == ObjectVertexSnapSessionState.SelectSourceVertex) { if (_sourceObjects == null || _sourceObjects.Count == 0) { _objectMask.ObjectCollectionMask.UnmaskAll(); MouseCursorRayHit cursorRayHit = GetCursorRayHit(); if (cursorRayHit.WasAnObjectHit) { GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit; MeshRayHit meshRayHit = objectRayHit.ObjectMeshHit; if (meshRayHit != null) { Octave3DMesh octaveMesh = meshRayHit.HitMesh; Triangle3D sourceTriangle = octaveMesh.GetTriangle(meshRayHit.HitTriangleIndex); sourceTriangle.TransformPoints(objectRayHit.HitObject.transform.localToWorldMatrix); _sourceVertex = sourceTriangle.GetPointClosestToPoint(meshRayHit.HitPoint); _sourceObject = objectRayHit.HitObject; _objectMask.ObjectCollectionMask.Mask(_sourceObject.transform.root.gameObject.GetAllChildrenIncludingSelf()); } else { SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>(); if (spriteRenderer != null) { _sourceObject = objectRayHit.HitObject; _sourceVertex = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint); _objectMask.ObjectCollectionMask.Mask(_sourceObject.transform.root.gameObject.GetAllChildrenIncludingSelf()); } } } } else { MouseCursorRayHit cursorRayHit = GetCursorRayHit(); if (cursorRayHit.WasAnObjectHit) { GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit; MeshRayHit meshRayHit = objectRayHit.ObjectMeshHit; if (meshRayHit != null) { Octave3DMesh octaveMesh = meshRayHit.HitMesh; Triangle3D sourceTriangle = octaveMesh.GetTriangle(meshRayHit.HitTriangleIndex); sourceTriangle.TransformPoints(objectRayHit.HitObject.transform.localToWorldMatrix); _sourceVertex = sourceTriangle.GetPointClosestToPoint(meshRayHit.HitPoint); } else { SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>(); if (spriteRenderer != null) { _sourceVertex = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint); } } } foreach (var parent in _sourceParents) { _objectMask.ObjectCollectionMask.Mask(parent.GetAllChildrenIncludingSelf()); } } } else { MouseCursorRayHit cursorRayHit = GetCursorRayHit(); if (cursorRayHit.WasAnythingHit) { bool useGridCellHit = false; if (!cursorRayHit.WasAnObjectHit) { useGridCellHit = true; } else if (cursorRayHit.WasAnObjectHit && cursorRayHit.WasACellHit) { float gridCellHitEnter = cursorRayHit.GridCellRayHit.HitEnter; float objectHitEnter = cursorRayHit.ClosestObjectRayHit.HitEnter; if (gridCellHitEnter < Mathf.Max(0.0f, (objectHitEnter - 1e-3f))) { useGridCellHit = true; } } if (useGridCellHit) { XZGridCell hitCell = cursorRayHit.GridCellRayHit.HitCell; XZOrientedQuad3D cellQuad = hitCell.Quad; _destinationObject = null; _destinationGridCell = hitCell; _snapPosition = cellQuad.GetPointClosestToPoint(cursorRayHit.GridCellRayHit.HitPoint, true); SnapToDestination(); } else { GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit; MeshRayHit meshRayHit = objectRayHit.ObjectMeshHit; if (meshRayHit != null) { _destinationObject = objectRayHit.HitObject; Triangle3D destinationTriangle = meshRayHit.HitMesh.GetTriangle(meshRayHit.HitTriangleIndex); destinationTriangle.TransformPoints(_destinationObject.transform.localToWorldMatrix); _destinationGridCell = null; _snapPosition = destinationTriangle.GetPointClosestToPoint(meshRayHit.HitPoint); SnapToDestination(); } else { SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>(); if (spriteRenderer != null) { _destinationGridCell = null; _destinationObject = objectRayHit.HitObject; _snapPosition = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint); SnapToDestination(); } } } } } }