RenderTextureBlitter.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEngine;
  2. #if URS_USE_HDRP_RUNTIME
  3. #if UNITY_2019_1 || UNITY_2019_2 //HDRP 5.x, 6.x
  4. using UnityEngine.Experimental.Rendering.HDPipeline;
  5. #else //HDRP 7.x and above
  6. using UnityEngine.Rendering.HighDefinition;
  7. #endif
  8. #endif
  9. namespace Unity.RenderStreaming
  10. {
  11. [RequireComponent(typeof(Camera))]
  12. #if URS_USE_HDRP_RUNTIME
  13. [RequireComponent(typeof(HDAdditionalCameraData))]
  14. #endif
  15. internal class RenderTextureBlitter : MonoBehaviour
  16. {
  17. [SerializeField] Camera m_rtCamera = null;
  18. Camera m_cam;
  19. #if URS_USE_HDRP_RUNTIME
  20. HDAdditionalCameraData m_hdData;
  21. #endif
  22. private void OnEnable()
  23. {
  24. m_cam = GetComponent<Camera>();
  25. //Render nothing
  26. m_cam.clearFlags = CameraClearFlags.Nothing;
  27. m_cam.cullingMask = 0;
  28. #if URS_USE_HDRP_RUNTIME
  29. m_hdData = GetComponent<HDAdditionalCameraData>();
  30. m_hdData.fullscreenPassthrough = true;
  31. m_hdData.customRender += BlitRenderStreamingRT;
  32. #elif URS_USE_URP_RUNTIME
  33. UnityEngine.Rendering.RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
  34. #else
  35. Camera.onPreRender += BlitTexture;
  36. #endif
  37. }
  38. private void OnDisable()
  39. {
  40. #if URS_USE_HDRP_RUNTIME
  41. m_hdData.customRender -= BlitRenderStreamingRT;
  42. #elif URS_USE_URP_RUNTIME
  43. UnityEngine.Rendering.RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
  44. #else
  45. Camera.onPreRender -= BlitTexture;
  46. #endif
  47. }
  48. #if URS_USE_HDRP_RUNTIME
  49. public void BlitRenderStreamingRT(UnityEngine.Rendering.ScriptableRenderContext context, HDCamera cam)
  50. {
  51. Graphics.Blit(m_rtCamera.targetTexture, (RenderTexture)null);
  52. }
  53. #elif URS_USE_URP_RUNTIME
  54. void OnEndCameraRendering(UnityEngine.Rendering.ScriptableRenderContext context, Camera cam)
  55. {
  56. if (cam == m_cam && null != m_rtCamera.targetTexture)
  57. {
  58. //This seems to work only if we have setup PostProcessing Stack V2
  59. Graphics.Blit(m_rtCamera.targetTexture, (RenderTexture)null);
  60. }
  61. }
  62. #else
  63. private void BlitTexture(Camera cam)
  64. {
  65. if (m_cam == cam && null != m_rtCamera.targetTexture)
  66. {
  67. Graphics.Blit(m_rtCamera.targetTexture, (RenderTexture)null);
  68. }
  69. }
  70. #endif
  71. }
  72. }