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