Assignment for RMIT Mixed Reality in 2020
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.

145 lines
5.8 KiB

  1. // Desktop Camera|Prefabs|0040
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. /// <summary>
  9. /// Allows rendering a separate camera that is shown on the desktop only, without changing what's seen in VR headsets.
  10. /// </summary>
  11. /// <remarks>
  12. /// **Prefab Usage:**
  13. /// * Place the `VRTK/Prefabs/DesktopCamera/DesktopCamera` prefab in the scene.
  14. /// </remarks>
  15. [AddComponentMenu("VRTK/Scripts/Prefabs/VRTK_DesktopCamera")]
  16. public class VRTK_DesktopCamera : MonoBehaviour
  17. {
  18. [Header("Desktop Camera")]
  19. [Tooltip("The camera to use for the desktop view. If left blank the camera on the game object this script is attached to or any of its children will be used.")]
  20. public Camera desktopCamera;
  21. [Tooltip("The follow script to use for following the headset. If left blank the follow script on the game object this script is attached to or any of its children will be used.")]
  22. public VRTK_ObjectFollow followScript;
  23. [Header("Headset Image")]
  24. [Tooltip("The optional image to render the headset's view into. Can be left blank.")]
  25. public RawImage headsetImage;
  26. [Tooltip("The optional render texture to render the headset's view into. Can be left blank. If this is blank and `headsetImage` is set a default render texture will be created.")]
  27. public RenderTexture headsetRenderTexture;
  28. protected Camera headsetCameraCopy;
  29. protected VRTK_TransformFollow headsetCameraTransformFollow;
  30. protected virtual void OnEnable()
  31. {
  32. desktopCamera = desktopCamera == null ? GetComponentInChildren<Camera>() : desktopCamera;
  33. followScript = followScript == null ? GetComponentInChildren<VRTK_ObjectFollow>() : followScript;
  34. if (desktopCamera == null)
  35. {
  36. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_NOT_INJECTED, "VRTK_DesktopCamera", "Camera", "desktopCamera", "the same", " or any child of it"));
  37. return;
  38. }
  39. if (followScript == null)
  40. {
  41. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_NOT_INJECTED, "VRTK_DesktopCamera", "VRTK_ObjectFollow", "followScript", "the same", " or any child of it"));
  42. return;
  43. }
  44. headsetCameraTransformFollow = gameObject.AddComponent<VRTK_TransformFollow>();
  45. headsetCameraTransformFollow.moment = VRTK_TransformFollow.FollowMoment.OnLateUpdate;
  46. if (VRTK_SDKManager.SubscribeLoadedSetupChanged(LoadedSetupChanged) && VRTK_SDKManager.GetLoadedSDKSetup() != null)
  47. {
  48. ConfigureForCurrentSDKSetup();
  49. }
  50. }
  51. protected virtual void OnDisable()
  52. {
  53. VRTK_SDKManager.UnsubscribeLoadedSetupChanged(LoadedSetupChanged);
  54. Destroy(headsetCameraTransformFollow);
  55. if (headsetCameraCopy != null)
  56. {
  57. Destroy(headsetCameraCopy.gameObject);
  58. }
  59. }
  60. protected virtual void LoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManager.LoadedSetupChangeEventArgs e)
  61. {
  62. ConfigureForCurrentSDKSetup();
  63. }
  64. protected virtual void ConfigureForCurrentSDKSetup()
  65. {
  66. if (headsetCameraCopy != null)
  67. {
  68. Destroy(headsetCameraCopy.gameObject);
  69. }
  70. headsetCameraTransformFollow.enabled = false;
  71. followScript.enabled = false;
  72. if (VRTK_SDKManager.GetLoadedSDKSetup() == null)
  73. {
  74. return;
  75. }
  76. Camera headsetCamera = VRTK_DeviceFinder.HeadsetCamera().GetComponent<Camera>();
  77. desktopCamera.depth = headsetCamera.depth + 1;
  78. desktopCamera.stereoTargetEye = StereoTargetEyeMask.None;
  79. followScript.gameObjectToFollow = headsetCamera.gameObject;
  80. followScript.gameObjectToChange = desktopCamera.gameObject;
  81. followScript.Follow();
  82. followScript.enabled = true;
  83. if (headsetImage == null)
  84. {
  85. return;
  86. }
  87. if (headsetRenderTexture == null)
  88. {
  89. headsetRenderTexture = new RenderTexture(
  90. (int)headsetImage.rectTransform.rect.width,
  91. (int)headsetImage.rectTransform.rect.height,
  92. 24,
  93. RenderTextureFormat.ARGB32)
  94. {
  95. name = VRTK_SharedMethods.GenerateVRTKObjectName(true, "Headset RenderTexture")
  96. };
  97. }
  98. headsetCameraCopy = Instantiate(headsetCamera, transform);
  99. headsetCameraCopy.name = VRTK_SharedMethods.GenerateVRTKObjectName(true, "Headset Camera Copy");
  100. headsetCameraCopy.targetTexture = headsetRenderTexture;
  101. foreach (Transform child in headsetCameraCopy.transform)
  102. {
  103. Destroy(child.gameObject);
  104. }
  105. IEnumerable<Component> componentsToDestroy = headsetCameraCopy
  106. .GetComponents<Component>()
  107. .Where(component => component != headsetCameraCopy && !(component is Transform));
  108. foreach (Component component in componentsToDestroy)
  109. {
  110. Destroy(component);
  111. }
  112. headsetCameraTransformFollow.gameObjectToFollow = headsetCamera.gameObject;
  113. headsetCameraTransformFollow.gameObjectToChange = headsetCameraCopy.gameObject;
  114. headsetCameraTransformFollow.Follow();
  115. headsetCameraTransformFollow.enabled = true;
  116. headsetImage.texture = headsetRenderTexture;
  117. headsetImage.SetNativeSize();
  118. }
  119. }
  120. }