Advertisement
Guest User

Untitled

a guest
Mar 30th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.28 KB | None | 0 0
  1. //-----------------------------------------------------------------------
  2. // <copyright file="EditorHeadsetProvider.cs" company="Google Inc.">
  3. // Copyright 2017 Google Inc. All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. //     http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. // </copyright>
  17. //-----------------------------------------------------------------------
  18.  
  19. /// @cond
  20. namespace Gvr.Internal
  21. {
  22.     using Gvr;
  23.     using System.Collections.Generic;
  24. #if UNITY_EDITOR
  25.     using UnityEditor;
  26. #endif // UNITY_EDITOR
  27.     using UnityEngine;
  28.  
  29.     public class EditorHeadsetProvider : IHeadsetProvider
  30.     {
  31.         public const float DEFAULT_FLOOR_HEIGHT_3DOF = -1.6f;
  32.         public static readonly Vector3 DEFAULT_RECENTER_TRANSFORM_POSITION = Vector3.zero;
  33.         public static readonly Quaternion DEFAULT_RECENTER_TRANSFORM_ROTATION
  34.                 = Quaternion.identity;
  35.         public const GvrSafetyRegionType DEFAULT_SAFETY_REGION_TYPE_3DOF
  36.                 = GvrSafetyRegionType.Cylinder;
  37.         public const float DEFAULT_SAFETY_CYLINDER_ENTER_RADIUS_3DOF = 0.6f;
  38.         public const float DEFAULT_SAFETY_CYLINDER_EXIT_RADIUS_3DOF = 0.7f;
  39.  
  40.         private HeadsetState dummyState;
  41. #if UNITY_EDITOR
  42.         private HashSet<string> printedErrorMessages = new HashSet<string>();
  43. #endif // UNITY_EDITOR
  44.  
  45.         public bool SupportsPositionalTracking
  46.         {
  47.             get
  48.             {
  49. #if UNITY_EDITOR
  50.                 UnityEditor.XR.Daydream.SupportedHeadTracking minTrackingState
  51.                         = UnityEditor.PlayerSettings.VRDaydream.minimumSupportedHeadTracking;
  52.                 UnityEditor.XR.Daydream.SupportedHeadTracking maxTrackingState
  53.                         = UnityEditor.PlayerSettings.VRDaydream.maximumSupportedHeadTracking;
  54.  
  55.                 if (minTrackingState == UnityEditor.XR.Daydream.SupportedHeadTracking.ThreeDoF
  56.                     && maxTrackingState == UnityEditor.XR.Daydream.SupportedHeadTracking.ThreeDoF)
  57.                 {
  58.                     return false;
  59.                 }
  60.                 else if (
  61.                     minTrackingState == UnityEditor.XR.Daydream.SupportedHeadTracking.ThreeDoF
  62.                     && maxTrackingState == UnityEditor.XR.Daydream.SupportedHeadTracking.SixDoF)
  63.                 {
  64.                     if (InstantPreview.IsActive
  65.                         && InstantPreview.Instance.supportsPositionalHeadTracking.isValid)
  66.                     {
  67.                         return InstantPreview.Instance.supportsPositionalHeadTracking.value;
  68.                     }
  69.                     else
  70.                     {
  71.                         return GvrHeadset.editorSupportsPositionalHeadTracking;
  72.                     }
  73.                 }
  74.                 else // Positional head tracking required.
  75.                 {
  76.                     if (InstantPreview.IsActive
  77.                         && InstantPreview.Instance.supportsPositionalHeadTracking.isValid
  78.                         && !InstantPreview.Instance.supportsPositionalHeadTracking.value)
  79.                     {
  80.                         string err_msg = "XRSettings > Daydream > Positional Head Tracking is "
  81.                                 + "'required', but the connected device only supports "
  82.                                 + "orientation.";
  83.                         if (!printedErrorMessages.Contains(err_msg))
  84.                         {
  85.                             Debug.LogError(err_msg);
  86.                             printedErrorMessages.Add(err_msg);
  87.                         }
  88.                     }
  89.                     return true;
  90.                 }
  91. #else // UNITY_EDITOR
  92.                 return false;
  93. #endif // UNITY_EDITOR
  94.             }
  95.         }
  96.  
  97.         public void PollEventState(ref HeadsetState state)
  98.         {
  99. #if UNITY_ANDROID && UNITY_EDITOR
  100.             if (InstantPreview.IsActive)
  101.             {
  102.                 if (InstantPreview.Instance.events.Count > 0)
  103.                 {
  104.                     InstantPreview.UnityGvrEvent eventState
  105.                         = InstantPreview.Instance.events.Dequeue();
  106.                     switch (eventState.type)
  107.                     {
  108.                         case InstantPreview.GvrEventType.GVR_EVENT_NONE:
  109.                             state.eventType = GvrEventType.Invalid;
  110.                             break;
  111.                         case InstantPreview.GvrEventType.GVR_EVENT_RECENTER:
  112.                             state.eventType = GvrEventType.Recenter;
  113.                             break;
  114.                         case InstantPreview.GvrEventType.GVR_EVENT_SAFETY_REGION_EXIT:
  115.                             state.eventType = GvrEventType.SafetyRegionExit;
  116.                             break;
  117.                         case InstantPreview.GvrEventType.GVR_EVENT_SAFETY_REGION_ENTER:
  118.                             state.eventType = GvrEventType.SafetyRegionEnter;
  119.                             break;
  120.                         case InstantPreview.GvrEventType.GVR_EVENT_HEAD_TRACKING_RESUMED:
  121.                             // Currently not supported.
  122.                             state.eventType = GvrEventType.Invalid;
  123.                             break;
  124.                         case InstantPreview.GvrEventType.GVR_EVENT_HEAD_TRACKING_PAUSED:
  125.                             // Currently not supported.
  126.                             state.eventType = GvrEventType.Invalid;
  127.                             break;
  128.                     }
  129.  
  130.                     state.eventFlags = (int)eventState.flags;
  131.                     state.eventTimestampNs = eventState.timestamp;
  132.  
  133.                     // Only add recenter-specific fields if this is a recenter event.
  134.                     if (eventState.type == InstantPreview.GvrEventType.GVR_EVENT_RECENTER)
  135.                     {
  136.                         switch (eventState.gvr_recenter_event_data.recenter_type)
  137.                         {
  138.                             case InstantPreview.GvrRecenterEventType.GVR_RECENTER_EVENT_NONE:
  139.                                 state.recenterEventType = GvrRecenterEventType.Invalid;
  140.                                 break;
  141.                             case InstantPreview.GvrRecenterEventType.GVR_RECENTER_EVENT_RESTART:
  142.                                 state.recenterEventType = GvrRecenterEventType.RecenterEventRestart;
  143.                                 break;
  144.                             case InstantPreview.GvrRecenterEventType.GVR_RECENTER_EVENT_ALIGNED:
  145.                                 state.recenterEventType = GvrRecenterEventType.RecenterEventAligned;
  146.                                 break;
  147.                             case InstantPreview.GvrRecenterEventType.GVR_RECENTER_EVENT_DON:
  148.                                 // Currently not supported.
  149.                                 state.recenterEventType = GvrRecenterEventType.Invalid;
  150.                                 break;
  151.                         }
  152.  
  153.                         state.recenterEventFlags
  154.                             = eventState.gvr_recenter_event_data.recenter_event_flags;
  155.                         GvrMathHelpers.GvrMatrixToUnitySpace(
  156.                             eventState.gvr_recenter_event_data
  157.                                 .start_space_from_tracking_space_transform,
  158.                             out state.recenteredPosition,
  159.                             out state.recenteredRotation);
  160.                     }
  161.                 }
  162.                 else
  163.                 {
  164.                     state.eventType = GvrEventType.Invalid;
  165.                 }
  166.             }
  167.  
  168.             return;
  169. #endif // UNITY_ANDROID && UNITY_EDITOR
  170.             // Events are unavailable through emulation.
  171.         }
  172.  
  173.         public bool TryGetFloorHeight(ref float floorHeight)
  174.         {
  175. #if UNITY_ANDROID && UNITY_EDITOR
  176.             if (InstantPreview.IsActive)
  177.             {
  178.                 if (InstantPreview.Instance.floorHeight.isValid)
  179.                 {
  180.                     floorHeight = InstantPreview.Instance.floorHeight.value;
  181.                 }
  182.  
  183.                 return InstantPreview.Instance.floorHeight.isValid;
  184.             }
  185. #endif // UNITY_ANDROID && UNITY_EDITOR
  186.             floorHeight = DEFAULT_FLOOR_HEIGHT_3DOF;
  187.             return true;
  188.         }
  189.  
  190.         public bool TryGetRecenterTransform(ref Vector3 position, ref Quaternion rotation)
  191.         {
  192. #if UNITY_ANDROID && UNITY_EDITOR
  193.             if (InstantPreview.IsActive)
  194.             {
  195.                 if (InstantPreview.Instance.recenterTransform.isValid)
  196.                 {
  197.                     GvrMathHelpers.GvrMatrixToUnitySpace(
  198.                         InstantPreview.Instance.recenterTransform.value,
  199.                         out position,
  200.                         out rotation);
  201.                 }
  202.  
  203.                 return InstantPreview.Instance.recenterTransform.isValid;
  204.             }
  205. #endif // UNITY_ANDROID && UNITY_EDITOR
  206.             position = DEFAULT_RECENTER_TRANSFORM_POSITION;
  207.             rotation = DEFAULT_RECENTER_TRANSFORM_ROTATION;
  208.             return true;
  209.         }
  210.  
  211.         public bool TryGetSafetyRegionType(ref GvrSafetyRegionType safetyType)
  212.         {
  213. #if UNITY_ANDROID && UNITY_EDITOR
  214.             if (InstantPreview.IsActive)
  215.             {
  216.                 if (InstantPreview.Instance.safetyRegionType.isValid)
  217.                 {
  218.                     safetyType
  219.                         = (GvrSafetyRegionType)InstantPreview.Instance.safetyRegionType.value;
  220.                 }
  221.  
  222.                 return InstantPreview.Instance.safetyRegionType.isValid;
  223.             }
  224. #endif // UNITY_ANDROID && UNITY_EDITOR
  225.             safetyType = DEFAULT_SAFETY_REGION_TYPE_3DOF;
  226.             return true;
  227.         }
  228.  
  229.         public bool TryGetSafetyCylinderInnerRadius(ref float innerRadius)
  230.         {
  231. #if UNITY_ANDROID && UNITY_EDITOR
  232.             if (InstantPreview.IsActive)
  233.             {
  234.                 if (InstantPreview.Instance.safetyCylinderEnterRadius.isValid)
  235.                 {
  236.                     innerRadius = InstantPreview.Instance.safetyCylinderEnterRadius.value;
  237.                 }
  238.  
  239.                 return InstantPreview.Instance.safetyCylinderEnterRadius.isValid;
  240.             }
  241. #endif // UNITY_ANDROID && UNITY_EDITOR
  242.             innerRadius = DEFAULT_SAFETY_CYLINDER_ENTER_RADIUS_3DOF;
  243.             return true;
  244.         }
  245.  
  246.         public bool TryGetSafetyCylinderOuterRadius(ref float outerRadius)
  247.         {
  248. #if UNITY_ANDROID && UNITY_EDITOR
  249.             if (InstantPreview.IsActive)
  250.             {
  251.                 if (InstantPreview.Instance.safetyCylinderExitRadius.isValid)
  252.                 {
  253.                     outerRadius = InstantPreview.Instance.safetyCylinderExitRadius.value;
  254.                 }
  255.  
  256.                 return InstantPreview.Instance.safetyCylinderExitRadius.isValid;
  257.             }
  258. #endif // UNITY_ANDROID && UNITY_EDITOR
  259.             outerRadius = DEFAULT_SAFETY_CYLINDER_EXIT_RADIUS_3DOF;
  260.             return true;
  261.         }
  262.     }
  263. }
  264.  
  265. /// @endcond
  266.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement