ZEDMirror.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using UnityEngine.XR;
  4. #if ZED_HDRP || ZED_URP
  5. using UnityEngine.Rendering;
  6. #endif
  7. /// <summary>
  8. /// In AR mode, displays a full-screen, non-timewarped view of the scene for the editor's Game window.
  9. /// Replaces Unity's default behavior of replicating the left eye view directly,
  10. /// which would otherwise have black borders and move around when the headset moves because of
  11. /// latency compensation.
  12. /// ZEDManager creates a hidden camera with this script attached when in AR mode (see ZEDManager.CreateMirror()).
  13. /// </summary>
  14. public class ZEDMirror : MonoBehaviour
  15. {
  16. /// <summary>
  17. /// The scene's ZEDManager component, for getting the texture overlay.
  18. /// </summary>
  19. public ZEDManager manager;
  20. /// <summary>
  21. /// Reference to the ZEDRenderingPlane that renders the left eye, so we can get its target RenderTexture.
  22. /// </summary>
  23. private ZEDRenderingPlane textureOverlayLeft;
  24. //private RenderTexture bufferTexture;
  25. void Start()
  26. {
  27. XRSettings.showDeviceView = false; //Turn off default behavior.
  28. #if ZED_HDRP || ZED_URP
  29. RenderPipelineManager.endFrameRendering += OnFrameEnd;
  30. #endif
  31. }
  32. private void Update()
  33. {
  34. if (textureOverlayLeft == null && manager != null)
  35. {
  36. textureOverlayLeft = manager.GetLeftCameraTransform().GetComponent<ZEDRenderingPlane>();
  37. }
  38. }
  39. #if !ZED_HDRP && !ZED_URP
  40. private void OnRenderImage(RenderTexture source, RenderTexture destination)
  41. {
  42. if (textureOverlayLeft != null)
  43. {
  44. //Ignore source. Copy ZEDRenderingPlane's texture as the final image.
  45. Graphics.Blit(textureOverlayLeft.target, destination);
  46. }
  47. }
  48. #else
  49. /// <summary>
  50. /// Blits the intermediary targetTexture to the final outputTexture for rendering. Used in SRP because there is no OnRenderImage automatic function.
  51. /// </summary>
  52. private void OnFrameEnd(ScriptableRenderContext context, Camera[] cams)
  53. {
  54. if (textureOverlayLeft != null)
  55. {
  56. Graphics.Blit(textureOverlayLeft.target, (RenderTexture)null);
  57. }
  58. }
  59. #endif
  60. private void OnDestroy()
  61. {
  62. #if ZED_URP || ZED_HDRP
  63. RenderPipelineManager.endFrameRendering -= OnFrameEnd;
  64. #endif
  65. }
  66. }