//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
using UnityEngine;
using UnityEngine.XR;
#if ZED_HDRP || ZED_URP
using UnityEngine.Rendering;
#endif
///
/// In AR mode, displays a full-screen, non-timewarped view of the scene for the editor's Game window.
/// Replaces Unity's default behavior of replicating the left eye view directly,
/// which would otherwise have black borders and move around when the headset moves because of
/// latency compensation.
/// ZEDManager creates a hidden camera with this script attached when in AR mode (see ZEDManager.CreateMirror()).
///
public class ZEDMirror : MonoBehaviour
{
///
/// The scene's ZEDManager component, for getting the texture overlay.
///
public ZEDManager manager;
///
/// Reference to the ZEDRenderingPlane that renders the left eye, so we can get its target RenderTexture.
///
private ZEDRenderingPlane textureOverlayLeft;
//private RenderTexture bufferTexture;
void Start()
{
XRSettings.showDeviceView = false; //Turn off default behavior.
#if ZED_HDRP || ZED_URP
RenderPipelineManager.endFrameRendering += OnFrameEnd;
#endif
}
private void Update()
{
if (textureOverlayLeft == null && manager != null)
{
textureOverlayLeft = manager.GetLeftCameraTransform().GetComponent();
}
}
#if !ZED_HDRP && !ZED_URP
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (textureOverlayLeft != null)
{
//Ignore source. Copy ZEDRenderingPlane's texture as the final image.
Graphics.Blit(textureOverlayLeft.target, destination);
}
}
#else
///
/// Blits the intermediary targetTexture to the final outputTexture for rendering. Used in SRP because there is no OnRenderImage automatic function.
///
private void OnFrameEnd(ScriptableRenderContext context, Camera[] cams)
{
if (textureOverlayLeft != null)
{
Graphics.Blit(textureOverlayLeft.target, (RenderTexture)null);
}
}
#endif
private void OnDestroy()
{
#if ZED_URP || ZED_HDRP
RenderPipelineManager.endFrameRendering -= OnFrameEnd;
#endif
}
}