void Update() { var activeController = OVRInputHelpers.GetConnectedControllers((OVRInputHelpers.HandFilter)handType); Ray selectionRay; bool gotRay = OVRInputHelpers.GetSelectionRay(activeController, trackingSpace, out selectionRay); SetPointerVisibility(gotRay && trackingSpace != null && activeController != OVRInput.Controller.None); if (gotRay) { SetPointer(selectionRay); } }
// Overridden so that we can process the two types of pointer separately // The following 2 functions are equivalent to PointerInputModule.GetMousePointerEventData but are customized to // get data for ray pointers and canvas mouse pointers. /// <summary> /// State for a pointer controlled by a world space ray. E.g. gaze pointer /// </summary> /// <returns></returns> protected MouseState GetGazePointerData(bool isLeft) { // Get the OVRRayPointerEventData reference OVRRayPointerEventData leftData; GetPointerData(kMouseLeftId, out leftData, true, isLeft); leftData.Reset(); leftData.worldSpaceRay = default(Ray); var activeController = OVRInputHelpers.GetConnectedControllers(isLeft ? OVRInputHelpers.HandFilter.Left : OVRInputHelpers.HandFilter.Right); bool gotRay = OVRInputHelpers.GetSelectionRay(activeController, trackingSpace, out leftData.worldSpaceRay); leftData.scrollDelta = GetExtraScrollDelta(); //Populate some default values leftData.button = UnityEngine.EventSystems.PointerEventData.InputButton.Left; leftData.useDragThreshold = true; // Perform raycast to find intersections with world eventSystem.RaycastAll(leftData, m_RaycastResultCache); var raycast = FindFirstRaycast(m_RaycastResultCache); leftData.pointerCurrentRaycast = raycast; m_RaycastResultCache.Clear(); OVRRaycaster ovrRaycaster = raycast.module as OVRRaycaster; // We're only interested in intersections from OVRRaycasters if (ovrRaycaster) { // The Unity UI system expects event data to have a screen position // so even though this raycast came from a world space ray we must get a screen // space position for the camera attached to this raycaster for compatability leftData.position = ovrRaycaster.GetScreenPosition(raycast); // Find the world position and normal the Graphic the ray intersected RectTransform graphicRect = raycast.gameObject.GetComponent <RectTransform>(); if (graphicRect != null) { // Set are gaze indicator with this world position and normal // Vector3 worldPos = raycast.worldPosition; //Vector3 normal = GetRectTransformNormal(graphicRect); if (OnSelectionRayHit != null) { OnSelectionRayHit(raycast.worldPosition, raycast.worldNormal); } } } OVRPhysicsRaycaster physicsRaycaster = raycast.module as OVRPhysicsRaycaster; if (physicsRaycaster) { leftData.position = physicsRaycaster.GetScreenPos(raycast.worldPosition); if (OnSelectionRayHit != null) { OnSelectionRayHit(raycast.worldPosition, raycast.worldNormal); } } // Stick default data values in right and middle slots for compatability // copy the apropriate data into right and middle slots OVRRayPointerEventData rightData; GetPointerData(kMouseRightId, out rightData, true, isLeft); CopyFromTo(leftData, rightData); rightData.button = UnityEngine.EventSystems.PointerEventData.InputButton.Right; OVRRayPointerEventData middleData; GetPointerData(kMouseMiddleId, out middleData, true, isLeft); CopyFromTo(leftData, middleData); middleData.button = UnityEngine.EventSystems.PointerEventData.InputButton.Middle; var mouseState = isLeft ? m_MouseStateLeft : m_MouseStateRight; mouseState.SetButtonState(UnityEngine.EventSystems.PointerEventData.InputButton.Left, GetGazeButtonState(activeController), leftData); mouseState.SetButtonState(UnityEngine.EventSystems.PointerEventData.InputButton.Right, UnityEngine.EventSystems.PointerEventData.FramePressState.NotChanged, rightData); mouseState.SetButtonState(UnityEngine.EventSystems.PointerEventData.InputButton.Middle, UnityEngine.EventSystems.PointerEventData.FramePressState.NotChanged, middleData); return(mouseState); }
void Update() { Ray pointerLeft; Ray pointerRight; // Get left & right rays var activeControllerLeft = OVRInputHelpers.GetConnectedControllers(OVRInputHelpers.HandFilter.Left); var activeControllerRight = OVRInputHelpers.GetConnectedControllers(OVRInputHelpers.HandFilter.Right); bool gotRayLeft = OVRInputHelpers.GetSelectionRay(activeControllerLeft, trackingSpace, out pointerLeft); bool gotRayRight = OVRInputHelpers.GetSelectionRay(activeControllerRight, trackingSpace, out pointerRight); // Cast the rays RaycastHit hitLeft = default(RaycastHit); RaycastHit hitRight = default(RaycastHit); bool isHitLeft = gotRayLeft && Physics.Raycast(pointerLeft, out hitLeft, raycastDistance, ~excludeLayers); bool isHitRight = gotRayRight && Physics.Raycast(pointerRight, out hitRight, raycastDistance, ~excludeLayers); // Process the hits _actions.Clear(); if (lastHitLeft != null) { _actions[lastHitLeft] = HoverAction.None; } if (lastHitRight != null) { _actions[lastHitRight] = HoverAction.None; } if (isHitLeft) { _actions[hitLeft.transform] = HoverAction.None; } if (isHitRight) { _actions[hitRight.transform] = HoverAction.None; } ProcessHit(_actions, isHitLeft, hitLeft.transform, ref lastHitLeft); ProcessHit(_actions, isHitRight, hitRight.transform, ref lastHitRight); // Perform the actions foreach (KeyValuePair <Transform, HoverAction> kvp in _actions) { // Only perform enter / exit actions if we are not staying on existing transform // and if we're not both entering & exiting if ((kvp.Value & HoverAction.Stay) == HoverAction.None && (kvp.Value & (HoverAction.Enter | HoverAction.Exit)) != (HoverAction.Enter | HoverAction.Exit)) { if ((kvp.Value & HoverAction.Enter) != HoverAction.None && onHoverEnter != null) { onHoverEnter.Invoke(kvp.Key); } if ((kvp.Value & HoverAction.Exit) != HoverAction.None && onHoverExit != null) { onHoverExit.Invoke(kvp.Key); } } if ((kvp.Value & HoverAction.Hover) != HoverAction.None && onHover != null) { // Hovering over transform onHover.Invoke(kvp.Key); } } if (isHitLeft && !_isDragging) { ProcessButtonPresses(activeControllerLeft, true, lastHitLeft, ref triggerButtonDownLeft, ref padButtonDownLeft, ref tertiaryButtonDownLeft); ProcessHandPinch(activeControllerLeft, lastHitLeft, ref triggerFingerDownLeft, ref padFingerDownLeft, ref tertiaryFingerDownLeft); } if (isHitRight && !_isDragging) { ProcessButtonPresses(activeControllerRight, false, lastHitRight, ref triggerButtonDownRight, ref padButtonDownRight, ref tertiaryButtonDownRight); ProcessHandPinch(activeControllerRight, lastHitRight, ref triggerFingerDownRight, ref padFingerDownRight, ref tertiaryFingerDownRight); } }