暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BlitToRTHandlePass.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.RenderGraphModule;
  4. using UnityEngine.Rendering.Universal;
  5. // This pass creates an RTHandle and blits the camera color to it.
  6. // The RTHandle is then set as a global texture, which is available to shaders in the scene.
  7. public class BlitToRTHandlePass : ScriptableRenderPass
  8. {
  9. class PassData
  10. {
  11. public TextureHandle source;
  12. public Material material;
  13. public Vector4 scaleBias;
  14. }
  15. private Vector4 m_ScaleBias = new Vector4(1f, 1f, 0f, 0f);
  16. private ProfilingSampler m_ProfilingSampler = new ProfilingSampler("BlitToRTHandle_CopyColor");
  17. private RTHandle m_InputHandle;
  18. private RTHandle m_OutputHandle;
  19. private const string k_OutputName = "_CopyColorTexture";
  20. private int m_OutputId = Shader.PropertyToID(k_OutputName);
  21. private Material m_Material;
  22. public BlitToRTHandlePass(RenderPassEvent evt, Material mat)
  23. {
  24. renderPassEvent = evt;
  25. m_Material = mat;
  26. }
  27. #pragma warning disable 618, 672 // Type or member is obsolete, Member overrides obsolete member
  28. // Unity calls the Configure method in the Compatibility mode (non-RenderGraph path)
  29. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  30. {
  31. // Configure the custom RTHandle
  32. var desc = cameraTextureDescriptor;
  33. desc.depthBufferBits = 0;
  34. desc.msaaSamples = 1;
  35. RenderingUtils.ReAllocateIfNeeded(ref m_OutputHandle, desc, FilterMode.Bilinear, TextureWrapMode.Clamp, name: k_OutputName );
  36. // Set the RTHandle as the output target in the Compatibility mode
  37. ConfigureTarget(m_OutputHandle);
  38. }
  39. // Unity calls the Execute method in the Compatibility mode
  40. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  41. {
  42. // Set camera color as the input
  43. m_InputHandle = renderingData.cameraData.renderer.cameraColorTargetHandle;
  44. CommandBuffer cmd = CommandBufferPool.Get();
  45. using (new ProfilingScope(cmd, m_ProfilingSampler))
  46. {
  47. // Blit the input RTHandle to the output one
  48. Blitter.BlitCameraTexture(cmd, m_InputHandle, m_OutputHandle, m_Material, 0);
  49. // Make the output texture available for the shaders in the scene
  50. cmd.SetGlobalTexture(m_OutputId, m_OutputHandle.nameID);
  51. }
  52. context.ExecuteCommandBuffer(cmd);
  53. cmd.Clear();
  54. CommandBufferPool.Release(cmd);
  55. }
  56. #pragma warning restore 618, 672
  57. // Unity calls the RecordRenderGraph method to add and configure one or more render passes in the render graph system.
  58. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
  59. {
  60. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  61. UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
  62. if (cameraData.camera.cameraType != CameraType.Game)
  63. return;
  64. // Create the custom RTHandle
  65. var desc = cameraData.cameraTargetDescriptor;
  66. desc.depthBufferBits = 0;
  67. desc.msaaSamples = 1;
  68. RenderingUtils.ReAllocateHandleIfNeeded(ref m_OutputHandle, desc, FilterMode.Bilinear, TextureWrapMode.Clamp, name: k_OutputName );
  69. // Set camera color as a texture resource for this render graph instance
  70. TextureHandle source = resourceData.activeColorTexture;
  71. // Set RTHandle as a texture resource for this render graph instance
  72. TextureHandle destination = renderGraph.ImportTexture(m_OutputHandle);
  73. using (var builder = renderGraph.AddRasterRenderPass<PassData>("BlitToRTHandle_CopyColor", out var passData))
  74. {
  75. if (!source.IsValid() || !destination.IsValid())
  76. return;
  77. passData.source = source;
  78. passData.material = m_Material;
  79. passData.scaleBias = m_ScaleBias;
  80. builder.UseTexture(source, AccessFlags.Read); // Set the camera color as the input
  81. builder.SetRenderAttachment(destination, 0, AccessFlags.Write); // Set the target texture
  82. builder.SetGlobalTextureAfterPass(destination, m_OutputId); // Make the output texture available for the shaders in the scene
  83. builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
  84. {
  85. // Blit the input texture to the destination target specified in the SetRenderAttachment method
  86. Blitter.BlitTexture(context.cmd, data.source, data.scaleBias, data.material, 0);
  87. });
  88. }
  89. // In this example the pass executes after rendering transparent objects, and the transparent objects are reading the destination texture.
  90. // The following code sets the TextureHandle as the camera color target to avoid visual artefacts.
  91. resourceData.cameraColor = destination;
  92. }
  93. public void Dispose()
  94. {
  95. m_InputHandle?.Release();
  96. m_OutputHandle?.Release();
  97. }
  98. }