public void OnInputDown(InputEventData eventData) { if (currentInputSource == null) { IMixedRealityPointer pointer = eventData.InputSource.Pointers[0]; Ray ray; if (pointer.TryGetPointingRay(out ray)) { handleMoveType = HandleMoveType.Ray; float distance; Collider grabbedCollider = GetGrabbedCollider(ray, out distance); if (grabbedCollider != null) { currentInputSource = eventData.InputSource; currentPointer = pointer; grabbedHandle = grabbedCollider.gameObject; currentHandleType = GetHandleType(grabbedHandle); currentRotationAxis = GetRotationAxis(grabbedHandle); currentPointer.TryGetPointingRay(out initialGrabRay); initialGrabMag = distance; initialGrabbedPosition = grabbedHandle.transform.position; initialGrabbedCentroid = targetObject.transform.position; initialScale = targetObject.transform.localScale; pointer.TryGetPointerPosition(out initialGrabPoint); ShowOneHandle(grabbedHandle); initialGazePoint = Vector3.zero; } } } }
void IMixedRealityInputHandler <MixedRealityPose> .OnInputChanged(InputEventData <MixedRealityPose> eventData) { if (eventData.MixedRealityInputAction != dragPositionAction || !isDraggingEnabled || !isDragging || eventData.SourceId != currentInputSource?.SourceId) { return; } Transform cameraTransform = CameraCache.Main.transform; Vector3 pivotPosition = Vector3.zero; if (isDraggingWithSourcePose) { Vector3 inputPosition = eventData.InputData.Position; pivotPosition = HandPivotPosition; Vector3 newHandDirection = Vector3.Normalize(inputPosition - pivotPosition); if (handRefDistance < 0) { handRefDistance = Vector3.Magnitude(inputPosition - pivotPosition); Vector3 handDirection = cameraTransform.InverseTransformDirection(Vector3.Normalize(inputPosition - pivotPosition)); // Store the initial offset between the hand and the object, so that we can consider it when dragging gazeAngularOffset = Quaternion.FromToRotation(handDirection, objectReferenceDirection); } // in camera space newHandDirection = cameraTransform.InverseTransformDirection(newHandDirection); Vector3 targetDirection = Vector3.Normalize(gazeAngularOffset * newHandDirection); // back to world space targetDirection = cameraTransform.TransformDirection(targetDirection); float currentHandDistance = Vector3.Magnitude(inputPosition - pivotPosition); float distanceRatio = currentHandDistance / handRefDistance; float distanceOffset = distanceRatio > 0 ? (distanceRatio - 1f) * distanceScale : 0; float targetDistance = objectReferenceDistance + distanceOffset; draggingPosition = pivotPosition + (targetDirection * targetDistance); } else { pivotPosition = cameraTransform.position; Vector3 pointerPosition; currentPointer.TryGetPointerPosition(out pointerPosition); Ray pointingRay; currentPointer.TryGetPointingRay(out pointingRay); Vector3 currentPosition = pointerPosition; Vector3 currentPositionHeadSpace = cameraTransform.InverseTransformPoint(currentPosition); Vector3 positionDeltaHeadSpace = currentPositionHeadSpace - previousPointerPositionHeadSpace; float pushDistance = Vector3.Dot(positionDeltaHeadSpace, cameraTransform.InverseTransformDirection(pointingRay.direction.normalized)); if (Mathf.Abs(Vector3.Dot(positionDeltaHeadSpace.normalized, Vector3.forward)) > zPushTolerance) { stickLength = DistanceRamp(stickLength, pushDistance); } draggingPosition = pointingRay.GetPoint(stickLength); previousPointerPositionHeadSpace = currentPositionHeadSpace; } switch (rotationMode) { case RotationModeEnum.OrientTowardUser: case RotationModeEnum.OrientTowardUserAndKeepUpright: draggingRotation = Quaternion.LookRotation(hostTransform.position - pivotPosition); break; case RotationModeEnum.LockObjectRotation: draggingRotation = hostTransform.rotation; break; default: // in world space Vector3 objForward = cameraTransform.TransformDirection(objectReferenceForward); // in world space Vector3 objUp = cameraTransform.TransformDirection(objectReferenceUp); draggingRotation = Quaternion.LookRotation(objForward, objUp); break; } Vector3 newPosition = Vector3.Lerp(hostTransform.position, draggingPosition + cameraTransform.TransformDirection(objectReferenceGrabPoint), positionLerpSpeed); // Apply Final Position if (hostRigidbody == null) { hostTransform.position = newPosition; } else { hostRigidbody.MovePosition(newPosition); } // Apply Final Rotation Quaternion newRotation = Quaternion.Lerp(hostTransform.rotation, draggingRotation, rotationLerpSpeed); if (hostRigidbody == null) { hostTransform.rotation = newRotation; } else { hostRigidbody.MoveRotation(newRotation); } if (rotationMode == RotationModeEnum.OrientTowardUserAndKeepUpright) { Quaternion upRotation = Quaternion.FromToRotation(hostTransform.up, Vector3.up); hostTransform.rotation = upRotation * hostTransform.rotation; } }