Exemple #1
0
    // use screen midpoint as locked pointer location, enabling look location to be the "mouse"
    private PointerEventData GetLookPointerEventData()
    {
        VRCursorController cursor        = VRCursorController.GetInstance();
        Vector3            lookPosition  = cursor.GetCursorRaycastPosition(RAYCAST_OFFSET);
        RaycastHit         testRayResult = new RaycastHit();

        if (m_hoverData == null)
        {
            m_hoverData = new PointerEventData(eventSystem);
        }

        m_hoverData.Reset();
        m_hoverData.delta       = Vector2.zero;
        m_hoverData.position    = lookPosition;
        m_hoverData.scrollDelta = Vector2.zero;

        // Only bother trying to raycast to UI on the XY plane
        if (cursor.CursorPlane == VRCursorController.eCursorPlane.XYPlane)
        {
            Vector3 lookDirection = cursor.GetCursorRaycastDirection();
            Ray     testRay       = new Ray(lookPosition, lookDirection);

            if (Physics.Raycast(testRay, out testRayResult, RAYCAST_OFFSET + 1.0f, UI_COLLISION_LAYER_MASK))
            {
                RaycastResult raycastResult = new RaycastResult();

                raycastResult.Clear();
                raycastResult.gameObject = testRayResult.collider.gameObject;
                raycastResult.depth      = 0;
                raycastResult.distance   = testRayResult.distance;

                m_hoverData.pointerCurrentRaycast = raycastResult;
            }
        }

        if (m_hoverData.pointerCurrentRaycast.gameObject != null)
        {
            m_guiRaycastHit = true;
        }
        else
        {
            m_guiRaycastHit = false;
        }

        return(m_hoverData);
    }
Exemple #2
0
    private void CastRay()
    {
        Vector2 currentPose = lastPose;

        if (IsPointerActiveAndAvailable())
        {
            currentPose = GvrMathHelpers.NormalizedCartesianToSpherical(Pointer.PointerTransform.forward);
        }

        if (CurrentEventData == null)
        {
            CurrentEventData = new PointerEventData(ModuleController.eventSystem);
            lastPose         = currentPose;
        }

        // Store the previous raycast result.
        RaycastResult previousRaycastResult = CurrentEventData.pointerCurrentRaycast;

        // The initial cast must use the enter radius.
        if (IsPointerActiveAndAvailable())
        {
            Pointer.ShouldUseExitRadiusForRaycast = false;
        }

        // Cast a ray into the scene
        CurrentEventData.Reset();
        // Set the position to the center of the camera.
        // This is only necessary if using the built-in Unity raycasters.
        RaycastResult raycastResult;

        CurrentEventData.position = GvrMathHelpers.GetViewportCenter();
        bool isPointerActiveAndAvailable = IsPointerActiveAndAvailable();

        if (isPointerActiveAndAvailable)
        {
            RaycastAll();
            raycastResult = ModuleController.FindFirstRaycast(ModuleController.RaycastResultCache);
        }
        else
        {
            raycastResult = new RaycastResult();
            raycastResult.Clear();
        }

        // If we were already pointing at an object we must check that object against the exit radius
        // to make sure we are no longer pointing at it to prevent flicker.
        if (previousRaycastResult.gameObject != null &&
            raycastResult.gameObject != previousRaycastResult.gameObject &&
            isPointerActiveAndAvailable)
        {
            Pointer.ShouldUseExitRadiusForRaycast = true;
            RaycastAll();
            RaycastResult firstResult = ModuleController.FindFirstRaycast(ModuleController.RaycastResultCache);
            if (firstResult.gameObject == previousRaycastResult.gameObject)
            {
                raycastResult = firstResult;
            }
        }

        if (raycastResult.gameObject != null && raycastResult.worldPosition == Vector3.zero)
        {
            raycastResult.worldPosition =
                GvrMathHelpers.GetIntersectionPosition(CurrentEventData.enterEventCamera, raycastResult);
        }

        CurrentEventData.pointerCurrentRaycast = raycastResult;

        // Find the real screen position associated with the raycast
        // Based on the results of the hit and the state of the pointerData.
        if (raycastResult.gameObject != null)
        {
            CurrentEventData.position = raycastResult.screenPosition;
        }
        else if (IsPointerActiveAndAvailable() && CurrentEventData.enterEventCamera != null)
        {
            Vector3 pointerPos = Pointer.MaxPointerEndPoint;
            CurrentEventData.position = CurrentEventData.enterEventCamera.WorldToScreenPoint(pointerPos);
        }

        ModuleController.RaycastResultCache.Clear();
        CurrentEventData.delta = currentPose - lastPose;
        lastPose = currentPose;

        // Check to make sure the Raycaster being used is a GvrRaycaster.
        if (raycastResult.module != null &&
            !(raycastResult.module is GvrPointerGraphicRaycaster) &&
            !(raycastResult.module is GvrPointerPhysicsRaycaster))
        {
            Debug.LogWarning("Using Raycaster (Raycaster: " + raycastResult.module.GetType() +
                             ", Object: " + raycastResult.module.name + "). It is recommended to use " +
                             "GvrPointerPhysicsRaycaster or GvrPointerGrahpicRaycaster with GvrPointerInputModule.");
        }
    }
