< 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:112
Uncovered lines:8
Coverable lines:120
Total lines:255
Line coverage:93.3% (112 of 120)
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%990100%
OnShapeAttached(...)0%44096.3%
OnShapeFinishedLoading()0%550100%
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.Helpers;
 2using DCL.Models;
 3using System.Collections;
 4using System.Collections.Generic;
 5using Cysharp.Threading.Tasks;
 6using UnityEngine;
 7
 8namespace DCL.Components
 9{
 10    public abstract class ParametrizedShape<T> : BaseShape
 11        where T : BaseShape.Model, new()
 12    {
 17513        public Dictionary<IDCLEntity, Rendereable> attachedRendereables = new Dictionary<IDCLEntity, Rendereable>();
 14        bool visibilityDirty = false;
 15        bool collisionsDirty = false;
 16
 17        public virtual Mesh GenerateGeometry()
 18        {
 019            return null;
 20        }
 21
 22        protected virtual void DestroyGeometry()
 23        {
 4724            if (currentMesh == null)
 025                return;
 26
 4727            Object.Destroy(currentMesh);
 4728            currentMesh = null;
 4729        }
 30
 031        public Mesh currentMesh { get; protected set; }
 32        private Model previousModel;
 33        private Model cachedModel;
 34
 17535        public ParametrizedShape()
 36        {
 17537            OnAttach += OnShapeAttached;
 17538            OnDetach += OnShapeDetached;
 17539        }
 40
 41        public override void UpdateFromModel(BaseModel newModel)
 42        {
 24643            cachedModel = (Model) newModel;
 24644            base.UpdateFromModel(newModel);
 24645        }
 46
 47        void UpdateRenderer(IDCLEntity entity, Model model = null)
 48        {
 26649            if (model == null)
 19050                model = (T) this.model;
 51
 26652            if (visibilityDirty)
 53            {
 22554                ConfigureVisibility(entity.meshRootGameObject, model.visible && entity.isInsideSceneBoundaries, entity.m
 22555                visibilityDirty = false;
 56            }
 57
 26658            if (collisionsDirty)
 59            {
 21360                CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions && entity.isInside
 21361                collisionsDirty = false;
 62            }
 63
 26664            if (entity.meshesInfo.meshFilters.Length > 0 && entity.meshesInfo.meshFilters[0].sharedMesh != currentMesh)
 65            {
 366                entity.meshesInfo.UpdateExistingMeshAtIndex(currentMesh, 0);
 67            }
 68
 26669            DCL.Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity);
 26670        }
 71
 72        void OnShapeAttached(IDCLEntity entity)
 73        {
 19074            if (entity == null)
 075                return;
 76
 77            // First we remove the old rendereable, then we compute and add the new one.
 19078            RemoveRendereableFromDataStore(entity);
 79
 19080            entity.EnsureMeshGameObject(componentName + " mesh");
 81
 19082            if (currentMesh == null)
 83            {
 16584                currentMesh = GenerateGeometry();
 85            }
 86
 19087            MeshFilter meshFilter = entity.meshRootGameObject.AddComponent<MeshFilter>();
 19088            MeshRenderer meshRenderer = entity.meshRootGameObject.AddComponent<MeshRenderer>();
 89
 19090            entity.meshesInfo.renderers = new Renderer[] { meshRenderer };
 19091            entity.meshesInfo.currentShape = this;
 92
 19093            meshFilter.sharedMesh = currentMesh;
 94
 19095            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            {
 189108                meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 109            }
 110
 190111            visibilityDirty = true;
 190112            collisionsDirty = true;
 190113            UpdateRenderer(entity);
 190114            OnShapeFinishedLoading(entity);
 190115            AddRendereableToDataStore(entity);
 190116        }
 117
 118
 119        async UniTaskVoid OnShapeFinishedLoading(IDCLEntity entity)
 120        {
 121            // We need to wait for a frame so that MaterialTransitionController has been destroyed.
 191122            await UniTask.Yield();
 123
 191124            entity.OnShapeUpdated?.Invoke(entity);
 191125            DCL.Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity);
 191126        }
 127
 128        void OnShapeDetached(IDCLEntity entity)
 129        {
 146130            if (entity == null || entity.meshRootGameObject == null)
 0131                return;
 132
 146133            if (attachedEntities.Count == 0)
 134            {
 121135                DestroyGeometry();
 121136                Utils.CleanMaterials(entity.meshRootGameObject.GetComponent<Renderer>());
 121137                currentMesh = null;
 138            }
 139
 146140            Utils.SafeDestroy(entity.meshRootGameObject);
 146141            entity.meshesInfo.CleanReferences();
 142
 146143            RemoveRendereableFromDataStore(entity);
 146144        }
 145
 146        public override IEnumerator ApplyChanges(BaseModel newModelRaw)
 147        {
 246148            var newModel = (T) newModelRaw;
 149
 246150            if (previousModel != null)
 151            {
 71152                visibilityDirty = newModel.visible != previousModel.visible;
 71153                collisionsDirty = newModel.withCollisions != previousModel.withCollisions || newModel.isPointerBlocker !
 154            }
 155
 246156            bool shouldGenerateMesh = ShouldGenerateNewMesh(previousModel);
 157
 158            //NOTE(Brian): Only generate meshes here if they already are attached to something.
 159            //             Otherwise, the mesh will be created on the OnShapeAttached.
 246160            if (attachedEntities.Count > 0)
 161            {
 60162                using (var iterator = attachedEntities.GetEnumerator())
 163                {
 136164                    while (iterator.MoveNext())
 165                    {
 76166                        var entity = iterator.Current;
 76167                        RemoveRendereableFromDataStore(entity);
 168                    }
 60169                }
 170
 60171                if (shouldGenerateMesh)
 172                {
 6173                    DestroyGeometry();
 6174                    currentMesh = GenerateGeometry();
 175                }
 176
 60177                using (var iterator = attachedEntities.GetEnumerator())
 178                {
 60179                    bool cachedVisibilityDirty = visibilityDirty;
 60180                    bool cachedCollisionDirty = collisionsDirty;
 136181                    while (iterator.MoveNext())
 182                    {
 183                        //NOTE(Alex): Since UpdateRenderer updates the dirty flags as well we have to make sure every en
 184                        //            gets updated accordingly to the original flags.
 76185                        visibilityDirty = cachedVisibilityDirty;
 76186                        collisionsDirty = cachedCollisionDirty;
 187
 76188                        var entity = iterator.Current;
 76189                        UpdateRenderer(entity, newModel);
 190
 76191                        entity.OnShapeUpdated?.Invoke(entity);
 192                    }
 60193                }
 194
 60195                using (var iterator = attachedEntities.GetEnumerator())
 196                {
 136197                    while (iterator.MoveNext())
 198                    {
 76199                        var entity = iterator.Current;
 76200                        AddRendereableToDataStore(entity);
 201                    }
 60202                }
 203            }
 204
 246205            previousModel = newModel;
 246206            return null;
 207        }
 208
 209        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 210        {
 190211            if (attachedEntities.Contains(entity))
 0212                return;
 213
 190214            base.AttachTo(entity);
 190215        }
 216
 161217        public override bool IsVisible() { return cachedModel.visible; }
 218
 177219        public override bool HasCollisions() { return cachedModel.withCollisions; }
 220
 0221        protected virtual bool ShouldGenerateNewMesh(BaseShape.Model newModel) { return true; }
 222
 223        private void RemoveRendereableFromDataStore(IDCLEntity entity)
 224        {
 412225            if (!attachedRendereables.ContainsKey(entity))
 190226                return;
 227
 222228            DataStore.i.sceneWorldObjects.RemoveRendereable(entity.scene.sceneData.id, attachedRendereables[entity]);
 222229            attachedRendereables.Remove(entity);
 222230        }
 231
 232        private void AddRendereableToDataStore(IDCLEntity entity)
 233        {
 266234            if (attachedRendereables.ContainsKey(entity))
 0235                return;
 236
 266237            int triangleCount = currentMesh.triangles.Length;
 238
 266239            var newRendereable =
 240                new Rendereable()
 241                {
 242                    container = entity.meshRootGameObject,
 243                    totalTriangleCount = triangleCount,
 244                    meshes = new HashSet<Mesh>() { currentMesh },
 245                    meshToTriangleCount = new Dictionary<Mesh, int>() { { currentMesh, triangleCount } }
 246                };
 247
 266248            newRendereable.renderers = MeshesInfoUtils.ExtractUniqueRenderers(entity.meshRootGameObject);
 266249            newRendereable.ownerId = entity.entityId;
 250
 266251            attachedRendereables.Add(entity, newRendereable);
 266252            DataStore.i.sceneWorldObjects.AddRendereable(entity.scene.sceneData.id, newRendereable);
 266253        }
 254    }
 255}