< Summary

Class:DCL.ECSComponents.GLTFShapeComponentHandler
Assembly:DCL.ECSComponents.GLTFShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/GLTFShape/Handler/GLTFShapeComponentHandler.cs
Covered lines:33
Uncovered lines:27
Coverable lines:60
Total lines:139
Line coverage:55% (33 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GLTFShapeComponentHandler(...)0%110100%
OnComponentCreated(...)0%2100%
OnComponentRemoved(...)0%220100%
OnComponentModelUpdated(...)0%220100%
IsVisible()0%2100%
HasCollisions()0%2100%
ShouldLoadShape(...)0%330100%
LoadShape(...)0%5.923031.25%
ApplyModel(...)0%110100%
DisposeMesh(...)0%4.024090%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/GLTFShape/Handler/GLTFShapeComponentHandler.cs

#LineLine coverage
 1using System;
 2using DCL.Components;
 3using DCL.Configuration;
 4using DCL.Controllers;
 5using DCL.ECSRuntime;
 6using DCL.Models;
 7using DCL.ECSComponents;
 8using UnityEngine;
 9
 10namespace DCL.ECSComponents
 11{
 12    public class GLTFShapeComponentHandler : IECSComponentHandler<PBGLTFShape>
 13    {
 14        internal MeshesInfo meshesInfo;
 15        internal Rendereable rendereable;
 16        internal PBGLTFShape model;
 17        internal readonly LoadWrapper_GLTF loadWrapper;
 18        internal IDCLEntity entity;
 19
 20        private readonly ShapeRepresentation shapeRepresentation;
 21        private readonly DataStore_ECS7 dataStore;
 22
 923        public GLTFShapeComponentHandler(DataStore_ECS7 dataStoreEcs7)
 24        {
 925            shapeRepresentation = new ShapeRepresentation();
 926            dataStore = dataStoreEcs7;
 927            loadWrapper = new LoadWrapper_GLTF();
 928        }
 29
 030        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 31
 32        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 33        {
 1234            loadWrapper?.Unload();
 1235            DisposeMesh(scene);
 1236        }
 37
 38        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBGLTFShape model)
 39        {
 540            this.entity = entity;
 41
 42            // If we didn't create the shape, or if the shape is different from the last time, we load the shape, if not
 543            if(ShouldLoadShape(model))
 444                LoadShape(scene,entity,model);
 45            else
 146                ApplyModel(model);
 47
 548            this.model = model;
 549        }
 50
 051        public bool IsVisible() {  return model.GetVisible(); }
 52
 053        public bool HasCollisions() {  return model.GetWithCollisions(); }
 54
 55        private bool ShouldLoadShape(PBGLTFShape model)
 56        {
 57            // If we didn't create the shape, or if the shape is different from the last time, we load the shape, if not
 558            return meshesInfo == null || (this.model != null && this.model.Src != model.Src);
 59        }
 60
 61        private void LoadShape(IParcelScene scene, IDCLEntity entity, PBGLTFShape model)
 62        {
 463            ContentProvider provider = scene.contentProvider;
 464            if (provider.HasContentsUrl(model.Src))
 65            {
 066                entity.EnsureMeshGameObject("GLTF mesh");
 67
 068                if (loadWrapper != null)
 69                {
 070                    loadWrapper.Unload();
 071                    DisposeMesh(scene);
 72                }
 73
 74                // We prepare the load wrapper to load the GLTF
 075                loadWrapper.customContentProvider = provider;
 76
 077                entity.meshesInfo.currentShape = shapeRepresentation;
 78
 079                loadWrapper.entity = entity;
 080                loadWrapper.useVisualFeedback = Configuration.ParcelSettings.VISUAL_LOADING_ENABLED;
 081                loadWrapper.initialVisibility = model.GetVisible(); // TODO: check if entity is inside scene's boundarie
 082                loadWrapper.Load(model.Src, (wrapper) =>
 83                {
 84                    // We remove the transition from the GLTF
 085                    if (!model.GetVisible())
 86                    {
 087                        MaterialTransitionController[] materialTransitionControllers = entity.meshRootGameObject.GetComp
 88
 089                        for (var i = 0; i < materialTransitionControllers.Length; i++)
 090                            GameObject.Destroy(materialTransitionControllers[i]);
 91                    }
 092                    entity.meshesInfo.meshRootGameObject = entity.meshRootGameObject;
 093                    meshesInfo = entity.meshesInfo;
 94
 95                    // Apply the model for visibility, collision and event pointer
 096                    ApplyModel(model);
 097                    dataStore.RemovePendingResource(scene.sceneData.id, model);
 098                    dataStore.AddShapeReady(entity.entityId,meshesInfo.meshRootGameObject);
 99
 0100                }, (wrapper, exception) =>
 101                {
 0102                    dataStore.RemovePendingResource(scene.sceneData.id, model);
 0103                });
 0104            }
 105            else
 106            {
 107#if UNITY_EDITOR
 4108                Debug.LogWarning($"LoadableShape '{model.Src}' not found in scene '{scene.sceneData.id}' mappings");
 109#endif
 110            }
 4111            dataStore.AddPendingResource(scene.sceneData.id, model);
 4112        }
 113
 114        internal void ApplyModel(PBGLTFShape model)
 115        {
 1116            shapeRepresentation.UpdateModel(model.GetVisible(), model.GetWithCollisions());
 117
 118            // Set visibility
 1119            meshesInfo.meshRootGameObject.SetActive(model.GetVisible());
 120
 121            // Set collisions and pointer blocker
 1122            ECSComponentsUtils.UpdateMeshInfoColliders(model.GetWithCollisions(), model.GetIsPointerBlocker(), meshesInf
 1123        }
 124
 125        internal void DisposeMesh(IParcelScene scene)
 126        {
 13127            if (entity != null)
 9128                dataStore.RemoveShapeReady(entity.entityId);
 13129            if (rendereable != null)
 0130                ECSComponentsUtils.RemoveRendereableFromDataStore( scene.sceneData.id, rendereable);
 13131            if (model != null)
 5132                dataStore.RemovePendingResource(scene.sceneData.id, model);
 133
 13134            meshesInfo = null;
 13135            rendereable = null;
 13136            model = null;
 13137        }
 138    }
 139}