< Summary

Class:DCL.Models.MeshesInfoUtils
Assembly:MeshesInfo
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/MeshesInfo/MeshesInfoUtils.cs
Covered lines:51
Uncovered lines:1
Coverable lines:52
Total lines:171
Line coverage:98% (51 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%440100%
ExtractUniqueRenderers(...)0%110100%
ExtractUniqueMaterials(...)0%220100%
ExtractUniqueTextures(...)0%220100%
ExtractUniqueMeshes(...)0%770100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/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        {
 58311            Bounds bounds = new Bounds();
 12
 289813            for (int i = 0; i < renderers.Length; i++)
 14            {
 86615                if (renderers[i] == null)
 16                    continue;
 17
 86618                if (i == 0)
 58319                    bounds = renderers[i].GetSafeBounds();
 20                else
 28321                    bounds.Encapsulate(renderers[i].GetSafeBounds());
 22            }
 23
 58324            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
 95239            if (renderer.transform.position.sqrMagnitude > POSITION_OVERFLOW_LIMIT_SQR)
 040                return new Bounds(Vector3.one * POSITION_OVERFLOW_LIMIT, Vector3.one * 0.1f);
 41
 95242            return renderer.bounds;
 43        }
 44
 45        public static int ComputeTotalTriangles(IEnumerable<Renderer> renderers,
 46            Dictionary<Mesh, int> meshToTriangleCount)
 47        {
 15348            int result = 0;
 49
 110850            foreach (var renderer in renderers)
 51            {
 52                switch (renderer)
 53                {
 54                    case MeshRenderer r:
 20655                        MeshFilter mf = r.GetComponent<MeshFilter>();
 56
 20657                        if (mf == null)
 58                            continue;
 59
 20660                        int triangles = meshToTriangleCount[mf.sharedMesh];
 20661                        result += triangles;
 20662                        break;
 63                    case SkinnedMeshRenderer skr:
 19564                        result += meshToTriangleCount[skr.sharedMesh];
 65                        break;
 66                }
 67            }
 68
 15369            return result;
 70        }
 71
 72        public static Dictionary<Mesh, int> ExtractMeshToTriangleMap(IEnumerable<Mesh> meshes)
 73        {
 3974            Dictionary<Mesh, int> result = new Dictionary<Mesh, int>();
 75
 19876            foreach (var mesh in meshes)
 77            {
 6078                result[mesh] = mesh.triangles.Length;
 79            }
 80
 3981            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        {
 11489            return new HashSet<Animation>(container.GetComponentsInChildren<Animation>(true));
 90        }
 91
 92        public static HashSet<AnimationClip> ExtractUniqueAnimationClips(HashSet<Animation> animations)
 93        {
 10294            HashSet<AnimationClip> result = new HashSet<AnimationClip>();
 95
 22496            foreach (var anim in animations)
 97            {
 4698                foreach (AnimationState state in anim)
 99                {
 13100                    result.Add(state.clip);
 101                }
 102            }
 103
 102104            return result;
 105        }
 106
 107        public static HashSet<Renderer> ExtractUniqueRenderers(GameObject container)
 108        {
 549109            return new HashSet<Renderer>(container.GetComponentsInChildren<Renderer>(true));
 110        }
 111
 112        public static HashSet<Material> ExtractUniqueMaterials(IEnumerable<Renderer> renderers)
 113        {
 153114            return new HashSet<Material>(renderers.SelectMany((x) =>
 816115                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        {
 153121            return new HashSet<Texture>(
 122                materials.SelectMany(
 123                    (mat) =>
 124                    {
 264125                        mat.GetTexturePropertyNameIDs(texIdsCache);
 264126                        mat.GetTexturePropertyNames(texNameCache);
 264127                        List<Texture> result = new List<Texture>();
 9312128                        for (int i = 0; i < texIdsCache.Count; i++)
 129                        {
 4392130                            var tex = mat.GetTexture(texIdsCache[i]);
 131
 4392132                            if (tex != null)
 133                            {
 280134                                result.Add(tex);
 135                            }
 136                        }
 137
 264138                        return result;
 139                    }));
 140        }
 141
 142        public static HashSet<Mesh> ExtractUniqueMeshes(IEnumerable<Renderer> renderers)
 143        {
 51144            List<Mesh> result = new List<Mesh>();
 145
 318146            foreach (Renderer renderer in renderers)
 147            {
 148                switch (renderer)
 149                {
 150                    case SkinnedMeshRenderer skr:
 68151                        if (skr.sharedMesh == null)
 152                            continue;
 153
 68154                        result.Add(skr.sharedMesh);
 68155                        break;
 156                    case MeshRenderer mr:
 40157                        MeshFilter mf = mr.GetComponent<MeshFilter>();
 158
 40159                        if (mf.mesh == null)
 160                            continue;
 161
 40162                        result.Add(mf.mesh);
 163                        break;
 164                }
 165            }
 166
 167            // Ensure meshes are unique
 51168            return new HashSet<Mesh>(result);
 169        }
 170    }
 171}