Advertisement
443eb9

Untitled

Aug 22nd, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4.  
  5. public class PosterizeRenderPassFeature : ScriptableRendererFeature
  6. {
  7. class CustomRenderPass : ScriptableRenderPass
  8. {
  9. private Material _material;
  10. private RTHandle _tempHandle, _sourceHandle;
  11.  
  12. private const string TempTex = "_TempTex";
  13. private readonly int _tempTexID = Shader.PropertyToID(TempTex);
  14.  
  15. public CustomRenderPass(Material material)
  16. {
  17. _material = material;
  18. }
  19.  
  20. public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
  21. {
  22. _sourceHandle = renderingData.cameraData.renderer.cameraColorTargetHandle;
  23. _tempHandle = RTHandles.Alloc(new RenderTargetIdentifier(TempTex), name: TempTex);
  24. }
  25.  
  26. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  27. {
  28. if (_material == null)
  29. return;
  30.  
  31. CommandBuffer cmd = CommandBufferPool.Get();
  32. var descriptor = renderingData.cameraData.cameraTargetDescriptor;
  33. descriptor.depthBufferBits = 0;
  34. cmd.GetTemporaryRT(_tempTexID, descriptor, FilterMode.Bilinear);
  35.  
  36. Blit(cmd,_sourceHandle,_tempHandle,_material);
  37. Blit(cmd,_tempHandle,_sourceHandle);
  38.  
  39. context.ExecuteCommandBuffer(cmd);
  40. CommandBufferPool.Release(cmd);
  41. }
  42.  
  43. public override void OnCameraCleanup(CommandBuffer cmd)
  44. {
  45. _tempHandle.Release();
  46. }
  47. }
  48.  
  49. private CustomRenderPass _pass;
  50. public Material material;
  51.  
  52. public override void Create()
  53. {
  54. _pass = new CustomRenderPass(material)
  55. {
  56. renderPassEvent = RenderPassEvent.AfterRenderingOpaques
  57. };
  58. }
  59.  
  60. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  61. {
  62. renderer.EnqueuePass(_pass);
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement