NRBackGroundRender.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using UnityEngine;
  12. [RequireComponent(typeof(Camera))]
  13. public class NRBackGroundRender : MonoBehaviour
  14. {
  15. /// <summary> A material used to render the AR background image. </summary>
  16. [Tooltip("A material used to render the AR background image.")]
  17. [SerializeField] Material m_Material;
  18. private Camera m_Camera;
  19. private MeshRenderer m_Renderer;
  20. private MeshFilter m_MeshFilter;
  21. private Mesh m_PlaneMesh;
  22. private Vector3[] m_Corners;
  23. private int[] Triangles = new int[6] {
  24. 0,1,2,0,2,3
  25. };
  26. private Vector2[] UV = new Vector2[4] {
  27. new Vector2(0,0),
  28. new Vector2(0,1),
  29. new Vector2(1,1),
  30. new Vector2(1,0),
  31. };
  32. private void OnEnable()
  33. {
  34. m_Camera = GetComponent<Camera>();
  35. EnableARBackgroundRendering();
  36. }
  37. private void OnDisable()
  38. {
  39. DisableARBackgroundRendering();
  40. }
  41. private void UpdateBackGroundMesh()
  42. {
  43. if (m_Corners == null)
  44. {
  45. m_Corners = new Vector3[4];
  46. }
  47. m_Camera.CalculateFrustumCorners(new Rect(0, 0, 1, 1), m_Camera.farClipPlane - 100, Camera.MonoOrStereoscopicEye.Mono, m_Corners);
  48. for (int i = 0; i < m_Corners.Length; i++)
  49. {
  50. m_Corners[i] = m_Camera.transform.TransformPoint(m_Corners[i]);
  51. }
  52. Vector3 center = (m_Corners[0] + m_Corners[2]) * 0.5f;
  53. DrawBackGroundMesh(new Pose(center, m_Camera.transform.rotation), m_Corners);
  54. }
  55. public void EnableARBackgroundRendering(bool updatemesh = true)
  56. {
  57. if (updatemesh)
  58. {
  59. UpdateBackGroundMesh();
  60. }
  61. m_Renderer.gameObject.SetActive(true);
  62. }
  63. public void DisableARBackgroundRendering()
  64. {
  65. if (m_Renderer != null)
  66. {
  67. m_Renderer.gameObject.SetActive(false);
  68. }
  69. }
  70. public void SetMaterial(Material mat)
  71. {
  72. m_Material = mat;
  73. if (m_Renderer != null)
  74. {
  75. m_Renderer.material = m_Material;
  76. }
  77. }
  78. /// <summary> Draw from center. </summary>
  79. /// <param name="centerPose"> The center pose.</param>
  80. /// <param name="vectors"> The vectors.</param>
  81. private void DrawBackGroundMesh(Pose centerPose, Vector3[] vectors)
  82. {
  83. if (vectors == null || vectors.Length < 3)
  84. {
  85. return;
  86. }
  87. if (m_PlaneMesh == null)
  88. {
  89. m_PlaneMesh = new Mesh();
  90. }
  91. if (m_Renderer == null)
  92. {
  93. var go = new GameObject("background");
  94. go.transform.SetParent(transform);
  95. m_Renderer = go.AddComponent<MeshRenderer>();
  96. m_MeshFilter = go.AddComponent<MeshFilter>();
  97. }
  98. m_Renderer.transform.position = centerPose.position;
  99. m_Renderer.transform.rotation = centerPose.rotation;
  100. Vector3[] vertices3D = new Vector3[vectors.Length];
  101. for (int i = 0; i < vectors.Length; i++)
  102. {
  103. vertices3D[i] = m_Renderer.transform.InverseTransformPoint(vectors[i]);
  104. }
  105. m_PlaneMesh.vertices = vertices3D;
  106. m_PlaneMesh.triangles = Triangles;
  107. m_PlaneMesh.uv = UV;
  108. m_MeshFilter.mesh = m_PlaneMesh;
  109. m_Renderer.material = m_Material;
  110. }
  111. }
  112. //[RequireComponent(typeof(Camera))]
  113. //public class NRBackGroundRender : MonoBehaviour
  114. //{
  115. // /// <summary> A material used to render the AR background image. </summary>
  116. // [Tooltip("A material used to render the AR background image.")]
  117. // public Material BackgroundMaterial;
  118. // private Camera m_Camera;
  119. // private CameraClearFlags m_CameraClearFlags = CameraClearFlags.Skybox;
  120. // private CommandBuffer m_CommandBuffer = null;
  121. // private void OnEnable()
  122. // {
  123. // if (BackgroundMaterial == null)
  124. // {
  125. // NRDebugger.Error("[NRBackGroundRender] Material is null...");
  126. // return;
  127. // }
  128. // m_Camera = GetComponent<Camera>();
  129. // EnableARBackgroundRendering();
  130. // }
  131. // private void OnDisable()
  132. // {
  133. // DisableARBackgroundRendering();
  134. // }
  135. // public void SetMaterial(Material mat)
  136. // {
  137. // BackgroundMaterial = mat;
  138. // }
  139. // private void EnableARBackgroundRendering()
  140. // {
  141. // if (BackgroundMaterial == null || m_Camera == null)
  142. // {
  143. // return;
  144. // }
  145. // m_CameraClearFlags = m_Camera.clearFlags;
  146. // m_Camera.clearFlags = CameraClearFlags.Depth;
  147. // m_CommandBuffer = new CommandBuffer();
  148. // m_CommandBuffer.Blit(null, BuiltinRenderTextureType.CameraTarget, BackgroundMaterial);
  149. // m_Camera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, m_CommandBuffer);
  150. // m_Camera.AddCommandBuffer(CameraEvent.BeforeGBuffer, m_CommandBuffer);
  151. // }
  152. // private void DisableARBackgroundRendering()
  153. // {
  154. // if (m_CommandBuffer == null || m_Camera == null)
  155. // {
  156. // return;
  157. // }
  158. // m_Camera.clearFlags = m_CameraClearFlags;
  159. // m_Camera.RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, m_CommandBuffer);
  160. // m_Camera.RemoveCommandBuffer(CameraEvent.BeforeGBuffer, m_CommandBuffer);
  161. // }
  162. //}
  163. }