SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
8.6 KiB

4 years ago
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. #if MODULE_URP_ENABLED
  4. using UnityEngine.Rendering.Universal;
  5. #elif MODULE_LWRP_ENABLED
  6. using UnityEngine.Rendering.LWRP;
  7. #else
  8. using ScriptableRendererFeature = UnityEngine.ScriptableObject;
  9. #endif
  10. namespace UnityEngine.XR.ARFoundation
  11. {
  12. /// <summary>
  13. /// A render feature for rendering the camera background for AR devies.
  14. /// </summary>
  15. public class ARBackgroundRendererFeature : ScriptableRendererFeature
  16. {
  17. #if MODULE_URP_ENABLED || MODULE_LWRP_ENABLED
  18. /// <summary>
  19. /// The scriptable render pass to be added to the renderer when the camera background is to be rendered.
  20. /// </summary>
  21. CustomRenderPass m_ScriptablePass;
  22. /// <summary>
  23. /// The mesh for rendering the background shader.
  24. /// </summary>
  25. Mesh m_BackgroundMesh;
  26. /// <summary>
  27. /// Create the scriptable render pass.
  28. /// </summary>
  29. public override void Create()
  30. {
  31. #if !UNITY_EDITOR
  32. m_ScriptablePass = new CustomRenderPass(RenderPassEvent.BeforeRenderingOpaques);
  33. m_BackgroundMesh = new Mesh();
  34. m_BackgroundMesh.vertices = new Vector3[]
  35. {
  36. new Vector3(0f, 0f, 0.1f),
  37. new Vector3(0f, 1f, 0.1f),
  38. new Vector3(1f, 1f, 0.1f),
  39. new Vector3(1f, 0f, 0.1f),
  40. };
  41. m_BackgroundMesh.uv = new Vector2[]
  42. {
  43. new Vector2(0f, 0f),
  44. new Vector2(0f, 1f),
  45. new Vector2(1f, 1f),
  46. new Vector2(1f, 0f),
  47. };
  48. m_BackgroundMesh.triangles = new int[] {0, 1, 2, 0, 2, 3};
  49. #endif // !UNITY_EDITOR
  50. }
  51. /// <summary>
  52. /// Add the background rendering pass when rendering a game camera with an enabled AR camera background component.
  53. /// </summary>
  54. /// <param name="renderer">The sriptable renderer in which to enqueue the render pass.</param>
  55. /// <param name="renderingData">Additional rendering data about the current state of rendering.</param>
  56. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  57. {
  58. #if !UNITY_EDITOR
  59. Camera currentCamera = renderingData.cameraData.camera;
  60. if ((currentCamera != null) && (currentCamera.cameraType == CameraType.Game))
  61. {
  62. ARCameraBackground cameraBackground = currentCamera.gameObject.GetComponent<ARCameraBackground>();
  63. if ((cameraBackground != null) && cameraBackground.backgroundRenderingEnabled
  64. && (cameraBackground.material != null))
  65. {
  66. bool invertCulling = cameraBackground.GetComponent<ARCameraManager>()?.subsystem?.invertCulling ?? false;
  67. m_ScriptablePass.Setup(m_BackgroundMesh, cameraBackground.material, invertCulling,
  68. renderer.cameraColorTarget, renderer.cameraDepth);
  69. renderer.EnqueuePass(m_ScriptablePass);
  70. }
  71. }
  72. #endif // !UNITY_EDITOR
  73. }
  74. /// <summary>
  75. /// The custom render pass to render the camera background.
  76. /// </summary>
  77. class CustomRenderPass : ScriptableRenderPass
  78. {
  79. /// <summary>
  80. /// The name for the custom render pass which will be display in graphics debugging tools.
  81. /// </summary>
  82. const string k_CustomRenderPassName = "AR Background Pass (URP)";
  83. /// <summary>
  84. /// The orthogonal projection matrix for the background rendering.
  85. /// </summary>
  86. static readonly Matrix4x4 k_BackgroundOrthoProjection = Matrix4x4.Ortho(0f, 1f, 0f, 1f, -0.1f, 9.9f);
  87. /// <summary>
  88. /// The mesh for rendering the background material.
  89. /// </summary>
  90. Mesh m_BackgroundMesh;
  91. /// <summary>
  92. /// The material used for rendering the device background using the camera video texture and potentially
  93. /// other device-specific properties and textures.
  94. /// </summary>
  95. Material m_BackgroundMaterial;
  96. /// <summary>
  97. /// The destination render target identifier for rendering the background color.
  98. /// </summary>
  99. RenderTargetIdentifier m_ColorTargetIdentifier;
  100. /// <summary>
  101. /// The destination render target identifier for rendering the background depth.
  102. /// </summary>
  103. RenderTargetIdentifier m_DepthTargetIdentifier;
  104. /// <summary>
  105. /// Whether the culling mode should be inverted.
  106. /// ([CommandBuffer.SetInvertCulling](https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetInvertCulling.html)).
  107. /// </summary>
  108. bool m_InvertCulling;
  109. /// <summary>
  110. /// Constructs the background render pass.
  111. /// </summary>
  112. /// <param name="renderPassEvent">The render pass event when this pass should be rendered.</param>
  113. public CustomRenderPass(RenderPassEvent renderPassEvent)
  114. {
  115. this.renderPassEvent = renderPassEvent;
  116. }
  117. /// <summary>
  118. /// Setup the background render pass.
  119. /// </summary>
  120. /// <param name="backgroundMesh">The mesh used for rendering the device background.</param>
  121. /// <param name="backgroundMaterial">The material used for rendering the device background.</param>
  122. /// <param name="invertCulling">Whether the culling mode should be inverted.</param>
  123. /// <param name="colorTargetIdentifier">The color target to which to render the background texture.</param>
  124. /// <param name="depthTargetIdentifier">The depth target to which to render the background texture.</param>
  125. public void Setup(Mesh backgroundMesh, Material backgroundMaterial, bool invertCulling,
  126. RenderTargetIdentifier colorTargetIdentifier,
  127. RenderTargetIdentifier depthTargetIdentifier)
  128. {
  129. m_BackgroundMesh = backgroundMesh;
  130. m_BackgroundMaterial = backgroundMaterial;
  131. m_InvertCulling = invertCulling;
  132. m_ColorTargetIdentifier = colorTargetIdentifier;
  133. m_DepthTargetIdentifier = depthTargetIdentifier;
  134. }
  135. /// <summary>
  136. /// Configure the render pass by configuring the render target and clear values.
  137. /// </summary>
  138. /// <param name="commandBuffer">The command buffer for configuration.</param>
  139. /// <param name="renderTextureDescriptor">The descriptor of the target render texture.</param>
  140. public override void Configure(CommandBuffer commandBuffer, RenderTextureDescriptor renderTextureDescriptor)
  141. {
  142. ConfigureTarget(m_ColorTargetIdentifier, m_DepthTargetIdentifier);
  143. ConfigureClear(ClearFlag.Depth, Color.clear);
  144. }
  145. /// <summary>
  146. /// Execute the commands to render the camera background.
  147. /// </summary>
  148. /// <param name="context">The render context for executing the render commands.</param>
  149. /// <param name="renderingData">Additional rendering data about the current state of rendering.</param>
  150. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  151. {
  152. var cmd = CommandBufferPool.Get(k_CustomRenderPassName);
  153. cmd.BeginSample(k_CustomRenderPassName);
  154. ARCameraBackground.AddOpenGLES3ResetStateCommand(cmd);
  155. cmd.SetInvertCulling(m_InvertCulling);
  156. cmd.SetViewProjectionMatrices(Matrix4x4.identity, k_BackgroundOrthoProjection);
  157. cmd.DrawMesh(m_BackgroundMesh, Matrix4x4.identity, m_BackgroundMaterial);
  158. cmd.SetViewProjectionMatrices(renderingData.cameraData.camera.worldToCameraMatrix,
  159. renderingData.cameraData.camera.projectionMatrix);
  160. cmd.EndSample(k_CustomRenderPassName);
  161. context.ExecuteCommandBuffer(cmd);
  162. CommandBufferPool.Release(cmd);
  163. }
  164. /// <summary>
  165. /// Clean up any resources for the render pass.
  166. /// </summary>
  167. /// <param name="commandBuffer">The command buffer for frame cleanup.</param>
  168. public override void FrameCleanup(CommandBuffer commandBuffer)
  169. {
  170. }
  171. }
  172. #endif // MODULE_URP_ENABLED || MODULE_LWRP_ENABLED
  173. }
  174. }