< 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        {
 2018            if (currentMesh != null)
 19            {
 2020                GameObject.Destroy(currentMesh);
 2021                currentMesh = null;
 22            }
 2023        }
 24
 025        public Mesh currentMesh { get; protected set; }
 26        private Model previousModel;
 27        private Model cachedModel;
 28
 13229        public ParametrizedShape()
 30        {
 13231            OnAttach += OnShapeAttached;
 13232            OnDetach += OnShapeDetached;
 13233        }
 34
 35        public override void UpdateFromModel(BaseModel newModel)
 36        {
 18337            cachedModel = (Model) newModel;
 18338            base.UpdateFromModel(newModel);
 18339        }
 40
 41        void UpdateRenderer(IDCLEntity entity, Model model = null)
 42        {
 19443            if (model == null)
 13844                model = (T) this.model;
 45
 19446            if (visibilityDirty)
 47            {
 16248                ConfigureVisibility(entity.meshRootGameObject, model.visible, entity.meshesInfo.renderers);
 16249                visibilityDirty = false;
 50            }
 51
 19452            if (collisionsDirty)
 53            {
 16054                CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions, false, entity, Ca
 16055                collisionsDirty = false;
 56            }
 57
 19458            if (entity.meshesInfo.meshFilters.Length > 0 && entity.meshesInfo.meshFilters[0].sharedMesh != currentMesh)
 59            {
 060                entity.meshesInfo.UpdateExistingMeshAtIndex(currentMesh, 0);
 61            }
 19462        }
 63
 64        void OnShapeAttached(IDCLEntity entity)
 65        {
 13866            if (entity == null)
 067                return;
 68
 13869            entity.EnsureMeshGameObject(componentName + " mesh");
 70
 13871            if (currentMesh == null)
 12272                currentMesh = GenerateGeometry();
 73
 13874            MeshFilter meshFilter = entity.meshRootGameObject.AddComponent<MeshFilter>();
 13875            MeshRenderer meshRenderer = entity.meshRootGameObject.AddComponent<MeshRenderer>();
 13876            entity.meshesInfo.renderers = new Renderer[] { meshRenderer };
 13877            entity.meshesInfo.currentShape = this;
 78
 13879            meshFilter.sharedMesh = currentMesh;
 80
 13881            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            {
 13794                meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 95            }
 96
 13897            visibilityDirty = true;
 13898            collisionsDirty = true;
 13899            UpdateRenderer(entity);
 100
 138101            entity.OnShapeUpdated?.Invoke(entity);
 138102        }
 103
 104        void OnShapeDetached(IDCLEntity entity)
 105        {
 137106            if (entity == null || entity.meshRootGameObject == null)
 0107                return;
 108
 137109            if (attachedEntities.Count == 0)
 110            {
 121111                DestroyGeometry();
 121112                Utils.CleanMaterials(entity.meshRootGameObject.GetComponent<Renderer>());
 121113                currentMesh = null;
 114            }
 115
 137116            Utils.SafeDestroy(entity.meshRootGameObject);
 137117            entity.meshesInfo.CleanReferences();
 137118        }
 119
 120        public override IEnumerator ApplyChanges(BaseModel newModelRaw)
 121        {
 183122            var newModel = (T) newModelRaw;
 123
 183124            if (previousModel != null)
 125            {
 51126                visibilityDirty = newModel.visible != previousModel.visible;
 51127                collisionsDirty = newModel.withCollisions != previousModel.withCollisions || newModel.isPointerBlocker !
 128            }
 129
 183130            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.
 183134            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
 183158            previousModel = newModel;
 183159            return null;
 160        }
 161
 162        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 163        {
 138164            if (attachedEntities.Contains(entity))
 0165                return;
 166
 138167            base.AttachTo(entity);
 138168        }
 169
 58170        public override bool IsVisible() { return cachedModel.visible; }
 171
 27172        public override bool HasCollisions() { return cachedModel.withCollisions; }
 173
 0174        protected virtual bool ShouldGenerateNewMesh(BaseShape.Model newModel) { return true; }
 175    }
 176}