MyBlitRenderPass.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. class MyBlitRenderPass : ScriptableRenderPass
  5. {
  6. // used to label this pass in Unity's Frame Debug utility
  7. string profilerTag;
  8. Material materialToBlit;
  9. RenderTargetIdentifier cameraColorTargetIdent;
  10. RenderTargetHandle tempTexture;
  11. public MyBlitRenderPass(string profilerTag,
  12. RenderPassEvent renderPassEvent, Material materialToBlit)
  13. {
  14. this.profilerTag = profilerTag;
  15. this.renderPassEvent = renderPassEvent;
  16. this.materialToBlit = materialToBlit;
  17. }
  18. // This isn't part of the ScriptableRenderPass class and is our own addition.
  19. // For this custom pass we need the camera's color target, so that gets passed in.
  20. public void Setup(RenderTargetIdentifier cameraColorTargetIdent)
  21. {
  22. this.cameraColorTargetIdent = cameraColorTargetIdent;
  23. }
  24. // called each frame before Execute, use it to set up things the pass will need
  25. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  26. {
  27. // create a temporary render texture that matches the camera
  28. cmd.GetTemporaryRT(tempTexture.id, cameraTextureDescriptor);
  29. }
  30. // Execute is called for every eligible camera every frame. It's not called at the moment that
  31. // rendering is actually taking place, so don't directly execute rendering commands here.
  32. // Instead use the methods on ScriptableRenderContext to set up instructions.
  33. // RenderingData provides a bunch of (not very well documented) information about the scene
  34. // and what's being rendered.
  35. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  36. {
  37. // fetch a command buffer to use
  38. CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
  39. cmd.Clear();
  40. // the actual content of our custom render pass!
  41. // we apply our material while blitting to a temporary texture
  42. cmd.Blit(cameraColorTargetIdent, tempTexture.Identifier(), materialToBlit, 0);
  43. // ...then blit it back again
  44. cmd.Blit(tempTexture.Identifier(), cameraColorTargetIdent);
  45. // don't forget to tell ScriptableRenderContext to actually execute the commands
  46. context.ExecuteCommandBuffer(cmd);
  47. // tidy up after ourselves
  48. cmd.Clear();
  49. CommandBufferPool.Release(cmd);
  50. }
  51. // called after Execute, use it to clean up anything allocated in Configure
  52. public override void FrameCleanup(CommandBuffer cmd)
  53. {
  54. cmd.ReleaseTemporaryRT(tempTexture.id);
  55. }
  56. }