Helper functions to perform common math operations for Gvr.
Exemple #1
0
    void Update()
    {
        while (ExecuteOnMainThread.Count > 0)
        {
            ExecuteOnMainThread.Dequeue().Invoke();
        }

        if (VideoReady)
        {
            IssuePlayerEvent(RenderCommand.UpdateVideo);
            GetVideoMatrix(videoPlayerPtr, videoMatrixRaw);
            videoMatrix = GvrMathHelpers.ConvertFloatArrayToMatrix(videoMatrixRaw);
            long vidTimestamp = GetVideoTimestampNs(videoPlayerPtr);
            if (vidTimestamp != lastVideoTimestamp)
            {
                framecount++;
            }
            lastVideoTimestamp = vidTimestamp;

            UpdateMaterial();

            if (adjustAspectRatio)
            {
                AdjustAspectRatio();
            }

            long bp = BufferedPosition;
            if (bp != lastBufferedPosition)
            {
                lastBufferedPosition = bp;
            }
        }
    }
        private void _HandleRecenterEvent(ref HeadsetState state, gvr_recenter_event_data eventData)
        {
            state.recenterEventType  = (GvrRecenterEventType)eventData.recenter_event_type;
            state.recenterEventFlags = eventData.recenter_event_flags;
            Matrix4x4 gvrMatrix = GvrMathHelpers.ConvertFloatArrayToMatrix(eventData.pose_transform);

            GvrMathHelpers.GvrMatrixToUnitySpace(
                gvrMatrix, out state.recenteredPosition, out state.recenteredRotation);
        }
Exemple #3
0
 //更新纹理
 private void UpdateVideoRenderTexture()
 {
     if (VideoPlayerPtr != IntPtr.Zero && IsVideoReady)
     {
         IssuePlayerEvent(VideoPlayerEventBase, RenderCommand.UpdateVideo);        //更新视频帧
         GetVideoMatrix(VideoPlayerPtr, VideoMatrixRaw, 16);                       //获取旋转矩阵
         VideoMatrix = GvrMathHelpers.ConvertFloatArrayToMatrix(VideoMatrixRaw);   //矩阵转换
         VideoScreen.sharedMaterial.SetMatrix(VideoMatrixPropertyId, VideoMatrix); //设置矩阵
     }
 }
Exemple #4
0
 /// <summary>
 /// Update the video render texture.
 /// </summary>
 private void UpdateVideoRenderTexture()
 {
     if (VideoPlayerPtr != IntPtr.Zero && IsVideoReady)
     {
         // Update the video frame.
         IssuePlayerEvent(VideoPlayerEventBase, RenderCommand.UpdateVideo);
         GetVideoMatrix(VideoPlayerPtr, VideoMatrixRaw, 16);
         // Rotation the matrix .
         VideoMatrix = GvrMathHelpers.ConvertFloatArrayToMatrix(VideoMatrixRaw);
         // Set the matrix data for palyer.
         VideoScreen.sharedMaterial.SetMatrix(VideoMatrixPropertyId, VideoMatrix);
     }
 }
    /// <summary>Updates the endpoint of the laser based on max distance.</summary>
    protected virtual void UpdateLaserEndPoint()
    {
        if (Laser == null)
        {
            return;
        }

        Vector3 laserStartPoint = Vector3.zero;
        Vector3 laserEndPoint;

        if (controller != null)
        {
            Vector3 worldPosition   = transform.position;
            Vector3 rotatedPosition = controller.InverseTransformPoint(worldPosition);
            rotatedPosition = currentLocalRotation * rotatedPosition;
            laserStartPoint = controller.TransformPoint(rotatedPosition);
            laserStartPoint = transform.InverseTransformPoint(laserStartPoint);
        }

        laserEndPoint = Vector3.ClampMagnitude(currentLocalPosition, maxLaserDistance);

        if (shrinkLaser)
        {
            // Calculate the angle of rotation in degrees.
            float angle = Vector3.Angle(Vector3.forward, currentLocalPosition);

            // Calculate the shrink ratio based on the angle.
            float shrinkAngleDelta = endShrinkAngleDegrees - beginShrinkAngleDegrees;
            float clampedAngle     =
                Mathf.Clamp(angle - beginShrinkAngleDegrees, 0.0f, shrinkAngleDelta);
            shrinkRatio = clampedAngle / shrinkAngleDelta;

            // Calculate the shrink coeff.
            float shrinkCoeff = GvrMathHelpers.EaseOutCubic(shrunkScale, 1.0f, 1.0f - shrinkRatio);

            // Calculate the final distance of the laser.
            Vector3 diff = laserStartPoint - currentLocalPosition;
            Vector3 dir  = diff.normalized;
            float   dist = Mathf.Min(diff.magnitude, maxLaserDistance) * shrinkCoeff;

            // Update the laser start and end points.
            laserEndPoint   = currentLocalPosition;
            laserStartPoint = laserEndPoint + (dir * dist);
        }

        Laser.useWorldSpace = false;
        Laser.SetPosition(0, laserStartPoint);
        Laser.SetPosition(1, laserEndPoint);
    }
        public bool TryGetRecenterTransform(ref Vector3 position, ref Quaternion rotation)
        {
#if UNITY_ANDROID && UNITY_EDITOR
            if (InstantPreview.Instance != null)
            {
                if (InstantPreview.Instance.recenterTransform.isValid)
                {
                    GvrMathHelpers.GvrMatrixToUnitySpace(
                        InstantPreview.Instance.recenterTransform.value, out position, out rotation);
                }

                return(InstantPreview.Instance.recenterTransform.isValid);
            }
#endif // UNITY_ANDROID && UNITY_EDITOR
            position = DEFAULT_RECENTER_TRANSFORM_POSITION;
            rotation = DEFAULT_RECENTER_TRANSFORM_ROTATION;
            return(true);
        }
    /// <summary>Updates the alpha of the laser beam.</summary>
    protected virtual void UpdateLaserAlpha()
    {
        float alpha = ArmModel != null ? ArmModel.PreferredAlpha : 1.0f;

        Color finalStartColor = Color.Lerp(Color.clear, laserColor, alpha);
        Color finalEndColor   = laserColorEnd;

        // If shrinking the laser, the colors are inversed based on the shrink ratio.
        // This is to ensure that the feathering of the laser goes in the right direction.
        if (shrinkLaser)
        {
            float colorRatio = GvrMathHelpers.EaseOutCubic(0.0f, 1.0f, shrinkRatio);
            finalEndColor   = Color.Lerp(finalEndColor, finalStartColor, colorRatio);
            finalStartColor = Color.Lerp(finalStartColor, laserColorEnd, colorRatio);
        }

        Laser.startColor = finalStartColor;
        Laser.endColor   = finalEndColor;
    }
