ZEDMeshRenderer.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. /// <summary>
  4. /// Renders the mesh generated by ZEDSpatialMappingManager in a second, hidden camera created at runtume.
  5. /// This gets the wireframe style with no performance loss.
  6. /// This script is very similar to how ZEDPlaneRenderer works for Plane Detection.
  7. /// </summary>
  8. public class ZEDMeshRenderer : MonoBehaviour
  9. {
  10. private ZEDManager zedManager = null;
  11. /// <summary>
  12. /// Reference to the hidden camera we create at runtime.
  13. /// </summary>
  14. private Camera cam;
  15. /// <summary>
  16. /// Target texture of the rendering done by the new camera.
  17. /// </summary>
  18. private RenderTexture meshTex;
  19. /// <summary>
  20. /// Checks if the spatial mapping has started.
  21. /// </summary>
  22. private bool hasStarted = false;
  23. /// <summary>
  24. /// Checks if the mesh requested is textured. If so, deativate the wireframe.
  25. /// </summary>
  26. public bool isTextured = false;
  27. /// <summary>
  28. /// Shader used to render the wireframe. Normally Mat_ZED_Wireframe_Video_Overlay.
  29. /// </summary>
  30. private Shader shaderWireframe;
  31. /// <summary>
  32. /// Reference to the ZEDRenderingPlane component of the camera we copy.
  33. /// </summary>
  34. private ZEDRenderingPlane renderingPlane;
  35. /// <summary>
  36. /// Create the Mesh Rendering pipe
  37. /// </summary>
  38. public void Create()
  39. {
  40. Transform ObjParent = gameObject.transform;
  41. int tries = 0;
  42. while (zedManager == null && tries<5) {
  43. if (ObjParent!=null)
  44. zedManager= ObjParent.GetComponent<ZEDManager> ();
  45. if (zedManager == null && ObjParent!=null)
  46. ObjParent = ObjParent.parent;
  47. tries++;
  48. }
  49. if (zedManager == null) {
  50. return;
  51. }
  52. //Create the new GameObject and camera as a child of the corresponding ZED rig camera.
  53. GameObject go = new GameObject("MeshCamera");
  54. go.transform.parent = transform;
  55. go.transform.localPosition = Vector3.zero;
  56. go.transform.localRotation = Quaternion.identity;
  57. go.transform.localScale = Vector3.one;
  58. cam = go.AddComponent<Camera>();
  59. //go.hideFlags = HideFlags.HideInHierarchy;//This hides the new camera from scene view. Comment this out to see it in the hierarchy.
  60. //Set the target texture to a new RenderTexture that will be passed to ZEDRenderingPlane for blending.
  61. if (zedManager.zedCamera.IsCameraReady)
  62. {
  63. meshTex = new RenderTexture(zedManager.zedCamera.ImageWidth, zedManager.zedCamera.ImageHeight, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
  64. meshTex.Create();
  65. }
  66. //Set the camera's parameters.
  67. cam.enabled = false;
  68. cam.cullingMask = (1 << zedManager.zedCamera.TagInvisibleToZED); //Layer set aside for planes and spatial mapping meshes.
  69. cam.targetTexture = meshTex;
  70. cam.nearClipPlane = 0.1f;
  71. cam.farClipPlane = 500.0f;
  72. cam.fieldOfView = zedManager.zedCamera.GetFOV() * Mathf.Rad2Deg;
  73. cam.projectionMatrix = zedManager.zedCamera.Projection;
  74. cam.backgroundColor = new Color(0, 0, 0, 0);
  75. cam.clearFlags = CameraClearFlags.Color;
  76. cam.renderingPath = RenderingPath.VertexLit;
  77. cam.depth = 0;
  78. cam.depthTextureMode = DepthTextureMode.None;
  79. #if UNITY_5_6_OR_NEWER
  80. cam.allowMSAA = false;
  81. cam.allowHDR = false;
  82. #endif
  83. cam.useOcclusionCulling = false;
  84. shaderWireframe = (Resources.Load("Materials/SpatialMapping/Mat_ZED_Wireframe_Video_Overlay") as Material).shader;
  85. //Set the ZEDRenderingPlane blend texture to the one the new camera renders to.
  86. renderingPlane = GetComponent<ZEDRenderingPlane>();
  87. renderingPlane.SetTextureOverlayMapping(meshTex);
  88. renderingPlane.SetMeshRenderAvailable(false);
  89. hasStarted = true;
  90. }
  91. /// <summary>
  92. /// Set the rendering available. Used when loading mesh
  93. /// </summary>
  94. /// <param name="drawMesh"></param>
  95. public void UpdateRenderingPlane(bool drawMesh)
  96. {
  97. renderingPlane.SetMeshRenderAvailable(drawMesh);
  98. }
  99. /// <summary>
  100. /// Unsubscribes from relevant events.
  101. /// </summary>
  102. private void OnDisable()
  103. {
  104. hasStarted = false;
  105. }
  106. /// <summary>
  107. /// Renders the plane each frame, before cameras normally update, so the RenderTexture is ready to be blended.
  108. /// </summary>
  109. void OnPreRender()
  110. {
  111. if (zedManager.IsStereoRig)
  112. {
  113. if (zedManager != null)
  114. {
  115. if (zedManager.IsSpatialMappingDisplay && hasStarted)
  116. {
  117. cam.enabled = true;
  118. if (!isTextured)
  119. {
  120. GL.wireframe = true;
  121. cam.RenderWithShader(shaderWireframe, "RenderType");
  122. GL.wireframe = false;
  123. }
  124. else
  125. {
  126. cam.Render();
  127. }
  128. cam.enabled = false;
  129. if (zedManager.SpatialMappingHasChunks)
  130. renderingPlane.SetMeshRenderAvailable(true);
  131. }
  132. }
  133. }
  134. }
  135. private void Update()
  136. {
  137. if (!zedManager.IsStereoRig)
  138. {
  139. if (zedManager != null)
  140. {
  141. if (zedManager.IsSpatialMappingDisplay && hasStarted)
  142. {
  143. cam.enabled = true;
  144. if (!isTextured)
  145. {
  146. GL.wireframe = true;
  147. cam.RenderWithShader(shaderWireframe, "RenderType");
  148. GL.wireframe = false;
  149. }
  150. else
  151. {
  152. cam.Render();
  153. }
  154. cam.enabled = false;
  155. if (zedManager.SpatialMappingHasChunks)
  156. renderingPlane.SetMeshRenderAvailable(true);
  157. }
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// Releases the target RenderTexture when the application quits.
  163. /// </summary>
  164. private void OnApplicationQuit()
  165. {
  166. if (meshTex != null && meshTex.IsCreated())
  167. {
  168. meshTex.Release();
  169. }
  170. }
  171. }