| | 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; |
| 1017 | 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 | |
|
| 1017 | 30 | | public Avatar(IAvatarCurator avatarCurator, ILoader loader, IAnimator animator, IVisibility visibility, ILOD lod |
| | 31 | | { |
| 1017 | 32 | | this.avatarCurator = avatarCurator; |
| 1017 | 33 | | this.loader = loader; |
| 1017 | 34 | | this.animator = animator; |
| 1017 | 35 | | this.visibility = visibility; |
| 1017 | 36 | | this.lod = lod; |
| 1017 | 37 | | this.gpuSkinning = gpuSkinning; |
| 1017 | 38 | | this.gpuSkinningThrottler = gpuSkinningThrottler; |
| 1017 | 39 | | this.emoteAnimationEquipper = emoteAnimationEquipper; |
| 1017 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Starts the loading process for the Avatar. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="wearablesIds"></param> |
| | 46 | | /// <param name="settings"></param> |
| | 47 | | /// <param name="ct"></param> |
| | 48 | | public async UniTask Load(List<string> wearablesIds, AvatarSettings settings, CancellationToken ct = default) |
| | 49 | | { |
| 4 | 50 | | disposeCts ??= new CancellationTokenSource(); |
| | 51 | |
|
| 4 | 52 | | status = IAvatar.Status.Idle; |
| 4 | 53 | | CancellationToken linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct, disposeCts.Token).Token; |
| | 54 | |
|
| 4 | 55 | | linkedCt.ThrowIfCancellationRequested(); |
| | 56 | |
|
| | 57 | | try |
| | 58 | | { |
| 3 | 59 | | WearableItem bodyshape = null; |
| 3 | 60 | | WearableItem eyes = null; |
| 3 | 61 | | WearableItem eyebrows = null; |
| 3 | 62 | | WearableItem mouth = null; |
| 3 | 63 | | List<WearableItem> wearables = null; |
| 3 | 64 | | List<WearableItem> emotes = null; |
| | 65 | |
|
| 3 | 66 | | (bodyshape, eyes, eyebrows, mouth, wearables, emotes) = await avatarCurator.Curate(settings, wearablesId |
| 2 | 67 | | if (!loader.IsValidForBodyShape(bodyshape, eyes, eyebrows, mouth)) |
| | 68 | | { |
| 2 | 69 | | visibility.AddGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN); |
| | 70 | | } |
| 2 | 71 | | await loader.Load(bodyshape, eyes, eyebrows, mouth, wearables, settings, linkedCt); |
| | 72 | |
|
| | 73 | | //Scale the bounds due to the giant avatar not being skinned yet |
| 2 | 74 | | extents = loader.combinedRenderer.localBounds.extents * 2f / RESCALING_BOUNDS_FACTOR; |
| 1 | 75 | | animator.Prepare(settings.bodyshapeId, loader.bodyshapeContainer); |
| 1 | 76 | | emoteAnimationEquipper.SetEquippedEmotes(settings.bodyshapeId, emotes); |
| 1 | 77 | | gpuSkinning.Prepare(loader.combinedRenderer); |
| 1 | 78 | | gpuSkinningThrottler.Bind(gpuSkinning); |
| | 79 | |
|
| 1 | 80 | | visibility.Bind(gpuSkinning.renderer, loader.facialFeaturesRenderers); |
| 1 | 81 | | visibility.RemoveGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN); |
| | 82 | |
|
| 1 | 83 | | lod.Bind(gpuSkinning.renderer); |
| 1 | 84 | | gpuSkinningThrottler.Start(); |
| | 85 | |
|
| 1 | 86 | | status = IAvatar.Status.Loaded; |
| 1 | 87 | | } |
| 0 | 88 | | catch (OperationCanceledException) |
| | 89 | | { |
| 0 | 90 | | Dispose(); |
| 0 | 91 | | throw; |
| | 92 | | } |
| 2 | 93 | | catch (Exception e) |
| | 94 | | { |
| 2 | 95 | | Dispose(); |
| 2 | 96 | | Debug.Log($"Avatar.Load failed with wearables:[{string.Join(",", wearablesIds)}] for bodyshape:{settings |
| 2 | 97 | | if (e.InnerException != null) |
| 0 | 98 | | ExceptionDispatchInfo.Capture(e.InnerException).Throw(); |
| | 99 | | else |
| 2 | 100 | | throw; |
| 0 | 101 | | } |
| | 102 | | finally |
| | 103 | | { |
| 3 | 104 | | disposeCts?.Dispose(); |
| 3 | 105 | | disposeCts = null; |
| | 106 | | } |
| 1 | 107 | | } |
| | 108 | |
|
| 10 | 109 | | public void AddVisibilityConstrain(string key) { visibility.AddGlobalConstrain(key); } |
| | 110 | |
|
| 2 | 111 | | public void RemoveVisibilityConstrain(string key) { visibility.RemoveGlobalConstrain(key); } |
| | 112 | |
|
| 4 | 113 | | public void PlayEmote(string emoteId, long timestamps) { animator?.PlayEmote(emoteId, timestamps); } |
| | 114 | |
|
| 0 | 115 | | public void SetLODLevel(int lodIndex) { lod.SetLodIndex(lodIndex); } |
| | 116 | |
|
| 0 | 117 | | public void SetAnimationThrottling(int framesBetweenUpdate) { gpuSkinningThrottler.SetThrottling(framesBetweenUp |
| | 118 | |
|
| 0 | 119 | | public void SetImpostorTexture(Texture2D impostorTexture) { lod.SetImpostorTexture(impostorTexture); } |
| | 120 | |
|
| 0 | 121 | | public void SetImpostorTint(Color color) { lod.SetImpostorTint(color); } |
| | 122 | |
|
| 0 | 123 | | public Transform[] GetBones() => loader.GetBones(); |
| | 124 | |
|
| | 125 | | public void Dispose() |
| | 126 | | { |
| 1030 | 127 | | status = IAvatar.Status.Idle; |
| 1030 | 128 | | disposeCts?.Cancel(); |
| 1030 | 129 | | disposeCts?.Dispose(); |
| 1030 | 130 | | disposeCts = null; |
| 1030 | 131 | | avatarCurator?.Dispose(); |
| 1030 | 132 | | loader?.Dispose(); |
| 1030 | 133 | | visibility?.Dispose(); |
| 1030 | 134 | | lod?.Dispose(); |
| 1030 | 135 | | gpuSkinningThrottler?.Dispose(); |
| 1030 | 136 | | } |
| | 137 | | } |
| | 138 | | } |