< 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:61
Uncovered lines:18
Coverable lines:79
Total lines:169
Line coverage:77.2% (61 of 79)
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%
SetThrottling(...)0%330100%
StartTransition(...)0%110100%
Transition()0%7.017095%
SetAvatarFeatures(...)0%330100%
UpdateImpostorTint(...)0%2100%
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        void UpdateImpostorTint(float distanceToMainPlayer);
 16        void SetThrottling(int framesBetweenUpdates);
 17    }
 18
 19    public class AvatarLODController : IAvatarLODController
 20    {
 21        internal enum State
 22        {
 23            Invisible,
 24            FullAvatar,
 25            SimpleAvatar,
 26            Impostor,
 27        }
 28
 29        private const float TRANSITION_DURATION = 0.5f;
 30
 031        public Player player { get; }
 32
 33        internal float avatarFade;
 34        internal float impostorFade;
 35
 36        internal bool SSAOEnabled;
 37        internal bool facialFeaturesEnabled;
 38
 39        internal Coroutine currentTransition = null;
 40        internal State? lastRequestedState = null;
 41
 1442        public AvatarLODController(Player player)
 43        {
 1444            this.player = player;
 1445            avatarFade = 1;
 1446            impostorFade = 0;
 1447            SSAOEnabled = true;
 1448            facialFeaturesEnabled = true;
 1449            if (player?.renderer == null)
 050                return;
 1451            player.renderer.SetAvatarFade(avatarFade);
 1452            player.renderer.SetImpostorFade(impostorFade);
 1453        }
 54
 55        public void SetFullAvatar()
 56        {
 357            if (lastRequestedState == State.FullAvatar)
 058                return;
 59
 360            lastRequestedState = State.FullAvatar;
 361            if (player?.renderer == null)
 162                return;
 63
 264            SetAvatarFeatures(true, true);
 265            StartTransition(1, 0);
 266        }
 67
 68        public void SetSimpleAvatar()
 69        {
 370            if (lastRequestedState == State.SimpleAvatar)
 071                return;
 72
 373            lastRequestedState = State.SimpleAvatar;
 374            if (player?.renderer == null)
 175                return;
 76
 277            SetAvatarFeatures(false, false);
 278            StartTransition(1, 0);
 279        }
 80
 81        public void SetImpostor()
 82        {
 383            if (lastRequestedState == State.Impostor)
 084                return;
 85
 386            lastRequestedState = State.Impostor;
 387            if (player?.renderer == null)
 188                return;
 89
 290            SetAvatarFeatures(false, false);
 291            StartTransition(0, 1);
 292        }
 93
 94        public void SetInvisible()
 95        {
 096            if (lastRequestedState == State.Invisible)
 097                return;
 98
 099            lastRequestedState = State.Invisible;
 0100            if (player?.renderer == null)
 0101                return;
 102
 0103            SetAvatarFeatures(false, false);
 0104            StartTransition(0, 0);
 0105        }
 106
 6107        public void SetThrottling(int framesBetweenUpdates) { player?.renderer?.SetThrottling(framesBetweenUpdates); }
 108
 109        private void StartTransition(float newTargetAvatarFade, float newTargetImpostorFade)
 110        {
 6111            CoroutineStarter.Stop(currentTransition);
 6112            currentTransition = CoroutineStarter.Start(Transition(newTargetAvatarFade, newTargetImpostorFade));
 6113        }
 114
 115        internal IEnumerator Transition(float targetAvatarFade, float targetImpostorFade, float transitionDuration = TRA
 116        {
 7117            while (!player.renderer.isReady)
 118            {
 0119                yield return null;
 120            }
 121
 7122            player.renderer.SetAvatarFade(avatarFade);
 7123            player.renderer.SetImpostorFade(impostorFade);
 7124            player.renderer.SetRendererEnabled(true);
 7125            player.renderer.SetImpostorVisibility(true);
 126
 278127            while (!Mathf.Approximately(avatarFade, targetAvatarFade) || !Mathf.Approximately(impostorFade, targetImpost
 128            {
 271129                avatarFade = Mathf.MoveTowards(avatarFade, targetAvatarFade, (1f / transitionDuration) * Time.deltaTime)
 271130                impostorFade = Mathf.MoveTowards(impostorFade, targetImpostorFade, (1f / transitionDuration) * Time.delt
 271131                player.renderer.SetAvatarFade(avatarFade);
 271132                player.renderer.SetImpostorFade(impostorFade);
 271133                yield return null;
 134            }
 135
 7136            avatarFade = targetAvatarFade;
 7137            impostorFade = targetImpostorFade;
 138
 7139            bool avatarVisibility = !Mathf.Approximately(avatarFade, 0);
 7140            player.renderer.SetRendererEnabled(avatarVisibility);
 7141            bool impostorVisibility = !Mathf.Approximately(impostorFade, 0);
 7142            player.renderer.SetImpostorVisibility(impostorVisibility);
 7143            currentTransition = null;
 7144        }
 145
 146        private void SetAvatarFeatures(bool newSSAOEnabled, bool newFacialFeaturesEnabled)
 147        {
 6148            if (SSAOEnabled != newSSAOEnabled)
 149            {
 5150                player.renderer.SetSSAOEnabled(newSSAOEnabled);
 5151                SSAOEnabled = newSSAOEnabled;
 152            }
 153
 6154            if (facialFeaturesEnabled != newFacialFeaturesEnabled)
 155            {
 5156                player.renderer.SetFacialFeaturesVisible(newFacialFeaturesEnabled);
 5157                facialFeaturesEnabled = newFacialFeaturesEnabled;
 158            }
 6159        }
 160
 0161        public void UpdateImpostorTint(float distanceToMainPlayer) { player.renderer.SetImpostorColor(AvatarRendererHelp
 162
 163        public void Dispose()
 164        {
 0165            lastRequestedState = null;
 0166            CoroutineStarter.Stop(currentTransition);
 0167        }
 168    }
 169}