< Summary

Class:AvatarSystem.BodyShapeLoader
Assembly:AvatarSystem
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/Loader/BodyShapeLoader.cs
Covered lines:88
Uncovered lines:17
Coverable lines:105
Total lines:171
Line coverage:83.8% (88 of 105)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BodyShapeLoader(...)0%110100%
Load()0%7.397080%
LoadMouth()0%8.66058.33%
LoadEyebrows()0%8.66058.33%
LoadEyes()0%8.66058.33%
UpdateColors(...)0%770100%
LoadWearable()0%5.015091.67%
IsValid(...)0%22.9619077.78%
Dispose()0%880100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Threading;
 3using Cysharp.Threading.Tasks;
 4using DCL;
 5using DCL.Helpers;
 6using UnityEngine;
 7using Object = UnityEngine.Object;
 8
 9namespace AvatarSystem
 10{
 11    public class BodyShapeLoader : IBodyshapeLoader
 12    {
 113        public WearableItem wearable { get; }
 114        public Rendereable rendereable => bodyshapeRetriever?.rendereable;
 015        public IWearableLoader.Status status { get; private set; }
 116        public WearableItem eyes { get; }
 117        public WearableItem eyebrows { get; }
 118        public WearableItem mouth { get; }
 019        public SkinnedMeshRenderer eyesRenderer { get; private set; }
 020        public SkinnedMeshRenderer eyebrowsRenderer { get; private set; }
 021        public SkinnedMeshRenderer mouthRenderer { get; private set; }
 022        public SkinnedMeshRenderer headRenderer { get; private set; }
 023        public SkinnedMeshRenderer feetRenderer { get; private set; }
 024        public SkinnedMeshRenderer upperBodyRenderer { get; private set; }
 025        public SkinnedMeshRenderer lowerBodyRenderer { get; private set; }
 26
 27        internal readonly IWearableRetriever bodyshapeRetriever;
 28        internal readonly IFacialFeatureRetriever eyesRetriever;
 29        internal readonly IFacialFeatureRetriever eyebrowsRetriever;
 30        internal readonly IFacialFeatureRetriever mouthRetriever;
 31
 7032        public BodyShapeLoader(IRetrieverFactory retrieverFactory, WearableItem bodyshape, WearableItem eyes, WearableIt
 33        {
 7034            wearable = bodyshape;
 7035            this.eyes = eyes;
 7036            this.eyebrows = eyebrows;
 7037            this.mouth = mouth;
 38
 7039            bodyshapeRetriever = retrieverFactory.GetWearableRetriever();
 7040            eyesRetriever = retrieverFactory.GetFacialFeatureRetriever();
 7041            eyebrowsRetriever = retrieverFactory.GetFacialFeatureRetriever();
 7042            mouthRetriever = retrieverFactory.GetFacialFeatureRetriever();
 7043        }
 44
 45        public async UniTask Load(GameObject container, AvatarSettings avatarSettings, CancellationToken ct = default)
 46        {
 8647            ct.ThrowIfCancellationRequested();
 48
 49            try
 50            {
 8551                if (status == IWearableLoader.Status.Succeeded)
 52                {
 053                    UpdateColors(avatarSettings);
 054                    return;
 55                }
 56
 8557                status = IWearableLoader.Status.Idle;
 58
 23759                await LoadWearable(container, ct);
 60
 161                (headRenderer, upperBodyRenderer, lowerBodyRenderer, feetRenderer, eyesRenderer, eyebrowsRenderer, mouth
 62
 163                await (LoadEyes(ct), LoadEyebrows(ct), LoadMouth(ct));
 64
 165                UpdateColors(avatarSettings);
 166                status = IWearableLoader.Status.Succeeded;
 167            }
 8468            catch (Exception)
 69            {
 8470                status = IWearableLoader.Status.Failed;
 8471                Dispose();
 8472                throw;
 73            }
 174        }
 75
 76        private async UniTask LoadMouth(CancellationToken ct)
 77        {
 178            if (mouth == null) return;
 179            (Texture main, Texture mask) = await mouthRetriever.Retrieve(mouth, wearable.id, ct);
 180            mouthRenderer.material = new Material(Resources.Load<Material>("Mouth Material"));
 181            if (main == null)
 082                throw new Exception($"Couldn't fetch main texture for {mouth.id}");
 183            mouthRenderer.material.SetTexture(ShaderUtils.BaseMap, main);
 184            if (mask != null)
 085                mouthRenderer.material.SetTexture(ShaderUtils.TintMask, mask);
 186        }
 87
 88        private async UniTask LoadEyebrows(CancellationToken ct)
 89        {
 190            if (eyebrows == null) return;
 191            (Texture main, Texture mask) = await eyebrowsRetriever.Retrieve(eyebrows, wearable.id, ct);
 192            eyebrowsRenderer.material = new Material(Resources.Load<Material>("Eyebrow Material"));
 193            if (main == null)
 094                throw new Exception($"Couldn't fetch main texture for {eyebrows.id}");
 195            eyebrowsRenderer.material.SetTexture(ShaderUtils.BaseMap, main);
 196            if (mask != null)
 097                eyebrowsRenderer.material.SetTexture(ShaderUtils.BaseMap, mask);
 198        }
 99
 100        private async UniTask LoadEyes(CancellationToken ct)
 101        {
 1102            if (eyes == null) return;
 1103            (Texture main, Texture mask) = await eyesRetriever.Retrieve(eyes, wearable.id, ct);
 1104            eyesRenderer.material = new Material(Resources.Load<Material>("Eye Material"));
 1105            if (main == null)
 0106                throw new Exception($"Couldn't fetch main texture for {eyes.id}");
 1107            eyesRenderer.material.SetTexture(ShaderUtils.EyesTexture, main);
 1108            if (mask != null)
 0109                eyesRenderer.material.SetTexture(ShaderUtils.IrisMask, mask);
 1110        }
 111
 112        private void UpdateColors(AvatarSettings avatarSettings)
 113        {
 1114            AvatarSystemUtils.PrepareMaterialColors(rendereable, avatarSettings.skinColor, avatarSettings.hairColor);
 1115            eyesRenderer?.material?.SetColor(ShaderUtils.EyeTint, avatarSettings.eyesColor);
 1116            eyebrowsRenderer?.material?.SetColor(ShaderUtils.BaseColor, avatarSettings.hairColor);
 1117            mouthRenderer?.material?.SetColor(ShaderUtils.BaseColor, avatarSettings.skinColor);
 1118        }
 119
 120        private async UniTask<Rendereable> LoadWearable(GameObject container, CancellationToken ct)
 121        {
 85122            ct.ThrowIfCancellationRequested();
 123
 85124            bodyshapeRetriever.Dispose();
 125
 85126            WearableItem.Representation representation = wearable.GetRepresentation(wearable.id);
 85127            if (representation == null)
 0128                throw new Exception("Couldn't find a representation for this bodyshape");
 129
 237130            Rendereable bodyshapeRenderable = await bodyshapeRetriever.Retrieve(container, wearable.GetContentProvider(w
 131
 8132            if (bodyshapeRenderable == null) // fail safe, we shouldnt reach this since .Retrieve should throw if anythi
 7133                throw new Exception("Couldn't load bodyshape");
 134
 1135            return bodyshapeRenderable;
 1136        }
 137
 138        public bool IsValid(WearableItem bodyshape, WearableItem eyebrows, WearableItem eyes, WearableItem mouth)
 139        {
 36140            if (wearable.id != bodyshape?.id) return false;
 35141            if (this.eyebrows?.id != eyebrows?.id) return false;
 17142            if (this.mouth?.id != mouth?.id) return false;
 17143            if (this.eyes?.id != eyes?.id) return false;
 17144            return true;
 145        }
 146
 147        public void Dispose()
 148        {
 305149            status = IWearableLoader.Status.Idle;
 305150            bodyshapeRetriever?.Dispose();
 305151            eyesRetriever?.Dispose();
 305152            eyebrowsRetriever?.Dispose();
 305153            mouthRetriever?.Dispose();
 154
 305155            if (eyesRenderer != null)
 1156                Object.Destroy(eyesRenderer.material);
 305157            if (eyebrowsRenderer != null)
 1158                Object.Destroy(eyebrowsRenderer.material);
 305159            if (mouthRenderer != null)
 1160                Object.Destroy(mouthRenderer.material);
 161
 305162            upperBodyRenderer = null;
 305163            lowerBodyRenderer = null;
 305164            feetRenderer = null;
 305165            headRenderer = null;
 305166            eyesRenderer = null;
 305167            eyebrowsRenderer = null;
 305168            mouthRenderer = null;
 305169        }
 170    }
 171}