Exemple #8
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.");
        }
    }
        public void PollEventState(ref HeadsetState state)
        {
#if UNITY_ANDROID && UNITY_EDITOR
            if (InstantPreview.Instance != null)
            {
                if (InstantPreview.Instance.events.Count > 0)
                {
                    InstantPreview.UnityGvrEvent eventState = InstantPreview.Instance.events.Dequeue();
                    switch (eventState.type)
                    {
                    case InstantPreview.GvrEventType.GVR_EVENT_NONE:
                        state.eventType = GvrEventType.Invalid;
                        break;

                    case InstantPreview.GvrEventType.GVR_EVENT_RECENTER:
                        state.eventType = GvrEventType.Recenter;
                        break;

                    case InstantPreview.GvrEventType.GVR_EVENT_SAFETY_REGION_EXIT:
                        state.eventType = GvrEventType.SafetyRegionExit;
                        break;

                    case InstantPreview.GvrEventType.GVR_EVENT_SAFETY_REGION_ENTER:
                        state.eventType = GvrEventType.SafetyRegionEnter;
                        break;

                    case InstantPreview.GvrEventType.GVR_EVENT_HEAD_TRACKING_RESUMED:
                        // Currently not supported.
                        state.eventType = GvrEventType.Invalid;
                        break;

                    case InstantPreview.GvrEventType.GVR_EVENT_HEAD_TRACKING_PAUSED:
                        // Currently not supported.
                        state.eventType = GvrEventType.Invalid;
                        break;
                    }

                    state.eventFlags       = (int)eventState.flags;
                    state.eventTimestampNs = eventState.timestamp;

                    // Only add recenter-specific fields if this is a recenter event.
                    if (eventState.type == InstantPreview.GvrEventType.GVR_EVENT_RECENTER)
                    {
                        switch (eventState.gvr_recenter_event_data.recenter_type)
                        {
                        case InstantPreview.GvrRecenterEventType.GVR_RECENTER_EVENT_NONE:
                            state.recenterEventType = GvrRecenterEventType.Invalid;
                            break;

                        case InstantPreview.GvrRecenterEventType.GVR_RECENTER_EVENT_RESTART:
                            state.recenterEventType = GvrRecenterEventType.RecenterEventRestart;
                            break;

                        case InstantPreview.GvrRecenterEventType.GVR_RECENTER_EVENT_ALIGNED:
                            state.recenterEventType = GvrRecenterEventType.RecenterEventAligned;
                            break;

                        case InstantPreview.GvrRecenterEventType.GVR_RECENTER_EVENT_DON:
                            // Currently not supported.
                            state.recenterEventType = GvrRecenterEventType.Invalid;
                            break;
                        }

                        state.recenterEventFlags = eventState.gvr_recenter_event_data.recenter_event_flags;
                        GvrMathHelpers.GvrMatrixToUnitySpace(
                            eventState.gvr_recenter_event_data.start_space_from_tracking_space_transform,
                            out state.recenteredPosition,
                            out state.recenteredRotation);
                    }
                }
                else
                {
                    state.eventType = GvrEventType.Invalid;
                }
            }

            return;
#endif // UNITY_ANDROID && UNITY_EDITOR
            // Events are unavailable through emulation.
        }