| | 1 | | using DCL; |
| | 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 INNER_COLOR = Shader.PropertyToID("_InnerColor"); |
| 0 | 11 | | private static readonly int OUTLINE_COLOR = Shader.PropertyToID("_OutlineColor"); |
| 0 | 12 | | private static readonly int OUTLINE_SIZE = Shader.PropertyToID("_OutlineSize"); |
| 0 | 13 | | private static readonly int BLUR_COLOR = Shader.PropertyToID("_BlurColor"); |
| 0 | 14 | | private static readonly int BLUR_SIZE = Shader.PropertyToID("_BlurSize"); |
| 0 | 15 | | private static readonly int SIGMA = Shader.PropertyToID("_BlurSigma"); |
| 0 | 16 | | private static readonly int FADE = Shader.PropertyToID("_Fade"); |
| 0 | 17 | | private static readonly int OUTLINER_EFFECT = Shader.PropertyToID("_OutlinerEffect_Camera"); |
| 0 | 18 | | private static readonly int OUTLINE_HORIZONTAL = Shader.PropertyToID("_OutlinerEffect_Outline1"); |
| 0 | 19 | | private static readonly int OUTLINE_VERTICAL = Shader.PropertyToID("_OutlinerEffect_Outline2"); |
| | 20 | |
|
| | 21 | | private readonly OutlineSettings settings; |
| | 22 | | private const string PROFILER_TAG = "Outline Screen Effect Pass"; |
| | 23 | | private readonly Material material = null; |
| | 24 | |
|
| 0 | 25 | | private ScriptableRenderer renderer { get; set; } |
| 0 | 26 | | private RenderTargetHandle destination { get; set; } |
| 0 | 27 | | private RenderTargetHandle outlineMask { get; set; } |
| | 28 | |
|
| | 29 | | private enum ShaderPasses |
| | 30 | | { |
| | 31 | | Outline = 0, |
| | 32 | | BlurHorizontal = 1, |
| | 33 | | BlurVertical = 2, |
| | 34 | | Compose = 3 |
| | 35 | | } |
| | 36 | |
|
| 2 | 37 | | public OutlinePass(OutlineSettings settings) |
| | 38 | | { |
| 2 | 39 | | this.settings = settings; |
| 2 | 40 | | material = CoreUtils.CreateEngineMaterial("DCL/OutlinerEffect"); |
| 2 | 41 | | } |
| | 42 | |
|
| | 43 | | public void Setup(ScriptableRenderer renderer, RenderTargetHandle destination, RenderTargetHandle outlineTexture |
| | 44 | | { |
| 0 | 45 | | this.renderer = renderer; |
| 0 | 46 | | this.destination = destination; |
| 0 | 47 | | this.outlineMask = outlineTexture; |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) |
| | 51 | | { |
| 0 | 52 | | if (!IsOutlineAvailable()) return; |
| | 53 | |
|
| 0 | 54 | | material.SetColor(INNER_COLOR, settings.innerColor); |
| 0 | 55 | | material.SetColor(OUTLINE_COLOR, settings.outlineColor); |
| 0 | 56 | | material.SetFloat(OUTLINE_SIZE, settings.outlineThickness); |
| 0 | 57 | | material.SetColor(BLUR_COLOR, settings.blurColor); |
| 0 | 58 | | material.SetFloat(BLUR_SIZE, settings.blurSize); |
| 0 | 59 | | material.SetFloat(SIGMA, settings.blurSigma); |
| 0 | 60 | | material.SetFloat(FADE, settings.effectFade); |
| 0 | 61 | | CommandBuffer cmd = CommandBufferPool.Get(PROFILER_TAG); |
| | 62 | |
|
| 0 | 63 | | using (new ProfilingScope(cmd, new ProfilingSampler(PROFILER_TAG))) |
| | 64 | | { |
| 0 | 65 | | RenderTextureDescriptor lowResDescriptor = renderingData.cameraData.cameraTargetDescriptor; |
| 0 | 66 | | RenderTextureDescriptor mainDescriptor = renderingData.cameraData.cameraTargetDescriptor; |
| | 67 | |
|
| | 68 | | // For high resolutions we dont need so much quality and the blur effect gets exponentially expensive so |
| 0 | 69 | | float resolutionScale = 1; |
| | 70 | |
|
| 0 | 71 | | if (lowResDescriptor.width > 1920) |
| 0 | 72 | | resolutionScale = (1920f * 100f / lowResDescriptor.width) / 100f; |
| | 73 | |
|
| 0 | 74 | | lowResDescriptor.width = Mathf.RoundToInt(lowResDescriptor.width * resolutionScale); |
| 0 | 75 | | lowResDescriptor.height = Mathf.RoundToInt(lowResDescriptor.height * resolutionScale); |
| 0 | 76 | | lowResDescriptor.depthBufferBits = 0; |
| | 77 | |
|
| 0 | 78 | | RenderTargetHandle camera = new RenderTargetHandle { id = OUTLINER_EFFECT }; |
| 0 | 79 | | RenderTargetHandle outline1 = new RenderTargetHandle { id = OUTLINE_HORIZONTAL }; |
| 0 | 80 | | RenderTargetHandle outline2 = new RenderTargetHandle { id = OUTLINE_VERTICAL }; |
| | 81 | |
|
| 0 | 82 | | cmd.GetTemporaryRT(camera.id, mainDescriptor, FilterMode.Point); |
| 0 | 83 | | cmd.GetTemporaryRT(outline1.id, lowResDescriptor, (FilterMode)settings.filterMode); |
| 0 | 84 | | cmd.GetTemporaryRT(outline2.id, lowResDescriptor, (FilterMode)settings.filterMode); |
| | 85 | |
|
| 0 | 86 | | Blit(cmd, outlineMask.id, outline1.id, material, (int)ShaderPasses.Outline); // Get the outline. Output |
| 0 | 87 | | Blit(cmd, outline1.id, outline2.id, material, (int)ShaderPasses.BlurHorizontal); // Apply Vertical blur. |
| 0 | 88 | | Blit(cmd, outline2.id, outline1.id, material, (int)ShaderPasses.BlurVertical); // Apply Horizontal blur. |
| | 89 | |
|
| 0 | 90 | | Blit(cmd, renderer.cameraColorTargetHandle, camera.id); // Get camera in a RT |
| 0 | 91 | | cmd.SetGlobalTexture("_Source", camera.id); // Apply RT as _Source for the material |
| 0 | 92 | | cmd.SetGlobalTexture("_ComposeMask", outlineMask.id); // Set the original outline mask |
| | 93 | |
|
| 0 | 94 | | Blit(cmd, outline1.id, renderer.cameraColorTargetHandle, material, (int)ShaderPasses.Compose); |
| | 95 | |
|
| 0 | 96 | | cmd.ReleaseTemporaryRT(camera.id); |
| 0 | 97 | | cmd.ReleaseTemporaryRT(outline1.id); |
| 0 | 98 | | cmd.ReleaseTemporaryRT(outline2.id); |
| 0 | 99 | | } |
| | 100 | |
|
| 0 | 101 | | context.ExecuteCommandBuffer(cmd); |
| 0 | 102 | | CommandBufferPool.Release(cmd); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | private static bool IsOutlineAvailable() => |
| 0 | 106 | | DataStore.i.outliner.avatarOutlined.Get().renderer != null; |
| | 107 | |
|
| 0 | 108 | | public override void FrameCleanup(CommandBuffer cmd) { } |
| | 109 | | } |
| | 110 | |
|
| | 111 | | [System.Serializable] |
| | 112 | | public class OutlineSettings |
| | 113 | | { |
| | 114 | | public float outlineThickness; |
| | 115 | | public float blurSize; |
| | 116 | | public float blurSigma; |
| 0 | 117 | | public Color outlineColor = Color.cyan; |
| 0 | 118 | | public Color blurColor = Color.cyan; |
| 0 | 119 | | public Color innerColor = Color.cyan; |
| 0 | 120 | | [Range(0, 1)] public float effectFade = 1; |
| | 121 | |
|
| | 122 | | public int filterMode; |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | public OutlineSettings settings = new (); |
| | 126 | | private OutlinePass outlinePass; |
| | 127 | | private RenderTargetHandle outlineTexture; |
| | 128 | |
|
| | 129 | | public override void Create() |
| | 130 | | { |
| 2 | 131 | | outlinePass = new OutlinePass(settings) |
| | 132 | | { |
| | 133 | | renderPassEvent = RenderPassEvent.AfterRenderingTransparents, |
| | 134 | | }; |
| | 135 | |
|
| 2 | 136 | | outlineTexture.Init("_OutlineTexture"); |
| 2 | 137 | | } |
| | 138 | |
|
| | 139 | | public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) |
| | 140 | | { |
| 0 | 141 | | outlinePass.Setup(renderer, RenderTargetHandle.CameraTarget, outlineTexture); |
| 0 | 142 | | renderer.EnqueuePass(outlinePass); |
| 0 | 143 | | } |
| | 144 | | } |