< Summary

Class:DCL.AvatarLODController
Assembly:AvatarLODController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarLOD/AvatarLODController/AvatarLODController.cs
Covered lines:60
Uncovered lines:17
Coverable lines:77
Total lines:163
Line coverage:77.9% (60 of 77)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarLODController(...)0%4.014090.91%
SetFullAvatar()0%5.055087.5%
SetSimpleAvatar()0%5.055087.5%
SetImpostor()0%5.055087.5%
SetInvisible()0%30500%
StartTransition(...)0%110100%
Transition()0%7.017095%
SetAvatarFeatures(...)0%330100%
Dispose()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarLOD/AvatarLODController/AvatarLODController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using UnityEngine;
 4
 5namespace DCL
 6{
 7    //TODO Rename to IAvatar (not now to avoid conflicts)
 8    public interface IAvatarLODController : IDisposable
 9    {
 10        Player player { get; }
 11        void SetFullAvatar();
 12        void SetSimpleAvatar();
 13        void SetImpostor();
 14        void SetInvisible();
 15    }
 16
 17    public class AvatarLODController : IAvatarLODController
 18    {
 19        internal enum State
 20        {
 21            Invisible,
 22            FullAvatar,
 23            SimpleAvatar,
 24            Impostor,
 25        }
 26
 27        private const float TRANSITION_DURATION = 0.5f;
 28
 029        public Player player { get; }
 30
 31        internal float avatarFade;
 32        internal float impostorFade;
 33
 34        internal bool SSAOEnabled;
 35        internal bool facialFeaturesEnabled;
 36
 37        internal Coroutine currentTransition = null;
 38        internal State? lastRequestedState = null;
 39
 1140        public AvatarLODController(Player player)
 41        {
 1142            this.player = player;
 1143            avatarFade = 1;
 1144            impostorFade = 0;
 1145            SSAOEnabled = true;
 1146            facialFeaturesEnabled = true;
 1147            if (player?.renderer == null)
 048                return;
 1149            player.renderer.SetAvatarFade(avatarFade);
 1150            player.renderer.SetImpostorFade(impostorFade);
 1151        }
 52
 53        public void SetFullAvatar()
 54        {
 355            if (lastRequestedState == State.FullAvatar)
 056                return;
 57
 358            lastRequestedState = State.FullAvatar;
 359            if (player?.renderer == null)
 160                return;
 61
 262            SetAvatarFeatures(true, true);
 263            StartTransition(1, 0);
 264        }
 65
 66        public void SetSimpleAvatar()
 67        {
 368            if (lastRequestedState == State.SimpleAvatar)
 069                return;
 70
 371            lastRequestedState = State.SimpleAvatar;
 372            if (player?.renderer == null)
 173                return;
 74
 275            SetAvatarFeatures(false, false);
 276            StartTransition(1, 0);
 277        }
 78
 79        public void SetImpostor()
 80        {
 381            if (lastRequestedState == State.Impostor)
 082                return;
 83
 384            lastRequestedState = State.Impostor;
 385            if (player?.renderer == null)
 186                return;
 87
 288            SetAvatarFeatures(false, false);
 289            StartTransition(0, 1);
 290        }
 91
 92        public void SetInvisible()
 93        {
 094            if (lastRequestedState == State.Invisible)
 095                return;
 96
 097            lastRequestedState = State.Invisible;
 098            if (player?.renderer == null)
 099                return;
 100
 0101            SetAvatarFeatures(false, false);
 0102            StartTransition(0, 0);
 0103        }
 104
 105        private void StartTransition(float newTargetAvatarFade, float newTargetImpostorFade)
 106        {
 6107            CoroutineStarter.Stop(currentTransition);
 6108            currentTransition = CoroutineStarter.Start(Transition(newTargetAvatarFade, newTargetImpostorFade));
 6109        }
 110
 111        internal IEnumerator Transition(float targetAvatarFade, float targetImpostorFade, float transitionDuration = TRA
 112        {
 7113            while (!player.renderer.isReady)
 114            {
 0115                yield return null;
 116            }
 117
 7118            player.renderer.SetAvatarFade(avatarFade);
 7119            player.renderer.SetImpostorFade(impostorFade);
 7120            player.renderer.SetRendererEnabled(true);
 7121            player.renderer.SetImpostorVisibility(true);
 122
 299123            while (!Mathf.Approximately(avatarFade, targetAvatarFade) || !Mathf.Approximately(impostorFade, targetImpost
 124            {
 292125                avatarFade = Mathf.MoveTowards(avatarFade, targetAvatarFade, (1f / transitionDuration) * Time.deltaTime)
 292126                impostorFade = Mathf.MoveTowards(impostorFade, targetImpostorFade, (1f / transitionDuration) * Time.delt
 292127                player.renderer.SetAvatarFade(avatarFade);
 292128                player.renderer.SetImpostorFade(impostorFade);
 292129                yield return null;
 130            }
 131
 7132            avatarFade = targetAvatarFade;
 7133            impostorFade = targetImpostorFade;
 134
 7135            bool avatarVisibility = !Mathf.Approximately(avatarFade, 0);
 7136            player.renderer.SetRendererEnabled(avatarVisibility);
 7137            bool impostorVisibility = !Mathf.Approximately(impostorFade, 0);
 7138            player.renderer.SetImpostorVisibility(impostorVisibility);
 7139            currentTransition = null;
 7140        }
 141
 142        private void SetAvatarFeatures(bool newSSAOEnabled, bool newFacialFeaturesEnabled)
 143        {
 6144            if (SSAOEnabled != newSSAOEnabled)
 145            {
 5146                player.renderer.SetSSAOEnabled(newSSAOEnabled);
 5147                SSAOEnabled = newSSAOEnabled;
 148            }
 149
 6150            if (facialFeaturesEnabled != newFacialFeaturesEnabled)
 151            {
 5152                player.renderer.SetFacialFeaturesVisible(newFacialFeaturesEnabled);
 5153                facialFeaturesEnabled = newFacialFeaturesEnabled;
 154            }
 6155        }
 156
 157        public void Dispose()
 158        {
 0159            lastRequestedState = null;
 0160            CoroutineStarter.Stop(currentTransition);
 0161        }
 162    }
 163}