Advertisement
-dane-

FullscreenQuadRenderer

Sep 27th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. // This is a modified version of a Scriptable Render Pass written by Harry Heath.
  2. // Twitter: https://twitter.com/harryh___h/status/1328024692431540224
  3. // Pastebin: https://pastebin.com/LstBHRZF
  4.  
  5. // The original code was used in a recreation of a Mako illustration:
  6. // https://twitter.com/harryh___h/status/1328006632102526976
  7.  
  8. using UnityEngine;
  9. using UnityEngine.Rendering;
  10. using UnityEngine.Rendering.Universal;
  11.  
  12. public class FullscreenQuadRenderer : ScriptableRenderPass
  13. {
  14.     string m_ProfilerTag;
  15.     private Material _material;
  16.  
  17.     private ScriptableRenderer _renderer;
  18.     private bool _isDepth;
  19.  
  20.     int textureId = -1;
  21.     RenderTargetIdentifier _texture;
  22.     bool _newTexture;
  23.  
  24.     public FullscreenQuadRenderer(string name)
  25.     {
  26.         renderPassEvent = RenderPassEvent.BeforeRenderingTransparents;
  27.         m_ProfilerTag = name;
  28.     }
  29.  
  30.     public void Init(Material initMaterial, ScriptableRenderer newRenderer, bool depth)
  31.     {
  32.         _material = initMaterial;
  33.         _renderer = newRenderer;
  34.         textureId = -1;
  35.         _isDepth = depth;
  36.     }
  37.  
  38.     public void Init(Material initMaterial, string textureName, bool newTexture)
  39.     {
  40.         _material = initMaterial;
  41.         textureId = Shader.PropertyToID(textureName);
  42.         _texture = new RenderTargetIdentifier(textureId);
  43.         _renderer = null;
  44.         _newTexture = newTexture;
  45.     }
  46.  
  47.     public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  48.     {
  49.         if (textureId != -1 && _newTexture)
  50.         {
  51.             cmd.GetTemporaryRT(textureId, cameraTextureDescriptor.width, cameraTextureDescriptor.height, 24, FilterMode.Point,
  52.                 RenderTextureFormat.ARGBFloat);
  53.         }
  54.         else if (textureId == -1)
  55.         {
  56.             _texture = _isDepth ? _renderer.cameraDepthTarget : _renderer.cameraColorTarget;
  57.         }
  58.  
  59.         ConfigureTarget(_texture);
  60.         if (_newTexture) ConfigureClear(ClearFlag.All, Color.black);
  61.     }
  62.  
  63.     public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  64.     {
  65.         if (_material == null) return;
  66.  
  67.         CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  68.         Camera camera = renderingData.cameraData.camera;
  69.  
  70.         cmd.SetGlobalTexture("_MainTex", _texture);
  71.         cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
  72.         cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, _material);
  73.         cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
  74.  
  75.         context.ExecuteCommandBuffer(cmd);
  76.         CommandBufferPool.Release(cmd);
  77.     }
  78.  
  79.     public override void FrameCleanup(CommandBuffer cmd)
  80.     {
  81.         if (textureId != -1 && _newTexture) cmd.ReleaseTemporaryRT(textureId);
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement