< Summary

Class:DCL.Rendering.CullingControllerUtils
Assembly:CullingController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/Culling/CullingControllerUtils.cs
Covered lines:17
Uncovered lines:38
Coverable lines:55
Total lines:177
Line coverage:30.9% (17 of 55)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TestSkinnedRendererOffscreenRule(...)0%330100%
TestRendererVisibleRule(...)0%440100%
TestRendererShadowRule(...)0%110100%
TestAvatarShadowRule(...)0%2100%
CullingControllerUtils()0%110100%
IsOpaque(...)0%20400%
IsEmissive(...)0%42600%
ComputeShadowMapTexelSize(...)0%110100%
DrawBounds(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/Culling/CullingControllerUtils.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using UnityEngine;
 3using DCL.Shaders;
 4
 5namespace DCL.Rendering
 6{
 7    public static class CullingControllerUtils
 8    {
 9        /// <summary>
 10        /// Computes the rule used for toggling skinned meshes updateWhenOffscreen param.
 11        /// Skinned meshes should be always updated if near the camera to avoid false culling positives on screen edges.
 12        /// </summary>
 13        /// <param name="settings">Any settings object to use thresholds for computing the rule.</param>
 14        /// <param name="distanceToCamera">Mesh distance from camera used for computing the rule.</param>
 15        /// <returns>True if mesh should be updated when offscreen, false if otherwise.</returns>
 16        internal static bool TestSkinnedRendererOffscreenRule(CullingControllerSettings settings, float distanceToCamera
 17        {
 318            bool finalValue = true;
 19
 320            if (settings.enableAnimationCulling)
 21            {
 222                if (distanceToCamera > settings.enableAnimationCullingDistance)
 123                    finalValue = false;
 24            }
 25
 326            return finalValue;
 27        }
 28
 29        /// <summary>
 30        /// Computes the rule used for toggling renderers visibility.
 31        /// </summary>
 32        /// <param name="profile">Profile used for size and distance thresholds needed for the rule.</param>
 33        /// <param name="viewportSize">Diagonal viewport size of the renderer.</param>
 34        /// <param name="distanceToCamera">Distance to camera of the renderer.</param>
 35        /// <param name="boundsContainsCamera">Renderer bounds contains camera?</param>
 36        /// <param name="isOpaque">Renderer is opaque?</param>
 37        /// <param name="isEmissive">Renderer is emissive?</param>
 38        /// <returns>True if renderer should be visible, false if otherwise.</returns>
 39        internal static bool TestRendererVisibleRule(CullingControllerProfile profile, float viewportSize, float distanc
 40        {
 741            bool shouldBeVisible = boundsContainsCamera || distanceToCamera < profile.visibleDistanceThreshold;
 42
 743            if (isEmissive)
 644                shouldBeVisible |= viewportSize > profile.emissiveSizeThreshold;
 45
 746            if (isOpaque)
 447                shouldBeVisible |= viewportSize > profile.opaqueSizeThreshold;
 48
 749            return shouldBeVisible;
 50        }
 51
 52        /// <summary>
 53        /// Computes the rule used for toggling renderer shadow casting.
 54        /// </summary>
 55        /// <param name="profile">Profile used for size and distance thresholds needed for the rule.</param>
 56        /// <param name="viewportSize">Diagonal viewport size of the renderer.</param>
 57        /// <param name="distanceToCamera">Distance from renderer to camera.</param>
 58        /// <param name="shadowMapTexelSize">Shadow map bounding box size in texels.</param>
 59        /// <returns>True if renderer should have shadow, false otherwise.</returns>
 60        internal static bool TestRendererShadowRule(CullingControllerProfile profile, float viewportSize, float distance
 61        {
 1662            bool shouldHaveShadow = distanceToCamera < profile.shadowDistanceThreshold;
 1663            shouldHaveShadow |= viewportSize > profile.shadowRendererSizeThreshold;
 1664            shouldHaveShadow &= shadowMapTexelSize > profile.shadowMapProjectionSizeThreshold;
 65
 1666            return shouldHaveShadow;
 67        }
 68
 069        internal static bool TestAvatarShadowRule(CullingControllerProfile profile, float avatarDistance) { return avata
 70
 171        private static readonly List<Material> allocMaterialList = new List<Material>(3);
 72        /// <summary>
 73        /// Determines if the given renderer is going to be enqueued at the opaque section of the rendering pipeline.
 74        /// </summary>
 75        /// <param name="renderer">Renderer to be checked.</param>
 76        /// <returns>True if its opaque</returns>
 77        internal static bool IsOpaque(Renderer renderer)
 78        {
 079            renderer.GetSharedMaterials(allocMaterialList);
 080            Material firstMat = allocMaterialList[0];
 81
 082            if (firstMat == null)
 083                return true;
 84
 085            if (firstMat.HasProperty(ShaderUtils.ZWrite) &&
 86                (int) firstMat.GetFloat(ShaderUtils.ZWrite) == 0)
 87            {
 088                return false;
 89            }
 90
 091            return true;
 92        }
 93
 94        /// <summary>
 95        /// Determines if the given renderer has emissive material traits.
 96        /// </summary>
 97        /// <param name="renderer">Renderer to be checked.</param>
 98        /// <returns>True if the renderer is emissive.</returns>
 99        internal static bool IsEmissive(Renderer renderer)
 100        {
 0101            renderer.GetSharedMaterials(allocMaterialList);
 0102            Material firstMat = allocMaterialList[0];
 103
 0104            if (firstMat == null)
 0105                return false;
 106
 0107            if (firstMat.HasProperty(ShaderUtils.EmissionMap) && firstMat.GetTexture(ShaderUtils.EmissionMap) != null)
 0108                return true;
 109
 0110            if (firstMat.HasProperty(ShaderUtils.EmissionColor) && firstMat.GetColor(ShaderUtils.EmissionColor) != Color
 0111                return true;
 112
 0113            return false;
 114        }
 115
 116        /// <summary>
 117        /// ComputeShadowMapTexelSize computes the shadow-map bounding box diagonal texel size
 118        /// for the given bounds size.
 119        /// </summary>
 120        /// <param name="boundsSize">Diagonal bounds size of the object</param>
 121        /// <param name="shadowDistance">Shadow distance as set in the quality settings</param>
 122        /// <param name="shadowMapRes">Shadow map resolution as set in the quality settings (128, 256, etc)</param>
 123        /// <returns>The computed shadow map diagonal texel size for the object.</returns>
 124        /// <remarks>
 125        /// This is calculated by doing the following:
 126        ///
 127        /// - We get the boundsSize to a normalized viewport size.
 128        /// - We multiply the resulting value by the shadow map resolution.
 129        ///
 130        /// To get the viewport size, we assume the shadow distance value is directly correlated by
 131        /// the orthogonal projection size used for rendering the shadow map.
 132        ///
 133        /// We can use the bounds size and shadow distance to obtain the normalized shadow viewport
 134        /// value because both are expressed in world units.
 135        ///
 136        /// After getting the normalized size, we scale it by the shadow map resolution to get the
 137        /// diagonal texel size of the bounds shadow.
 138        ///
 139        /// This leaves us with:
 140        ///     <c>shadowTexelSize = boundsSize / shadow dist * shadow res</c>
 141        ///
 142        /// This is a lazy approximation and most likely will need some refinement in the future.
 143        /// </remarks>
 11144        internal static float ComputeShadowMapTexelSize(float boundsSize, float shadowDistance, float shadowMapRes) { re
 145
 146        public static void DrawBounds(Bounds b, Color color, float delay = 0)
 147        {
 148            // bottom
 0149            var p1 = new Vector3(b.min.x, b.min.y, b.min.z);
 0150            var p2 = new Vector3(b.max.x, b.min.y, b.min.z);
 0151            var p3 = new Vector3(b.max.x, b.min.y, b.max.z);
 0152            var p4 = new Vector3(b.min.x, b.min.y, b.max.z);
 153
 0154            Debug.DrawLine(p1, p2, color, delay);
 0155            Debug.DrawLine(p2, p3, color, delay);
 0156            Debug.DrawLine(p3, p4, color, delay);
 0157            Debug.DrawLine(p4, p1, color, delay);
 158
 159            // top
 0160            var p5 = new Vector3(b.min.x, b.max.y, b.min.z);
 0161            var p6 = new Vector3(b.max.x, b.max.y, b.min.z);
 0162            var p7 = new Vector3(b.max.x, b.max.y, b.max.z);
 0163            var p8 = new Vector3(b.min.x, b.max.y, b.max.z);
 164
 0165            Debug.DrawLine(p5, p6, color, delay);
 0166            Debug.DrawLine(p6, p7, color, delay);
 0167            Debug.DrawLine(p7, p8, color, delay);
 0168            Debug.DrawLine(p8, p5, color, delay);
 169
 170            // sides
 0171            Debug.DrawLine(p1, p5, color, delay);
 0172            Debug.DrawLine(p2, p6, color, delay);
 0173            Debug.DrawLine(p3, p7, color, delay);
 0174            Debug.DrawLine(p4, p8, color, delay);
 0175        }
 176    }
 177}