< 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:26
Uncovered lines:9
Coverable lines:35
Total lines:97
Line coverage:74.2% (26 of 35)
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
 13        private Transform entityTransform;
 114        private Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition;
 15        private UnityEngine.Vector3 lastPosition;
 16
 17        private IDCLEntity entity;
 18        private IParcelScene scene;
 19        private PBBillboard model;
 20
 221        public BillboardComponentHandler(IUpdateEventHandler updateEventHandler)
 22        {
 223            this.updateEventHandler = updateEventHandler;
 224            updateEventHandler.AddListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate);
 225        }
 26
 27        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 28        {
 329            this.entity = entity;
 330            this.scene = scene;
 31
 32            // The billboard will rotate the entity transform toward the camera
 333            entityTransform = entity.gameObject.transform;
 334        }
 35
 36        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 37        {
 238            updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate);
 239        }
 40
 41        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBBillboard model)
 42        {
 143            this.model = model;
 44
 145            ChangeOrientation();
 146        }
 47
 48        // This runs on LateUpdate() instead of Update() to be applied AFTER the transform was moved by the transform co
 49        private void LateUpdate()
 50        {
 51            //NOTE(Brian): This fixes #757 (https://github.com/decentraland/unity-client/issues/757)
 52            //             We must find a more performant way to handle this, until that time, this is the approach.
 53
 054            if (entityTransform == null)
 055                return;
 056            if (entityTransform.position == lastPosition)
 057                return;
 58
 059            lastPosition = entityTransform.position;
 60
 061            ChangeOrientation();
 062        }
 63
 64        /// <summary>
 65        /// 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
 66        /// </summary>
 67        /// <returns></returns>
 68        private UnityEngine.Vector3 GetLookAtVector()
 69        {
 170            UnityEngine.Vector3 lookAtDir =(cameraPosition - entityTransform.position);
 71
 72            // Note (Zak): This check is here to avoid normalizing twice if not needed
 173            if (!(model.X && model.Y && model.Z))
 74            {
 175                lookAtDir.Normalize();
 76
 77                // Note (Zak): Model x,y,z are axis that we want to enable/disable
 78                // while lookAtDir x,y,z are the components of the look-at vector
 179                if (!model.X || model.Z)
 080                    lookAtDir.y = entityTransform.forward.y;
 181                if (!model.Y)
 182                    lookAtDir.x = entityTransform.forward.x;
 83            }
 84
 185            return lookAtDir.normalized;
 86        }
 87
 88        private void ChangeOrientation()
 89        {
 190            if (entityTransform == null || model == null)
 091                return;
 192            UnityEngine.Vector3 lookAtVector = GetLookAtVector();
 193            if (lookAtVector != UnityEngine.Vector3.zero)
 194                entityTransform.forward = lookAtVector;
 195        }
 96    }
 97}