SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
5.5 KiB

4 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if URP_PRESENT
  5. using UnityEngine.Rendering.Universal;
  6. #endif
  7. #if HDRP_PRESENT
  8. using UnityEngine.Rendering.HighDefinition;
  9. #endif
  10. #if ENABLE_VR || ENABLE_AR
  11. using UnityEngine.SpatialTracking;
  12. #if XRI_PRESENT
  13. #else
  14. namespace UnityEditor.XR.LegacyInputHelpers
  15. {
  16. internal static class MenuUtils
  17. {
  18. static readonly string kMainCamera = "MainCamera";
  19. static readonly Vector3 kDefaultCameraPosition = new Vector3(0.0f, 1.0f, -10.0f);
  20. static bool CreateSimpleXRRig(Camera xrCamera, out GameObject gameObj)
  21. {
  22. var xrRigGO = ObjectFactory.CreateGameObject("XRRig");
  23. var cameraOffsetGO = ObjectFactory.CreateGameObject("Camera Offset");
  24. Undo.SetTransformParent(cameraOffsetGO.transform, xrRigGO.transform, "Parent Camera Offset to XR Rig");
  25. Pose camPose = new Pose();
  26. // we only want to move the rig to the camera position if one is passed in.
  27. bool camExistsAndNeedsMoving = false;
  28. if (xrCamera == null)
  29. {
  30. var xrCameraGO = ObjectFactory.CreateGameObject("Main Camera", typeof(Camera));
  31. xrCamera = xrCameraGO.GetComponent<Camera>();
  32. }
  33. else
  34. {
  35. camPose.position = xrCamera.transform.position;
  36. // if initial camera position, move to the floor
  37. if(camPose.position == kDefaultCameraPosition)
  38. {
  39. camPose.position.y = 0.0f;
  40. }
  41. camPose.rotation = xrCamera.transform.rotation;
  42. camExistsAndNeedsMoving = true;
  43. }
  44. Undo.SetTransformParent(xrCamera.transform, cameraOffsetGO.transform, "Parent Camera to Camera Offset");
  45. xrCamera.transform.localPosition = Vector3.zero;
  46. xrCamera.transform.localRotation = Quaternion.identity;
  47. xrCamera.tag = kMainCamera;
  48. if (camExistsAndNeedsMoving)
  49. {
  50. xrRigGO.transform.position = camPose.position;
  51. xrRigGO.transform.rotation = camPose.rotation;
  52. }
  53. TrackedPoseDriver trackedPoseDriver = xrCamera.gameObject.GetComponent<TrackedPoseDriver>();
  54. if (trackedPoseDriver == null)
  55. {
  56. trackedPoseDriver = Undo.AddComponent<TrackedPoseDriver>(xrCamera.gameObject);
  57. }
  58. trackedPoseDriver.SetPoseSource(TrackedPoseDriver.DeviceType.GenericXRDevice, TrackedPoseDriver.TrackedPose.Center);
  59. trackedPoseDriver.UseRelativeTransform = false;
  60. var coh = xrRigGO.AddComponent<CameraOffset>();
  61. coh.cameraFloorOffsetObject = cameraOffsetGO;
  62. #if UNITY_2019_3_OR_NEWER
  63. coh.TrackingOriginMode = UnityEngine.XR.TrackingOriginModeFlags.Device;
  64. #else
  65. coh.trackingSpace = UnityEngine.XR.TrackingSpaceType.Stationary;
  66. #endif
  67. gameObj = xrRigGO;
  68. return true;
  69. }
  70. [MenuItem("GameObject/XR/Convert Main Camera To XR Rig", false, 10)]
  71. static void UpgradeToXRRig()
  72. {
  73. Debug.Log("Determining if we can automatically upgrade this scene to use an XR Rig");
  74. // rules are
  75. // only upgrade an empty scene with a directional light and a camera at the root node.
  76. var xrCameraList = Object.FindObjectsOfType<Camera>();
  77. Debug.Log("Checking number of cameras in the scene");
  78. if (xrCameraList.Length != 1)
  79. {
  80. // if the camera exists, and isn't at the root node. bail.
  81. 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.");
  82. return;
  83. }
  84. var xrCamera = xrCameraList.Length > 0 ? xrCameraList[0] : null;
  85. if (xrCamera != null)
  86. {
  87. Debug.Log("Checking Main Camera is at the root of the hierarchy");
  88. if (!(xrCamera.tag == kMainCamera && xrCamera.transform != null && xrCamera.transform.parent == null))
  89. {
  90. // if the camera exists, and isn't at the root node. bail.
  91. 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.");
  92. return;
  93. }
  94. Debug.Log("Checking camera components");
  95. var componentList = xrCamera.gameObject.GetComponents(typeof(MonoBehaviour));
  96. if(componentList.Length != 0)
  97. {
  98. #if HDRP_PRESENT
  99. if (!(componentList.Length == 1 && componentList[0].GetType() == typeof(HDAdditionalCameraData)))
  100. #endif
  101. #if URP_PRESENT
  102. if (!(componentList.Length == 1 && componentList[0].GetType() == typeof(UniversalAdditionalCameraData)))
  103. #endif
  104. {
  105. 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.");
  106. return;
  107. }
  108. }
  109. }
  110. GameObject vrCameraRig;
  111. CreateSimpleXRRig(xrCamera, out vrCameraRig);
  112. }
  113. }
  114. }
  115. #endif
  116. #endif