< Summary

Class:DCL.Components.ParametrizedShape[T]
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/ParametrizedShapes/ParametrizedShape.cs
Covered lines:78
Uncovered lines:6
Coverable lines:84
Total lines:176
Line coverage:92.8% (78 of 84)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DestroyGeometry()0%220100%
ParametrizedShape()0%110100%
UpdateFromModel(...)0%110100%
UpdateRenderer(...)0%6.036090.91%
OnShapeAttached(...)0%55096%
OnShapeDetached(...)0%4.024088.89%
ApplyChanges(...)0%770100%
AttachTo(...)0%2.062075%
IsVisible()0%110100%
HasCollisions()0%110100%
ShouldGenerateNewMesh(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/ParametrizedShapes/ParametrizedShape.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Helpers;
 3using DCL.Models;
 4using System.Collections;
 5using UnityEngine;
 6
 7namespace DCL.Components
 8{
 9    public abstract class ParametrizedShape<T> : BaseShape where T : BaseShape.Model, new()
 10    {
 11        bool visibilityDirty = false;
 12        bool collisionsDirty = false;
 13
 14        public abstract Mesh GenerateGeometry();
 15
 16        protected virtual void DestroyGeometry()
 17        {
 2718            if (currentMesh != null)
 19            {
 2720                GameObject.Destroy(currentMesh);
 2721                currentMesh = null;
 22            }
 2723        }
 24
 025        public Mesh currentMesh { get; protected set; }
 26        private Model previousModel;
 27        private Model cachedModel;
 28
 13929        public ParametrizedShape()
 30        {
 13931            OnAttach += OnShapeAttached;
 13932            OnDetach += OnShapeDetached;
 13933        }
 34
 35        public override void UpdateFromModel(BaseModel newModel)
 36        {
 19037            cachedModel = (Model) newModel;
 19038            base.UpdateFromModel(newModel);
 19039        }
 40
 41        void UpdateRenderer(IDCLEntity entity, Model model = null)
 42        {
 20143            if (model == null)
 14544                model = (T) this.model;
 45
 20146            if (visibilityDirty)
 47            {
 16948                ConfigureVisibility(entity.meshRootGameObject, model.visible, entity.meshesInfo.renderers);
 16949                visibilityDirty = false;
 50            }
 51
 20152            if (collisionsDirty)
 53            {
 16754                CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions, false, entity, Ca
 16755                collisionsDirty = false;
 56            }
 57
 20158            if (entity.meshesInfo.meshFilters.Length > 0 && entity.meshesInfo.meshFilters[0].sharedMesh != currentMesh)
 59            {
 060                entity.meshesInfo.UpdateExistingMeshAtIndex(currentMesh, 0);
 61            }
 20162        }
 63
 64        void OnShapeAttached(IDCLEntity entity)
 65        {
 14566            if (entity == null)
 067                return;
 68
 14569            entity.EnsureMeshGameObject(componentName + " mesh");
 70
 14571            if (currentMesh == null)
 12972                currentMesh = GenerateGeometry();
 73
 14574            MeshFilter meshFilter = entity.meshRootGameObject.AddComponent<MeshFilter>();
 14575            MeshRenderer meshRenderer = entity.meshRootGameObject.AddComponent<MeshRenderer>();
 14576            entity.meshesInfo.renderers = new Renderer[] { meshRenderer };
 14577            entity.meshesInfo.currentShape = this;
 78
 14579            meshFilter.sharedMesh = currentMesh;
 80
 14581            if (Configuration.ParcelSettings.VISUAL_LOADING_ENABLED)
 82            {
 183                MaterialTransitionController transition = entity.meshRootGameObject.AddComponent<MaterialTransitionContr
 184                Material finalMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 185                transition.delay = 0;
 186                transition.useHologram = false;
 187                transition.fadeThickness = 20;
 188                transition.OnDidFinishLoading(finalMaterial);
 89
 390                transition.onFinishedLoading += () => { entity.OnShapeUpdated?.Invoke(entity); };
 191            }
 92            else
 93            {
 14494                meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 95            }
 96
 14597            visibilityDirty = true;
 14598            collisionsDirty = true;
 14599            UpdateRenderer(entity);
 100
 145101            entity.OnShapeUpdated?.Invoke(entity);
 145102        }
 103
 104        void OnShapeDetached(IDCLEntity entity)
 105        {
 144106            if (entity == null || entity.meshRootGameObject == null)
 0107                return;
 108
 144109            if (attachedEntities.Count == 0)
 110            {
 128111                DestroyGeometry();
 128112                Utils.CleanMaterials(entity.meshRootGameObject.GetComponent<Renderer>());
 128113                currentMesh = null;
 114            }
 115
 144116            Utils.SafeDestroy(entity.meshRootGameObject);
 144117            entity.meshesInfo.CleanReferences();
 144118        }
 119
 120        public override IEnumerator ApplyChanges(BaseModel newModelRaw)
 121        {
 190122            var newModel = (T) newModelRaw;
 123
 190124            if (previousModel != null)
 125            {
 51126                visibilityDirty = newModel.visible != previousModel.visible;
 51127                collisionsDirty = newModel.withCollisions != previousModel.withCollisions || newModel.isPointerBlocker !
 128            }
 129
 190130            bool shouldGenerateMesh = ShouldGenerateNewMesh(previousModel);
 131
 132            //NOTE(Brian): Only generate meshes here if they already are attached to something.
 133            //             Otherwise, the mesh will be created on the OnShapeAttached.
 190134            if (attachedEntities.Count > 0)
 135            {
 40136                if (shouldGenerateMesh)
 2137                    currentMesh = GenerateGeometry();
 138
 40139                using (var iterator = attachedEntities.GetEnumerator())
 140                {
 40141                    bool cachedVisibilityDirty = visibilityDirty;
 40142                    bool cachedCollisionDirty = collisionsDirty;
 96143                    while (iterator.MoveNext())
 144                    {
 145                        //NOTE(Alex): Since UpdateRenderer updates the dirty flags as well we have to make sure every en
 146                        //            gets updated accordingly to the original flags.
 56147                        visibilityDirty = cachedVisibilityDirty;
 56148                        collisionsDirty = cachedCollisionDirty;
 149
 56150                        var entity = iterator.Current;
 56151                        UpdateRenderer(entity, newModel);
 152
 56153                        entity.OnShapeUpdated?.Invoke(entity);
 154                    }
 40155                }
 156            }
 157
 190158            previousModel = newModel;
 190159            return null;
 160        }
 161
 162        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 163        {
 145164            if (attachedEntities.Contains(entity))
 0165                return;
 166
 145167            base.AttachTo(entity);
 145168        }
 169
 65170        public override bool IsVisible() { return cachedModel.visible; }
 171
 30172        public override bool HasCollisions() { return cachedModel.withCollisions; }
 173
 0174        protected virtual bool ShouldGenerateNewMesh(BaseShape.Model newModel) { return true; }
 175    }
 176}