| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Runtime.ExceptionServices; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL; |
| | 7 | | using DCL.Emotes; |
| | 8 | | using DCL.Tasks; |
| | 9 | | using GPUSkinning; |
| | 10 | | using UnityEngine; |
| | 11 | |
|
| | 12 | | namespace AvatarSystem |
| | 13 | | { |
| | 14 | | // [ADR 65 - https://github.com/decentraland/adr] |
| | 15 | | public class Avatar : IAvatar |
| | 16 | | { |
| | 17 | | private const string NEW_CDN_FF = "ab-new-cdn"; |
| | 18 | |
|
| | 19 | | private const float RESCALING_BOUNDS_FACTOR = 100f; |
| | 20 | | internal const string LOADING_VISIBILITY_CONSTRAIN = "Loading"; |
| | 21 | |
|
| | 22 | | protected readonly ILoader loader; |
| | 23 | | protected readonly IVisibility visibility; |
| | 24 | |
|
| | 25 | | private readonly IAvatarCurator avatarCurator; |
| | 26 | | private readonly ILOD lod; |
| | 27 | | private readonly IGPUSkinning gpuSkinning; |
| | 28 | | private readonly IGPUSkinningThrottlerService gpuSkinningThrottlerService; |
| | 29 | | protected readonly IAvatarEmotesController emotesController; |
| | 30 | |
|
| | 31 | | private CancellationTokenSource loadCancellationToken; |
| 521 | 32 | | public IAvatar.Status status { get; private set; } = IAvatar.Status.Idle; |
| 0 | 33 | | public Vector3 extents { get; private set; } |
| 0 | 34 | | public int lodLevel => lod?.lodIndex ?? 0; |
| | 35 | | public event Action<Renderer> OnCombinedRendererUpdate; |
| 0 | 36 | | private FeatureFlag featureFlags => DataStore.i.featureFlags.flags.Get(); |
| | 37 | |
|
| 518 | 38 | | internal Avatar(IAvatarCurator avatarCurator, ILoader loader, |
| | 39 | | IVisibility visibility, ILOD lod, IGPUSkinning gpuSkinning, IGPUSkinningThrottlerService gpuSkinningThrottle |
| | 40 | | IAvatarEmotesController emotesController) |
| | 41 | | { |
| 518 | 42 | | this.avatarCurator = avatarCurator; |
| 518 | 43 | | this.loader = loader; |
| 518 | 44 | | this.visibility = visibility; |
| 518 | 45 | | this.lod = lod; |
| 518 | 46 | | this.gpuSkinning = gpuSkinning; |
| 518 | 47 | | this.gpuSkinningThrottlerService = gpuSkinningThrottlerService; |
| 518 | 48 | | this.emotesController = emotesController; |
| 518 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Starts the loading process for the Avatar. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="wearablesIds"></param> |
| | 55 | | /// <param name="settings"></param> |
| | 56 | | /// <param name="ct"></param> |
| | 57 | | public async UniTask Load(List<string> wearablesIds, List<string> emotesIds, AvatarSettings settings, Cancellati |
| | 58 | | { |
| 0 | 59 | | status = IAvatar.Status.Idle; |
| | 60 | |
|
| 0 | 61 | | loadCancellationToken = loadCancellationToken.SafeRestart(); |
| 0 | 62 | | CancellationToken linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct, loadCancellationToken.Token |
| | 63 | |
|
| 0 | 64 | | try { await LoadTry(wearablesIds, emotesIds, settings, linkedCt); } |
| 0 | 65 | | catch (OperationCanceledException) |
| | 66 | | { |
| | 67 | | // Cancel any ongoing process except the current loadCancellationToken |
| | 68 | | // since it was provoking a double cancellation thus inconsistencies in the flow |
| | 69 | | // TODO: disposing collaborators is an anti-pattern in the current context. Disposed objects should not |
| 0 | 70 | | CancelAndRestoreOngoingProcessesExceptTheLoading(); |
| | 71 | |
|
| 0 | 72 | | throw; |
| | 73 | | } |
| 0 | 74 | | catch (Exception e) |
| | 75 | | { |
| | 76 | | // Cancel any ongoing process except the current loadCancellationToken |
| | 77 | | // since it was provoking a double cancellation thus inconsistencies in the flow |
| | 78 | | // TODO: disposing collaborators is an anti-pattern in the current context. Disposed objects should not |
| 0 | 79 | | CancelAndRestoreOngoingProcessesExceptTheLoading(); |
| | 80 | |
|
| 0 | 81 | | Debug.Log($"Avatar.Load failed with wearables:[{string.Join(",", wearablesIds)}] " + |
| | 82 | | $"for bodyshape:{settings.bodyshapeId} and player {settings.playerName}"); |
| | 83 | |
|
| 0 | 84 | | if (e.InnerException != null) |
| 0 | 85 | | ExceptionDispatchInfo.Capture(e.InnerException).Throw(); |
| | 86 | | else |
| 0 | 87 | | throw; |
| 0 | 88 | | } |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | protected virtual async UniTask LoadTry(List<string> wearablesIds, List<string> emotesIds, AvatarSettings settin |
| | 92 | | { |
| 0 | 93 | | List<WearableItem> emotes = await LoadWearables(wearablesIds, emotesIds, settings, linkedCt: linkedCt); |
| | 94 | |
|
| 0 | 95 | | GameObject container = loader.bodyshapeContainer; |
| | 96 | |
|
| 0 | 97 | | if (featureFlags.IsFeatureEnabled(NEW_CDN_FF)) |
| | 98 | | { |
| 0 | 99 | | if (loader.bodyshapeContainer.transform.childCount > 0) |
| | 100 | | { |
| 0 | 101 | | loader.bodyshapeContainer.transform.Find("Armature"); |
| 0 | 102 | | Transform child = loader.bodyshapeContainer.transform.GetChild(0); |
| | 103 | |
|
| | 104 | | // Asset bundles assets dont have the gltf-scene name as the root, they have the file hash, for this |
| 0 | 105 | | child.name = "Armature"; |
| | 106 | | } |
| | 107 | | } |
| | 108 | | else |
| | 109 | | { |
| 0 | 110 | | GameObject parent = GetDeepParentOf(loader.bodyshapeContainer, "Armature"); |
| 0 | 111 | | container = parent != null ? parent : container; |
| | 112 | | } |
| | 113 | |
|
| 0 | 114 | | emotesController.Prepare(settings.bodyshapeId, container); |
| 0 | 115 | | Prepare(settings, emotes); |
| 0 | 116 | | Bind(); |
| 0 | 117 | | Inform(loader.combinedRenderer); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | private GameObject GetDeepParentOf(GameObject container, string childName) |
| | 121 | | { |
| 0 | 122 | | foreach (Transform child in container.transform) |
| 0 | 123 | | return child.name == childName ? child.parent.gameObject : GetDeepParentOf(child.gameObject, childName); |
| | 124 | |
|
| 0 | 125 | | return null; |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | protected async UniTask<List<WearableItem>> LoadWearables(List<string> wearablesIds, List<string> emotesIds, Ava |
| | 129 | | { |
| | 130 | | BodyWearables bodyWearables; |
| | 131 | | List<WearableItem> wearables; |
| | 132 | | List<WearableItem> emotes; |
| | 133 | |
|
| 0 | 134 | | (bodyWearables, wearables, emotes) = await avatarCurator.Curate(settings, wearablesIds, emotesIds, linkedCt) |
| | 135 | |
|
| 0 | 136 | | if (!loader.IsValidForBodyShape(bodyWearables)) |
| 0 | 137 | | visibility.AddGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN); |
| | 138 | |
|
| 0 | 139 | | await loader.Load(bodyWearables, wearables, settings, bonesRenderers, linkedCt); |
| 0 | 140 | | return emotes; |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | protected void Prepare(AvatarSettings settings, List<WearableItem> emotes) |
| | 144 | | { |
| | 145 | | //Scale the bounds due to the giant avatar not being skinned yet |
| 0 | 146 | | extents = loader.combinedRenderer.localBounds.extents * 2f / RESCALING_BOUNDS_FACTOR; |
| | 147 | |
|
| 0 | 148 | | emotesController.LoadEmotes(settings.bodyshapeId, emotes); |
| 0 | 149 | | gpuSkinning.Prepare(loader.combinedRenderer); |
| 0 | 150 | | gpuSkinningThrottlerService.Register(gpuSkinning); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | protected void Bind() |
| | 154 | | { |
| 0 | 155 | | visibility.Bind(gpuSkinning.renderer, loader.facialFeaturesRenderers); |
| 0 | 156 | | visibility.RemoveGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN); |
| 0 | 157 | | lod.Bind(gpuSkinning.renderer); |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | protected void Inform(Renderer loaderCombinedRenderer) |
| | 161 | | { |
| 0 | 162 | | status = IAvatar.Status.Loaded; |
| 0 | 163 | | OnCombinedRendererUpdate?.Invoke(loaderCombinedRenderer); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | public virtual void AddVisibilityConstraint(string key) |
| | 167 | | { |
| 0 | 168 | | visibility.AddGlobalConstrain(key); |
| 0 | 169 | | emotesController.AddVisibilityConstraint(key); |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | public void RemoveVisibilityConstrain(string key) |
| | 173 | | { |
| 0 | 174 | | visibility.RemoveGlobalConstrain(key); |
| 0 | 175 | | emotesController.RemoveVisibilityConstraint(key); |
| 0 | 176 | | } |
| | 177 | |
|
| 218 | 178 | | public IAvatarEmotesController GetEmotesController() => emotesController; |
| | 179 | |
|
| | 180 | | public void SetLODLevel(int lodIndex) => |
| 0 | 181 | | lod.SetLodIndex(lodIndex); |
| | 182 | |
|
| | 183 | | public void SetAnimationThrottling(int framesBetweenUpdate) => |
| 0 | 184 | | gpuSkinningThrottlerService.ModifyThrottling(gpuSkinning, framesBetweenUpdate); |
| | 185 | |
|
| | 186 | | public void SetImpostorTexture(Texture2D impostorTexture) => |
| 0 | 187 | | lod.SetImpostorTexture(impostorTexture); |
| | 188 | |
|
| | 189 | | public void SetImpostorTint(Color color) => |
| 0 | 190 | | lod.SetImpostorTint(color); |
| | 191 | |
|
| | 192 | | public Transform[] GetBones() => |
| 0 | 193 | | loader.GetBones(); |
| | 194 | |
|
| | 195 | | public Renderer GetMainRenderer() => |
| 0 | 196 | | gpuSkinning.renderer; |
| | 197 | |
|
| 0 | 198 | | public IReadOnlyList<SkinnedMeshRenderer> originalVisibleRenderers => loader.originalVisibleRenderers; |
| | 199 | |
|
| | 200 | | public void Dispose() |
| | 201 | | { |
| 521 | 202 | | loadCancellationToken?.Cancel(); |
| 521 | 203 | | loadCancellationToken?.Dispose(); |
| 521 | 204 | | loadCancellationToken = null; |
| 521 | 205 | | CancelAndRestoreOngoingProcessesExceptTheLoading(); |
| 521 | 206 | | } |
| | 207 | |
|
| | 208 | | private void CancelAndRestoreOngoingProcessesExceptTheLoading() |
| | 209 | | { |
| 521 | 210 | | status = IAvatar.Status.Idle; |
| 521 | 211 | | avatarCurator?.Dispose(); |
| 521 | 212 | | loader?.Dispose(); |
| 521 | 213 | | visibility?.Dispose(); |
| 521 | 214 | | lod?.Dispose(); |
| 521 | 215 | | gpuSkinningThrottlerService?.Unregister(gpuSkinning); |
| 521 | 216 | | } |
| | 217 | | } |
| | 218 | | } |