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