< Summary

Class:DCL.Billboard
Assembly:DCL.Components.Billboard
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Billboard/Billboard.cs
Covered lines:41
Uncovered lines:1
Coverable lines:42
Total lines:102
Line coverage:97.6% (41 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
Awake()0%110100%
ApplyChanges()0%440100%
GetModel()0%2100%
OnDestroy()0%110100%
LateUpdate()0%330100%
GetLookAtVector()0%880100%
ChangeOrientation()0%330100%
CameraPositionChanged(...)0%110100%
GetClassId()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Billboard/Billboard.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Components;
 4using DCL.Helpers;
 5using DCL.Models;
 6using UnityEngine;
 7
 8namespace DCL
 9{
 10    public class Billboard : BaseComponent
 11    {
 12        [Serializable]
 13        public class Model : BaseModel
 14        {
 1715            public bool x = true;
 1716            public bool y = true;
 1717            public bool z = true;
 18
 719            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 20        }
 21
 22        Transform entityTransform;
 3223        Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition;
 24        Vector3 lastPosition;
 25
 526        public override string componentName => "billboard";
 27
 828        private void Awake() { model = new Model(); }
 29
 30        public override IEnumerator ApplyChanges(BaseModel newModel)
 31        {
 732            cameraPosition.OnChange -= CameraPositionChanged;
 733            cameraPosition.OnChange += CameraPositionChanged;
 34
 735            Model model = (Model)newModel;
 36
 737            ChangeOrientation();
 38
 39
 740            if (entityTransform == null)
 41            {
 842                yield return new WaitUntil(() => entity.gameObject != null);
 443                entityTransform = entity.gameObject.transform;
 44            }
 745        }
 46
 047        new public Model GetModel() { return (Model)model; }
 48
 849        public void OnDestroy() { cameraPosition.OnChange -= CameraPositionChanged; }
 50
 51        // This runs on LateUpdate() instead of Update() to be applied AFTER the transform was moved by the transform co
 52        public void LateUpdate()
 53        {
 54            //NOTE(Brian): This fixes #757 (https://github.com/decentraland/unity-client/issues/757)
 55            //             We must find a more performant way to handle this, until that time, this is the approach.
 56
 2057            if (entityTransform == null)
 458                return;
 1659            if (transform.position == lastPosition)
 860                return;
 61
 862            lastPosition = transform.position;
 63
 864            ChangeOrientation();
 865        }
 66
 67        Vector3 GetLookAtVector()
 68        {
 1469            bool hasTextShape = scene.componentsManagerLegacy.HasComponent(entity, CLASS_ID_COMPONENT.TEXT_SHAPE);
 1470            Vector3 lookAtDir = hasTextShape ? (entityTransform.position - cameraPosition) : (cameraPosition - entityTra
 71
 1472            Model model = (Model) this.model;
 73            // Note (Zak): This check is here to avoid normalizing twice if not needed
 1474            if (!(model.x && model.y && model.z))
 75            {
 776                lookAtDir.Normalize();
 77
 78                // Note (Zak): Model x,y,z are axis that we want to enable/disable
 79                // while lookAtDir x,y,z are the components of the look-at vector
 780                if (!model.x || model.z)
 781                    lookAtDir.y = entityTransform.forward.y;
 782                if (!model.y)
 283                    lookAtDir.x = entityTransform.forward.x;
 84            }
 85
 1486            return lookAtDir.normalized;
 87        }
 88
 89        void ChangeOrientation()
 90        {
 2091            if (entityTransform == null)
 692                return;
 1493            Vector3 lookAtVector = GetLookAtVector();
 1494            if (lookAtVector != Vector3.zero)
 1495                entityTransform.forward = lookAtVector;
 1496        }
 97
 1098        private void CameraPositionChanged(Vector3 current, Vector3 previous) { ChangeOrientation(); }
 99
 1100        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.BILLBOARD; }
 101    }
 102}