| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Runtime.ExceptionServices; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using GPUSkinning; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace AvatarSystem |
| | 10 | | { |
| | 11 | | // [ADR 65 - https://github.com/decentraland/adr] |
| | 12 | | public class Avatar : IAvatar |
| | 13 | | { |
| | 14 | | private const float RESCALING_BOUNDS_FACTOR = 100f; |
| | 15 | | internal const string LOADING_VISIBILITY_CONSTRAIN = "Loading"; |
| | 16 | | private readonly IAvatarCurator avatarCurator; |
| | 17 | | private readonly ILoader loader; |
| | 18 | | private readonly IAnimator animator; |
| | 19 | | private readonly IVisibility visibility; |
| | 20 | | private readonly ILOD lod; |
| | 21 | | private readonly IGPUSkinning gpuSkinning; |
| | 22 | | private readonly IGPUSkinningThrottler gpuSkinningThrottler; |
| | 23 | | private readonly IEmoteAnimationEquipper emoteAnimationEquipper; |
| 1019 | 24 | | private CancellationTokenSource disposeCts = new CancellationTokenSource(); |
| | 25 | |
|
| 0 | 26 | | public IAvatar.Status status { get; private set; } = IAvatar.Status.Idle; |
| 0 | 27 | | public Vector3 extents { get; private set; } |
| 0 | 28 | | public int lodLevel => lod?.lodIndex ?? 0; |
| | 29 | |
|
| 1019 | 30 | | public Avatar(IAvatarCurator avatarCurator, ILoader loader, IAnimator animator, IVisibility visibility, ILOD lod |
| | 31 | | { |
| 1019 | 32 | | this.avatarCurator = avatarCurator; |
| 1019 | 33 | | this.loader = loader; |
| 1019 | 34 | | this.animator = animator; |
| 1019 | 35 | | this.visibility = visibility; |
| 1019 | 36 | | this.lod = lod; |
| 1019 | 37 | | this.gpuSkinning = gpuSkinning; |
| 1019 | 38 | | this.gpuSkinningThrottler = gpuSkinningThrottler; |
| 1019 | 39 | | this.emoteAnimationEquipper = emoteAnimationEquipper; |
| 1019 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Starts the loading process for the Avatar. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="wearablesIds"></param> |
| | 46 | | /// <param name="emotesIds"></param> |
| | 47 | | /// <param name="settings"></param> |
| | 48 | | /// <param name="ct"></param> |
| | 49 | | public async UniTask Load(List<string> wearablesIds, List<string> emotesIds, AvatarSettings settings, Cancellati |
| | 50 | | { |
| 4 | 51 | | disposeCts ??= new CancellationTokenSource(); |
| | 52 | |
|
| 4 | 53 | | status = IAvatar.Status.Idle; |
| 4 | 54 | | CancellationToken linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct, disposeCts.Token).Token; |
| | 55 | |
|
| 4 | 56 | | linkedCt.ThrowIfCancellationRequested(); |
| | 57 | |
|
| | 58 | | try |
| | 59 | | { |
| 3 | 60 | | WearableItem bodyshape = null; |
| 3 | 61 | | WearableItem eyes = null; |
| 3 | 62 | | WearableItem eyebrows = null; |
| 3 | 63 | | WearableItem mouth = null; |
| 3 | 64 | | List<WearableItem> wearables = null; |
| 3 | 65 | | List<WearableItem> emotes = null; |
| | 66 | |
|
| 3 | 67 | | (bodyshape, eyes, eyebrows, mouth, wearables, emotes) = await avatarCurator.Curate(settings, wearablesId |
| 2 | 68 | | if (!loader.IsValidForBodyShape(bodyshape, eyes, eyebrows, mouth)) |
| | 69 | | { |
| 2 | 70 | | visibility.AddGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN); |
| | 71 | | } |
| 2 | 72 | | await loader.Load(bodyshape, eyes, eyebrows, mouth, wearables, settings, linkedCt); |
| | 73 | |
|
| | 74 | | //Scale the bounds due to the giant avatar not being skinned yet |
| 2 | 75 | | extents = loader.combinedRenderer.localBounds.extents * 2f / RESCALING_BOUNDS_FACTOR; |
| 1 | 76 | | animator.Prepare(settings.bodyshapeId, loader.bodyshapeContainer); |
| 1 | 77 | | emoteAnimationEquipper.SetEquippedEmotes(settings.bodyshapeId, emotes); |
| 1 | 78 | | gpuSkinning.Prepare(loader.combinedRenderer); |
| 1 | 79 | | gpuSkinningThrottler.Bind(gpuSkinning); |
| | 80 | |
|
| 1 | 81 | | visibility.Bind(gpuSkinning.renderer, loader.facialFeaturesRenderers); |
| 1 | 82 | | visibility.RemoveGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN); |
| | 83 | |
|
| 1 | 84 | | lod.Bind(gpuSkinning.renderer); |
| 1 | 85 | | gpuSkinningThrottler.Start(); |
| | 86 | |
|
| 1 | 87 | | status = IAvatar.Status.Loaded; |
| 1 | 88 | | } |
| 0 | 89 | | catch (OperationCanceledException) |
| | 90 | | { |
| 0 | 91 | | Dispose(); |
| 0 | 92 | | throw; |
| | 93 | | } |
| 2 | 94 | | catch (Exception e) |
| | 95 | | { |
| 2 | 96 | | Dispose(); |
| 2 | 97 | | Debug.Log($"Avatar.Load failed with wearables:[{string.Join(",", wearablesIds)}] " + |
| | 98 | | $"for bodyshape:{settings.bodyshapeId} and player {settings.playerName}"); |
| 2 | 99 | | if (e.InnerException != null) |
| 0 | 100 | | ExceptionDispatchInfo.Capture(e.InnerException).Throw(); |
| | 101 | | else |
| 2 | 102 | | throw; |
| 0 | 103 | | } |
| | 104 | | finally |
| | 105 | | { |
| 3 | 106 | | disposeCts?.Dispose(); |
| 3 | 107 | | disposeCts = null; |
| | 108 | | } |
| 1 | 109 | | } |
| | 110 | |
|
| 10 | 111 | | public void AddVisibilityConstrain(string key) { visibility.AddGlobalConstrain(key); } |
| | 112 | |
|
| 2 | 113 | | public void RemoveVisibilityConstrain(string key) { visibility.RemoveGlobalConstrain(key); } |
| | 114 | |
|
| 4 | 115 | | public void PlayEmote(string emoteId, long timestamps) { animator?.PlayEmote(emoteId, timestamps); } |
| | 116 | |
|
| 0 | 117 | | public void SetLODLevel(int lodIndex) { lod.SetLodIndex(lodIndex); } |
| | 118 | |
|
| 0 | 119 | | public void SetAnimationThrottling(int framesBetweenUpdate) { gpuSkinningThrottler.SetThrottling(framesBetweenUp |
| | 120 | |
|
| 0 | 121 | | public void SetImpostorTexture(Texture2D impostorTexture) { lod.SetImpostorTexture(impostorTexture); } |
| | 122 | |
|
| 0 | 123 | | public void SetImpostorTint(Color color) { lod.SetImpostorTint(color); } |
| | 124 | |
|
| 0 | 125 | | public Transform[] GetBones() => loader.GetBones(); |
| | 126 | |
|
| | 127 | | public void Dispose() |
| | 128 | | { |
| 1032 | 129 | | status = IAvatar.Status.Idle; |
| 1032 | 130 | | disposeCts?.Cancel(); |
| 1032 | 131 | | disposeCts?.Dispose(); |
| 1032 | 132 | | disposeCts = null; |
| 1032 | 133 | | avatarCurator?.Dispose(); |
| 1032 | 134 | | loader?.Dispose(); |
| 1032 | 135 | | visibility?.Dispose(); |
| 1032 | 136 | | lod?.Dispose(); |
| 1032 | 137 | | gpuSkinningThrottler?.Dispose(); |
| 1032 | 138 | | } |
| | 139 | | } |
| | 140 | | } |