XRGraphics.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using UnityEditor;
  3. #if ENABLE_VR && ENABLE_VR_MODULE
  4. using UnityEngine.XR;
  5. #endif
  6. namespace UnityEngine.Rendering
  7. {
  8. /// <summary>
  9. /// XRGraphics insulates SRP from API changes across platforms, Editor versions, and as XR transitions into XR SDK
  10. /// </summary>
  11. [Serializable]
  12. public class XRGraphics
  13. {
  14. /// <summary>
  15. /// Stereo Rendering Modes.
  16. /// </summary>
  17. public enum StereoRenderingMode
  18. {
  19. /// <summary>Multi Pass.</summary>
  20. MultiPass = 0,
  21. /// <summary>Single Pass.</summary>
  22. SinglePass,
  23. /// <summary>Single Pass Instanced.</summary>
  24. SinglePassInstanced,
  25. /// <summary>Single Pass Multi View.</summary>
  26. SinglePassMultiView
  27. };
  28. /// <summary>
  29. /// Eye texture resolution scale.
  30. /// </summary>
  31. public static float eyeTextureResolutionScale
  32. {
  33. get
  34. {
  35. #if ENABLE_VR && ENABLE_VR_MODULE
  36. if (enabled)
  37. return XRSettings.eyeTextureResolutionScale;
  38. #endif
  39. return 1.0f;
  40. }
  41. set
  42. {
  43. #if ENABLE_VR && ENABLE_VR_MODULE
  44. XRSettings.eyeTextureResolutionScale = value;
  45. #endif
  46. }
  47. }
  48. /// <summary>
  49. /// Render viewport scale.
  50. /// </summary>
  51. public static float renderViewportScale
  52. {
  53. get
  54. {
  55. #if ENABLE_VR && ENABLE_VR_MODULE
  56. if (enabled)
  57. return XRSettings.renderViewportScale;
  58. #endif
  59. return 1.0f;
  60. }
  61. }
  62. /// <summary>
  63. /// Try enable.
  64. /// </summary>
  65. #if UNITY_EDITOR
  66. // TryEnable gets updated before "play" is pressed- we use this for updating GUI only.
  67. public static bool tryEnable
  68. {
  69. get
  70. {
  71. #if UNITY_2020_1_OR_NEWER
  72. return false;
  73. #else
  74. return UnityEditorInternal.VR.VREditor.GetVREnabledOnTargetGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget));
  75. #endif
  76. }
  77. }
  78. #endif
  79. /// <summary>
  80. /// SRP should use this to safely determine whether XR is enabled at runtime.
  81. /// </summary>
  82. public static bool enabled
  83. {
  84. get
  85. {
  86. #if ENABLE_VR && ENABLE_VR_MODULE
  87. return XRSettings.enabled;
  88. #else
  89. return false;
  90. #endif
  91. }
  92. }
  93. /// <summary>
  94. /// Returns true if the XR device is active.
  95. /// </summary>
  96. public static bool isDeviceActive
  97. {
  98. get
  99. {
  100. #if ENABLE_VR && ENABLE_VR_MODULE
  101. if (enabled)
  102. return XRSettings.isDeviceActive;
  103. #endif
  104. return false;
  105. }
  106. }
  107. /// <summary>
  108. /// Name of the loaded XR device.
  109. /// </summary>
  110. public static string loadedDeviceName
  111. {
  112. get
  113. {
  114. #if ENABLE_VR && ENABLE_VR_MODULE
  115. if (enabled)
  116. return XRSettings.loadedDeviceName;
  117. #endif
  118. return "No XR device loaded";
  119. }
  120. }
  121. /// <summary>
  122. /// List of supported XR devices.
  123. /// </summary>
  124. public static string[] supportedDevices
  125. {
  126. get
  127. {
  128. #if ENABLE_VR && ENABLE_VR_MODULE
  129. if (enabled)
  130. return XRSettings.supportedDevices;
  131. #endif
  132. return new string[1];
  133. }
  134. }
  135. /// <summary>
  136. /// Stereo rendering mode.
  137. /// </summary>
  138. public static StereoRenderingMode stereoRenderingMode
  139. {
  140. get
  141. {
  142. #if ENABLE_VR && ENABLE_VR_MODULE
  143. if (enabled)
  144. return (StereoRenderingMode)XRSettings.stereoRenderingMode;
  145. #endif
  146. return StereoRenderingMode.SinglePass;
  147. }
  148. }
  149. /// <summary>
  150. /// Eye texture descriptor.
  151. /// </summary>
  152. public static RenderTextureDescriptor eyeTextureDesc
  153. {
  154. get
  155. {
  156. #if ENABLE_VR && ENABLE_VR_MODULE
  157. if (enabled)
  158. return XRSettings.eyeTextureDesc;
  159. #endif
  160. return new RenderTextureDescriptor(0, 0);
  161. }
  162. }
  163. /// <summary>
  164. /// Eye texture width.
  165. /// </summary>
  166. public static int eyeTextureWidth
  167. {
  168. get
  169. {
  170. #if ENABLE_VR && ENABLE_VR_MODULE
  171. if (enabled)
  172. return XRSettings.eyeTextureWidth;
  173. #endif
  174. return 0;
  175. }
  176. }
  177. /// <summary>
  178. /// Eye texture height.
  179. /// </summary>
  180. public static int eyeTextureHeight
  181. {
  182. get
  183. {
  184. #if ENABLE_VR && ENABLE_VR_MODULE
  185. if (enabled)
  186. return XRSettings.eyeTextureHeight;
  187. #endif
  188. return 0;
  189. }
  190. }
  191. }
  192. }