< Summary

Class:DCL.Models.MeshesInfoUtils
Assembly:MeshesInfo
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/MeshesInfo/MeshesInfoUtils.cs
Covered lines:45
Uncovered lines:1
Coverable lines:46
Total lines:151
Line coverage:97.8% (45 of 46)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuildMergedBounds(...)0%440100%
GetSafeBounds(...)0%2.152066.67%
ComputeTotalTriangles(...)0%660100%
ExtractMeshToTriangleMap(...)0%330100%
MeshesInfoUtils()0%110100%
ExtractUniqueRenderers(...)0%110100%
ExtractUniqueMaterials(...)0%220100%
ExtractUniqueTextures(...)0%220100%
ExtractUniqueMeshes(...)0%770100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/MeshesInfo/MeshesInfoUtils.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using UnityEngine;
 4
 5namespace DCL.Models
 6{
 7    public static class MeshesInfoUtils
 8    {
 9        public static Bounds BuildMergedBounds(Renderer[] renderers)
 10        {
 37411            Bounds bounds = new Bounds();
 12
 173013            for (int i = 0; i < renderers.Length; i++)
 14            {
 49115                if (renderers[i] == null)
 16                    continue;
 17
 49118                if (i == 0)
 37419                    bounds = renderers[i].GetSafeBounds();
 20                else
 11721                    bounds.Encapsulate(renderers[i].GetSafeBounds());
 22            }
 23
 37424            return bounds;
 25        }
 26
 27        /// <summary>
 28        /// This get the renderer bounds with a check to ensure the renderer is at a safe position.
 29        /// If the renderer is too far away from 0,0,0, wasm target ensures a crash.
 30        /// </summary>
 31        /// <param name="renderer"></param>
 32        /// <returns>The bounds value if the value is correct, or a mocked bounds object with clamped values if its too 
 33        public static Bounds GetSafeBounds( this Renderer renderer )
 34        {
 35            // World extents are of 4800 world mts, so this limit far exceeds the world size.
 36            const float POSITION_OVERFLOW_LIMIT = 10000;
 37            const float POSITION_OVERFLOW_LIMIT_SQR = POSITION_OVERFLOW_LIMIT * POSITION_OVERFLOW_LIMIT;
 38
 57939            if ( renderer.transform.position.sqrMagnitude > POSITION_OVERFLOW_LIMIT_SQR )
 040                return new Bounds( Vector3.one * POSITION_OVERFLOW_LIMIT, Vector3.one * 0.1f );
 41
 57942            return renderer.bounds;
 43        }
 44
 45        public static int ComputeTotalTriangles(IEnumerable<Renderer> renderers,
 46            Dictionary<Mesh, int> meshToTriangleCount)
 47        {
 12748            int result = 0;
 49
 90250            foreach ( var renderer in renderers )
 51            {
 52                switch (renderer)
 53                {
 54                    case MeshRenderer r:
 17655                        MeshFilter mf = r.GetComponent<MeshFilter>();
 56
 17657                        if ( mf == null )
 58                            continue;
 59
 17660                        int triangles = meshToTriangleCount[ mf.sharedMesh ];
 17661                        result += triangles;
 17662                        break;
 63                    case SkinnedMeshRenderer skr:
 14864                        result += meshToTriangleCount[ skr.sharedMesh ];
 65                        break;
 66                }
 67            }
 68
 12769            return result;
 70        }
 71
 72        public static Dictionary<Mesh, int> ExtractMeshToTriangleMap(IEnumerable<Mesh> meshes)
 73        {
 1974            Dictionary<Mesh, int> result = new Dictionary<Mesh, int>();
 75
 7676            foreach (var mesh in meshes)
 77            {
 1978                result[mesh] = mesh.triangles.Length;
 79            }
 80
 1981            return result;
 82        }
 83
 184        private static List<int> texIdsCache = new List<int>();
 185        private static List<string> texNameCache = new List<string>();
 86
 87        public static HashSet<Renderer> ExtractUniqueRenderers(GameObject container)
 88        {
 50089            return new HashSet<Renderer>(container.GetComponentsInChildren<Renderer>(true));
 90        }
 91
 92        public static HashSet<Material> ExtractUniqueMaterials(IEnumerable<Renderer> renderers)
 93        {
 12794            return new HashSet<Material>( renderers.SelectMany( (x) =>
 64895                x.sharedMaterials.Where((mat) => mat != null && mat.shader.name != "DCL/FX/Hologram")
 96            ) );
 97        }
 98
 99        public static HashSet<Texture> ExtractUniqueTextures(IEnumerable<Material> materials)
 100        {
 127101            return new HashSet<Texture>(
 102                materials.SelectMany(
 103                    (mat) =>
 104                    {
 191105                        mat.GetTexturePropertyNameIDs(texIdsCache);
 191106                        mat.GetTexturePropertyNames(texNameCache);
 191107                        List<Texture> result = new List<Texture>();
 6972108                        for ( int i = 0; i < texIdsCache.Count; i++ )
 109                        {
 3295110                            var tex = mat.GetTexture(texIdsCache[i]);
 111
 3295112                            if ( tex != null )
 113                            {
 234114                                result.Add(tex);
 115                            }
 116                        }
 117
 191118                        return result;
 119                    } ) );
 120        }
 121
 122        public static HashSet<Mesh> ExtractUniqueMeshes(IEnumerable<Renderer> renderers)
 123        {
 30124            List<Mesh> result = new List<Mesh>();
 125
 168126            foreach ( Renderer renderer in renderers )
 127            {
 128                switch ( renderer )
 129                {
 130                    case SkinnedMeshRenderer skr:
 43131                        if ( skr.sharedMesh == null )
 132                            continue;
 133
 43134                        result.Add(skr.sharedMesh);
 43135                        break;
 136                    case MeshRenderer mr:
 11137                        MeshFilter mf = mr.GetComponent<MeshFilter>();
 138
 11139                        if ( mf.mesh == null )
 140                            continue;
 141
 11142                        result.Add( mf.mesh );
 143                        break;
 144                }
 145            }
 146
 147            // Ensure meshes are unique
 30148            return new HashSet<Mesh>(result);
 149        }
 150    }
 151}