< 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:46
Uncovered lines:6
Coverable lines:52
Total lines:171
Line coverage:88.4% (46 of 52)
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%
ExtractUniqueAnimations(...)0%110100%
ExtractUniqueAnimationClips(...)0%20400%
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        {
 12948            int result = 0;
 49
 92250            foreach ( var renderer in renderers )
 51            {
 52                switch (renderer)
 53                {
 54                    case MeshRenderer r:
 17755                        MeshFilter mf = r.GetComponent<MeshFilter>();
 56
 17757                        if ( mf == null )
 58                            continue;
 59
 17760                        int triangles = meshToTriangleCount[ mf.sharedMesh ];
 17761                        result += triangles;
 17762                        break;
 63                    case SkinnedMeshRenderer skr:
 15564                        result += meshToTriangleCount[ skr.sharedMesh ];
 65                        break;
 66                }
 67            }
 68
 12969            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<Animation> ExtractUniqueAnimations(GameObject container)
 88        {
 1189            return new HashSet<Animation>(container.GetComponentsInChildren<Animation>(true));
 90        }
 91
 92        public static HashSet<AnimationClip> ExtractUniqueAnimationClips(HashSet<Animation> animations)
 93        {
 094            HashSet<AnimationClip> result = new HashSet<AnimationClip>();
 95
 096            foreach (var anim in animations)
 97            {
 098                foreach (AnimationState state in anim)
 99                {
 0100                    result.Add(state.clip);
 101                }
 102            }
 103
 0104            return result;
 105        }
 106
 107        public static HashSet<Renderer> ExtractUniqueRenderers(GameObject container)
 108        {
 502109            return new HashSet<Renderer>(container.GetComponentsInChildren<Renderer>(true));
 110        }
 111
 112        public static HashSet<Material> ExtractUniqueMaterials(IEnumerable<Renderer> renderers)
 113        {
 129114            return new HashSet<Material>( renderers.SelectMany( (x) =>
 664115                x.sharedMaterials.Where((mat) => mat != null && mat.shader.name != "DCL/FX/Hologram")
 116            ) );
 117        }
 118
 119        public static HashSet<Texture> ExtractUniqueTextures(IEnumerable<Material> materials)
 120        {
 129121            return new HashSet<Texture>(
 122                materials.SelectMany(
 123                    (mat) =>
 124                    {
 199125                        mat.GetTexturePropertyNameIDs(texIdsCache);
 199126                        mat.GetTexturePropertyNames(texNameCache);
 199127                        List<Texture> result = new List<Texture>();
 7276128                        for ( int i = 0; i < texIdsCache.Count; i++ )
 129                        {
 3439130                            var tex = mat.GetTexture(texIdsCache[i]);
 131
 3439132                            if ( tex != null )
 133                            {
 235134                                result.Add(tex);
 135                            }
 136                        }
 137
 199138                        return result;
 139                    } ) );
 140        }
 141
 142        public static HashSet<Mesh> ExtractUniqueMeshes(IEnumerable<Renderer> renderers)
 143        {
 30144            List<Mesh> result = new List<Mesh>();
 145
 168146            foreach ( Renderer renderer in renderers )
 147            {
 148                switch ( renderer )
 149                {
 150                    case SkinnedMeshRenderer skr:
 43151                        if ( skr.sharedMesh == null )
 152                            continue;
 153
 43154                        result.Add(skr.sharedMesh);
 43155                        break;
 156                    case MeshRenderer mr:
 11157                        MeshFilter mf = mr.GetComponent<MeshFilter>();
 158
 11159                        if ( mf.mesh == null )
 160                            continue;
 161
 11162                        result.Add( mf.mesh );
 163                        break;
 164                }
 165            }
 166
 167            // Ensure meshes are unique
 30168            return new HashSet<Mesh>(result);
 169        }
 170    }
 171}