| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using UnityEngine; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | namespace AvatarSystem |
| | 10 | | { |
| | 11 | | public class BodyShapeLoader : IBodyshapeLoader |
| | 12 | | { |
| 1 | 13 | | public WearableItem wearable { get; } |
| 1 | 14 | | public Rendereable rendereable => bodyshapeRetriever?.rendereable; |
| 0 | 15 | | public IWearableLoader.Status status { get; private set; } |
| 1 | 16 | | public WearableItem eyes { get; } |
| 1 | 17 | | public WearableItem eyebrows { get; } |
| 1 | 18 | | public WearableItem mouth { get; } |
| 0 | 19 | | public SkinnedMeshRenderer eyesRenderer { get; private set; } |
| 0 | 20 | | public SkinnedMeshRenderer eyebrowsRenderer { get; private set; } |
| 0 | 21 | | public SkinnedMeshRenderer mouthRenderer { get; private set; } |
| 0 | 22 | | public SkinnedMeshRenderer headRenderer { get; private set; } |
| 0 | 23 | | public SkinnedMeshRenderer feetRenderer { get; private set; } |
| 0 | 24 | | public SkinnedMeshRenderer upperBodyRenderer { get; private set; } |
| 0 | 25 | | 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 | |
|
| 70 | 32 | | public BodyShapeLoader(IRetrieverFactory retrieverFactory, WearableItem bodyshape, WearableItem eyes, WearableIt |
| | 33 | | { |
| 70 | 34 | | wearable = bodyshape; |
| 70 | 35 | | this.eyes = eyes; |
| 70 | 36 | | this.eyebrows = eyebrows; |
| 70 | 37 | | this.mouth = mouth; |
| | 38 | |
|
| 70 | 39 | | bodyshapeRetriever = retrieverFactory.GetWearableRetriever(); |
| 70 | 40 | | eyesRetriever = retrieverFactory.GetFacialFeatureRetriever(); |
| 70 | 41 | | eyebrowsRetriever = retrieverFactory.GetFacialFeatureRetriever(); |
| 70 | 42 | | mouthRetriever = retrieverFactory.GetFacialFeatureRetriever(); |
| 70 | 43 | | } |
| | 44 | |
|
| | 45 | | public async UniTask Load(GameObject container, AvatarSettings avatarSettings, CancellationToken ct = default) |
| | 46 | | { |
| 86 | 47 | | ct.ThrowIfCancellationRequested(); |
| | 48 | |
|
| | 49 | | try |
| | 50 | | { |
| 85 | 51 | | if (status == IWearableLoader.Status.Succeeded) |
| | 52 | | { |
| 0 | 53 | | UpdateColors(avatarSettings); |
| 0 | 54 | | return; |
| | 55 | | } |
| | 56 | |
|
| 85 | 57 | | status = IWearableLoader.Status.Idle; |
| | 58 | |
|
| 237 | 59 | | await LoadWearable(container, ct); |
| | 60 | |
|
| 1 | 61 | | (headRenderer, upperBodyRenderer, lowerBodyRenderer, feetRenderer, eyesRenderer, eyebrowsRenderer, mouth |
| | 62 | |
|
| 1 | 63 | | await (LoadEyes(ct), LoadEyebrows(ct), LoadMouth(ct)); |
| | 64 | |
|
| 1 | 65 | | UpdateColors(avatarSettings); |
| 1 | 66 | | status = IWearableLoader.Status.Succeeded; |
| 1 | 67 | | } |
| 84 | 68 | | catch (Exception) |
| | 69 | | { |
| 84 | 70 | | status = IWearableLoader.Status.Failed; |
| 84 | 71 | | Dispose(); |
| 84 | 72 | | throw; |
| | 73 | | } |
| 1 | 74 | | } |
| | 75 | |
|
| | 76 | | private async UniTask LoadMouth(CancellationToken ct) |
| | 77 | | { |
| 1 | 78 | | if (mouth == null) return; |
| 1 | 79 | | (Texture main, Texture mask) = await mouthRetriever.Retrieve(mouth, wearable.id, ct); |
| 1 | 80 | | mouthRenderer.material = new Material(Resources.Load<Material>("Mouth Material")); |
| 1 | 81 | | if (main == null) |
| 0 | 82 | | throw new Exception($"Couldn't fetch main texture for {mouth.id}"); |
| 1 | 83 | | mouthRenderer.material.SetTexture(ShaderUtils.BaseMap, main); |
| 1 | 84 | | if (mask != null) |
| 0 | 85 | | mouthRenderer.material.SetTexture(ShaderUtils.TintMask, mask); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | private async UniTask LoadEyebrows(CancellationToken ct) |
| | 89 | | { |
| 1 | 90 | | if (eyebrows == null) return; |
| 1 | 91 | | (Texture main, Texture mask) = await eyebrowsRetriever.Retrieve(eyebrows, wearable.id, ct); |
| 1 | 92 | | eyebrowsRenderer.material = new Material(Resources.Load<Material>("Eyebrow Material")); |
| 1 | 93 | | if (main == null) |
| 0 | 94 | | throw new Exception($"Couldn't fetch main texture for {eyebrows.id}"); |
| 1 | 95 | | eyebrowsRenderer.material.SetTexture(ShaderUtils.BaseMap, main); |
| 1 | 96 | | if (mask != null) |
| 0 | 97 | | eyebrowsRenderer.material.SetTexture(ShaderUtils.BaseMap, mask); |
| 1 | 98 | | } |
| | 99 | |
|
| | 100 | | private async UniTask LoadEyes(CancellationToken ct) |
| | 101 | | { |
| 1 | 102 | | if (eyes == null) return; |
| 1 | 103 | | (Texture main, Texture mask) = await eyesRetriever.Retrieve(eyes, wearable.id, ct); |
| 1 | 104 | | eyesRenderer.material = new Material(Resources.Load<Material>("Eye Material")); |
| 1 | 105 | | if (main == null) |
| 0 | 106 | | throw new Exception($"Couldn't fetch main texture for {eyes.id}"); |
| 1 | 107 | | eyesRenderer.material.SetTexture(ShaderUtils.EyesTexture, main); |
| 1 | 108 | | if (mask != null) |
| 0 | 109 | | eyesRenderer.material.SetTexture(ShaderUtils.IrisMask, mask); |
| 1 | 110 | | } |
| | 111 | |
|
| | 112 | | private void UpdateColors(AvatarSettings avatarSettings) |
| | 113 | | { |
| 1 | 114 | | AvatarSystemUtils.PrepareMaterialColors(rendereable, avatarSettings.skinColor, avatarSettings.hairColor); |
| 1 | 115 | | eyesRenderer?.material?.SetColor(ShaderUtils.EyeTint, avatarSettings.eyesColor); |
| 1 | 116 | | eyebrowsRenderer?.material?.SetColor(ShaderUtils.BaseColor, avatarSettings.hairColor); |
| 1 | 117 | | mouthRenderer?.material?.SetColor(ShaderUtils.BaseColor, avatarSettings.skinColor); |
| 1 | 118 | | } |
| | 119 | |
|
| | 120 | | private async UniTask<Rendereable> LoadWearable(GameObject container, CancellationToken ct) |
| | 121 | | { |
| 85 | 122 | | ct.ThrowIfCancellationRequested(); |
| | 123 | |
|
| 85 | 124 | | bodyshapeRetriever.Dispose(); |
| | 125 | |
|
| 85 | 126 | | WearableItem.Representation representation = wearable.GetRepresentation(wearable.id); |
| 85 | 127 | | if (representation == null) |
| 0 | 128 | | throw new Exception("Couldn't find a representation for this bodyshape"); |
| | 129 | |
|
| 237 | 130 | | Rendereable bodyshapeRenderable = await bodyshapeRetriever.Retrieve(container, wearable.GetContentProvider(w |
| | 131 | |
|
| 8 | 132 | | if (bodyshapeRenderable == null) // fail safe, we shouldnt reach this since .Retrieve should throw if anythi |
| 7 | 133 | | throw new Exception("Couldn't load bodyshape"); |
| | 134 | |
|
| 1 | 135 | | return bodyshapeRenderable; |
| 1 | 136 | | } |
| | 137 | |
|
| | 138 | | public bool IsValid(WearableItem bodyshape, WearableItem eyebrows, WearableItem eyes, WearableItem mouth) |
| | 139 | | { |
| 36 | 140 | | if (wearable.id != bodyshape?.id) return false; |
| 35 | 141 | | if (this.eyebrows?.id != eyebrows?.id) return false; |
| 17 | 142 | | if (this.mouth?.id != mouth?.id) return false; |
| 17 | 143 | | if (this.eyes?.id != eyes?.id) return false; |
| 17 | 144 | | return true; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | public void Dispose() |
| | 148 | | { |
| 305 | 149 | | status = IWearableLoader.Status.Idle; |
| 305 | 150 | | bodyshapeRetriever?.Dispose(); |
| 305 | 151 | | eyesRetriever?.Dispose(); |
| 305 | 152 | | eyebrowsRetriever?.Dispose(); |
| 305 | 153 | | mouthRetriever?.Dispose(); |
| | 154 | |
|
| 305 | 155 | | if (eyesRenderer != null) |
| 1 | 156 | | Object.Destroy(eyesRenderer.material); |
| 305 | 157 | | if (eyebrowsRenderer != null) |
| 1 | 158 | | Object.Destroy(eyebrowsRenderer.material); |
| 305 | 159 | | if (mouthRenderer != null) |
| 1 | 160 | | Object.Destroy(mouthRenderer.material); |
| | 161 | |
|
| 305 | 162 | | upperBodyRenderer = null; |
| 305 | 163 | | lowerBodyRenderer = null; |
| 305 | 164 | | feetRenderer = null; |
| 305 | 165 | | headRenderer = null; |
| 305 | 166 | | eyesRenderer = null; |
| 305 | 167 | | eyebrowsRenderer = null; |
| 305 | 168 | | mouthRenderer = null; |
| 305 | 169 | | } |
| | 170 | | } |
| | 171 | | } |