| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL; |
| | 7 | | using DCL.Helpers; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace AvatarSystem |
| | 11 | | { |
| | 12 | | public class Loader : ILoader |
| | 13 | | { |
| 0 | 14 | | public GameObject bodyshapeContainer => bodyshapeLoader?.rendereable?.container; |
| 0 | 15 | | public SkinnedMeshRenderer combinedRenderer { get; private set; } |
| 0 | 16 | | public List<Renderer> facialFeaturesRenderers { get; private set; } |
| 0 | 17 | | public ILoader.Status status { get; private set; } = ILoader.Status.Idle; |
| | 18 | |
|
| | 19 | | private readonly IWearableLoaderFactory wearableLoaderFactory; |
| | 20 | | private readonly GameObject container; |
| | 21 | |
|
| | 22 | | internal IBodyshapeLoader bodyshapeLoader; |
| 1055 | 23 | | internal readonly Dictionary<string, IWearableLoader> loaders = new Dictionary<string, IWearableLoader>(); |
| | 24 | | private readonly IAvatarMeshCombinerHelper avatarMeshCombiner; |
| | 25 | |
|
| 1055 | 26 | | public Loader(IWearableLoaderFactory wearableLoaderFactory, GameObject container, IAvatarMeshCombinerHelper avat |
| | 27 | | { |
| 1055 | 28 | | this.wearableLoaderFactory = wearableLoaderFactory; |
| 1055 | 29 | | this.container = container; |
| | 30 | |
|
| 1055 | 31 | | this.avatarMeshCombiner = avatarMeshCombiner; |
| 1055 | 32 | | avatarMeshCombiner.prepareMeshForGpuSkinning = true; |
| 1055 | 33 | | avatarMeshCombiner.uploadMeshToGpu = true; |
| 1055 | 34 | | } |
| | 35 | |
|
| | 36 | | public async UniTask Load(WearableItem bodyshape, WearableItem eyes, WearableItem eyebrows, WearableItem mouth, |
| | 37 | | List<WearableItem> wearables, AvatarSettings settings, CancellationToken ct = default) |
| | 38 | | { |
| 0 | 39 | | await Load(bodyshape, eyes, eyebrows, mouth, wearables, settings, null, ct); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public async UniTask Load(WearableItem bodyshape, WearableItem eyes, WearableItem eyebrows, WearableItem mouth, |
| | 43 | | { |
| 7 | 44 | | ct.ThrowIfCancellationRequested(); |
| | 45 | |
|
| 7 | 46 | | List<IWearableLoader> toCleanUp = new List<IWearableLoader>(); |
| | 47 | | try |
| | 48 | | { |
| 7 | 49 | | status = ILoader.Status.Loading; |
| 7 | 50 | | await LoadBodyshape(settings, bodyshape, eyes, eyebrows, mouth, toCleanUp, ct); |
| 6 | 51 | | await LoadWearables(wearables, settings, toCleanUp, ct); |
| 6 | 52 | | SkinnedMeshRenderer skinnedContainer = bonesContainer == null ? bodyshapeLoader.upperBodyRenderer : bone |
| | 53 | | // Update Status accordingly |
| 6 | 54 | | status = ComposeStatus(loaders); |
| 6 | 55 | | if (status == ILoader.Status.Failed_Major) |
| 1 | 56 | | throw new Exception($"Couldnt load (nor fallback) wearables with required category: {string.Join(", |
| | 57 | |
|
| | 58 | |
|
| 52 | 59 | | foreach (IWearableLoader wearableLoader in loaders.Values) |
| | 60 | | { |
| 21 | 61 | | wearableLoader.SetBones(skinnedContainer.rootBone, skinnedContainer.bones); |
| | 62 | | } |
| | 63 | |
|
| 5 | 64 | | if (bodyshapeLoader.rendereable != null) |
| | 65 | | { |
| 4 | 66 | | bodyshapeLoader.SetBones(skinnedContainer.rootBone, skinnedContainer.bones); |
| | 67 | | } |
| | 68 | |
|
| 5 | 69 | | (bool headVisible, bool upperBodyVisible, bool lowerBodyVisible, bool feetVisible) = AvatarSystemUtils.G |
| | 70 | |
|
| 15 | 71 | | combinedRenderer = await MergeAvatar(settings, wearables, headVisible, upperBodyVisible, lowerBodyVisibl |
| 5 | 72 | | facialFeaturesRenderers = new List<Renderer>(); |
| 5 | 73 | | if (headVisible) |
| | 74 | | { |
| 4 | 75 | | if (eyes != null) |
| 4 | 76 | | facialFeaturesRenderers.Add(bodyshapeLoader.eyesRenderer); |
| 4 | 77 | | if (eyebrows != null) |
| 4 | 78 | | facialFeaturesRenderers.Add(bodyshapeLoader.eyebrowsRenderer); |
| 4 | 79 | | if (mouth != null) |
| 4 | 80 | | facialFeaturesRenderers.Add(bodyshapeLoader.mouthRenderer); |
| 4 | 81 | | } |
| | 82 | | else |
| | 83 | | { |
| 1 | 84 | | if(bodyshapeLoader != null) |
| 1 | 85 | | bodyshapeLoader.DisableFacialRenderers(); |
| | 86 | | } |
| 5 | 87 | | } |
| 0 | 88 | | catch (OperationCanceledException) |
| | 89 | | { |
| 0 | 90 | | Dispose(); |
| 0 | 91 | | throw; |
| | 92 | | } |
| 2 | 93 | | catch |
| | 94 | | { |
| 2 | 95 | | Dispose(); |
| 2 | 96 | | Debug.Log("Failed Loading avatar"); |
| 2 | 97 | | throw; |
| | 98 | | } |
| | 99 | | finally |
| | 100 | | { |
| 28 | 101 | | for (int i = 0; i < toCleanUp.Count; i++) |
| | 102 | | { |
| 7 | 103 | | if (toCleanUp[i] == null) |
| | 104 | | continue; |
| 0 | 105 | | toCleanUp[i].Dispose(); |
| | 106 | | } |
| | 107 | | } |
| 5 | 108 | | } |
| | 109 | |
|
| | 110 | | private static List<string> ConstructRequiredFailedWearablesList(IEnumerable<IWearableLoader> loaders) |
| | 111 | | { |
| 6 | 112 | | return loaders.Where(x => x.status == IWearableLoader.Status.Failed && AvatarSystemUtils.IsCategoryRequired( |
| 2 | 113 | | .Select(x => x.wearable.id) |
| | 114 | | .ToList(); |
| | 115 | | } |
| | 116 | |
|
| | 117 | | private async UniTask LoadBodyshape(AvatarSettings settings, WearableItem bodyshape, WearableItem eyes, Wearable |
| | 118 | | { |
| | 119 | | //We get a new loader if any of the subparts of the bodyshape changes |
| 7 | 120 | | if (!IsValidForBodyShape(bodyshape, eyes, eyebrows, mouth)) |
| | 121 | | { |
| 7 | 122 | | loadersToCleanUp.Add(bodyshapeLoader); |
| 7 | 123 | | bodyshapeLoader = wearableLoaderFactory.GetBodyshapeLoader(bodyshape, eyes, eyebrows, mouth); |
| | 124 | | } |
| | 125 | |
|
| 7 | 126 | | await bodyshapeLoader.Load(container, settings, ct); |
| | 127 | |
|
| 7 | 128 | | if (bodyshapeLoader.status == IWearableLoader.Status.Failed) |
| | 129 | | { |
| 1 | 130 | | status = ILoader.Status.Failed_Major; |
| 1 | 131 | | throw new Exception($"Couldnt load bodyshape"); |
| | 132 | | } |
| 6 | 133 | | } |
| | 134 | |
|
| | 135 | | private async UniTask LoadWearables(List<WearableItem> wearables, AvatarSettings settings, List<IWearableLoader> |
| | 136 | | { |
| 6 | 137 | | (List<IWearableLoader> notReusableLoaders, List<IWearableLoader> newLoaders) = GetNewLoaders(wearables, load |
| 6 | 138 | | loadersToCleanUp.AddRange(notReusableLoaders); |
| 6 | 139 | | loaders.Clear(); |
| 64 | 140 | | for (int i = 0; i < newLoaders.Count; i++) |
| | 141 | | { |
| 26 | 142 | | IWearableLoader loader = newLoaders[i]; |
| 26 | 143 | | loaders.Add(loader.wearable.data.category, loader); |
| | 144 | | } |
| | 145 | |
|
| 32 | 146 | | await UniTask.WhenAll(loaders.Values.Select(x => x.Load(container, settings, ct))); |
| 6 | 147 | | } |
| | 148 | |
|
| | 149 | | internal static (List<IWearableLoader> notReusableLoaders, List<IWearableLoader> newLoaders) GetNewLoaders(List< |
| | 150 | | { |
| | 151 | | // Initialize with all loaders and remove from cleaning-up the ones that can be reused |
| 9 | 152 | | List<IWearableLoader> notReusableLoaders = new List<IWearableLoader>(currentLoaders.Values); |
| 9 | 153 | | List<IWearableLoader> newLoaders = new List<IWearableLoader>(); |
| | 154 | |
|
| 94 | 155 | | for (int i = 0; i < wearables.Count; i++) |
| | 156 | | { |
| 38 | 157 | | WearableItem wearable = wearables[i]; |
| | 158 | |
|
| 38 | 159 | | if (currentLoaders.TryGetValue(wearable.data.category, out IWearableLoader loader)) |
| | 160 | | { |
| | 161 | | //We can reuse this loader |
| 4 | 162 | | if (loader.wearable.id == wearable.id) |
| | 163 | | { |
| 3 | 164 | | newLoaders.Add(loader); |
| 3 | 165 | | notReusableLoaders.Remove(loader); |
| 3 | 166 | | continue; |
| | 167 | | } |
| | 168 | | } |
| 35 | 169 | | newLoaders.Add(wearableLoaderFactory.GetWearableLoader(wearable)); |
| | 170 | | } |
| | 171 | |
|
| 9 | 172 | | return (notReusableLoaders, newLoaders); |
| | 173 | | } |
| | 174 | |
|
| 0 | 175 | | public Transform[] GetBones() { return bodyshapeLoader?.upperBodyRenderer?.bones; } |
| | 176 | |
|
| | 177 | | public bool IsValidForBodyShape(WearableItem bodyshape, WearableItem eyes, WearableItem eyebrows, WearableItem m |
| | 178 | | { |
| 7 | 179 | | return bodyshapeLoader != null && bodyshapeLoader.IsValid(bodyshape, eyebrows, eyes, mouth); |
| | 180 | | } |
| | 181 | |
|
| | 182 | | private async UniTask<SkinnedMeshRenderer> MergeAvatar(AvatarSettings settings, List<WearableItem> wearables, |
| | 183 | | bool headVisible, bool upperBodyVisible, bool lowerBodyVisible, bool feetVisible, SkinnedMeshRenderer bonesC |
| | 184 | | CancellationToken ct) |
| | 185 | | { |
| 5 | 186 | | var activeBodyParts = AvatarSystemUtils.GetActiveBodyPartsRenderers(bodyshapeLoader, headVisible, upperBodyV |
| 26 | 187 | | IEnumerable<SkinnedMeshRenderer> allRenderers = activeBodyParts.Union(loaders.Values.SelectMany(x => x.rende |
| | 188 | |
|
| | 189 | | // AvatarMeshCombiner is a bit buggy when performing the combine of the same meshes on the same frame, |
| | 190 | | // once that's fixed we can remove this wait |
| | 191 | | // AttachExternalCancellation is needed because cancellation will take a wait to trigger |
| 15 | 192 | | await UniTask.WaitForEndOfFrame(ct).AttachExternalCancellation(ct); |
| 5 | 193 | | var featureFlags = DataStore.i.featureFlags.flags.Get(); |
| 5 | 194 | | avatarMeshCombiner.useCullOpaqueHeuristic = true; |
| 5 | 195 | | avatarMeshCombiner.enableCombinedMesh = false; |
| 5 | 196 | | bool success = avatarMeshCombiner.Combine(bonesContainer, allRenderers.ToArray()); |
| 5 | 197 | | if (!success) |
| | 198 | | { |
| 0 | 199 | | status = ILoader.Status.Failed_Major; |
| 0 | 200 | | throw new Exception("Couldnt merge avatar"); |
| | 201 | | } |
| 5 | 202 | | avatarMeshCombiner.container.transform.SetParent(container.transform, true); |
| 5 | 203 | | avatarMeshCombiner.container.transform.localPosition = Vector3.zero; |
| 5 | 204 | | return avatarMeshCombiner.renderer; |
| 5 | 205 | | } |
| | 206 | |
|
| | 207 | | internal static ILoader.Status ComposeStatus(Dictionary<string, IWearableLoader> loaders) |
| | 208 | | { |
| 9 | 209 | | ILoader.Status composedStatus = ILoader.Status.Succeeded; |
| 78 | 210 | | foreach ((string category, IWearableLoader loader) in loaders) |
| | 211 | | { |
| 31 | 212 | | if (loader.status == IWearableLoader.Status.Defaulted) |
| 0 | 213 | | composedStatus = ILoader.Status.Failed_Minor; |
| 31 | 214 | | else if (loader.status == IWearableLoader.Status.Failed) |
| | 215 | | { |
| 6 | 216 | | if (AvatarSystemUtils.IsCategoryRequired(category)) |
| 2 | 217 | | return ILoader.Status.Failed_Major; |
| 4 | 218 | | composedStatus = ILoader.Status.Failed_Minor; |
| | 219 | | } |
| | 220 | | } |
| 7 | 221 | | return composedStatus; |
| 2 | 222 | | } |
| | 223 | |
|
| | 224 | | private void ClearLoaders() |
| | 225 | | { |
| 1069 | 226 | | bodyshapeLoader?.Dispose(); |
| 2190 | 227 | | foreach (IWearableLoader wearableLoader in loaders.Values) |
| | 228 | | { |
| 26 | 229 | | wearableLoader.Dispose(); |
| | 230 | | } |
| 1069 | 231 | | loaders.Clear(); |
| 1069 | 232 | | } |
| | 233 | |
|
| | 234 | | public void Dispose() |
| | 235 | | { |
| 1069 | 236 | | avatarMeshCombiner.Dispose(); |
| 1069 | 237 | | status = ILoader.Status.Idle; |
| 1069 | 238 | | ClearLoaders(); |
| 1069 | 239 | | } |
| | 240 | | } |
| | 241 | | } |