暫無描述
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.

XRLayout.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace UnityEngine.Experimental.Rendering
  4. {
  5. /// <summary>
  6. /// Used by render pipelines to store information about the XR device layout.
  7. /// </summary>
  8. public class XRLayout
  9. {
  10. readonly List<(Camera, XRPass)> m_ActivePasses = new List<(Camera, XRPass)>();
  11. /// <summary>
  12. /// Configure the layout to render from the specified camera by generating passes from the the connected XR device.
  13. /// </summary>
  14. /// <param name="camera"> Camera that has XR device connected to. </param>
  15. /// <param name="enableXR"> Determines XR capability of the generated layout. Can be used to force generate non-XR layout. </param>
  16. public void AddCamera(Camera camera, bool enableXR)
  17. {
  18. if (camera == null)
  19. return;
  20. // Enable XR layout only for game camera
  21. bool isGameCamera = (camera.cameraType == CameraType.Game || camera.cameraType == CameraType.VR);
  22. bool xrSupported = isGameCamera && camera.targetTexture == null && enableXR;
  23. if (XRSystem.displayActive && xrSupported)
  24. {
  25. XRSystem.SetDisplayZRange(camera.nearClipPlane, camera.farClipPlane);
  26. XRSystem.CreateDefaultLayout(camera, this);
  27. }
  28. else
  29. {
  30. AddPass(camera, XRSystem.emptyPass);
  31. }
  32. }
  33. /// <summary>
  34. /// Used by render pipelines to reconfigure a pass from a camera.
  35. /// </summary>
  36. /// <param name="xrPass"> XRPass that needs to be reconfigured. </param>
  37. /// <param name="camera"> The camera that XRPass configures against. </param>
  38. public void ReconfigurePass(XRPass xrPass, Camera camera)
  39. {
  40. if (xrPass.enabled)
  41. {
  42. XRSystem.ReconfigurePass(xrPass, camera);
  43. xrPass.UpdateCombinedOcclusionMesh();
  44. }
  45. }
  46. /// <summary>
  47. /// Used by render pipelines to access all registered passes on this layout.
  48. /// </summary>
  49. /// <returns> A list of registered camera/XRPass tuples. </returns>
  50. public List<(Camera, XRPass)> GetActivePasses()
  51. {
  52. return m_ActivePasses;
  53. }
  54. internal void AddPass(Camera camera, XRPass xrPass)
  55. {
  56. xrPass.UpdateCombinedOcclusionMesh();
  57. m_ActivePasses.Add((camera, xrPass));
  58. }
  59. internal void Clear()
  60. {
  61. for (int i = 0; i < m_ActivePasses.Count; i++)
  62. {
  63. // Pop from the back to keep initial ordering (see implementation of ObjectPool)
  64. (Camera _, XRPass xrPass) = m_ActivePasses[m_ActivePasses.Count - i - 1];
  65. if (xrPass != XRSystem.emptyPass)
  66. xrPass.Release();
  67. }
  68. m_ActivePasses.Clear();
  69. }
  70. internal void LogDebugInfo()
  71. {
  72. var sb = new StringBuilder();
  73. sb.AppendFormat("XRSystem setup for frame {0}, active: {1}", Time.frameCount, XRSystem.displayActive);
  74. sb.AppendLine();
  75. for (int passIndex = 0; passIndex < m_ActivePasses.Count; passIndex++)
  76. {
  77. var pass = m_ActivePasses[passIndex].Item2;
  78. for (int viewIndex = 0; viewIndex < pass.viewCount; viewIndex++)
  79. {
  80. var viewport = pass.GetViewport(viewIndex);
  81. sb.AppendFormat("XR Pass {0} Cull {1} View {2} Slice {3} : {4} x {5}",
  82. pass.multipassId,
  83. pass.cullingPassId,
  84. viewIndex,
  85. pass.GetTextureArraySlice(viewIndex),
  86. viewport.width,
  87. viewport.height);
  88. sb.AppendLine();
  89. }
  90. }
  91. Debug.Log(sb);
  92. }
  93. }
  94. }