< Summary

Class:DCL.ECSComponents.BillboardComponentHandler
Assembly:DCL.ECSComponents.Billboard
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Billboard/Handler/BillboardComponentHandler.cs
Covered lines:22
Uncovered lines:8
Coverable lines:30
Total lines:87
Line coverage:73.3% (22 of 30)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BillboardComponentHandler(...)0%110100%
OnComponentCreated(...)0%110100%
OnComponentRemoved(...)0%110100%
OnComponentModelUpdated(...)0%110100%
LateUpdate()0%12300%
GetLookAtVector()0%330100%
ChangeOrientation()0%4.074083.33%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Billboard/Handler/BillboardComponentHandler.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECSRuntime;
 3using DCL.Models;
 4using DCL.ECSComponents;
 5using UnityEngine;
 6
 7namespace DCL.ECSComponents
 8{
 9    public class BillboardComponentHandler : IECSComponentHandler<PBBillboard>
 10    {
 11        private readonly IUpdateEventHandler updateEventHandler;
 12        private readonly DataStore_Player playerDataStore;
 13
 14        private Transform entityTransform;
 115        private Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition;
 16        private UnityEngine.Vector3 lastPosition;
 17
 18        private PBBillboard model;
 19
 220        public BillboardComponentHandler(IUpdateEventHandler updateEventHandler)
 21        {
 222            this.updateEventHandler = updateEventHandler;
 223            updateEventHandler.AddListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate);
 224        }
 25
 26        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 27        {
 28            // The billboard will rotate the entity transform toward the camera
 329            entityTransform = entity.gameObject.transform;
 330        }
 31
 32        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 33        {
 234            updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate);
 235        }
 36
 37        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBBillboard model)
 38        {
 139            this.model = model;
 40
 141            ChangeOrientation();
 142        }
 43
 44        // This runs on LateUpdate() instead of Update() to be applied AFTER the transform was moved by the transform co
 45        private void LateUpdate()
 46        {
 47            //NOTE(Brian): This fixes #757 (https://github.com/decentraland/unity-client/issues/757)
 48            //             We must find a more performant way to handle this, until that time, this is the approach.
 49
 050            if (entityTransform == null)
 051                return;
 52
 053            if (cameraPosition == lastPosition)
 054                return;
 55
 056            lastPosition = cameraPosition;
 57
 058            ChangeOrientation();
 059        }
 60
 61        /// <summary>
 62        /// We use the model axis as a lock values. This means, that if the model.X comes true, we lock the X axis to lo
 63        /// </summary>
 64        /// <returns></returns>
 65        private UnityEngine.Vector3 GetLookAtVector()
 66        {
 167            UnityEngine.Vector3 lookAtDir = model.OppositeDirection ? (cameraPosition - entityTransform.position) : (ent
 68
 169            if (model.BillboardMode == BillboardMode.BmYAxe)
 70            {
 171                lookAtDir.Normalize();
 172                lookAtDir.y = entityTransform.forward.y;
 73            }
 74
 175            return lookAtDir.normalized;
 76        }
 77
 78        private void ChangeOrientation()
 79        {
 180            if (entityTransform == null || model == null)
 081                return;
 182            UnityEngine.Vector3 lookAtVector = GetLookAtVector();
 183            if (lookAtVector != UnityEngine.Vector3.zero)
 184                entityTransform.forward = lookAtVector;
 185        }
 86    }
 87}