Beispiel #1
0
    /// <summary>
    /// Calculates the ray for the segment of the Hybrid raycast determined by the raycast mode
    /// passed in.
    /// </summary>
    /// <remarks>
    /// Throws an exception if Hybrid is passed in.
    /// </remarks>
    /// <param name="pointer">Which pointer to project the ray from.</param>
    /// <param name="hybridMode">
    /// Which Raycast sub-mode to use within Hybrid mode.  Must be Camera or Direct.
    /// </param>
    /// <returns>The PointerRay as projected from the GvrbasePointer in the given mode.</returns>
    public static PointerRay CalculateHybridRay(GvrBasePointer pointer, RaycastMode hybridMode)
    {
        PointerRay result;

        switch (hybridMode)
        {
        case RaycastMode.Direct:
            result          = CalculateRay(pointer, hybridMode);
            result.distance = pointer.CameraRayIntersectionDistance;
            break;

        case RaycastMode.Camera:
            result = CalculateRay(pointer, hybridMode);
            PointerRay directRay = CalculateHybridRay(pointer, RaycastMode.Direct);
            result.ray.origin        = directRay.ray.GetPoint(directRay.distance);
            result.distanceFromStart = directRay.distance;
            result.distance          = pointer.MaxPointerDistance - directRay.distance;
            break;

        default:
            throw new UnityException(
                      "Invalid RaycastMode " + hybridMode + " passed into CalculateHybridRay.");
        }

        return(result);
    }
Beispiel #2
0
    /// @endcond
#if UNITY_EDITOR
    /// <summary>Draws gizmos that visualize the rays used by the pointer for raycasting.</summary>
    /// <remarks><para>
    /// These rays will change based on the `raycastMode` selected.
    /// </para><para>
    /// This is a `MonoBehavior` builtin implementation: Implement `OnDrawGizmos` if you want to
    /// draw gizmos that are also pickable and always drawn.  This allows you to quickly pick
    /// important objects in your Scene.  Note that `OnDrawGizmos` will use a mouse position that is
    /// relative to the Scene View.  This function does not get called if the component is collapsed
    /// in the inspector. Use `OnDrawGizmosSelected` to draw gizmos when the game object is
    /// selected.
    /// </para></remarks>
    protected virtual void OnDrawGizmos()
    {
        if (drawDebugRays && Application.isPlaying && isActiveAndEnabled)
        {
            switch (raycastMode)
            {
            case RaycastMode.Camera:
                // Camera line.
                Gizmos.color = Color.green;
                PointerRay pointerRay = CalculateRay(this, RaycastMode.Camera);
                Gizmos.DrawLine(pointerRay.ray.origin,
                                pointerRay.ray.GetPoint(pointerRay.distance));
                Camera camera = PointerCamera;

                // Pointer to intersection dotted line.
                Vector3 intersection = PointerTransform.position +
                                       (PointerTransform.forward *
                                        CameraRayIntersectionDistance);
                UnityEditor.Handles.DrawDottedLine(PointerTransform.position,
                                                   intersection,
                                                   1.0f);
                break;

            case RaycastMode.Direct:
                // Direct line.
                Gizmos.color = Color.blue;
                pointerRay   = CalculateRay(this, RaycastMode.Direct);
                Gizmos.DrawLine(pointerRay.ray.origin,
                                pointerRay.ray.GetPoint(pointerRay.distance));
                break;

            case RaycastMode.Hybrid:
                // Direct line.
                Gizmos.color = Color.blue;
                pointerRay   = CalculateHybridRay(this, RaycastMode.Direct);
                Gizmos.DrawLine(pointerRay.ray.origin,
                                pointerRay.ray.GetPoint(pointerRay.distance));

                // Camera line.
                Gizmos.color = Color.green;
                pointerRay   = CalculateHybridRay(this, RaycastMode.Camera);
                Gizmos.DrawLine(pointerRay.ray.origin,
                                pointerRay.ray.GetPoint(pointerRay.distance));

                // Camera to intersection dotted line.
                camera = PointerCamera;
                if (camera != null)
                {
                    UnityEditor.Handles.DrawDottedLine(camera.transform.position,
                                                       pointerRay.ray.origin,
                                                       1.0f);
                }

                break;

            default:
                break;
            }
        }
    }
Beispiel #3
0
        public override void OnVRPlayerDeath(PhysicalVRPlayer player)
        {
            if (player != _player)
            {
                throw new Exception("The passed player is not the same as the saved player");
            }

            _player = null;

            if (CloneManager.Instance.GetNumClones() == 0) // this is a game over, go to UI
            {
                TransportUtils.TransportTo(VRManager.Instance.Player.transform, VRManager.Instance.Player.transform.position, VRManager.Instance.Player.transform.rotation, new Vector3(0f, 40f, 0f), Quaternion.identity, 5f, delegate
                {
                    PointerRay.SetEnabled(true);
                    PointerRay.OnAnyButtonClicked = delegate
                    {
                        FindPlayer();

                        PointerRay.SetEnabled(false);
                        PointerRay.OnAnyButtonClicked = null;
                    };
                });
                return;
            }

            FindPlayer();
        }
