< Summary

Class:OutlineScreenEffectFeature
Assembly:Outliner
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/Outliner/OutlineScreenEffectFeature.cs
Covered lines:7
Uncovered lines:65
Coverable lines:72
Total lines:144
Line coverage:9.7% (7 of 72)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:16
Method coverage:12.5% (2 of 16)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
OutlinePass()0%2100%
OutlinePass(...)0%110100%
Setup(...)0%2100%
Execute(...)0%12300%
IsOutlineAvailable()0%2100%
FrameCleanup(...)0%2100%
OutlineSettings()0%2100%
OutlineScreenEffectFeature()0%2100%
Create()0%110100%
AddRenderPasses(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/Outliner/OutlineScreenEffectFeature.cs

#LineLine coverage
 1using DCL;
 2using UnityEngine;
 3using UnityEngine.Rendering;
 4using UnityEngine.Rendering.Universal;
 5
 6public class OutlineScreenEffectFeature : ScriptableRendererFeature
 7{
 8    private class OutlinePass : ScriptableRenderPass
 9    {
 010        private static readonly int INNER_COLOR = Shader.PropertyToID("_InnerColor");
 011        private static readonly int OUTLINE_COLOR = Shader.PropertyToID("_OutlineColor");
 012        private static readonly int OUTLINE_SIZE = Shader.PropertyToID("_OutlineSize");
 013        private static readonly int BLUR_COLOR = Shader.PropertyToID("_BlurColor");
 014        private static readonly int BLUR_SIZE = Shader.PropertyToID("_BlurSize");
 015        private static readonly int SIGMA = Shader.PropertyToID("_BlurSigma");
 016        private static readonly int FADE = Shader.PropertyToID("_Fade");
 017        private static readonly int OUTLINER_EFFECT = Shader.PropertyToID("_OutlinerEffect_Camera");
 018        private static readonly int OUTLINE_HORIZONTAL = Shader.PropertyToID("_OutlinerEffect_Outline1");
 019        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
 025        private ScriptableRenderer renderer { get; set; }
 026        private RenderTargetHandle destination { get; set; }
 027        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
 237        public OutlinePass(OutlineSettings settings)
 38        {
 239            this.settings = settings;
 240            material = CoreUtils.CreateEngineMaterial("DCL/OutlinerEffect");
 241        }
 42
 43        public void Setup(ScriptableRenderer renderer, RenderTargetHandle destination, RenderTargetHandle outlineTexture
 44        {
 045            this.renderer = renderer;
 046            this.destination = destination;
 047            this.outlineMask = outlineTexture;
 048        }
 49
 50        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
 51        {
 052            if (!IsOutlineAvailable()) return;
 53
 054            material.SetColor(INNER_COLOR, settings.innerColor);
 055            material.SetColor(OUTLINE_COLOR, settings.outlineColor);
 056            material.SetFloat(OUTLINE_SIZE, settings.outlineThickness);
 057            material.SetColor(BLUR_COLOR, settings.blurColor);
 058            material.SetFloat(BLUR_SIZE, settings.blurSize);
 059            material.SetFloat(SIGMA, settings.blurSigma);
 060            material.SetFloat(FADE, settings.effectFade);
 061            CommandBuffer cmd = CommandBufferPool.Get(PROFILER_TAG);
 62
 063            using (new ProfilingScope(cmd, new ProfilingSampler(PROFILER_TAG)))
 64            {
 065                RenderTextureDescriptor lowResDescriptor = renderingData.cameraData.cameraTargetDescriptor;
 066                RenderTextureDescriptor mainDescriptor = renderingData.cameraData.cameraTargetDescriptor;
 67
 68                // For high resolutions we dont need so much quality and the blur effect gets exponentially expensive so
 069                float resolutionScale = 1;
 70
 071                if (lowResDescriptor.width > 1920)
 072                    resolutionScale = (1920f * 100f / lowResDescriptor.width) / 100f;
 73
 074                lowResDescriptor.width = Mathf.RoundToInt(lowResDescriptor.width * resolutionScale);
 075                lowResDescriptor.height = Mathf.RoundToInt(lowResDescriptor.height * resolutionScale);
 076                lowResDescriptor.depthBufferBits = 0;
 77
 078                RenderTargetHandle camera = new RenderTargetHandle { id = OUTLINER_EFFECT };
 079                RenderTargetHandle outline1 = new RenderTargetHandle { id = OUTLINE_HORIZONTAL };
 080                RenderTargetHandle outline2 = new RenderTargetHandle { id = OUTLINE_VERTICAL };
 81
 082                cmd.GetTemporaryRT(camera.id, mainDescriptor, FilterMode.Point);
 083                cmd.GetTemporaryRT(outline1.id, lowResDescriptor, (FilterMode)settings.filterMode);
 084                cmd.GetTemporaryRT(outline2.id, lowResDescriptor, (FilterMode)settings.filterMode);
 85
 086                Blit(cmd, outlineMask.id, outline1.id, material, (int)ShaderPasses.Outline); // Get the outline. Output 
 087                Blit(cmd, outline1.id, outline2.id, material, (int)ShaderPasses.BlurHorizontal); // Apply Vertical blur.
 088                Blit(cmd, outline2.id, outline1.id, material, (int)ShaderPasses.BlurVertical); // Apply Horizontal blur.
 89
 090                Blit(cmd, renderer.cameraColorTargetHandle, camera.id); // Get camera in a RT
 091                cmd.SetGlobalTexture("_Source", camera.id); // Apply RT as _Source for the material
 092                cmd.SetGlobalTexture("_ComposeMask", outlineMask.id); // Set the original outline mask
 93
 094                Blit(cmd, outline1.id, renderer.cameraColorTargetHandle, material, (int)ShaderPasses.Compose);
 95
 096                cmd.ReleaseTemporaryRT(camera.id);
 097                cmd.ReleaseTemporaryRT(outline1.id);
 098                cmd.ReleaseTemporaryRT(outline2.id);
 099            }
 100
 0101            context.ExecuteCommandBuffer(cmd);
 0102            CommandBufferPool.Release(cmd);
 0103        }
 104
 105        private static bool IsOutlineAvailable() =>
 0106            DataStore.i.outliner.avatarOutlined.Get().renderer != null;
 107
 0108        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;
 0117        public Color outlineColor = Color.cyan;
 0118        public Color blurColor = Color.cyan;
 0119        public Color innerColor = Color.cyan;
 0120        [Range(0, 1)] public float effectFade = 1;
 121
 122        public int filterMode;
 123    }
 124
 0125    public OutlineSettings settings = new ();
 126    private OutlinePass outlinePass;
 127    private RenderTargetHandle outlineTexture;
 128
 129    public override void Create()
 130    {
 2131        outlinePass = new OutlinePass(settings)
 132        {
 133            renderPassEvent = RenderPassEvent.AfterRenderingTransparents,
 134        };
 135
 2136        outlineTexture.Init("_OutlineTexture");
 2137    }
 138
 139    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
 140    {
 0141        outlinePass.Setup(renderer, RenderTargetHandle.CameraTarget, outlineTexture);
 0142        renderer.EnqueuePass(outlinePass);
 0143    }
 144}