Advertisement
Guest User

Passes

a guest
Nov 15th, 2020
2,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.Rendering.Universal;
  6.  
  7. namespace HarryH.Outline
  8. {
  9.     public class ShaderPassToTextureRenderer : ScriptableRenderPass
  10.     {
  11.         public OutlineSettings settings;
  12.         string profilerTag;
  13.  
  14.         ShaderTagId shaderTagId;
  15.         FilteringSettings filteringSettings;
  16.         RenderQueueRange renderQueue;
  17.  
  18.         int textureId;
  19.         RenderTargetIdentifier texture;
  20.         bool createTexture;
  21.  
  22.         int textureDepth;
  23.         RenderTextureFormat textureFormat;
  24.  
  25.         public ShaderPassToTextureRenderer(string name, string shaderName, RenderQueueRange renderQueue, string textureName, RenderPassEvent renderPassEvent, bool createTexture, int depth, RenderTextureFormat format)
  26.         {
  27.             this.renderPassEvent = renderPassEvent;
  28.  
  29.             profilerTag = name;
  30.             shaderTagId = new ShaderTagId(shaderName);
  31.             this.renderQueue = renderQueue;
  32.             filteringSettings = new FilteringSettings(renderQueue, ~0);
  33.             textureId = Shader.PropertyToID(textureName);
  34.             texture = new RenderTargetIdentifier(textureId);
  35.             this.createTexture = createTexture;
  36.             textureDepth = depth;
  37.             textureFormat = format;
  38.  
  39.         }
  40.  
  41.         public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  42.         {
  43.             if (createTexture) cmd.GetTemporaryRT(textureId, cameraTextureDescriptor.width, cameraTextureDescriptor.height, textureDepth, FilterMode.Point, textureFormat);
  44.  
  45.             ConfigureTarget(texture);
  46.             if (createTexture) ConfigureClear(ClearFlag.All, Color.black);
  47.         }
  48.  
  49.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  50.         {
  51.             CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
  52.  
  53.             SortingCriteria sortingCriteria = renderQueue == RenderQueueRange.opaque ? renderingData.cameraData.defaultOpaqueSortFlags : SortingCriteria.CommonTransparent;
  54.             DrawingSettings drawingSettings = CreateDrawingSettings(shaderTagId, ref renderingData, sortingCriteria);
  55.             context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings);
  56.  
  57.             context.ExecuteCommandBuffer(cmd);
  58.             CommandBufferPool.Release(cmd);
  59.         }
  60.  
  61.         public override void FrameCleanup(CommandBuffer cmd)
  62.         {
  63.             if (createTexture) cmd.ReleaseTemporaryRT(textureId);
  64.         }
  65.     }
  66.  
  67.  
  68.     public class FullscreenQuadRenderer : ScriptableRenderPass
  69.     {
  70.         string profilerTag;
  71.         public Material material;
  72.  
  73.         public ScriptableRenderer renderer;
  74.         bool isDepth;
  75.        
  76.         int textureId = -1;
  77.         RenderTargetIdentifier texture;
  78.         bool newTexture;
  79.  
  80.         public FullscreenQuadRenderer(string name)
  81.         {
  82.             renderPassEvent = RenderPassEvent.BeforeRenderingTransparents;
  83.             profilerTag = name;
  84.         }
  85.  
  86.         public void Init(Material material, ScriptableRenderer newRenderer, bool depth)
  87.         {
  88.             this.material = material;
  89.             renderer = newRenderer;
  90.             textureId = -1;
  91.             isDepth = depth;
  92.         }
  93.  
  94.         public void Init(Material material, string textureName, bool newTexture)
  95.         {
  96.             this.material = material;
  97.             textureId = Shader.PropertyToID(textureName);
  98.             texture = new RenderTargetIdentifier(textureId);
  99.             renderer = null;
  100.             this.newTexture = newTexture;
  101.         }
  102.  
  103.         public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  104.         {
  105.             if (textureId != -1 && newTexture)
  106.             {
  107.                 cmd.GetTemporaryRT(textureId, cameraTextureDescriptor.width, cameraTextureDescriptor.height, 24, FilterMode.Point, RenderTextureFormat.ARGBFloat);
  108.             }
  109.             else if (textureId == -1)
  110.             {
  111.                 texture = isDepth ? renderer.cameraDepth : renderer.cameraColorTarget;
  112.             }
  113.  
  114.             ConfigureTarget(texture);
  115.             if (newTexture) ConfigureClear(ClearFlag.All, Color.black);
  116.         }
  117.  
  118.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  119.         {
  120.             if (material == null) return;
  121.  
  122.             CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
  123.             Camera camera = renderingData.cameraData.camera;
  124.  
  125.             cmd.SetGlobalTexture("_MainTex", texture);
  126.             cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
  127.             cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, material);
  128.             cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
  129.  
  130.             context.ExecuteCommandBuffer(cmd);
  131.             CommandBufferPool.Release(cmd);
  132.         }
  133.  
  134.         public override void FrameCleanup(CommandBuffer cmd)
  135.         {
  136.             if (textureId != -1 && newTexture) cmd.ReleaseTemporaryRT(textureId);
  137.         }
  138.  
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement