Advertisement
-dane-

OutlineFeature

Sep 27th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.68 KB | None | 0 0
  1. // This is a modified version of a Renderer Feature written by Harry Heath.
  2. // Twitter: https://twitter.com/harryh___h/status/1328024692431540224
  3. // Pastebin: https://pastebin.com/rvju9psM
  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. namespace HarryH.Outline
  13. {
  14.     [System.Serializable]
  15.     public class OutlineSettings
  16.     {
  17.         public BlitSettings blitSettings;
  18.  
  19.         [Tooltip("Object Threshold.")] public float outerThreshold = 0.0f;
  20.         [Tooltip("Inner Threshold.")] public float innerThreshold = 0.0f;
  21.  
  22.         [Tooltip("Outline size X.")] public float outlineSizeX = 1.0f;
  23.         [Tooltip("Outline size Y.")] public float outlineSizeY = 1.0f;
  24.  
  25.         [Tooltip("Rotations.")] public int rotations = 8;
  26.         [Tooltip("Depth Push.")] public float depthPush = 1e-6f;
  27.  
  28.         [Tooltip("Object LUT.")] public Texture2D outerLUT;
  29.         [Tooltip("Inner LUT.")] public Texture2D innerLUT;
  30.     }
  31.  
  32.     public class OutlineFeature : ScriptableRendererFeature
  33.     {
  34.         ShaderPassToTextureRenderer outlinePassTexture;
  35.         // ShaderPassToTextureRenderer outlineDirectTexture;
  36.  
  37.         FullscreenQuadRenderer outlinePass;
  38.         FullscreenQuadRenderer colorBlitPass;
  39.         FullscreenQuadRenderer depthBlitPass;
  40.  
  41.         public OutlineSettings outlineSettings = new();
  42.  
  43.         Material outlineEncoderMaterial;
  44.         private string outlineEncoderName => outlineSettings.blitSettings.outlineEncoderString;
  45.  
  46.         private static readonly int HhoOuterThreshold = Shader.PropertyToID("_HHO_OuterThreshold");
  47.         private static readonly int HhoInnerThreshold = Shader.PropertyToID("_HHO_InnerThreshold");
  48.         private static readonly int HhoOutlineSizeX = Shader.PropertyToID("_HHO_OutlineSizeX");
  49.         private static readonly int HhoOutlineSizeY = Shader.PropertyToID("_HHO_OutlineSizeY");
  50.         private static readonly int HhoRotations = Shader.PropertyToID("_HHO_Rotations");
  51.         private static readonly int HhoDepthPush = Shader.PropertyToID("_HHO_DepthPush");
  52.         private static readonly int HhoOuterLut = Shader.PropertyToID("_HHO_OuterLUT");
  53.         private static readonly int HhoInnerLut = Shader.PropertyToID("_HHO_InnerLUT");
  54.  
  55.         private static OutlinePassTarget[] OutlinePassTargets => new[] { OutlineOpaqueTarget, OutlinePostTarget, OutlineDepthTarget };
  56.  
  57.         private static OutlinePassTarget
  58.             OutlineOpaqueTarget = new("Outline Pass Opaque Texture", "Outline", "_HHO_OutlineOpaqueTexture", true, 24, RenderTextureFormat.ARGBFloat);
  59.  
  60.         private static OutlinePassTarget
  61.             OutlinePostTarget = new("Outline Post", "Outline", "_HHO_OutlineTexture", true, 24, RenderTextureFormat.ARGBFloat);
  62.  
  63.         private static OutlinePassTarget
  64.             OutlineDepthTarget = new("Outline Pass Depth", "OutlineDepth", "_HHO_OutlineDepthTexture", true, 24, RenderTextureFormat.Depth);
  65.  
  66.         public override void Create()
  67.         {
  68.             outlinePassTexture = new ShaderPassToTextureRenderer(OutlinePassTargets, RenderQueueRange.opaque, RenderPassEvent.AfterRenderingOpaques);
  69.  
  70.             outlinePass = new FullscreenQuadRenderer("Outline Encoder");
  71.             // colorBlitPass = new FullscreenQuadRenderer("Outline Color Blit");
  72.             // depthBlitPass = new FullscreenQuadRenderer("Outline Depth Blit");
  73.             GetMaterial();
  74.         }
  75.  
  76.         public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  77.         {
  78.             if (!GetMaterial())
  79.             {
  80.                 Debug.LogErrorFormat(
  81.                     "{0}.AddRenderPasses(): Missing material. Make sure to add a blit material, or make sure {1} exists.",
  82.                     GetType().Name, outlineEncoderName);
  83.                 return;
  84.             }
  85.  
  86.             Shader.SetGlobalFloat(HhoOuterThreshold, outlineSettings.outerThreshold);
  87.             Shader.SetGlobalFloat(HhoInnerThreshold, outlineSettings.innerThreshold);
  88.             Shader.SetGlobalFloat(HhoOutlineSizeX, outlineSettings.outlineSizeX);
  89.             Shader.SetGlobalFloat(HhoOutlineSizeY, outlineSettings.outlineSizeY);
  90.             Shader.SetGlobalInt(HhoRotations, outlineSettings.rotations);
  91.             Shader.SetGlobalFloat(HhoDepthPush, outlineSettings.depthPush);
  92.             Shader.SetGlobalTexture(HhoOuterLut, outlineSettings.outerLUT);
  93.             Shader.SetGlobalTexture(HhoInnerLut, outlineSettings.innerLUT);
  94.  
  95.             // outlineSettings.blitSettings.Update();
  96.             renderer.EnqueuePass(outlinePass);
  97.             outlinePass.Init(outlineEncoderMaterial, "_HHO_Pass", true);
  98.             renderer.EnqueuePass(outlinePass);
  99.             // renderer.EnqueuePass(outlineDirectTexture);
  100.             colorBlitPass.Init(outlineSettings.blitSettings.blitMaterial, renderer, false);
  101.             renderer.EnqueuePass(colorBlitPass);
  102.             depthBlitPass.Init(outlineSettings.blitSettings.blitMaterial, "_CameraDepthTexture", false);
  103.             renderer.EnqueuePass(depthBlitPass);
  104.         }
  105.  
  106.         private bool GetMaterial()
  107.         {
  108.             if (outlineEncoderMaterial && outlineSettings.blitSettings && outlineSettings.blitSettings.blitMaterial)
  109.             {
  110.                 return true;
  111.             }
  112.  
  113.             var outlineEncoderShader = Shader.Find(outlineEncoderName);
  114.             if (outlineEncoderShader == null || !outlineSettings.blitSettings || !outlineSettings.blitSettings.blitMaterial) return false;
  115.             outlineEncoderMaterial = new Material(outlineEncoderShader);
  116.             return true;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement