< 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    {
 15415        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        {
 2326            if (currentMesh == null)
 027                return;
 28
 2329            Object.Destroy(currentMesh);
 2330            currentMesh = null;
 2331        }
 32
 033        public Mesh currentMesh { get; protected set; }
 34        private Model previousModel;
 35        private Model cachedModel;
 36
 15437        public ParametrizedShape()
 38        {
 15439            OnAttach += OnShapeAttached;
 15440            OnDetach += OnShapeDetached;
 15441        }
 42
 43        public override void UpdateFromModel(BaseModel newModel)
 44        {
 20945            cachedModel = (Model) newModel;
 20946            base.UpdateFromModel(newModel);
 20947        }
 48
 49        void UpdateRenderer(IDCLEntity entity, Model model = null)
 50        {
 22051            if (model == null)
 16052                model = (T) this.model;
 53
 22054            if (visibilityDirty)
 55            {
 18556                ConfigureVisibility(entity.meshRootGameObject, model.visible, entity.meshesInfo.renderers);
 18557                visibilityDirty = false;
 58            }
 59
 22060            if (collisionsDirty)
 61            {
 18362                CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions, false, entity, Ca
 18363                collisionsDirty = false;
 64            }
 65
 22066            if (entity.meshesInfo.meshFilters.Length > 0 && entity.meshesInfo.meshFilters[0].sharedMesh != currentMesh)
 67            {
 268                entity.meshesInfo.UpdateExistingMeshAtIndex(currentMesh, 0);
 69            }
 70
 71            // First we remove the old rendereable, then we compute and add the new one.
 22072            RemoveRendereableFromDataStore(entity);
 22073            AddRendereableToDataStore(entity);
 22074        }
 75
 76        void OnShapeAttached(IDCLEntity entity)
 77        {
 16078            if (entity == null)
 079                return;
 80
 16081            entity.EnsureMeshGameObject(componentName + " mesh");
 82
 16083            if (currentMesh == null)
 84            {
 14485                currentMesh = GenerateGeometry();
 86            }
 87
 16088            MeshFilter meshFilter = entity.meshRootGameObject.AddComponent<MeshFilter>();
 16089            MeshRenderer meshRenderer = entity.meshRootGameObject.AddComponent<MeshRenderer>();
 90
 16091            entity.meshesInfo.renderers = new Renderer[] { meshRenderer };
 16092            entity.meshesInfo.currentShape = this;
 93
 16094            meshFilter.sharedMesh = currentMesh;
 95
 16096            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            {
 159109                meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 110            }
 111
 160112            visibilityDirty = true;
 160113            collisionsDirty = true;
 160114            UpdateRenderer(entity);
 160115            OnShapeFinishedLoading(entity);
 160116        }
 117
 118        void OnShapeFinishedLoading(IDCLEntity entity)
 119        {
 161120            entity.OnShapeUpdated?.Invoke(entity);
 161121        }
 122
 123        void OnShapeDetached(IDCLEntity entity)
 124        {
 115125            if (entity == null || entity.meshRootGameObject == null)
 0126                return;
 127
 115128            if (attachedEntities.Count == 0)
 129            {
 99130                DestroyGeometry();
 99131                Utils.CleanMaterials(entity.meshRootGameObject.GetComponent<Renderer>());
 99132                currentMesh = null;
 133            }
 134
 115135            Utils.SafeDestroy(entity.meshRootGameObject);
 115136            entity.meshesInfo.CleanReferences();
 137
 115138            RemoveRendereableFromDataStore(entity);
 115139        }
 140
 141        public override IEnumerator ApplyChanges(BaseModel newModelRaw)
 142        {
 209143            var newModel = (T) newModelRaw;
 144
 209145            if (previousModel != null)
 146            {
 55147                visibilityDirty = newModel.visible != previousModel.visible;
 55148                collisionsDirty = newModel.withCollisions != previousModel.withCollisions || newModel.isPointerBlocker !
 149            }
 150
 209151            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.
 209155            if (attachedEntities.Count > 0)
 156            {
 44157                if (shouldGenerateMesh)
 158                {
 5159                    DestroyGeometry();
 5160                    currentMesh = GenerateGeometry();
 161                }
 162
 44163                using (var iterator = attachedEntities.GetEnumerator())
 164                {
 44165                    bool cachedVisibilityDirty = visibilityDirty;
 44166                    bool cachedCollisionDirty = collisionsDirty;
 104167                    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.
 60171                        visibilityDirty = cachedVisibilityDirty;
 60172                        collisionsDirty = cachedCollisionDirty;
 173
 60174                        var entity = iterator.Current;
 60175                        UpdateRenderer(entity, newModel);
 176
 60177                        entity.OnShapeUpdated?.Invoke(entity);
 178                    }
 44179                }
 180            }
 181
 209182            previousModel = newModel;
 209183            return null;
 184        }
 185
 186        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 187        {
 160188            if (attachedEntities.Contains(entity))
 0189                return;
 190
 160191            base.AttachTo(entity);
 160192        }
 193
 66194        public override bool IsVisible() { return cachedModel.visible; }
 195
 31196        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        {
 335202            if ( attachedRendereables.ContainsKey(entity) )
 203            {
 175204                DataStore.i.sceneWorldObjects.RemoveRendereable(entity, attachedRendereables[entity]);
 175205                attachedRendereables.Remove(entity);
 206            }
 335207        }
 208
 209        private void AddRendereableToDataStore(IDCLEntity entity)
 210        {
 220211            int triangleCount = currentMesh.triangles.Length;
 212
 220213            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
 220222            if ( !attachedRendereables.ContainsKey(entity) )
 223            {
 220224                attachedRendereables.Add(entity, newRendereable);
 220225                DataStore.i.sceneWorldObjects.AddRendereable(entity, newRendereable);
 226            }
 220227        }
 228    }
 229}