HideFromWrongCameras.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if ZED_HDRP || ZED_URP
  5. using UnityEngine.Rendering;
  6. #endif
  7. /// <summary>
  8. /// Stops an object from being drawn for cameras that shouldn't draw it, without the use of layers.
  9. /// Used so that the left ZED eye doesn't see the right eye's canvas object, and vice versa, and for
  10. /// cameras seeing frames from other rigs.
  11. /// </summary><remarks>
  12. /// These are attached to canvas object in ZED_Rig_Mono and ZED_Rig_Stereo prefabs, and
  13. /// assigned to Quad objects in the (normally hidden) AR rig created by ZEDManager when in stereo AR mode.
  14. /// </remarks
  15. public class HideFromWrongCameras : MonoBehaviour
  16. {
  17. /// <summary>
  18. /// List of all cameras from the ZED plugin that should see exactly one frame object.
  19. /// <para>Effectively unused if showInNonZEDCameras is set to false.</para>
  20. /// </summary>
  21. private static List<Camera> zedCamList = new List<Camera>();
  22. /// <summary>
  23. /// If true, the object will be visible to all cameras not in zedCamList, namely cameras
  24. /// that aren't part of the ZED plugin.
  25. /// </summary>
  26. public bool showInNonZEDCameras = true;
  27. /// <summary>
  28. /// The camera that is allowed to render this object.
  29. /// </summary>
  30. private Camera renderableCamera = null;
  31. /// <summary>
  32. /// MeshFilter assigned to this object. Only needed in SRP so we can call the appropriate draw manually.
  33. /// </summary>
  34. private MeshFilter mfilter;
  35. /// <summary>
  36. /// Renderer attached to this object.
  37. /// </summary>
  38. private Renderer rend;
  39. /// <summary>
  40. /// Enabled state of the attached Renderer prior to Unity's rendering stage.
  41. /// <para>Used so that manually disabling the object's MeshRenderer won't be undone by this script.</para>
  42. /// </summary>
  43. private bool lastRenderState = true;
  44. public void Awake()
  45. {
  46. rend = GetComponent<Renderer>();
  47. #if !ZED_HDRP && !ZED_URP
  48. Camera.onPreRender += PreRender;
  49. Camera.onPostRender += PostRender;
  50. #else
  51. mfilter = GetComponent<MeshFilter>();
  52. RenderPipelineManager.beginFrameRendering += SRPStartFrame;
  53. #endif
  54. }
  55. /// <summary>
  56. /// Adds a camera to an internal static list of cameras that should see exactly one ZED frame/canvas object.
  57. /// <para>Also called automatically if you call SetRenderCamera as a backup.</para>
  58. /// </summary>
  59. /// <param name="cam"></param>
  60. public static void RegisterZEDCam(Camera cam)
  61. {
  62. if (!zedCamList.Contains(cam))
  63. zedCamList.Add(cam);
  64. }
  65. /// <summary>
  66. /// Assigns a camera to this object, making it the only camera of the registered cameras that can see it.
  67. /// </summary>
  68. /// <param name="cam"></param>
  69. public void SetRenderCamera(Camera cam)
  70. {
  71. RegisterZEDCam(cam); //In case it's not registered yet, make sure it is.
  72. renderableCamera = cam;
  73. }
  74. #if ZED_HDRP || ZED_URP
  75. private void SRPStartFrame(ScriptableRenderContext context, Camera[] cams)
  76. {
  77. rend.enabled = false;
  78. foreach(Camera rendcam in cams)
  79. {
  80. if (rendcam == renderableCamera || //Draw if it's the camera that this frame is designated for.
  81. rendcam.name.ToLower().Contains("scenecamera") || //Draw if it's the camera from the Unity Editor Scene window.
  82. (rendcam.name.ToLower().Contains("preview") && rendcam.transform.position == renderableCamera.transform.position && rendcam.transform.rotation == renderableCamera.transform.rotation) || //Editor preview.
  83. (showInNonZEDCameras == true && !zedCamList.Contains(rendcam) && !rendcam.name.ToLower().Contains("preview"))) //All other cameras, other than other ZED cameras and preview cameras, if the user wants.
  84. {
  85. DrawCanvas(rendcam);
  86. }
  87. }
  88. }
  89. private void DrawCanvas(Camera drawcam)
  90. {
  91. Matrix4x4 canvastrs = Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale);
  92. Graphics.DrawMesh(mfilter.mesh, canvastrs, rend.material, gameObject.layer, drawcam);
  93. }
  94. #else
  95. /// <summary>
  96. /// Turns the renderer on or off depending on whether it should be drawn.
  97. /// <para>Subscribed to Camera.onPreRender in Awake(), which is called anytime any camera starts rendering.</para>
  98. /// </summary>
  99. /// <param name="currentcam"></param>
  100. //private void PreRender(Camera currentcam)
  101. private void PreRender(Camera currentcam)
  102. {
  103. lastRenderState = rend.enabled;
  104. if (!rend.enabled) return; //We weren't going to render this object anyway, so skip the rest of the logic.
  105. //rend.enabled = false;
  106. //If it's a Unity scene camera, always show it.
  107. if (currentcam.name.ToLower().Contains("scenecamera")) //There are more robust ways of checking this, but they are expensive.
  108. {
  109. rend.enabled = true;
  110. return;
  111. }
  112. //Is it the renderable camera we assigned to this instance?
  113. if (currentcam == renderableCamera)
  114. {
  115. rend.enabled = true; //Always render that camera, no matter what.
  116. }
  117. else if (zedCamList.Contains(currentcam)) //Is it included in the ZED cameras that should only see one of these objects?
  118. {
  119. rend.enabled = false; //Those cameras can only render one quad, and it's not this one.
  120. }
  121. else if (currentcam.name.ToLower().Contains("preview") &&
  122. currentcam.transform.position == renderableCamera.transform.position && currentcam.transform.rotation == renderableCamera.transform.rotation)
  123. {
  124. //Unity editor Preview camera. Transform check is to assure it's the corresponding preview camera.
  125. rend.enabled = true;
  126. }
  127. else //If it's a camera not on the list, render depending on showInNonZEDCameras.
  128. {
  129. rend.enabled = showInNonZEDCameras;
  130. }
  131. }
  132. /// <summary>
  133. /// Sets the renderer's state to what it was before PreRender() messed with it.
  134. /// <para>Subscribed to Camera.onPostRender in Awake(), which is called anytime any camera finishes rendering.</para>
  135. /// </summary>
  136. private void PostRender(Camera currentcam)
  137. {
  138. rend.enabled = lastRenderState;
  139. }
  140. #endif
  141. private void OnDestroy()
  142. {
  143. #if !ZED_HDRP && !ZED_URP
  144. Camera.onPreRender -= PreRender;
  145. Camera.onPostRender -= PostRender;
  146. #else
  147. RenderPipelineManager.beginFrameRendering -= SRPStartFrame;
  148. #endif
  149. }
  150. }