Exemple #3
0
        private void CastRayFromGaze()
        {
            Vector2 headPose =
                NormalizedCartesianToSpherical(NxrViewer.Instance.HeadPose.Orientation * Vector3.forward);

            if (pointerData == null)
            {
                pointerData  = new PointerEventData(eventSystem);
                lastHeadPose = headPose;
            }

            Vector2 diff = headPose - lastHeadPose;

            if (screenCenterVec.x == 0)
            {
                screenCenterVec = new Vector2(0.5f * Screen.width, 0.5f * Screen.height);
            }

            // Cast a ray into the scene
            pointerData.Reset();
            //
            var raycastResult = new RaycastResult();

            raycastResult.Clear();
            Transform mTrans = NxrViewer.Instance.GetHead().GetTransform();

            raycastResult.worldPosition       = mTrans.position;
            raycastResult.worldNormal         = mTrans.forward;
            pointerData.pointerCurrentRaycast = raycastResult;
            //
            pointerData.position = new Vector2(0.5f * Screen.width, 0.5f * Screen.height);
            eventSystem.RaycastAll(pointerData, m_RaycastResultCache);
            foreach (var mRaycastResult in m_RaycastResultCache)
            {
                if (mRaycastResult.gameObject.layer == 8)
                {
                    pointerData.pointerCurrentRaycast = mRaycastResult;
                    isSpecial = true;
                    break;
                }
            }

            if (!isSpecial)
            {
                pointerData.pointerCurrentRaycast = FindFirstRaycast(m_RaycastResultCache);
                foreach (RaycastResult mRaycastResult in m_RaycastResultCache)
                {
                    bool IsUICanvasObject = mRaycastResult.gameObject != null &&
                                            mRaycastResult.gameObject.GetComponent <NxrUICanvas>() != null;
                    if (!IsUICanvasObject)
                    {
                        pointerData.pointerCurrentRaycast = mRaycastResult;
                        break;
                    }
                }
            }

            isSpecial = false;
            m_RaycastResultCache.Clear();
            pointerData.delta = diff;
            lastHeadPose      = headPose;
            if (pointerData.pointerCurrentRaycast.gameObject == null && eventSystem.currentSelectedGameObject != null)
            {
                Debug.LogError("Clear Seleted GameObject-Gaze=>" + eventSystem.currentSelectedGameObject.name);
                eventSystem.SetSelectedGameObject(null);
            }
        }
    /// @endcond

    private void CastRay()
    {
        if (pointer == null || pointer.PointerTransform == null)
        {
            return;
        }
        Vector2 currentPose = NormalizedCartesianToSpherical(pointer.PointerTransform.forward);

        if (pointerData == null)
        {
            pointerData = new PointerEventData(eventSystem);
            lastPose    = currentPose;
        }

        // Store the previous raycast result.
        RaycastResult previousRaycastResult = pointerData.pointerCurrentRaycast;

        // The initial cast must use the enter radius.
        if (pointer != null)
        {
            pointer.ShouldUseExitRadiusForRaycast = false;
        }

        // Cast a ray into the scene
        pointerData.Reset();
        // Set the position to the center of the camera.
        // This is only necessary if using the built-in Unity raycasters.
        RaycastResult raycastResult;

        pointerData.position = GetViewportCenter();
        bool isPointerActiveAndAvailable = IsPointerActiveAndAvailable();

        if (isPointerActiveAndAvailable)
        {
            eventSystem.RaycastAll(pointerData, m_RaycastResultCache);
            raycastResult = FindFirstRaycast(m_RaycastResultCache);
        }
        else
        {
            raycastResult = new RaycastResult();
            raycastResult.Clear();
        }

        // If we were already pointing at an object we must check that object against the exit radius
        // to make sure we are no longer pointing at it to prevent flicker.
        if (previousRaycastResult.gameObject != null &&
            raycastResult.gameObject != previousRaycastResult.gameObject &&
            isPointerActiveAndAvailable)
        {
            if (pointer != null)
            {
                pointer.ShouldUseExitRadiusForRaycast = true;
            }
            m_RaycastResultCache.Clear();
            eventSystem.RaycastAll(pointerData, m_RaycastResultCache);
            RaycastResult firstResult = FindFirstRaycast(m_RaycastResultCache);
            if (firstResult.gameObject == previousRaycastResult.gameObject)
            {
                raycastResult = firstResult;
            }
        }

        if (raycastResult.gameObject != null && raycastResult.worldPosition == Vector3.zero)
        {
            raycastResult.worldPosition = GetIntersectionPosition(pointerData.enterEventCamera, raycastResult);
        }

        pointerData.pointerCurrentRaycast = raycastResult;

        // Find the real screen position associated with the raycast
        // Based on the results of the hit and the state of the pointerData.
        if (raycastResult.gameObject != null)
        {
            pointerData.position = raycastResult.screenPosition;
        }
        else
        {
            Transform pointerTransform   = pointer.PointerTransform;
            float     maxPointerDistance = pointer.MaxPointerDistance;
            Vector3   pointerPos         = pointerTransform.position + (pointerTransform.forward * maxPointerDistance);
            if (pointerData.pressEventCamera != null)
            {
                pointerData.position = pointerData.pressEventCamera.WorldToScreenPoint(pointerPos);
            }
            else if (Camera.main != null)
            {
                pointerData.position = Camera.main.WorldToScreenPoint(pointerPos);
            }
        }

        m_RaycastResultCache.Clear();
        pointerData.delta = currentPose - lastPose;
        lastPose          = currentPose;

        // Check to make sure the Raycaster being used is a GvrRaycaster.
//    if (raycastResult.module != null
//     // && !(raycastResult.module is GvrPointerGraphicRaycaster)
//      && !(raycastResult.module is GvrPointerPhysicsRaycaster)) {
//      Debug.LogWarning("Using Raycaster (Raycaster: " + raycastResult.module.GetType() +
//        ", Object: " + raycastResult.module.name + "). It is recommended to use " +
//        "GvrPointerPhysicsRaycaster or GvrPointerGrahpicRaycaster with GvrPointerInputModule.");
//    }
    }