< Summary

Class:DCL.Components.ParametrizedShape[T]
Assembly:DCL.Components.ParametrizedShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/ParametrizedShapes/ParametrizedShape.cs
Covered lines:109
Uncovered lines:8
Coverable lines:117
Total lines:250
Line coverage:93.1% (109 of 117)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ParametrizedShape()0%110100%
GenerateGeometry()0%2100%
DestroyGeometry()0%2.032080%
UpdateFromModel(...)0%110100%
UpdateRenderer(...)0%660100%
OnShapeAttached(...)0%44096.3%
OnShapeFinishedLoading(...)0%220100%
OnShapeDetached(...)0%4.024090%
ApplyChanges(...)0%990100%
AttachTo(...)0%2.062075%
IsVisible()0%110100%
HasCollisions()0%110100%
ShouldGenerateNewMesh(...)0%2100%
RemoveRendereableFromDataStore(...)0%220100%
AddRendereableToDataStore(...)0%2.012088.89%

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 System.Collections.Generic;
 6using System.Linq;
 7using UnityEditor;
 8using UnityEngine;
 9
 10namespace DCL.Components
 11{
 12    public abstract class ParametrizedShape<T> : BaseShape
 13        where T : BaseShape.Model, new()
 14    {
 17315        public Dictionary<IDCLEntity, Rendereable> attachedRendereables = new Dictionary<IDCLEntity, Rendereable>();
 16        bool visibilityDirty = false;
 17        bool collisionsDirty = false;
 18
 19        public virtual Mesh GenerateGeometry()
 20        {
 021            return null;
 22        }
 23
 24        protected virtual void DestroyGeometry()
 25        {
 3626            if (currentMesh == null)
 027                return;
 28
 3629            Object.Destroy(currentMesh);
 3630            currentMesh = null;
 3631        }
 32
 033        public Mesh currentMesh { get; protected set; }
 34        private Model previousModel;
 35        private Model cachedModel;
 36
 17337        public ParametrizedShape()
 38        {
 17339            OnAttach += OnShapeAttached;
 17340            OnDetach += OnShapeDetached;
 17341        }
 42
 43        public override void UpdateFromModel(BaseModel newModel)
 44        {
 22945            cachedModel = (Model) newModel;
 22946            base.UpdateFromModel(newModel);
 22947        }
 48
 49        void UpdateRenderer(IDCLEntity entity, Model model = null)
 50        {
 24951            if (model == null)
 18852                model = (T) this.model;
 53
 24954            if (visibilityDirty)
 55            {
 21356                ConfigureVisibility(entity.meshRootGameObject, model.visible, entity.meshesInfo.renderers);
 21357                visibilityDirty = false;
 58            }
 59
 24960            if (collisionsDirty)
 61            {
 21162                CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions, false, entity, Ca
 21163                collisionsDirty = false;
 64            }
 65
 24966            if (entity.meshesInfo.meshFilters.Length > 0 && entity.meshesInfo.meshFilters[0].sharedMesh != currentMesh)
 67            {
 368                entity.meshesInfo.UpdateExistingMeshAtIndex(currentMesh, 0);
 69            }
 24970        }
 71
 72        void OnShapeAttached(IDCLEntity entity)
 73        {
 18874            if (entity == null)
 075                return;
 76
 77            // First we remove the old rendereable, then we compute and add the new one.
 18878            RemoveRendereableFromDataStore(entity);
 79
 18880            entity.EnsureMeshGameObject(componentName + " mesh");
 81
 18882            if (currentMesh == null)
 83            {
 16384                currentMesh = GenerateGeometry();
 85            }
 86
 18887            MeshFilter meshFilter = entity.meshRootGameObject.AddComponent<MeshFilter>();
 18888            MeshRenderer meshRenderer = entity.meshRootGameObject.AddComponent<MeshRenderer>();
 89
 18890            entity.meshesInfo.renderers = new Renderer[] { meshRenderer };
 18891            entity.meshesInfo.currentShape = this;
 92
 18893            meshFilter.sharedMesh = currentMesh;
 94
 18895            if (Configuration.ParcelSettings.VISUAL_LOADING_ENABLED)
 96            {
 197                MaterialTransitionController transition = entity.meshRootGameObject.AddComponent<MaterialTransitionContr
 198                Material finalMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 199                transition.delay = 0;
 1100                transition.useHologram = false;
 1101                transition.fadeThickness = 20;
 1102                transition.OnDidFinishLoading(finalMaterial);
 103
 3104                transition.onFinishedLoading += () => { OnShapeFinishedLoading(entity); };
 1105            }
 106            else
 107            {
 187108                meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 109            }
 110
 188111            visibilityDirty = true;
 188112            collisionsDirty = true;
 188113            UpdateRenderer(entity);
 188114            OnShapeFinishedLoading(entity);
 188115            AddRendereableToDataStore(entity);
 188116        }
 117
 118        void OnShapeFinishedLoading(IDCLEntity entity)
 119        {
 189120            entity.OnShapeUpdated?.Invoke(entity);
 189121        }
 122
 123        void OnShapeDetached(IDCLEntity entity)
 124        {
 61125            if (entity == null || entity.meshRootGameObject == null)
 0126                return;
 127
 61128            if (attachedEntities.Count == 0)
 129            {
 52130                DestroyGeometry();
 52131                Utils.CleanMaterials(entity.meshRootGameObject.GetComponent<Renderer>());
 52132                currentMesh = null;
 133            }
 134
 61135            Utils.SafeDestroy(entity.meshRootGameObject);
 61136            entity.meshesInfo.CleanReferences();
 137
 61138            RemoveRendereableFromDataStore(entity);
 61139        }
 140
 141        public override IEnumerator ApplyChanges(BaseModel newModelRaw)
 142        {
 229143            var newModel = (T) newModelRaw;
 144
 229145            if (previousModel != null)
 146            {
 56147                visibilityDirty = newModel.visible != previousModel.visible;
 56148                collisionsDirty = newModel.withCollisions != previousModel.withCollisions || newModel.isPointerBlocker !
 149            }
 150
 229151            bool shouldGenerateMesh = ShouldGenerateNewMesh(previousModel);
 152
 153            //NOTE(Brian): Only generate meshes here if they already are attached to something.
 154            //             Otherwise, the mesh will be created on the OnShapeAttached.
 229155            if (attachedEntities.Count > 0)
 156            {
 45157                using (var iterator = attachedEntities.GetEnumerator())
 158                {
 106159                    while (iterator.MoveNext())
 160                    {
 61161                        var entity = iterator.Current;
 61162                        RemoveRendereableFromDataStore(entity);
 163                    }
 45164                }
 165
 45166                if (shouldGenerateMesh)
 167                {
 6168                    DestroyGeometry();
 6169                    currentMesh = GenerateGeometry();
 170                }
 171
 45172                using (var iterator = attachedEntities.GetEnumerator())
 173                {
 45174                    bool cachedVisibilityDirty = visibilityDirty;
 45175                    bool cachedCollisionDirty = collisionsDirty;
 106176                    while (iterator.MoveNext())
 177                    {
 178                        //NOTE(Alex): Since UpdateRenderer updates the dirty flags as well we have to make sure every en
 179                        //            gets updated accordingly to the original flags.
 61180                        visibilityDirty = cachedVisibilityDirty;
 61181                        collisionsDirty = cachedCollisionDirty;
 182
 61183                        var entity = iterator.Current;
 61184                        UpdateRenderer(entity, newModel);
 185
 61186                        entity.OnShapeUpdated?.Invoke(entity);
 187                    }
 45188                }
 189
 45190                using (var iterator = attachedEntities.GetEnumerator())
 191                {
 106192                    while (iterator.MoveNext())
 193                    {
 61194                        var entity = iterator.Current;
 61195                        AddRendereableToDataStore(entity);
 196                    }
 45197                }
 198            }
 199
 229200            previousModel = newModel;
 229201            return null;
 202        }
 203
 204        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 205        {
 188206            if (attachedEntities.Contains(entity))
 0207                return;
 208
 188209            base.AttachTo(entity);
 188210        }
 211
 65212        public override bool IsVisible() { return cachedModel.visible; }
 213
 31214        public override bool HasCollisions() { return cachedModel.withCollisions; }
 215
 0216        protected virtual bool ShouldGenerateNewMesh(BaseShape.Model newModel) { return true; }
 217
 218        private void RemoveRendereableFromDataStore(IDCLEntity entity)
 219        {
 310220            if (!attachedRendereables.ContainsKey(entity))
 188221                return;
 222
 122223            DataStore.i.sceneWorldObjects.RemoveRendereable(entity.scene.sceneData.id, attachedRendereables[entity]);
 122224            attachedRendereables.Remove(entity);
 122225        }
 226
 227        private void AddRendereableToDataStore(IDCLEntity entity)
 228        {
 249229            if (attachedRendereables.ContainsKey(entity))
 0230                return;
 231
 249232            int triangleCount = currentMesh.triangles.Length;
 233
 249234            var newRendereable =
 235                new Rendereable()
 236                {
 237                    container = entity.meshRootGameObject,
 238                    totalTriangleCount = triangleCount,
 239                    meshes = new HashSet<Mesh>() { currentMesh },
 240                    meshToTriangleCount = new Dictionary<Mesh, int>() { { currentMesh, triangleCount } }
 241                };
 242
 249243            newRendereable.renderers = MeshesInfoUtils.ExtractUniqueRenderers(entity.meshRootGameObject);
 249244            newRendereable.ownerId = entity.entityId;
 245
 249246            attachedRendereables.Add(entity, newRendereable);
 249247            DataStore.i.sceneWorldObjects.AddRendereable(entity.scene.sceneData.id, newRendereable);
 249248        }
 249    }
 250}