< 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:27
Uncovered lines:10
Coverable lines:37
Total lines:102
Line coverage:72.9% (27 of 37)
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%7.17087.5%
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 IDCLEntity entity;
 19        private IParcelScene scene;
 20        private PBBillboard model;
 21
 222        public BillboardComponentHandler(DataStore_Player playerDataStore, IUpdateEventHandler updateEventHandler)
 23        {
 224            this.playerDataStore = playerDataStore;
 225            this.updateEventHandler = updateEventHandler;
 226            updateEventHandler.AddListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate);
 227        }
 28
 29        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 30        {
 331            this.entity = entity;
 332            this.scene = scene;
 33
 34            // The billboard will rotate the entity transform toward the camera
 335            entityTransform = entity.gameObject.transform;
 336        }
 37
 38        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 39        {
 240            updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate);
 241        }
 42
 43        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBBillboard model)
 44        {
 145            this.model = model;
 46
 147            ChangeOrientation();
 148        }
 49
 50        // This runs on LateUpdate() instead of Update() to be applied AFTER the transform was moved by the transform co
 51        private void LateUpdate()
 52        {
 53            //NOTE(Brian): This fixes #757 (https://github.com/decentraland/unity-client/issues/757)
 54            //             We must find a more performant way to handle this, until that time, this is the approach.
 55
 056            if (entityTransform == null)
 057                return;
 58
 059            UnityEngine.Vector3 playerPosition = playerDataStore.playerUnityPosition.Get();
 60
 061            if (playerPosition == lastPosition)
 062                return;
 63
 064            lastPosition = playerPosition;
 65
 066            ChangeOrientation();
 067        }
 68
 69        /// <summary>
 70        /// 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
 71        /// </summary>
 72        /// <returns></returns>
 73        private UnityEngine.Vector3 GetLookAtVector()
 74        {
 175            UnityEngine.Vector3 lookAtDir =(cameraPosition - entityTransform.position);
 76
 77            // Note (Zak): This check is here to avoid normalizing twice if not needed
 178            if (!(model.GetX() && model.GetY() && model.GetZ()))
 79            {
 180                lookAtDir.Normalize();
 81
 82                // Note (Zak): Model x,y,z are axis that we want to enable/disable
 83                // while lookAtDir x,y,z are the components of the look-at vector
 184                if (!model.GetX() || model.GetZ())
 085                    lookAtDir.y = entityTransform.forward.y;
 186                if (!model.GetY())
 187                    lookAtDir.x = entityTransform.forward.x;
 88            }
 89
 190            return lookAtDir.normalized;
 91        }
 92
 93        private void ChangeOrientation()
 94        {
 195            if (entityTransform == null || model == null)
 096                return;
 197            UnityEngine.Vector3 lookAtVector = GetLookAtVector();
 198            if (lookAtVector != UnityEngine.Vector3.zero)
 199                entityTransform.forward = lookAtVector;
 1100        }
 101    }
 102}