-dane-

ShaderPassToTextureRenderer

Sep 27th, 2021 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 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 System.Linq;
  9. using UnityEngine;
  10. using UnityEngine.Rendering;
  11. using UnityEngine.Rendering.Universal;
  12.  
  13. namespace HarryH.Outline
  14. {
  15.     public class ShaderPassToTextureRenderer : ScriptableRenderPass
  16.     {
  17.         public OutlineSettings settings;
  18.         private string[] _profilerTag;
  19.  
  20.         private ShaderTagId[] _shaderTagId;
  21.         private FilteringSettings _filteringSettings;
  22.         private RenderQueueRange _renderQueue;
  23.  
  24.         private int[] _textureId;
  25.         private string[] _textureNames;
  26.  
  27.         RenderTargetIdentifier[] _textureRTId;
  28.         private bool[] _createTexture;
  29.         int[] _textureDepth;
  30.         RenderTextureFormat[] _textureFormats;
  31.  
  32.         public ShaderPassToTextureRenderer(OutlinePassTarget[] outlinePassTargets, RenderQueueRange renderQueue, RenderPassEvent renderPassEvent)
  33.         {
  34.             this.renderPassEvent = renderPassEvent;
  35.             _filteringSettings = new FilteringSettings(renderQueue, ~0);
  36.             _renderQueue = renderQueue;
  37.            
  38.             for (var i = 0; i < outlinePassTargets.Length; i++)
  39.             {
  40.                 _profilerTag[i] = outlinePassTargets[i].name;
  41.                 _shaderTagId[i] = new ShaderTagId(outlinePassTargets[i].shaderName);
  42.                 _textureId[i] = Shader.PropertyToID(outlinePassTargets[i].textureName);
  43.                 _textureRTId = _textureId.Select(texId => new RenderTargetIdentifier(texId)).ToArray();
  44.                 _createTexture[i] = outlinePassTargets[i].createTexture;
  45.                 _textureDepth[i] = outlinePassTargets[i].depth;
  46.                 _textureFormats[i] = outlinePassTargets[i].format;
  47.             }
  48.         }
  49.        
  50.         public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  51.         {
  52.             foreach (var createTexBool in _createTexture)
  53.             {
  54.                 if (createTexBool)
  55.                     for (var texName = 0; texName < _textureNames.Length; texName++)
  56.                     {
  57.                         cmd.GetTemporaryRT(_textureId[texName], cameraTextureDescriptor.width, cameraTextureDescriptor.height, _textureDepth[texName],
  58.                             FilterMode.Point,
  59.                             _textureFormats[texName]);
  60.                     }
  61.  
  62.                 foreach (var targetID in _textureRTId)
  63.                 {
  64.                     ConfigureTarget(new[] { targetID }, depthAttachment: _textureRTId[^1]);
  65.                 }
  66.  
  67.                 if (createTexBool) ConfigureClear(ClearFlag.All, Color.black);
  68.             }
  69.         }
  70.  
  71.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  72.         {
  73.             foreach (var tagName in _profilerTag)
  74.             {
  75.                 var cmd = CommandBufferPool.Get(tagName);
  76.  
  77.                 var sortingCriteria = _renderQueue == RenderQueueRange.opaque
  78.                     ? renderingData.cameraData.defaultOpaqueSortFlags
  79.                     : SortingCriteria.CommonTransparent;
  80.                 foreach (var tagId in _shaderTagId)
  81.                 {
  82.                     var drawingSettings = CreateDrawingSettings(tagId, ref renderingData, sortingCriteria);
  83.                     context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref _filteringSettings);
  84.                 }
  85.  
  86.                 context.ExecuteCommandBuffer(cmd);
  87.                 CommandBufferPool.Release(cmd);    
  88.             }
  89.         }
  90.  
  91.         public override void FrameCleanup(CommandBuffer cmd)
  92.         {
  93.             foreach (var createTexBool in _createTexture)
  94.             {
  95.                 for (var texName = 0; texName < _textureNames.Length; texName++)
  96.                 {
  97.                     if (createTexBool) cmd.ReleaseTemporaryRT(_textureId[texName]);
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
Add Comment
Please, Sign In to add comment