MenuUtils.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. #if URP_PRESENT
  6. using UnityEngine.Rendering.Universal;
  7. #endif
  8. #if HDRP_PRESENT
  9. using UnityEngine.Rendering.HighDefinition;
  10. #endif
  11. #if ENABLE_VR || ENABLE_AR
  12. using UnityEngine.SpatialTracking;
  13. #if XRI_PRESENT
  14. #else
  15. namespace UnityEditor.XR.LegacyInputHelpers
  16. {
  17. internal static class MenuUtils
  18. {
  19. static readonly string kMainCamera = "MainCamera";
  20. static readonly Vector3 kDefaultCameraPosition = new Vector3(0.0f, 1.0f, -10.0f);
  21. const float kDefaultCameraNearClip = 0.01f;
  22. static bool CreateSimpleXRRig(Camera xrCamera, out GameObject gameObj)
  23. {
  24. var xrRigGO = ObjectFactory.CreateGameObject("XRRig");
  25. var cameraOffsetGO = ObjectFactory.CreateGameObject("Camera Offset");
  26. Undo.SetTransformParent(cameraOffsetGO.transform, xrRigGO.transform, "Parent Camera Offset to XR Rig");
  27. Pose camPose = new Pose();
  28. // we only want to move the rig to the camera position if one is passed in.
  29. bool camExistsAndNeedsMoving = false;
  30. if (xrCamera == null)
  31. {
  32. var xrCameraGO = ObjectFactory.CreateGameObject("Main Camera", typeof(Camera));
  33. xrCamera = xrCameraGO.GetComponent<Camera>();
  34. }
  35. else
  36. {
  37. camPose.position = xrCamera.transform.position;
  38. // if initial camera position, move to the floor
  39. if(camPose.position == kDefaultCameraPosition)
  40. {
  41. camPose.position.y = 0.0f;
  42. }
  43. camPose.rotation = xrCamera.transform.rotation;
  44. camExistsAndNeedsMoving = true;
  45. }
  46. Undo.SetTransformParent(xrCamera.transform, cameraOffsetGO.transform, "Parent Camera to Camera Offset");
  47. // Override the near clip to better handle controllers near the face
  48. xrCamera.nearClipPlane = kDefaultCameraNearClip;
  49. xrCamera.transform.localPosition = Vector3.zero;
  50. xrCamera.transform.localRotation = Quaternion.identity;
  51. xrCamera.tag = kMainCamera;
  52. if (camExistsAndNeedsMoving)
  53. {
  54. xrRigGO.transform.position = camPose.position;
  55. xrRigGO.transform.rotation = camPose.rotation;
  56. }
  57. TrackedPoseDriver trackedPoseDriver = xrCamera.gameObject.GetComponent<TrackedPoseDriver>();
  58. if (trackedPoseDriver == null)
  59. {
  60. trackedPoseDriver = Undo.AddComponent<TrackedPoseDriver>(xrCamera.gameObject);
  61. }
  62. trackedPoseDriver.SetPoseSource(TrackedPoseDriver.DeviceType.GenericXRDevice, TrackedPoseDriver.TrackedPose.Center);
  63. trackedPoseDriver.UseRelativeTransform = false;
  64. var coh = xrRigGO.AddComponent<CameraOffset>();
  65. coh.cameraFloorOffsetObject = cameraOffsetGO;
  66. #if UNITY_2019_3_OR_NEWER
  67. coh.TrackingOriginMode = UnityEngine.XR.TrackingOriginModeFlags.Device;
  68. #else
  69. coh.trackingSpace = UnityEngine.XR.TrackingSpaceType.Stationary;
  70. #endif
  71. gameObj = xrRigGO;
  72. Selection.activeGameObject = xrRigGO;
  73. return true;
  74. }
  75. static Camera VerifyWeCanUpgrade()
  76. {
  77. Debug.Log("Determining if we can automatically upgrade this scene to use an XR Rig");
  78. // rules are
  79. // only upgrade an empty scene with a directional light and a camera at the root node.
  80. var xrCameraList = Object.FindObjectsOfType<Camera>();
  81. Debug.Log("Checking number of cameras in the scene");
  82. if (xrCameraList.Length > 1)
  83. {
  84. // if the camera exists, and isn't at the root node. bail.
  85. Debug.LogError("You have more than one camera in your scene. We are unable to automatically convert your scene. Please see the documentation on how to upgrade your scene.");
  86. return null;
  87. }
  88. else if (xrCameraList.Length == 0)
  89. {
  90. Debug.LogError("You have no cameras in your scene. We are unable to automatically convert your scene. Please see the documentation on how to upgrade your scene.");
  91. return null;
  92. }
  93. var xrCamera = xrCameraList.Length > 0 ? xrCameraList[0] : null;
  94. if (xrCamera != null)
  95. {
  96. Debug.Log("Checking Main Camera is at the root of the hierarchy");
  97. if (!(xrCamera.tag == kMainCamera && xrCamera.transform != null && xrCamera.transform.parent == null))
  98. {
  99. // if the camera exists, and isn't at the root node. bail.
  100. Debug.LogError("Your Main Camera is not at the root of your hierarchy. We are unable to automatically convert your scene. Please see the documentation on how to upgrade your scene.");
  101. return null;
  102. }
  103. Debug.Log("Checking camera components");
  104. List<Component> componentList = new List<Component>(xrCamera.gameObject.GetComponents(typeof(MonoBehaviour)));
  105. if (componentList.Count != 0)
  106. {
  107. #if HDRP_PRESENT_10_0
  108. if (!xrCamera.IsHDCamera())
  109. #endif
  110. #if HDRP_PRESENT_10_1_OR_NEWER
  111. if (!ComponentUtility.IsHDCamera(xrCamera))
  112. #endif
  113. #if URP_PRESENT_10_0
  114. if (!xrCamera.IsUniversalCamera())
  115. #endif
  116. #if URP_PRESENT_10_1_OR_NEWER
  117. if (!ComponentUtility.IsUniversalCamera(xrCamera))
  118. #endif
  119. #if HDRP_PRESENT && !HDRP_PRESENT_10_1_OR_NEWER && !HDRP_PRESENT_10_0
  120. if (!(componentList.Count <= 2 && componentList.Find(x => x.GetType() == typeof(HDAdditionalCameraData))))
  121. #endif
  122. #if URP_PRESENT && !URP_PRESENT_10_1_OR_NEWER && !URP_PRESENT_10_0
  123. if (!(componentList.Count <= 2
  124. && componentList.Find(x => x.GetType() == typeof(UniversalAdditionalCameraData))))
  125. #endif
  126. {
  127. Debug.LogError("Your Main Camera has additional components that we do not recognize. We are unable to automatically convert your scene. Please see the documentation on how to upgrade your scene.");
  128. return null;
  129. }
  130. }
  131. return xrCamera;
  132. }
  133. return null;
  134. }
  135. [MenuItem("GameObject/XR/Convert Main Camera To XR Rig", false, 10)]
  136. static void UpgradeToXRRig()
  137. {
  138. var xrCamera = VerifyWeCanUpgrade();
  139. if (xrCamera != null)
  140. {
  141. GameObject vrCameraRig;
  142. CreateSimpleXRRig(xrCamera, out vrCameraRig);
  143. }
  144. }
  145. }
  146. }
  147. #endif
  148. #endif