< 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    {
 17613        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
 17635        public ParametrizedShape()
 36        {
 17637            OnAttach += OnShapeAttached;
 17638            OnDetach += OnShapeDetached;
 17639        }
 40
 41        public override void UpdateFromModel(BaseModel newModel)
 42        {
 24743            cachedModel = (Model) newModel;
 24744            base.UpdateFromModel(newModel);
 24745        }
 46
 47        void UpdateRenderer(IDCLEntity entity, Model model = null)
 48        {
 26749            if (model == null)
 19150                model = (T) this.model;
 51
 26752            if (visibilityDirty)
 53            {
 22654                ConfigureVisibility(entity.meshRootGameObject, model.visible && entity.isInsideSceneBoundaries, entity.m
 22655                visibilityDirty = false;
 56            }
 57
 26758            if (collisionsDirty)
 59            {
 21460                CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions && entity.isInside
 21461                collisionsDirty = false;
 62            }
 63
 26764            if (entity.meshesInfo.meshFilters.Length > 0 && entity.meshesInfo.meshFilters[0].sharedMesh != currentMesh)
 65            {
 366                entity.meshesInfo.UpdateExistingMeshAtIndex(currentMesh, 0);
 67            }
 68
 26769            DCL.Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity);
 26770        }
 71
 72        void OnShapeAttached(IDCLEntity entity)
 73        {
 19174            if (entity == null)
 075                return;
 76
 77            // First we remove the old rendereable, then we compute and add the new one.
 19178            RemoveRendereableFromDataStore(entity);
 79
 19180            entity.EnsureMeshGameObject(componentName + " mesh");
 81
 19182            if (currentMesh == null)
 83            {
 16684                currentMesh = GenerateGeometry();
 85            }
 86
 19187            MeshFilter meshFilter = entity.meshRootGameObject.AddComponent<MeshFilter>();
 19188            MeshRenderer meshRenderer = entity.meshRootGameObject.AddComponent<MeshRenderer>();
 89
 19190            entity.meshesInfo.renderers = new Renderer[] { meshRenderer };
 19191            entity.meshesInfo.currentShape = this;
 92
 19193            meshFilter.sharedMesh = currentMesh;
 94
 19195            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            {
 190108                meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default");
 109            }
 110
 191111            visibilityDirty = true;
 191112            collisionsDirty = true;
 191113            UpdateRenderer(entity);
 191114            OnShapeFinishedLoading(entity);
 191115            AddRendereableToDataStore(entity);
 191116        }
 117
 118
 119        async UniTaskVoid OnShapeFinishedLoading(IDCLEntity entity)
 120        {
 121            // We need to wait for a frame so that MaterialTransitionController has been destroyed.
 192122            await UniTask.Yield();
 123
 192124            entity.OnShapeUpdated?.Invoke(entity);
 192125            DCL.Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity);
 192126        }
 127
 128        void OnShapeDetached(IDCLEntity entity)
 129        {
 147130            if (entity == null || entity.meshRootGameObject == null)
 0131                return;
 132
 147133            if (attachedEntities.Count == 0)
 134            {
 122135                DestroyGeometry();
 122136                Utils.CleanMaterials(entity.meshRootGameObject.GetComponent<Renderer>());
 122137                currentMesh = null;
 138            }
 139
 147140            Utils.SafeDestroy(entity.meshRootGameObject);
 147141            entity.meshesInfo.CleanReferences();
 142
 147143            RemoveRendereableFromDataStore(entity);
 147144        }
 145
 146        public override IEnumerator ApplyChanges(BaseModel newModelRaw)
 147        {
 247148            var newModel = (T) newModelRaw;
 149
 247150            if (previousModel != null)
 151            {
 71152                visibilityDirty = newModel.visible != previousModel.visible;
 71153                collisionsDirty = newModel.withCollisions != previousModel.withCollisions || newModel.isPointerBlocker !
 154            }
 155
 247156            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.
 247160            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
 247205            previousModel = newModel;
 247206            return null;
 207        }
 208
 209        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 210        {
 191211            if (attachedEntities.Contains(entity))
 0212                return;
 213
 191214            base.AttachTo(entity);
 191215        }
 216
 119217        public override bool IsVisible() { return cachedModel.visible; }
 218
 149219        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        {
 414225            if (!attachedRendereables.ContainsKey(entity))
 191226                return;
 227
 223228            DataStore.i.sceneWorldObjects.RemoveRendereable(entity.scene.sceneData.id, attachedRendereables[entity]);
 223229            attachedRendereables.Remove(entity);
 223230        }
 231
 232        private void AddRendereableToDataStore(IDCLEntity entity)
 233        {
 267234            if (attachedRendereables.ContainsKey(entity))
 0235                return;
 236
 267237            int triangleCount = currentMesh.triangles.Length;
 238
 267239            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
 267248            newRendereable.renderers = MeshesInfoUtils.ExtractUniqueRenderers(entity.meshRootGameObject);
 267249            newRendereable.ownerId = entity.entityId;
 250
 267251            attachedRendereables.Add(entity, newRendereable);
 267252            DataStore.i.sceneWorldObjects.AddRendereable(entity.scene.sceneData.id, newRendereable);
 267253        }
 254    }
 255}