Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a modified version of a Renderer Feature written by Harry Heath.
- // Twitter: https://twitter.com/harryh___h/status/1328024692431540224
- // Pastebin: https://pastebin.com/rvju9psM
- // The original code was used in a recreation of a Mako illustration:
- // https://twitter.com/harryh___h/status/1328006632102526976
- using UnityEngine;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.Universal;
- namespace HarryH.Outline
- {
- [System.Serializable]
- public class OutlineSettings
- {
- public BlitSettings blitSettings;
- [Tooltip("Object Threshold.")] public float outerThreshold = 0.0f;
- [Tooltip("Inner Threshold.")] public float innerThreshold = 0.0f;
- [Tooltip("Outline size X.")] public float outlineSizeX = 1.0f;
- [Tooltip("Outline size Y.")] public float outlineSizeY = 1.0f;
- [Tooltip("Rotations.")] public int rotations = 8;
- [Tooltip("Depth Push.")] public float depthPush = 1e-6f;
- [Tooltip("Object LUT.")] public Texture2D outerLUT;
- [Tooltip("Inner LUT.")] public Texture2D innerLUT;
- }
- public class OutlineFeature : ScriptableRendererFeature
- {
- ShaderPassToTextureRenderer outlinePassTexture;
- // ShaderPassToTextureRenderer outlineDirectTexture;
- FullscreenQuadRenderer outlinePass;
- FullscreenQuadRenderer colorBlitPass;
- FullscreenQuadRenderer depthBlitPass;
- public OutlineSettings outlineSettings = new();
- Material outlineEncoderMaterial;
- private string outlineEncoderName => outlineSettings.blitSettings.outlineEncoderString;
- private static readonly int HhoOuterThreshold = Shader.PropertyToID("_HHO_OuterThreshold");
- private static readonly int HhoInnerThreshold = Shader.PropertyToID("_HHO_InnerThreshold");
- private static readonly int HhoOutlineSizeX = Shader.PropertyToID("_HHO_OutlineSizeX");
- private static readonly int HhoOutlineSizeY = Shader.PropertyToID("_HHO_OutlineSizeY");
- private static readonly int HhoRotations = Shader.PropertyToID("_HHO_Rotations");
- private static readonly int HhoDepthPush = Shader.PropertyToID("_HHO_DepthPush");
- private static readonly int HhoOuterLut = Shader.PropertyToID("_HHO_OuterLUT");
- private static readonly int HhoInnerLut = Shader.PropertyToID("_HHO_InnerLUT");
- private static OutlinePassTarget[] OutlinePassTargets => new[] { OutlineOpaqueTarget, OutlinePostTarget, OutlineDepthTarget };
- private static OutlinePassTarget
- OutlineOpaqueTarget = new("Outline Pass Opaque Texture", "Outline", "_HHO_OutlineOpaqueTexture", true, 24, RenderTextureFormat.ARGBFloat);
- private static OutlinePassTarget
- OutlinePostTarget = new("Outline Post", "Outline", "_HHO_OutlineTexture", true, 24, RenderTextureFormat.ARGBFloat);
- private static OutlinePassTarget
- OutlineDepthTarget = new("Outline Pass Depth", "OutlineDepth", "_HHO_OutlineDepthTexture", true, 24, RenderTextureFormat.Depth);
- public override void Create()
- {
- outlinePassTexture = new ShaderPassToTextureRenderer(OutlinePassTargets, RenderQueueRange.opaque, RenderPassEvent.AfterRenderingOpaques);
- outlinePass = new FullscreenQuadRenderer("Outline Encoder");
- // colorBlitPass = new FullscreenQuadRenderer("Outline Color Blit");
- // depthBlitPass = new FullscreenQuadRenderer("Outline Depth Blit");
- GetMaterial();
- }
- public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
- {
- if (!GetMaterial())
- {
- Debug.LogErrorFormat(
- "{0}.AddRenderPasses(): Missing material. Make sure to add a blit material, or make sure {1} exists.",
- GetType().Name, outlineEncoderName);
- return;
- }
- Shader.SetGlobalFloat(HhoOuterThreshold, outlineSettings.outerThreshold);
- Shader.SetGlobalFloat(HhoInnerThreshold, outlineSettings.innerThreshold);
- Shader.SetGlobalFloat(HhoOutlineSizeX, outlineSettings.outlineSizeX);
- Shader.SetGlobalFloat(HhoOutlineSizeY, outlineSettings.outlineSizeY);
- Shader.SetGlobalInt(HhoRotations, outlineSettings.rotations);
- Shader.SetGlobalFloat(HhoDepthPush, outlineSettings.depthPush);
- Shader.SetGlobalTexture(HhoOuterLut, outlineSettings.outerLUT);
- Shader.SetGlobalTexture(HhoInnerLut, outlineSettings.innerLUT);
- // outlineSettings.blitSettings.Update();
- renderer.EnqueuePass(outlinePass);
- outlinePass.Init(outlineEncoderMaterial, "_HHO_Pass", true);
- renderer.EnqueuePass(outlinePass);
- // renderer.EnqueuePass(outlineDirectTexture);
- colorBlitPass.Init(outlineSettings.blitSettings.blitMaterial, renderer, false);
- renderer.EnqueuePass(colorBlitPass);
- depthBlitPass.Init(outlineSettings.blitSettings.blitMaterial, "_CameraDepthTexture", false);
- renderer.EnqueuePass(depthBlitPass);
- }
- private bool GetMaterial()
- {
- if (outlineEncoderMaterial && outlineSettings.blitSettings && outlineSettings.blitSettings.blitMaterial)
- {
- return true;
- }
- var outlineEncoderShader = Shader.Find(outlineEncoderName);
- if (outlineEncoderShader == null || !outlineSettings.blitSettings || !outlineSettings.blitSettings.blitMaterial) return false;
- outlineEncoderMaterial = new Material(outlineEncoderShader);
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement