| | 1 | | // unset:none |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Rendering; |
| | 4 | | using UnityEngine.Rendering.Universal; |
| | 5 | |
|
| | 6 | | public class OutlineScreenEffectFeature : ScriptableRendererFeature |
| | 7 | | { |
| | 8 | | private class OutlinePass : ScriptableRenderPass |
| | 9 | | { |
| 0 | 10 | | private static readonly int _InnerColor = Shader.PropertyToID("_InnerColor"); |
| 0 | 11 | | private static readonly int _OutlineColor = Shader.PropertyToID("_OutlineColor"); |
| 0 | 12 | | private static readonly int _OutlineSize = Shader.PropertyToID("_OutlineSize"); |
| 0 | 13 | | private static readonly int _BlurColor = Shader.PropertyToID("_BlurColor"); |
| 0 | 14 | | private static readonly int _BlurSize = Shader.PropertyToID("_BlurSize"); |
| 0 | 15 | | private static readonly int _Sigma = Shader.PropertyToID("_BlurSigma"); |
| 0 | 16 | | private static readonly int _Fade = Shader.PropertyToID("_Fade"); |
| | 17 | |
|
| | 18 | | private readonly OutlineSettings settings; |
| | 19 | | private const string PROFILER_TAG = "Outline Screen Effect Pass"; |
| | 20 | | private readonly Material material = null; |
| | 21 | |
|
| 0 | 22 | | private RenderTargetIdentifier source { get; set; } |
| 0 | 23 | | private RenderTargetHandle destination { get; set; } |
| 0 | 24 | | private RenderTargetHandle outlineMask { get; set; } |
| | 25 | |
|
| | 26 | | private enum ShaderPasses |
| | 27 | | { |
| | 28 | | Outline = 0, |
| | 29 | | BlurHorizontal = 1, |
| | 30 | | BlurVertical = 2, |
| | 31 | | Compose = 3 |
| | 32 | | } |
| | 33 | |
|
| 2 | 34 | | public OutlinePass(OutlineSettings settings) |
| | 35 | | { |
| 2 | 36 | | this.settings = settings; |
| 2 | 37 | | material = CoreUtils.CreateEngineMaterial("DCL/OutlinerEffect"); |
| 2 | 38 | | } |
| | 39 | |
|
| | 40 | | public void Setup(RenderTargetIdentifier source, RenderTargetHandle destination, RenderTargetHandle outlineTextu |
| | 41 | | { |
| 0 | 42 | | this.source = source; |
| 0 | 43 | | this.destination = destination; |
| 0 | 44 | | this.outlineMask = outlineTexture; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) |
| | 48 | | { |
| 0 | 49 | | material.SetColor(_InnerColor, settings.innerColor); |
| 0 | 50 | | material.SetColor(_OutlineColor, settings.outlineColor); |
| 0 | 51 | | material.SetFloat(_OutlineSize, settings.outlineSize); |
| 0 | 52 | | material.SetColor(_BlurColor, settings.blurColor); |
| 0 | 53 | | material.SetFloat(_BlurSize, settings.blurSize); |
| 0 | 54 | | material.SetFloat(_Sigma, settings.blurSigma); |
| 0 | 55 | | material.SetFloat(_Fade, settings.effectFade); |
| 0 | 56 | | CommandBuffer cmd = CommandBufferPool.Get(PROFILER_TAG); |
| | 57 | |
|
| 0 | 58 | | using (new ProfilingScope(cmd, new ProfilingSampler(PROFILER_TAG))) |
| | 59 | | { |
| 0 | 60 | | RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor; |
| 0 | 61 | | descriptor.depthBufferBits = 0; |
| 0 | 62 | | RenderTargetHandle camera = new RenderTargetHandle() { id = Shader.PropertyToID("_OutlinerEffect_Camera" |
| 0 | 63 | | RenderTargetHandle outline1 = new RenderTargetHandle() { id = Shader.PropertyToID("_OutlinerEffect_Outli |
| 0 | 64 | | RenderTargetHandle outline2 = new RenderTargetHandle() { id = Shader.PropertyToID("_OutlinerEffect_Outli |
| 0 | 65 | | cmd.GetTemporaryRT(camera.id, descriptor, FilterMode.Point); |
| 0 | 66 | | cmd.GetTemporaryRT(outline1.id, descriptor, FilterMode.Point); |
| 0 | 67 | | cmd.GetTemporaryRT(outline2.id, descriptor, FilterMode.Point); |
| | 68 | |
|
| 0 | 69 | | Blit(cmd, outlineMask.id, outline1.id, material, (int)ShaderPasses.Outline); // Get the outline. Output |
| 0 | 70 | | Blit(cmd, outline1.id, outline2.id, material, (int)ShaderPasses.BlurHorizontal); // Apply Vertical blur. |
| 0 | 71 | | Blit(cmd, outline2.id, outline1.id, material, (int)ShaderPasses.BlurVertical); // Apply Horizontal blur. |
| | 72 | |
|
| 0 | 73 | | Blit(cmd, source, camera.id); // Get camera in a RT |
| 0 | 74 | | cmd.SetGlobalTexture("_Source", camera.id); // Apply RT as _Source for the material |
| 0 | 75 | | cmd.SetGlobalTexture("_ComposeMask", outlineMask.id); // Set the original outline mask |
| 0 | 76 | | Blit(cmd, outline1.id, source, material, (int)ShaderPasses.Compose); |
| | 77 | |
|
| 0 | 78 | | cmd.ReleaseTemporaryRT(camera.id); |
| 0 | 79 | | cmd.ReleaseTemporaryRT(outline1.id); |
| 0 | 80 | | cmd.ReleaseTemporaryRT(outline2.id); |
| | 81 | |
|
| | 82 | |
|
| 0 | 83 | | } |
| | 84 | |
|
| 0 | 85 | | context.ExecuteCommandBuffer(cmd); |
| 0 | 86 | | CommandBufferPool.Release(cmd); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public override void FrameCleanup(CommandBuffer cmd) |
| | 90 | | { |
| 0 | 91 | | } |
| | 92 | | } |
| | 93 | |
|
| | 94 | | [System.Serializable] |
| | 95 | | public class OutlineSettings |
| | 96 | | { |
| 0 | 97 | | public float outlineSize = 0.001f; |
| 0 | 98 | | public float blurSize = 0.001f; |
| 0 | 99 | | public float blurSigma = 1; |
| 0 | 100 | | public Color outlineColor = Color.cyan; |
| 0 | 101 | | public Color blurColor = Color.cyan; |
| 0 | 102 | | public Color innerColor = Color.cyan; |
| 0 | 103 | | [Range(0, 1)] public float effectFade = 1; |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | public OutlineSettings settings = new (); |
| | 107 | | private OutlinePass outlinePass; |
| | 108 | | private RenderTargetHandle outlineTexture; |
| | 109 | |
|
| | 110 | | public override void Create() |
| | 111 | | { |
| 2 | 112 | | outlinePass = new OutlinePass(settings) |
| | 113 | | { |
| | 114 | | renderPassEvent = RenderPassEvent.AfterRenderingTransparents, |
| | 115 | | }; |
| | 116 | |
|
| 2 | 117 | | outlineTexture.Init("_OutlineTexture"); |
| 2 | 118 | | } |
| | 119 | |
|
| | 120 | | public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) |
| | 121 | | { |
| 0 | 122 | | outlinePass.Setup(renderer.cameraColorTarget, RenderTargetHandle.CameraTarget, outlineTexture); |
| 0 | 123 | | renderer.EnqueuePass(outlinePass); |
| 0 | 124 | | } |
| | 125 | | } |