< 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:52
Uncovered lines:12
Coverable lines:64
Total lines:168
Line coverage:81.2% (52 of 64)
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%330100%
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            {
 655429                return meshRootGameObjectValue;
 30            }
 31
 32            set
 33            {
 35034                meshRootGameObjectValue = value;
 35035                UpdateRenderersCollection();
 35036            }
 37        }
 38
 39        GameObject meshRootGameObjectValue;
 40
 41        public IShape currentShape;
 5443942        public Renderer[] renderers { get; private set; }
 43        public MeshFilter[] meshFilters;
 32244        public HashSet<Collider> colliders = new HashSet<Collider>();
 100945        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        {
 92195            if (meshRootGameObjectValue == null)
 20796                return;
 97
 71498            renderers = meshRootGameObjectValue.GetComponentsInChildren<Renderer>(true);
 71499            meshFilters = meshRootGameObjectValue.GetComponentsInChildren<MeshFilter>(true);
 714100            animation = meshRootGameObjectValue.GetComponentInChildren<Animation>();
 101
 714102            TextMeshPro[] tmpros = meshRootGameObjectValue.GetComponentsInChildren<TextMeshPro>(true);
 103
 714104            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
 714110            RecalculateBounds();
 714111            OnAnyUpdated?.Invoke();
 714112            OnUpdated?.Invoke();
 709113        }
 114
 115        public void RecalculateBounds()
 116        {
 736117            if ((renderers == null || renderers.Length == 0) && colliders.Count == 0)
 118            {
 265119                mergedBoundsValue = new Bounds();
 265120                return;
 121            }
 122
 471123            lastBoundsCalculationPosition = meshRootGameObjectValue.transform.position;
 471124            lastBoundsCalculationScale = meshRootGameObjectValue.transform.lossyScale;
 471125            lastBoundsCalculationRotation = meshRootGameObjectValue.transform.rotation;
 126
 471127            mergedBoundsValue = MeshesInfoUtils.BuildMergedBounds(renderers, colliders);
 471128        }
 129
 130        public void CleanReferences()
 131        {
 215132            OnCleanup?.Invoke();
 215133            meshRootGameObjectValue = null;
 215134            animation = null;
 215135            currentShape = null;
 215136            renderers = null;
 215137            colliders.Clear();
 138
 215139            var arrayLength = meshFilters.Length;
 1004140            for (int i = 0; i < arrayLength; i++)
 141            {
 287142                meshFilters[i] = null;
 143            }
 215144            innerGameObject = null;
 145
 215146            OnCleanup = null;
 215147        }
 148
 149        public void UpdateExistingMeshAtIndex(Mesh mesh, uint meshFilterIndex = 0)
 150        {
 3151            if (meshFilters != null && meshFilters.Length > meshFilterIndex)
 152            {
 3153                meshFilters[meshFilterIndex].sharedMesh = mesh;
 3154                OnUpdated?.Invoke();
 155            }
 156            else
 157            {
 0158                Debug.LogError(
 159                    $"MeshFilter index {meshFilterIndex} out of bounds - MeshesInfo.UpdateExistingMesh failed");
 160            }
 0161        }
 162
 163        public void OverrideRenderers(Renderer[] renderers)
 164        {
 382165            this.renderers = renderers.Where(r => r != null).ToArray();
 191166        }
 167    }
 168}