< Summary

Class:DCL.Models.MeshesInfo
Assembly:MeshesInfo
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/MeshesInfo/MeshesInfo.cs
Covered lines:46
Uncovered lines:12
Coverable lines:58
Total lines:158
Line coverage:79.3% (46 of 58)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MeshesInfo()0%110100%
UpdateRenderersCollection(...)0%20400%
UpdateRenderersCollection()0%770100%
RecalculateBounds()0%440100%
CleanReferences()0%220100%
UpdateExistingMeshAtIndex(...)0%5.024060%
OverrideRenderers(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/MeshesInfo/MeshesInfo.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using DCL.Components;
 5using TMPro;
 6using UnityEngine;
 7
 8namespace DCL.Models
 9{
 10    /// <summary>
 11    /// (NOTE) Kinerius: this class is a data holder that was made in the past for multiple systems to have access to th
 12    /// This has a big architectural issue where if we decide to remove any component (renderer, mesh filter, collider) 
 13    /// from anywhere within the system, we have to explicitly update this class values, it makes no sense,
 14    /// we need to reconsider this class functionality and purpose since it has an ambiguous design and it might be the 
 15    /// </summary>
 16    [Serializable]
 17    public class MeshesInfo
 18    {
 19        public static event Action OnAnyUpdated;
 20        public event Action OnUpdated;
 21        public event Action OnCleanup;
 22
 23        public GameObject innerGameObject;
 24
 25        public GameObject meshRootGameObject
 26        {
 27            get
 28            {
 655829                return meshRootGameObjectValue;
 30            }
 31
 32            set
 33            {
 35134                meshRootGameObjectValue = value;
 35135                UpdateRenderersCollection();
 35136            }
 37        }
 38
 39        GameObject meshRootGameObjectValue;
 40
 41        public IShape currentShape;
 4942542        public Renderer[] renderers { get; private set; }
 43        public MeshFilter[] meshFilters;
 31844        public HashSet<Collider> colliders = new HashSet<Collider>();
 79645        public Animation animation { get; private set; }
 46
 47        Vector3 lastBoundsCalculationPosition;
 48        Vector3 lastBoundsCalculationScale;
 49        Quaternion lastBoundsCalculationRotation;
 50        Bounds mergedBoundsValue;
 51
 52        public Bounds mergedBounds
 53        {
 54            get
 55            {
 24456                if (meshRootGameObject == null) { RecalculateBounds(); }
 57                else
 58                {
 24459                    if (meshRootGameObject.transform.position != lastBoundsCalculationPosition)
 60                    {
 4761                        mergedBoundsValue.center += meshRootGameObject.transform.position - lastBoundsCalculationPositio
 4762                        lastBoundsCalculationPosition = meshRootGameObject.transform.position;
 63                    }
 64
 24465                    if (meshRootGameObject.transform.lossyScale != lastBoundsCalculationScale ||
 66                        meshRootGameObject.transform.rotation != lastBoundsCalculationRotation)
 2267                        RecalculateBounds();
 68                }
 69
 24470                return mergedBoundsValue;
 71            }
 72
 73            set
 74            {
 075                mergedBoundsValue = value;
 076            }
 77        }
 78
 79        public void UpdateRenderersCollection(Renderer[] renderers, MeshFilter[] meshFilters, Animation animation = null
 80        {
 081            if (meshRootGameObjectValue != null)
 82            {
 083                this.renderers = renderers;
 084                this.meshFilters = meshFilters;
 085                this.animation = animation;
 86
 087                RecalculateBounds();
 088                OnAnyUpdated?.Invoke();
 089                OnUpdated?.Invoke();
 90            }
 091        }
 92
 93        public void UpdateRenderersCollection()
 94        {
 92495            if (meshRootGameObjectValue == null)
 20896                return;
 97
 71698            renderers = meshRootGameObjectValue.GetComponentsInChildren<Renderer>(true);
 71699            meshFilters = meshRootGameObjectValue.GetComponentsInChildren<MeshFilter>(true);
 716100            animation = meshRootGameObjectValue.GetComponentInChildren<Animation>();
 101
 716102            TextMeshPro[] tmpros = meshRootGameObjectValue.GetComponentsInChildren<TextMeshPro>(true);
 103
 716104            if (tmpros.Length > 0)
 105            {
 52106                renderers = renderers.Union(tmpros.Select(x => x.renderer)).ToArray();
 52107                meshFilters = meshFilters.Union(tmpros.Select(x => x.meshFilter)).ToArray();
 108            }
 109
 716110            RecalculateBounds();
 716111            OnAnyUpdated?.Invoke();
 716112            OnUpdated?.Invoke();
 711113        }
 114
 115        public void RecalculateBounds()
 116        {
 738117            if ((renderers == null || renderers.Length == 0) && colliders.Count == 0)
 118            {
 266119                mergedBoundsValue = new Bounds();
 266120                return;
 121            }
 122
 472123            lastBoundsCalculationPosition = meshRootGameObjectValue.transform.position;
 472124            lastBoundsCalculationScale = meshRootGameObjectValue.transform.lossyScale;
 472125            lastBoundsCalculationRotation = meshRootGameObjectValue.transform.rotation;
 126
 472127            mergedBoundsValue = MeshesInfoUtils.BuildMergedBounds(renderers, colliders);
 472128        }
 129
 130        public void CleanReferences()
 131        {
 216132            OnCleanup?.Invoke();
 216133            meshRootGameObjectValue = null;
 216134            currentShape = null;
 216135            renderers = null;
 216136            colliders.Clear();
 216137        }
 138
 139        public void UpdateExistingMeshAtIndex(Mesh mesh, uint meshFilterIndex = 0)
 140        {
 3141            if (meshFilters != null && meshFilters.Length > meshFilterIndex)
 142            {
 3143                meshFilters[meshFilterIndex].sharedMesh = mesh;
 3144                OnUpdated?.Invoke();
 145            }
 146            else
 147            {
 0148                Debug.LogError(
 149                    $"MeshFilter index {meshFilterIndex} out of bounds - MeshesInfo.UpdateExistingMesh failed");
 150            }
 0151        }
 152
 153        public void OverrideRenderers(Renderer[] renderers)
 154        {
 384155            this.renderers = renderers.Where(r => r != null).ToArray();
 192156        }
 157    }
 158}