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