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