< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BaseAvatarReveal()0%2100%
Start()0%6200%
GetMainRenderer()0%2100%
InitializeColorGradient()0%2100%
InjectLodSystem(...)0%2100%
AddTarget(...)0%20400%
Update()0%6200%
UpdateMaterials()0%20400%
StartAvatarRevealAnimation()0%20400%
OnRevealAnimationEnd()0%2100%
SetFullRendered()0%6200%
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    [ColorUsageAttribute(true, true, 0f, 8f, 0.125f, 3f)]
 18    public Color baseColor;
 19    [ColorUsageAttribute(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 revealSpeed;
 29
 30    public float fadeInSpeed;
 31    Material _ghostMaterial;
 32
 33    private float startH;
 34    private float startS;
 35    private float startV;
 36    private float endH;
 37    private float endS;
 38    private float endV;
 39    private bool isRevealing;
 40
 041    public List<Renderer> targets = new List<Renderer>();
 042    List<Material> _materials = new List<Material>();
 43
 44    private void Start()
 45    {
 046        _ghostMaterial = meshRenderer.material;
 047        InitializeColorGradient();
 048        foreach (Renderer r in targets)
 49        {
 050            _materials.Add(r.material);
 51        }
 052    }
 53
 54    public SkinnedMeshRenderer GetMainRenderer()
 55    {
 056        return meshRenderer;
 57    }
 58
 59    private void InitializeColorGradient()
 60    {
 061        Color.RGBToHSV(baseColor, out startH, out startS, out startV);
 062        Color.RGBToHSV(maxColor, out endH, out endS, out endV);
 063        Color newColor = Color.HSVToRGB(Random.Range(startH, endH), startS, startV);
 064        _ghostMaterial.SetColor("_Color", new Color(newColor.r, newColor.g, newColor.b, 0));
 065    }
 66
 67    public void InjectLodSystem(ILOD lod)
 68    {
 069        this.lod = lod;
 070    }
 71
 72    public void AddTarget(MeshRenderer newTarget)
 73    {
 074        if (newTarget == null)
 075            return;
 76
 077        targets.Add(newTarget);
 078        newTarget?.material.SetVector("_RevealPosition", Vector3.zero);
 079        newTarget?.material.SetVector("_RevealNormal", new Vector3(0, -1, 0));
 080        _materials.Add(newTarget.material);
 081    }
 82
 83    private void Update()
 84    {
 085        if (lod.lodIndex >= 2)
 086            SetFullRendered();
 87
 088        UpdateMaterials();
 089    }
 90
 91    void UpdateMaterials()
 92    {
 093        if (avatarLoaded)
 094            return;
 95
 096        if(_ghostMaterial.GetColor("_Color").a < 0.9f)
 97        {
 098            Color gColor = _ghostMaterial.GetColor("_Color");
 099            Color tempColor = new Color(gColor.r, gColor.g, gColor.b, gColor.a + Time.deltaTime * fadeInSpeed);
 0100            _ghostMaterial.SetColor("_Color", tempColor);
 101        }
 102
 0103        _ghostMaterial.SetVector("_RevealPosition", revealer.transform.localPosition);
 104
 0105        foreach (Material m in _materials)
 106        {
 0107            m.SetVector("_RevealPosition", -revealer.transform.localPosition);
 108        }
 0109    }
 110
 111    public async UniTask StartAvatarRevealAnimation(bool withTransition, CancellationToken cancellationToken)
 112    {
 113        try
 114        {
 0115            if (!withTransition)
 116            {
 0117                SetFullRendered();
 0118                return;
 119            }
 120
 0121            isRevealing = true;
 0122            animation.Play();
 0123            await UniTask.WaitUntil(() => !isRevealing, cancellationToken: cancellationToken).AttachExternalCancellation
 0124        }
 0125        catch(OperationCanceledException)
 126        {
 0127            SetFullRendered();
 0128        }
 0129    }
 130
 131    public void OnRevealAnimationEnd()
 132    {
 0133        isRevealing = false;
 0134        meshRenderer.enabled = false;
 0135    }
 136
 137    private void SetFullRendered()
 138    {
 0139        meshRenderer.enabled = false;
 0140        animation.Stop();
 141        const float REVEALED_POSITION = -10;
 0142        foreach (Material m in _materials)
 143        {
 0144            m.SetVector("_RevealPosition", new Vector3(0, REVEALED_POSITION, 0));
 145        }
 0146        _ghostMaterial.SetVector("_RevealPosition", new Vector3(0, 2.5f, 0));
 0147        DisableParticleEffects();
 0148        avatarLoaded = true;
 0149    }
 150
 151    public void Reset()
 152    {
 0153        Color gColor = _ghostMaterial.GetColor("_Color");
 0154        Color tempColor = new Color(gColor.r, gColor.g, gColor.b, 0);
 0155        _ghostMaterial.SetColor("_Color", tempColor);
 0156        avatarLoaded = false;
 0157        meshRenderer.enabled = true;
 0158        targets = new List<Renderer>();
 0159        _materials = new List<Material>();
 0160        revealer.transform.position = Vector3.zero;
 0161    }
 162
 163    private void DisableParticleEffects()
 164    {
 0165        foreach (GameObject p in particleEffects)
 166        {
 0167            p.SetActive(false);
 168        }
 0169    }
 170
 171    public void OnDisable()
 172    {
 0173        SetFullRendered();
 0174    }
 175}