< 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:39
Uncovered lines:2
Coverable lines:41
Total lines:99
Line coverage:95.1% (39 of 41)
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%2100%

File(s)

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

#LineLine coverage
 1using DCL.Components;
 2using System.Collections;
 3using DCL.Helpers;
 4using UnityEngine;
 5using DCL.Models;
 6
 7namespace DCL
 8{
 9    public class Billboard : BaseComponent
 10    {
 11        [System.Serializable]
 12        public class Model : BaseModel
 13        {
 1514            public bool x = true;
 1515            public bool y = true;
 1516            public bool z = true;
 17
 618            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 19        }
 20
 21        Transform entityTransform;
 2922        Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition;
 23        Vector3 lastPosition;
 24
 625        private void Awake() { model = new Model(); }
 26
 27        public override IEnumerator ApplyChanges(BaseModel newModel)
 28        {
 629            cameraPosition.OnChange -= CameraPositionChanged;
 630            cameraPosition.OnChange += CameraPositionChanged;
 31
 632            Model model = (Model)newModel;
 33
 634            ChangeOrientation();
 35
 36
 637            if (entityTransform == null)
 38            {
 639                yield return new WaitUntil(() => entity.gameObject != null);
 340                entityTransform = entity.gameObject.transform;
 41            }
 642        }
 43
 044        new public Model GetModel() { return (Model)model; }
 45
 646        public void OnDestroy() { cameraPosition.OnChange -= CameraPositionChanged; }
 47
 48        // This runs on LateUpdate() instead of Update() to be applied AFTER the transform was moved by the transform co
 49        public 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
 1954            if (entityTransform == null)
 355                return;
 1656            if (transform.position == lastPosition)
 857                return;
 58
 859            lastPosition = transform.position;
 60
 861            ChangeOrientation();
 862        }
 63
 64        Vector3 GetLookAtVector()
 65        {
 1466            bool hasTextShape = entity.components.ContainsKey(Models.CLASS_ID_COMPONENT.TEXT_SHAPE);
 1467            Vector3 lookAtDir = hasTextShape ? (entityTransform.position - cameraPosition) : (cameraPosition - entityTra
 68
 1469            Model model = (Model) this.model;
 70            // Note (Zak): This check is here to avoid normalizing twice if not needed
 1471            if (!(model.x && model.y && model.z))
 72            {
 773                lookAtDir.Normalize();
 74
 75                // Note (Zak): Model x,y,z are axis that we want to enable/disable
 76                // while lookAtDir x,y,z are the components of the look-at vector
 777                if (!model.x || model.z)
 778                    lookAtDir.y = entityTransform.forward.y;
 779                if (!model.y)
 280                    lookAtDir.x = entityTransform.forward.x;
 81            }
 82
 1483            return lookAtDir.normalized;
 84        }
 85
 86        void ChangeOrientation()
 87        {
 1888            if (entityTransform == null)
 489                return;
 1490            Vector3 lookAtVector = GetLookAtVector();
 1491            if (lookAtVector != Vector3.zero)
 1492                entityTransform.forward = lookAtVector;
 1493        }
 94
 895        private void CameraPositionChanged(Vector3 current, Vector3 previous) { ChangeOrientation(); }
 96
 097        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.BILLBOARD; }
 98    }
 99}