< Summary

Class:DCL.Rendering.CullingControllerUtils
Assembly:CullingController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/Culling/CullingControllerUtils.cs
Covered lines:49
Uncovered lines:3
Coverable lines:52
Total lines:172
Line coverage:94.2% (49 of 52)
Covered branches:0
Total branches:0

Metrics

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

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using UnityEngine;
 3
 4namespace DCL.Rendering
 5{
 6    public static class CullingControllerUtils
 7    {
 8        /// <summary>
 9        /// Computes the rule used for toggling skinned meshes updateWhenOffscreen param.
 10        /// Skinned meshes should be always updated if near the camera to avoid false culling positives on screen edges.
 11        /// </summary>
 12        /// <param name="settings">Any settings object to use thresholds for computing the rule.</param>
 13        /// <param name="distanceToCamera">Mesh distance from camera used for computing the rule.</param>
 14        /// <returns>True if mesh should be updated when offscreen, false if otherwise.</returns>
 15        internal static bool TestSkinnedRendererOffscreenRule(CullingControllerSettings settings, float distanceToCamera
 16        {
 817            bool finalValue = true;
 18
 819            if (settings.enableAnimationCulling)
 20            {
 221                if (distanceToCamera > settings.enableAnimationCullingDistance)
 122                    finalValue = false;
 23            }
 24
 825            return finalValue;
 26        }
 27
 28        /// <summary>
 29        /// Computes the rule used for toggling renderers visibility.
 30        /// </summary>
 31        /// <param name="profile">Profile used for size and distance thresholds needed for the rule.</param>
 32        /// <param name="viewportSize">Diagonal viewport size of the renderer.</param>
 33        /// <param name="distanceToCamera">Distance to camera of the renderer.</param>
 34        /// <param name="boundsContainsCamera">Renderer bounds contains camera?</param>
 35        /// <param name="isOpaque">Renderer is opaque?</param>
 36        /// <param name="isEmissive">Renderer is emissive?</param>
 37        /// <returns>True if renderer should be visible, false if otherwise.</returns>
 38        internal static bool TestRendererVisibleRule(CullingControllerProfile profile, float viewportSize, float distanc
 39        {
 358740            bool shouldBeVisible = distanceToCamera < profile.visibleDistanceThreshold || boundsContainsCamera;
 41
 358742            if (isEmissive)
 239343                shouldBeVisible |= viewportSize > profile.emissiveSizeThreshold;
 44
 358745            if (isOpaque)
 318746                shouldBeVisible |= viewportSize > profile.opaqueSizeThreshold;
 47
 358748            return shouldBeVisible;
 49        }
 50
 51        /// <summary>
 52        /// Computes the rule used for toggling renderer shadow casting.
 53        /// </summary>
 54        /// <param name="profile">Profile used for size and distance thresholds needed for the rule.</param>
 55        /// <param name="viewportSize">Diagonal viewport size of the renderer.</param>
 56        /// <param name="distanceToCamera">Distance from renderer to camera.</param>
 57        /// <param name="shadowMapTexelSize">Shadow map bounding box size in texels.</param>
 58        /// <returns>True if renderer should have shadow, false otherwise.</returns>
 59        internal static bool TestRendererShadowRule(CullingControllerProfile profile, float viewportSize, float distance
 60        {
 360461            bool shouldHaveShadow = distanceToCamera < profile.shadowDistanceThreshold;
 360462            shouldHaveShadow |= viewportSize > profile.shadowRendererSizeThreshold;
 360463            shouldHaveShadow &= shadowMapTexelSize > profile.shadowMapProjectionSizeThreshold;
 360464            return shouldHaveShadow;
 65        }
 66
 067        internal static bool TestAvatarShadowRule(CullingControllerProfile profile, float avatarDistance) { return avata
 68
 69        /// <summary>
 70        /// Determines if the given renderer is going to be enqueued at the opaque section of the rendering pipeline.
 71        /// </summary>
 72        /// <param name="renderer">Renderer to be checked.</param>
 73        /// <returns>True if its opaque</returns>
 74        internal static bool IsOpaque(Renderer renderer)
 75        {
 359976            Material firstMat = renderer.sharedMaterials[0];
 77
 359978            if (firstMat == null)
 579                return true;
 80
 359481            if (firstMat.HasProperty(ShaderUtils.ZWrite) &&
 82                (int) firstMat.GetFloat(ShaderUtils.ZWrite) == 0)
 83            {
 39984                return false;
 85            }
 86
 319587            return true;
 88        }
 89
 90        /// <summary>
 91        /// Determines if the given renderer has emissive material traits.
 92        /// </summary>
 93        /// <param name="renderer">Renderer to be checked.</param>
 94        /// <returns>True if the renderer is emissive.</returns>
 95        internal static bool IsEmissive(Renderer renderer)
 96        {
 359997            Material firstMat = renderer.sharedMaterials[0];
 98
 359999            if (firstMat == null)
 5100                return false;
 101
 3594102            if (firstMat.HasProperty(ShaderUtils.EmissionMap) && firstMat.GetTexture(ShaderUtils.EmissionMap) != null)
 0103                return true;
 104
 3594105            if (firstMat.HasProperty(ShaderUtils.EmissionColor) && firstMat.GetColor(ShaderUtils.EmissionColor) != Color
 2400106                return true;
 107
 1194108            return false;
 109        }
 110
 111        /// <summary>
 112        /// ComputeShadowMapTexelSize computes the shadow-map bounding box diagonal texel size
 113        /// for the given bounds size.
 114        /// </summary>
 115        /// <param name="boundsSize">Diagonal bounds size of the object</param>
 116        /// <param name="shadowDistance">Shadow distance as set in the quality settings</param>
 117        /// <param name="shadowMapRes">Shadow map resolution as set in the quality settings (128, 256, etc)</param>
 118        /// <returns>The computed shadow map diagonal texel size for the object.</returns>
 119        /// <remarks>
 120        /// This is calculated by doing the following:
 121        ///
 122        /// - We get the boundsSize to a normalized viewport size.
 123        /// - We multiply the resulting value by the shadow map resolution.
 124        ///
 125        /// To get the viewport size, we assume the shadow distance value is directly correlated by
 126        /// the orthogonal projection size used for rendering the shadow map.
 127        ///
 128        /// We can use the bounds size and shadow distance to obtain the normalized shadow viewport
 129        /// value because both are expressed in world units.
 130        ///
 131        /// After getting the normalized size, we scale it by the shadow map resolution to get the
 132        /// diagonal texel size of the bounds shadow.
 133        ///
 134        /// This leaves us with:
 135        ///     <c>shadowTexelSize = boundsSize / shadow dist * shadow res</c>
 136        ///
 137        /// This is a lazy approximation and most likely will need some refinement in the future.
 138        /// </remarks>
 0139        internal static float ComputeShadowMapTexelSize(float boundsSize, float shadowDistance, float shadowMapRes) { re
 140
 141        public static void DrawBounds(Bounds b, Color color, float delay = 0)
 142        {
 143            // bottom
 1016144            var p1 = new Vector3(b.min.x, b.min.y, b.min.z);
 1016145            var p2 = new Vector3(b.max.x, b.min.y, b.min.z);
 1016146            var p3 = new Vector3(b.max.x, b.min.y, b.max.z);
 1016147            var p4 = new Vector3(b.min.x, b.min.y, b.max.z);
 148
 1016149            Debug.DrawLine(p1, p2, color, delay);
 1016150            Debug.DrawLine(p2, p3, color, delay);
 1016151            Debug.DrawLine(p3, p4, color, delay);
 1016152            Debug.DrawLine(p4, p1, color, delay);
 153
 154            // top
 1016155            var p5 = new Vector3(b.min.x, b.max.y, b.min.z);
 1016156            var p6 = new Vector3(b.max.x, b.max.y, b.min.z);
 1016157            var p7 = new Vector3(b.max.x, b.max.y, b.max.z);
 1016158            var p8 = new Vector3(b.min.x, b.max.y, b.max.z);
 159
 1016160            Debug.DrawLine(p5, p6, color, delay);
 1016161            Debug.DrawLine(p6, p7, color, delay);
 1016162            Debug.DrawLine(p7, p8, color, delay);
 1016163            Debug.DrawLine(p8, p5, color, delay);
 164
 165            // sides
 1016166            Debug.DrawLine(p1, p5, color, delay);
 1016167            Debug.DrawLine(p2, p6, color, delay);
 1016168            Debug.DrawLine(p3, p7, color, delay);
 1016169            Debug.DrawLine(p4, p8, color, delay);
 1016170        }
 171    }
 172}