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