< Summary

Class:BaseAvatarReveal
Assembly:AvatarReveal
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/LoadingAvatar/CrossSection/BaseAvatarReveal.cs
Covered lines:2
Uncovered lines:67
Coverable lines:69
Total lines:174
Line coverage:2.8% (2 of 69)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BaseAvatarReveal()0%110100%
Start()0%6200%
GetMainRenderer()0%2100%
InitializeColorGradient()0%2100%
InjectLodSystem(...)0%2100%
AddTarget(...)0%20400%
StartAvatarRevealAnimation()0%56700%
UpdateRevealerPosition()0%20400%
FadeInGhostMaterial()0%20400%
OnRevealAnimationEnd()0%2100%
SetFullRendered()0%12300%
Reset()0%2100%
DisableParticleEffects()0%6200%
OnDisable()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/LoadingAvatar/CrossSection/BaseAvatarReveal.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Threading;
 5using UnityEngine;
 6using DCL.Helpers;
 7using DCL;
 8using AvatarSystem;
 9using Cysharp.Threading.Tasks;
 10using Random = UnityEngine.Random;
 11
 12public class BaseAvatarReveal : MonoBehaviour, IBaseAvatarRevealer
 13{
 14    [SerializeField] private Animation animation;
 15    [SerializeField] private List<GameObject> particleEffects;
 16
 17    [ColorUsage(true, true, 0f, 8f, 0.125f, 3f)]
 18    public Color baseColor;
 19    [ColorUsage(true, true, 0f, 8f, 0.125f, 3f)]
 20    public Color maxColor;
 21
 22    public SkinnedMeshRenderer meshRenderer;
 23    public bool avatarLoaded;
 24    public GameObject revealer;
 25
 26    private ILOD lod;
 27
 28    public float fadeInSpeed;
 29    Material _ghostMaterial;
 30
 31    private float startH;
 32    private float startS;
 33    private float startV;
 34    private float endH;
 35    private float endS;
 36    private float endV;
 37    private bool isRevealing;
 38
 139    public List<Renderer> targets = new List<Renderer>();
 140    List<Material> _materials = new List<Material>();
 41    private Coroutine updateAvatarRevealerRoutine = null;
 42
 43    private void Start()
 44    {
 045        _ghostMaterial = meshRenderer.material;
 046        InitializeColorGradient();
 47
 048        foreach (Renderer r in targets) { _materials.Add(r.material); }
 049        FadeInGhostMaterial().Forget();
 050    }
 51
 52    public SkinnedMeshRenderer GetMainRenderer()
 53    {
 054        return meshRenderer;
 55    }
 56
 57    private void InitializeColorGradient()
 58    {
 059        Color.RGBToHSV(baseColor, out startH, out startS, out startV);
 060        Color.RGBToHSV(maxColor, out endH, out endS, out endV);
 061        Color newColor = Color.HSVToRGB(Random.Range(startH, endH), startS, startV);
 062        _ghostMaterial.SetColor("_Color", new Color(newColor.r, newColor.g, newColor.b, 0));
 063    }
 64
 65    public void InjectLodSystem(ILOD lod)
 66    {
 067        this.lod = lod;
 068    }
 69
 70    public void AddTarget(MeshRenderer newTarget)
 71    {
 072        if (newTarget == null)
 073            return;
 74
 075        targets.Add(newTarget);
 076        newTarget?.material.SetVector("_RevealPosition", Vector3.zero);
 077        newTarget?.material.SetVector("_RevealNormal", new Vector3(0, -1, 0));
 078        _materials.Add(newTarget.material);
 079    }
 80
 81    public async UniTask StartAvatarRevealAnimation(bool withTransition, CancellationToken cancellationToken)
 82    {
 083        updateAvatarRevealerRoutine = StartCoroutine(UpdateRevealerPosition());
 84
 85        try
 86        {
 087            if (!withTransition || lod.lodIndex >= 2)
 88            {
 089                SetFullRendered();
 090                return;
 91            }
 92
 093            isRevealing = true;
 094            animation.Play();
 095            await UniTask.WaitUntil(() => !isRevealing, cancellationToken: cancellationToken).AttachExternalCancellation
 096        }
 097        catch (OperationCanceledException) { SetFullRendered(); }
 98        finally
 99        {
 0100            StopCoroutine(updateAvatarRevealerRoutine);
 0101            SetFullRendered();
 102        }
 0103    }
 104
 105    private IEnumerator UpdateRevealerPosition()
 106    {
 0107        _ghostMaterial.SetVector("_RevealPosition", revealer.transform.localPosition);
 108
 0109        while (true)
 110        {
 0111            foreach (Material m in _materials) { m.SetVector("_RevealPosition", -revealer.transform.localPosition); }
 112
 0113            yield return new WaitForSeconds(0.01f);
 114        }
 115    }
 116
 117    private async UniTask FadeInGhostMaterial()
 118    {
 119        Color gColor;
 120        Color tempColor;
 121
 0122        while (_ghostMaterial.GetColor("_Color").a < 0.9f)
 123        {
 0124            gColor = _ghostMaterial.GetColor("_Color");
 0125            tempColor = new Color(gColor.r, gColor.g, gColor.b, gColor.a + Time.deltaTime * fadeInSpeed);
 0126            _ghostMaterial.SetColor("_Color", tempColor);
 127
 0128            await UniTask.Delay(50);
 129        }
 0130    }
 131
 132    public void OnRevealAnimationEnd()
 133    {
 0134        isRevealing = false;
 0135        meshRenderer.enabled = false;
 0136    }
 137
 138    private void SetFullRendered()
 139    {
 0140        meshRenderer.enabled = false;
 0141        animation.Stop();
 142        const float REVEALED_POSITION = -10;
 0143        revealer.transform.position = new Vector3(0, REVEALED_POSITION, 0);
 144
 0145        foreach (Material m in _materials) { m.SetVector("_RevealPosition", new Vector3(0, REVEALED_POSITION, 0)); }
 146
 0147        _ghostMaterial?.SetVector("_RevealPosition", new Vector3(0, 2.5f, 0));
 0148        DisableParticleEffects();
 0149        avatarLoaded = true;
 0150    }
 151
 152    public void Reset()
 153    {
 0154        Color gColor = _ghostMaterial.GetColor("_Color");
 0155        Color tempColor = new Color(gColor.r, gColor.g, gColor.b, 0);
 0156        _ghostMaterial.SetColor("_Color", tempColor);
 0157        avatarLoaded = false;
 0158        meshRenderer.enabled = true;
 0159        targets = new List<Renderer>();
 0160        _materials = new List<Material>();
 0161        revealer.transform.position = Vector3.zero;
 0162        FadeInGhostMaterial().Forget();
 0163    }
 164
 165    private void DisableParticleEffects()
 166    {
 0167        foreach (GameObject p in particleEffects) { p.SetActive(false); }
 0168    }
 169
 170    public void OnDisable()
 171    {
 0172        SetFullRendered();
 0173    }
 174}