< 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:33
Uncovered lines:2
Coverable lines:35
Total lines:109
Line coverage:94.2% (33 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuildMergedBounds(...)0%440100%
GetSafeBounds(...)0%220100%
ComputeTotalTriangles(...)0%440100%
ExtractMeshToTriangleMap(...)0%220100%
ExtractMeshes(...)0%5.25080%

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        {
 33311            Bounds bounds = new Bounds();
 12
 166013            for (int i = 0; i < renderers.Length; i++)
 14            {
 49715                if (renderers[i] == null)
 16                    continue;
 17
 49718                if (i == 0)
 33319                    bounds = renderers[i].GetSafeBounds();
 20                else
 16421                    bounds.Encapsulate(renderers[i].GetSafeBounds());
 22            }
 23
 33324            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
 416739            if ( renderer.transform.position.sqrMagnitude > POSITION_OVERFLOW_LIMIT_SQR )
 1140                return new Bounds( Vector3.one * POSITION_OVERFLOW_LIMIT, Vector3.one * 0.1f );
 41
 415642            return renderer.bounds;
 43        }
 44
 45        public static int ComputeTotalTriangles(List<Renderer> renderers, Dictionary<Mesh, int> meshToTriangleCount)
 46        {
 11747            int result = 0;
 48
 78049            for ( int i = 0; i < renderers.Count; i++ )
 50            {
 27351                var r = renderers[i];
 52
 27353                if ( r is MeshRenderer )
 54                {
 16255                    int triangles = meshToTriangleCount[ r.GetComponent<MeshFilter>().sharedMesh ];
 16256                    result += triangles;
 57                }
 58
 27359                if ( r is SkinnedMeshRenderer skinnedMeshRenderer )
 60                {
 11161                    result += meshToTriangleCount[ skinnedMeshRenderer.sharedMesh ];
 62                }
 63            }
 64
 11765            return result;
 66        }
 67
 68        public static Dictionary<Mesh, int> ExtractMeshToTriangleMap(List<Mesh> meshes)
 69        {
 770            Dictionary<Mesh, int> result = new Dictionary<Mesh, int>();
 71
 7072            for ( int i = 0; i < meshes.Count; i++ )
 73            {
 2874                Mesh mesh = meshes[i];
 2875                result[mesh] = mesh.triangles.Length;
 76            }
 77
 778            return result;
 79        }
 80
 81        public static List<Mesh> ExtractMeshes(GameObject gameObject)
 82        {
 1783            List<Mesh> result = new List<Mesh>();
 1784            List<SkinnedMeshRenderer> skrList = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>(true).ToList();
 1785            List<MeshFilter> meshFilterList = gameObject.GetComponentsInChildren<MeshFilter>(true).ToList();
 86
 3487            foreach ( var skr in skrList )
 88            {
 089                if ( skr.sharedMesh == null )
 90                    continue;
 91
 092                result.Add(skr.sharedMesh);
 93            }
 94
 13095            foreach ( var meshFilter in meshFilterList )
 96            {
 4897                if ( meshFilter.mesh == null )
 98                    continue;
 99
 48100                result.Add( meshFilter.mesh );
 101            }
 102
 103            // Ensure meshes are unique
 17104            result = result.Distinct().ToList();
 105
 17106            return result;
 107        }
 108    }
 109}