< Summary

Class:AvatarSystem.Avatar
Assembly:AvatarSystem
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/Avatar.cs
Covered lines:56
Uncovered lines:7
Coverable lines:63
Total lines:128
Line coverage:88.8% (56 of 63)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Avatar(...)0%110100%
Load()0%990100%
AddVisibilityConstrain(...)0%110100%
RemoveVisibilityConstrain(...)0%110100%
SetExpression(...)0%220100%
SetLODLevel(...)0%2100%
SetAnimationThrottling(...)0%2100%
SetImpostorTexture(...)0%2100%
SetImpostorTint(...)0%2100%
GetBones()0%2100%
Dispose()0%880100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/Avatar.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Threading;
 4using Cysharp.Threading.Tasks;
 5using GPUSkinning;
 6using UnityEngine;
 7
 8namespace AvatarSystem
 9{
 10    public class Avatar : IAvatar
 11    {
 12        private const float RESCALING_BOUNDS_FACTOR = 100f;
 13        internal const string LOADING_VISIBILITY_CONSTRAIN = "Loading";
 14        private readonly IAvatarCurator avatarCurator;
 15        private readonly ILoader loader;
 16        private readonly IAnimator animator;
 17        private readonly IVisibility visibility;
 18        private readonly ILOD lod;
 19        private readonly IGPUSkinning gpuSkinning;
 20        private readonly IGPUSkinningThrottler gpuSkinningThrottler;
 130821        private CancellationTokenSource disposeCts = new CancellationTokenSource();
 22
 323        public IAvatar.Status status { get; private set; } = IAvatar.Status.Idle;
 024        public Vector3 extents { get; private set; }
 025        public int lodLevel => lod?.lodIndex ?? 0;
 26
 130827        public Avatar(IAvatarCurator avatarCurator, ILoader loader, IAnimator animator, IVisibility visibility, ILOD lod
 28        {
 130829            this.avatarCurator = avatarCurator;
 130830            this.loader = loader;
 130831            this.animator = animator;
 130832            this.visibility = visibility;
 130833            this.lod = lod;
 130834            this.gpuSkinning = gpuSkinning;
 130835            this.gpuSkinningThrottler = gpuSkinningThrottler;
 130836        }
 37
 38        /// <summary>
 39        /// Starts the loading process for the Avatar.
 40        /// </summary>
 41        /// <param name="wearablesIds"></param>
 42        /// <param name="settings"></param>
 43        /// <param name="ct"></param>
 44        public async UniTask Load(List<string> wearablesIds, AvatarSettings settings, CancellationToken ct = default)
 45        {
 8146            disposeCts ??= new CancellationTokenSource();
 47
 8148            status = IAvatar.Status.Idle;
 8149            CancellationToken linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct, disposeCts.Token).Token;
 50
 8151            linkedCt.ThrowIfCancellationRequested();
 52
 53            try
 54            {
 8055                visibility.AddGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN);
 8056                WearableItem bodyshape = null;
 8057                WearableItem eyes = null;
 8058                WearableItem eyebrows = null;
 8059                WearableItem mouth = null;
 8060                List<WearableItem> wearables = null;
 61
 8262                (bodyshape, eyes, eyebrows, mouth, wearables) = await avatarCurator.Curate(settings , wearablesIds, link
 63
 23064                await loader.Load(bodyshape, eyes, eyebrows, mouth, wearables, settings, linkedCt);
 65
 66                //Scale the bounds due to the giant avatar not being skinned yet
 167                extents = loader.combinedRenderer.localBounds.extents * 2f / RESCALING_BOUNDS_FACTOR;
 68
 169                animator.Prepare(settings.bodyshapeId, loader.bodyshapeContainer);
 70
 171                gpuSkinning.Prepare(loader.combinedRenderer);
 172                gpuSkinningThrottler.Bind(gpuSkinning);
 73
 174                visibility.Bind(gpuSkinning.renderer, loader.facialFeaturesRenderers);
 175                visibility.RemoveGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN);
 76
 177                lod.Bind(gpuSkinning.renderer);
 178                gpuSkinningThrottler.Start();
 79
 180                status = IAvatar.Status.Loaded;
 181            }
 7782            catch (OperationCanceledException)
 83            {
 7784                Dispose();
 7785            }
 286            catch (Exception e)
 87            {
 288                Dispose();
 289                Debug.Log($"Avatar.Load failed with wearables:[{string.Join(",", wearablesIds)}] for bodyshape:{settings
 290                throw;
 91            }
 92            finally
 93            {
 8094                disposeCts?.Dispose();
 8095                disposeCts = null;
 96            }
 7897        }
 98
 7699        public void AddVisibilityConstrain(string key) { visibility.AddGlobalConstrain(key); }
 100
 10101        public void RemoveVisibilityConstrain(string key) { visibility.RemoveGlobalConstrain(key); }
 102
 4103        public void SetExpression(string expressionId, long timestamps) { animator?.PlayExpression(expressionId, timesta
 104
 0105        public void SetLODLevel(int lodIndex) { lod.SetLodIndex(lodIndex); }
 106
 0107        public void SetAnimationThrottling(int framesBetweenUpdate) { gpuSkinningThrottler.SetThrottling(framesBetweenUp
 108
 0109        public void SetImpostorTexture(Texture2D impostorTexture) { lod.SetImpostorTexture(impostorTexture); }
 110
 0111        public void SetImpostorTint(Color color) { lod.SetImpostorTint(color); }
 112
 0113        public Transform[] GetBones() => loader.GetBones();
 114
 115        public void Dispose()
 116        {
 1407117            status = IAvatar.Status.Idle;
 1407118            disposeCts?.Cancel();
 1407119            disposeCts?.Dispose();
 1407120            disposeCts = null;
 1407121            avatarCurator?.Dispose();
 1407122            loader?.Dispose();
 1407123            visibility?.Dispose();
 1407124            lod?.Dispose();
 1407125            gpuSkinningThrottler?.Dispose();
 1407126        }
 127    }
 128}