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