Beispiel #4
0
    /// <summary>
    /// Calculates the ray for a given Raycast mode.
    /// </summary>
    /// <remarks>
    /// Will throw an exception if the raycast mode Hybrid is passed in.
    /// If you need to calculate the ray for the direct or camera segment of the Hybrid raycast,
    /// use CalculateHybridRay instead.
    /// </remarks>
    /// <param name="pointer">Which pointer to project the ray from.</param>
    /// <param name="mode">Which Raycast mode to use.  Must be Camera or Direct.</param>
    /// <returns>The PointerRay as projected from the GvrbasePointer in the given mode.</returns>
    public static PointerRay CalculateRay(GvrBasePointer pointer, RaycastMode mode)
    {
        PointerRay result = new PointerRay();

        if (pointer == null || !pointer.IsAvailable)
        {
            Debug.LogError("Cannot calculate ray when the pointer isn't available.");
            return(result);
        }

        Transform pointerTransform = pointer.PointerTransform;

        if (pointerTransform == null)
        {
            Debug.LogError("Cannot calculate ray when pointerTransform is null.");
            return(result);
        }

        result.distance = pointer.MaxPointerDistance;

        switch (mode)
        {
        case RaycastMode.Camera:
            Camera camera = pointer.PointerCamera;
            if (camera == null)
            {
                Debug.LogError("Cannot calculate ray because pointer.PointerCamera is null." +
                               "To fix this, either tag a Camera as \"MainCamera\" or set " +
                               "overridePointerCamera.");
                return(result);
            }

            Vector3 rayPointerStart = pointerTransform.position;
            Vector3 rayPointerEnd   = rayPointerStart +
                                      (pointerTransform.forward *
                                       pointer.CameraRayIntersectionDistance);

            Vector3 cameraLocation    = camera.transform.position;
            Vector3 finalRayDirection = rayPointerEnd - cameraLocation;
            finalRayDirection.Normalize();

            Vector3 finalRayStart = cameraLocation + (finalRayDirection * camera.nearClipPlane);

            result.ray = new Ray(finalRayStart, finalRayDirection);
            break;

        case RaycastMode.Direct:
            result.ray = new Ray(pointerTransform.position, pointerTransform.forward);
            break;

        default:
            throw new UnityException(
                      "Invalid RaycastMode " + mode + " passed into CalculateRay.");
        }

        return(result);
    }
Beispiel #5
0
        public override void OnGameModeStarted()
        {
            VRManager.Instance.Player.Scale = 1f;
            VRManager.Instance.Player.LeftController.ColliderActive  = true;
            VRManager.Instance.Player.RightController.ColliderActive = true;
            VRManager.Instance.Player.LeftController.RendererActive  = true;
            VRManager.Instance.Player.RightController.RendererActive = true;

            VRManager.Instance.Player.transform.position = new Vector3(0f, 40f, 0f);

            PointerRay.SetEnabled(true);
        }
    private PointerRay GetRayForDistance()
    {
        PointerRay result           = new PointerRay();
        Transform  pointerTransform = m_Raycaster.transform;

        if (pointerTransform == null)
        {
            Debug.LogError("Cannot calculate ray when pointerTransform is null.");
            return(result);
        }

        result.distance = defaultDistance;
        result.ray      = new Ray(pointerTransform.position, pointerTransform.forward);
        return(result);
    }
Beispiel #7
0
    /// <summary>
    /// Returns the ray used for projecting points out of the pointer for the given distance.
    /// </summary>
    /// <remarks>
    /// In Hybrid raycast mode, the ray will be different depending upon the distance.
    /// In Camera or Direct raycast mode, the ray will always be the same.
    /// </remarks>
    /// <param name="distance">The distance to check.</param>
    /// <returns>
    /// Either the Camera or Controller's PointerRay. For Hybrid mode, this will return Camera at
    /// large distances and Controller at close distances.  For other modes, the ray will always
    /// be the mode's associated ray (Camera=Camera, Direct=Controller).
    /// </returns>
    public PointerRay GetRayForDistance(float distance)
    {
        PointerRay result = new PointerRay();

        if (raycastMode == RaycastMode.Hybrid)
        {
            float directDistance = CameraRayIntersectionDistance;
            if (distance < directDistance)
            {
                result = CalculateHybridRay(this, RaycastMode.Direct);
            }
            else
            {
                result = CalculateHybridRay(this, RaycastMode.Camera);
            }
        }
        else
        {
            result = CalculateRay(this, raycastMode);
        }

        return(result);
    }
Beispiel #8
0
    /// <summary>
    /// Returns a point in worldspace a specified distance along the pointer.
    /// </summary>
    /// <remarks><para>
    /// What this point will be is different depending on the raycastMode.
    /// </para><para>
    /// Because raycast modes differ, use this function instead of manually calculating a point
    /// projected from the pointer.
    /// </para></remarks>
    /// <param name="distance">The distance along the pointer's laser.</param>
    /// <returns>A worlspace position along the pointer's laser.</returns>
    public Vector3 GetPointAlongPointer(float distance)
    {
        PointerRay pointerRay = GetRayForDistance(distance);

        return(pointerRay.ray.GetPoint(distance - pointerRay.distanceFromStart));
    }
Beispiel #9
0
 public override void OnGameModeQuit()
 {
     PointerRay.SetEnabled(false);
 }