| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using AvatarSystem; |
| | 6 | | using Cysharp.Threading.Tasks; |
| | 7 | | using DCL; |
| | 8 | | using DCL.Emotes; |
| | 9 | | using DCL.FatalErrorReporter; |
| | 10 | | using DCL.Interface; |
| | 11 | | using DCL.NotificationModel; |
| | 12 | | using GPUSkinning; |
| | 13 | | using SocialFeaturesAnalytics; |
| | 14 | | using UnityEngine; |
| | 15 | | using Type = DCL.NotificationModel.Type; |
| | 16 | |
|
| | 17 | | public class PlayerAvatarController : MonoBehaviour, IHideAvatarAreaHandler |
| | 18 | | { |
| | 19 | | private const string LOADING_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables"; |
| | 20 | | private const string IN_HIDE_AREA = "IN_HIDE_AREA"; |
| | 21 | | private const string INSIDE_CAMERA = "INSIDE_CAMERA"; |
| | 22 | |
|
| | 23 | | private IAvatar avatar; |
| | 24 | | private CancellationTokenSource avatarLoadingCts = null; |
| | 25 | | public GameObject avatarContainer; |
| | 26 | | public GameObject armatureContainer; |
| | 27 | | public Transform loadingAvatarContainer; |
| 596 | 28 | | private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() }; |
| | 29 | |
|
| | 30 | | public Collider avatarCollider; |
| | 31 | | [SerializeField] private GameObject loadingParticlesPrefab; |
| 596 | 32 | | public float cameraDistanceToDeactivate = 1.0f; |
| | 33 | |
|
| 2240 | 34 | | private UserProfile userProfile => UserProfile.GetOwnUserProfile(); |
| 0 | 35 | | private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame(); |
| | 36 | |
|
| | 37 | | private bool enableCameraCheck = false; |
| | 38 | | private Camera mainCamera; |
| | 39 | | private IFatalErrorReporter fatalErrorReporter; // TODO? |
| | 40 | | private string VISIBILITY_CONSTRAIN; |
| | 41 | |
|
| | 42 | | internal ISocialAnalytics socialAnalytics; |
| | 43 | |
|
| | 44 | | private void Start() |
| | 45 | | { |
| 559 | 46 | | DataStore.i.common.isPlayerRendererLoaded.Set(false); |
| 559 | 47 | | socialAnalytics = new SocialAnalytics( |
| | 48 | | DCL.Environment.i.platform.serviceProviders.analytics, |
| | 49 | | new UserProfileWebInterfaceBridge()); |
| | 50 | |
|
| 559 | 51 | | if (DataStore.i.avatarConfig.useHologramAvatar.Get()) |
| 0 | 52 | | avatar = GetAvatarWithHologram(); |
| | 53 | | else |
| 559 | 54 | | avatar = GetStandardAvatar(); |
| | 55 | |
|
| 559 | 56 | | if ( UserProfileController.i != null ) |
| | 57 | | { |
| 0 | 58 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| 0 | 59 | | UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail; |
| | 60 | | } |
| | 61 | |
|
| 559 | 62 | | CommonScriptableObjects.rendererState.AddLock(this); |
| | 63 | |
|
| | 64 | | #if UNITY_WEBGL |
| 559 | 65 | | fatalErrorReporter = new WebFatalErrorReporter(); |
| | 66 | | #else |
| | 67 | | fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i); |
| | 68 | | #endif |
| | 69 | |
|
| 559 | 70 | | mainCamera = Camera.main; |
| 559 | 71 | | } |
| | 72 | |
|
| | 73 | | private AvatarSystem.Avatar GetStandardAvatar() |
| | 74 | | { |
| 559 | 75 | | AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>(); |
| 559 | 76 | | AvatarSystem.NoLODs noLod = new NoLODs(); |
| 559 | 77 | | return new AvatarSystem.Avatar( |
| | 78 | | new AvatarCurator(new WearableItemResolver()), |
| | 79 | | new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()), |
| | 80 | | animator, |
| | 81 | | new Visibility(), |
| | 82 | | noLod, |
| | 83 | | new SimpleGPUSkinning(), |
| | 84 | | new GPUSkinningThrottler(), |
| | 85 | | new EmoteAnimationEquipper(animator, DataStore.i.emotes)); |
| | 86 | | } |
| | 87 | |
|
| | 88 | | private AvatarWithHologram GetAvatarWithHologram() |
| | 89 | | { |
| 0 | 90 | | AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>(); |
| 0 | 91 | | AvatarSystem.NoLODs noLod = new NoLODs(); |
| 0 | 92 | | BaseAvatar baseAvatar = new BaseAvatar(loadingAvatarContainer, armatureContainer, noLod); |
| 0 | 93 | | return new AvatarSystem.AvatarWithHologram( |
| | 94 | | baseAvatar, |
| | 95 | | new AvatarCurator(new WearableItemResolver()), |
| | 96 | | new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()), |
| | 97 | | animator, |
| | 98 | | new Visibility(), |
| | 99 | | noLod, |
| | 100 | | new SimpleGPUSkinning(), |
| | 101 | | new GPUSkinningThrottler(), |
| | 102 | | new EmoteAnimationEquipper(animator, DataStore.i.emotes)); |
| | 103 | | } |
| | 104 | |
|
| | 105 | | private void OnBaseWereablesFail() |
| | 106 | | { |
| 0 | 107 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| | 108 | |
|
| 0 | 109 | | if (enableCameraCheck) |
| 0 | 110 | | ShowWearablesWarning(); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | private void ShowWearablesWarning() |
| | 114 | | { |
| 0 | 115 | | NotificationsController.i.ShowNotification(new Model |
| | 116 | | { |
| | 117 | | message = LOADING_WEARABLES_ERROR_MESSAGE, |
| | 118 | | type = Type.GENERIC, |
| | 119 | | timer = 10f, |
| | 120 | | destroyOnFinish = true |
| | 121 | | }); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | private void Update() |
| | 125 | | { |
| 5253 | 126 | | if (!enableCameraCheck || repositioningWorld) |
| 5253 | 127 | | return; |
| | 128 | |
|
| 0 | 129 | | if (mainCamera == null) |
| | 130 | | { |
| 0 | 131 | | mainCamera = Camera.main; |
| | 132 | |
|
| 0 | 133 | | if (mainCamera == null) |
| 0 | 134 | | return; |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | if (Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDeactivate) |
| 0 | 138 | | avatar.RemoveVisibilityConstrain(INSIDE_CAMERA); |
| | 139 | | else |
| 0 | 140 | | avatar.AddVisibilityConstrain(INSIDE_CAMERA); |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | public void SetAvatarVisibility(bool isVisible) |
| | 144 | | { |
| 6 | 145 | | VISIBILITY_CONSTRAIN = "own_player_invisible"; |
| 6 | 146 | | if (isVisible) |
| 1 | 147 | | avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN); |
| | 148 | | else |
| 5 | 149 | | avatar.AddVisibilityConstrain(VISIBILITY_CONSTRAIN); |
| 5 | 150 | | } |
| | 151 | |
|
| | 152 | | private void OnEnable() |
| | 153 | | { |
| 560 | 154 | | userProfile.OnUpdate += OnUserProfileOnUpdate; |
| 560 | 155 | | userProfile.OnAvatarEmoteSet += OnAvatarEmote; |
| 560 | 156 | | } |
| | 157 | |
|
| | 158 | | private void OnAvatarEmote(string id, long timestamp, UserProfile.EmoteSource source) |
| | 159 | | { |
| 1 | 160 | | avatar.PlayEmote(id, timestamp); |
| | 161 | |
|
| 1 | 162 | | DataStore.i.common.wearables.TryGetValue(id, out WearableItem emoteItem); |
| | 163 | |
|
| 1 | 164 | | if (emoteItem != null) |
| | 165 | | { |
| 0 | 166 | | socialAnalytics.SendPlayEmote( |
| | 167 | | emoteItem.id, |
| | 168 | | emoteItem.GetName(), |
| | 169 | | emoteItem.rarity, |
| | 170 | | emoteItem.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE), |
| | 171 | | source, |
| | 172 | | $"{CommonScriptableObjects.playerCoords.Get().x},{CommonScriptableObjects.playerCoords.Get().y}"); |
| | 173 | | } |
| 1 | 174 | | } |
| | 175 | |
|
| | 176 | | private void OnUserProfileOnUpdate(UserProfile profile) |
| | 177 | | { |
| 7 | 178 | | avatarLoadingCts?.Cancel(); |
| 7 | 179 | | avatarLoadingCts?.Dispose(); |
| 7 | 180 | | avatarLoadingCts = new CancellationTokenSource(); |
| 7 | 181 | | LoadingAvatarRoutine(profile, avatarLoadingCts.Token); |
| 7 | 182 | | } |
| | 183 | |
|
| | 184 | | private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct) |
| | 185 | | { |
| 7 | 186 | | if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null) |
| | 187 | | { |
| 7 | 188 | | avatar.Dispose(); |
| 7 | 189 | | return; |
| | 190 | | } |
| | 191 | |
|
| | 192 | | try |
| | 193 | | { |
| 0 | 194 | | ct.ThrowIfCancellationRequested(); |
| 0 | 195 | | if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar)) |
| | 196 | | { |
| 0 | 197 | | currentAvatar.CopyFrom(profile.avatar); |
| | 198 | |
|
| 0 | 199 | | List<string> wearableItems = profile.avatar.wearables.ToList(); |
| 0 | 200 | | wearableItems.Add(profile.avatar.bodyShape); |
| | 201 | |
|
| | 202 | | //temporarily hardcoding the embedded emotes until the user profile provides the equipped ones |
| 0 | 203 | | var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes"); |
| 0 | 204 | | wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id)); |
| | 205 | |
|
| 0 | 206 | | await avatar.Load(wearableItems, new AvatarSettings |
| | 207 | | { |
| | 208 | | bodyshapeId = profile.avatar.bodyShape, |
| | 209 | | eyesColor = profile.avatar.eyeColor, |
| | 210 | | skinColor = profile.avatar.skinColor, |
| | 211 | | hairColor = profile.avatar.hairColor, |
| | 212 | | }, ct); |
| | 213 | |
|
| 0 | 214 | | if (avatar.lodLevel <= 1) |
| 0 | 215 | | AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab); |
| | 216 | | } |
| 0 | 217 | | } |
| 0 | 218 | | catch (OperationCanceledException) |
| | 219 | | { |
| 0 | 220 | | return; |
| | 221 | | } |
| | 222 | | catch (Exception e) |
| | 223 | | { |
| 0 | 224 | | Debug.LogException(e); |
| 0 | 225 | | WebInterface.ReportAvatarFatalError(); |
| 0 | 226 | | return; |
| | 227 | | } |
| | 228 | |
|
| 0 | 229 | | IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints(); |
| 0 | 230 | | anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), AvatarSystemUtils.AVATAR_Y_OFFSET + avatar.ex |
| | 231 | |
|
| 0 | 232 | | var player = new Player() |
| | 233 | | { |
| | 234 | | id = userProfile.userId, |
| | 235 | | name = userProfile.name, |
| | 236 | | avatar = avatar, |
| | 237 | | anchorPoints = anchorPoints, |
| | 238 | | collider = avatarCollider |
| | 239 | | }; |
| 0 | 240 | | DataStore.i.player.ownPlayer.Set(player); |
| | 241 | |
|
| 0 | 242 | | enableCameraCheck = true; |
| 0 | 243 | | avatarCollider.gameObject.SetActive(true); |
| 0 | 244 | | CommonScriptableObjects.rendererState.RemoveLock(this); |
| 0 | 245 | | DataStore.i.common.isPlayerRendererLoaded.Set(true); |
| 7 | 246 | | } |
| | 247 | |
|
| 0 | 248 | | public void ApplyHideModifier() { avatar.AddVisibilityConstrain(IN_HIDE_AREA); } |
| 0 | 249 | | public void RemoveHideModifier() { avatar.RemoveVisibilityConstrain(IN_HIDE_AREA); } |
| | 250 | |
|
| | 251 | | private void OnDisable() |
| | 252 | | { |
| 560 | 253 | | userProfile.OnUpdate -= OnUserProfileOnUpdate; |
| 560 | 254 | | userProfile.OnAvatarEmoteSet -= OnAvatarEmote; |
| 560 | 255 | | } |
| | 256 | |
|
| | 257 | | private void OnDestroy() |
| | 258 | | { |
| 583 | 259 | | avatarLoadingCts?.Cancel(); |
| 583 | 260 | | avatarLoadingCts?.Dispose(); |
| 583 | 261 | | avatarLoadingCts = null; |
| 583 | 262 | | avatar?.Dispose(); |
| 559 | 263 | | } |
| | 264 | | } |