< 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:96
Uncovered lines:7
Coverable lines:103
Total lines:229
Line coverage:93.2% (96 of 103)
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%
OnShapeFinishedLoading(...)0%220100%
OnShapeDetached(...)0%4.024090%
ApplyChanges(...)0%770100%
AttachTo(...)0%2.062075%
IsVisible()0%110100%
HasCollisions()0%110100%
ShouldGenerateNewMesh(...)0%2100%
RemoveRendereableFromDataStore(...)0%220100%
AddRendereableToDataStore(...)0%220100%

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    {
 14015        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        {
 2826            if (currentMesh == null)
 027                return;
 28
 2829            Object.Destroy(currentMesh);
 2830            currentMesh = null;
 2831        }
 32
 033        public Mesh currentMesh { get; protected set; }
 34        private Model previousModel;
 35        private Model cachedModel;
 36
 14037        public ParametrizedShape()
 38        {
 14039            OnAttach += OnShapeAttached;
 14040            OnDetach += OnShapeDetached;
 14041        }
 42
 43        public override void UpdateFromModel(BaseModel newModel)
 44        {
 19145            cachedModel = (Model) newModel;
 19146            base.UpdateFromModel(newModel);
 19147        }
 48
 49        void UpdateRenderer(IDCLEntity entity, Model model = null)
 50        {
 20251            if (model == null)
 14652                model = (T) this.model;
 53
 20254            if (visibilityDirty)
 55            {
 17056                ConfigureVisibility(entity.meshRootGameObject, model.visible, entity.meshesInfo.renderers);
 17057                visibilityDirty = false;
 58            }
 59
 20260            if (collisionsDirty)
 61            {
 16862                CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions, false, entity, Ca
 16863                collisionsDirty = false;
 64            }
 65
 20266            if (entity.meshesInfo.meshFilters.Length > 0 && entity.meshesInfo.meshFilters[0].sharedMesh != currentMesh)
 67            {
 168                entity.meshesInfo.UpdateExistingMeshAtIndex(currentMesh, 0);
 69            }
 70
 71            // First we remove the old rendereable, then we compute and add the new one.
 20272            RemoveRendereableFromDataStore(entity);
 20273            AddRendereableToDataStore(entity);
 20274        }
 75
 76        void OnShapeAttached(IDCLEntity entity)
 77        {
 14678            if (entity == null)
 079                return;
 80
 14681            entity.EnsureMeshGameObject(componentName + " mesh");
 82
 14683            if (currentMesh == null)
 84            {
 13085                currentMesh = GenerateGeometry();
 86            }
 87
 14688            MeshFilter meshFilter = entity.meshRootGameObject.AddComponent<MeshFilter>();
 14689            MeshRenderer meshRenderer = entity.meshRootGameObject.AddComponent<MeshRenderer>();
 90
 14691            entity.meshesInfo.renderers = new Renderer[] { meshRenderer };
 14692            entity.meshesInfo.currentShape = this;
 93
 14694            meshFilter.sharedMesh = currentMesh;
 95
 14696            if (Configuration.ParcelSettings.VISUAL_LOADING_ENABLED)
 97            {
 198                MaterialTransitionController transition = entity.meshRootGameObject.AddComponent<MaterialTransitionContr
 199                Material finalMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 1100                transition.delay = 0;
 1101                transition.useHologram = false;
 1102                transition.fadeThickness = 20;
 1103                transition.OnDidFinishLoading(finalMaterial);
 104
 3105                transition.onFinishedLoading += () => { OnShapeFinishedLoading(entity); };
 1106            }
 107            else
 108            {
 145109                meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 110            }
 111
 146112            visibilityDirty = true;
 146113            collisionsDirty = true;
 146114            UpdateRenderer(entity);
 146115            OnShapeFinishedLoading(entity);
 146116        }
 117
 118        void OnShapeFinishedLoading(IDCLEntity entity)
 119        {
 147120            entity.OnShapeUpdated?.Invoke(entity);
 147121        }
 122
 123        void OnShapeDetached(IDCLEntity entity)
 124        {
 144125            if (entity == null || entity.meshRootGameObject == null)
 0126                return;
 127
 144128            if (attachedEntities.Count == 0)
 129            {
 128130                DestroyGeometry();
 128131                Utils.CleanMaterials(entity.meshRootGameObject.GetComponent<Renderer>());
 128132                currentMesh = null;
 133            }
 134
 144135            Utils.SafeDestroy(entity.meshRootGameObject);
 144136            entity.meshesInfo.CleanReferences();
 137
 144138            RemoveRendereableFromDataStore(entity);
 144139        }
 140
 141        public override IEnumerator ApplyChanges(BaseModel newModelRaw)
 142        {
 191143            var newModel = (T) newModelRaw;
 144
 191145            if (previousModel != null)
 146            {
 51147                visibilityDirty = newModel.visible != previousModel.visible;
 51148                collisionsDirty = newModel.withCollisions != previousModel.withCollisions || newModel.isPointerBlocker !
 149            }
 150
 191151            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.
 191155            if (attachedEntities.Count > 0)
 156            {
 40157                if (shouldGenerateMesh)
 158                {
 2159                    DestroyGeometry();
 2160                    currentMesh = GenerateGeometry();
 161                }
 162
 40163                using (var iterator = attachedEntities.GetEnumerator())
 164                {
 40165                    bool cachedVisibilityDirty = visibilityDirty;
 40166                    bool cachedCollisionDirty = collisionsDirty;
 96167                    while (iterator.MoveNext())
 168                    {
 169                        //NOTE(Alex): Since UpdateRenderer updates the dirty flags as well we have to make sure every en
 170                        //            gets updated accordingly to the original flags.
 56171                        visibilityDirty = cachedVisibilityDirty;
 56172                        collisionsDirty = cachedCollisionDirty;
 173
 56174                        var entity = iterator.Current;
 56175                        UpdateRenderer(entity, newModel);
 176
 56177                        entity.OnShapeUpdated?.Invoke(entity);
 178                    }
 40179                }
 180            }
 181
 191182            previousModel = newModel;
 191183            return null;
 184        }
 185
 186        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 187        {
 146188            if (attachedEntities.Contains(entity))
 0189                return;
 190
 146191            base.AttachTo(entity);
 146192        }
 193
 61194        public override bool IsVisible() { return cachedModel.visible; }
 195
 26196        public override bool HasCollisions() { return cachedModel.withCollisions; }
 197
 0198        protected virtual bool ShouldGenerateNewMesh(BaseShape.Model newModel) { return true; }
 199
 200        private void RemoveRendereableFromDataStore(IDCLEntity entity)
 201        {
 346202            if ( attachedRendereables.ContainsKey(entity) )
 203            {
 200204                DataStore.i.sceneWorldObjects.RemoveRendereable(entity, attachedRendereables[entity]);
 200205                attachedRendereables.Remove(entity);
 206            }
 346207        }
 208
 209        private void AddRendereableToDataStore(IDCLEntity entity)
 210        {
 202211            int triangleCount = currentMesh.triangles.Length;
 212
 202213            var newRendereable = new Rendereable()
 214            {
 215                renderers = new List<Renderer>(entity.meshesInfo.renderers),
 216                container = entity.meshRootGameObject,
 217                totalTriangleCount = triangleCount,
 218                meshes = new List<Mesh>() { currentMesh },
 219                meshToTriangleCount = new Dictionary<Mesh, int>() { { currentMesh, triangleCount } }
 220            };
 221
 202222            if ( !attachedRendereables.ContainsKey(entity) )
 223            {
 202224                attachedRendereables.Add(entity, newRendereable);
 202225                DataStore.i.sceneWorldObjects.AddRendereable(entity, newRendereable);
 226            }
 202227        }
 228    }
 229}