| | 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 UnityEngine; |
| | 14 | | using Type = DCL.NotificationModel.Type; |
| | 15 | |
|
| | 16 | | public class PlayerAvatarController : MonoBehaviour |
| | 17 | | { |
| | 18 | | private const string LOADING_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables"; |
| | 19 | |
|
| | 20 | | private AvatarSystem.Avatar avatar; |
| | 21 | | private CancellationTokenSource avatarLoadingCts = null; |
| | 22 | | public GameObject avatarContainer; |
| 598 | 23 | | private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() }; |
| | 24 | |
|
| | 25 | | public Collider avatarCollider; |
| | 26 | | public AvatarVisibility avatarVisibility; |
| | 27 | | [SerializeField] private GameObject loadingParticlesPrefab; |
| 598 | 28 | | public float cameraDistanceToDeactivate = 1.0f; |
| | 29 | |
|
| 2292 | 30 | | private UserProfile userProfile => UserProfile.GetOwnUserProfile(); |
| 0 | 31 | | private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame(); |
| | 32 | |
|
| | 33 | | private bool enableCameraCheck = false; |
| | 34 | | private Camera mainCamera; |
| | 35 | | private PlayerAvatarAnalytics playerAvatarAnalytics; |
| | 36 | | private IFatalErrorReporter fatalErrorReporter; // TODO? |
| | 37 | | private string VISIBILITY_CONSTRAIN; |
| | 38 | |
|
| | 39 | | private void Start() |
| | 40 | | { |
| 572 | 41 | | DataStore.i.common.isPlayerRendererLoaded.Set(false); |
| 572 | 42 | | IAnalytics analytics = DCL.Environment.i.platform.serviceProviders.analytics; |
| 572 | 43 | | playerAvatarAnalytics = new PlayerAvatarAnalytics(analytics, CommonScriptableObjects.playerCoords); |
| | 44 | |
|
| 572 | 45 | | AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>(); |
| 572 | 46 | | avatar = new AvatarSystem.Avatar( |
| | 47 | | new AvatarCurator(new WearableItemResolver()), |
| | 48 | | new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()), |
| | 49 | | animator, |
| | 50 | | new Visibility(), |
| | 51 | | new NoLODs(), |
| | 52 | | new SimpleGPUSkinning(), |
| | 53 | | new GPUSkinningThrottler(), |
| | 54 | | new EmoteAnimationEquipper(animator, DataStore.i.emotes)); |
| | 55 | |
|
| 572 | 56 | | if ( UserProfileController.i != null ) |
| | 57 | | { |
| 0 | 58 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| 0 | 59 | | UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail; |
| | 60 | | } |
| | 61 | |
|
| 572 | 62 | | CommonScriptableObjects.rendererState.AddLock(this); |
| | 63 | |
|
| | 64 | | #if UNITY_WEBGL |
| | 65 | | fatalErrorReporter = new WebFatalErrorReporter(); |
| | 66 | | #else |
| 572 | 67 | | fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i); |
| | 68 | | #endif |
| | 69 | |
|
| 572 | 70 | | mainCamera = Camera.main; |
| 572 | 71 | | } |
| | 72 | |
|
| | 73 | | private void OnBaseWereablesFail() |
| | 74 | | { |
| 0 | 75 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| | 76 | |
|
| 0 | 77 | | if (enableCameraCheck) |
| 0 | 78 | | ShowWearablesWarning(); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | private void ShowWearablesWarning() |
| | 82 | | { |
| 0 | 83 | | NotificationsController.i.ShowNotification(new Model |
| | 84 | | { |
| | 85 | | message = LOADING_WEARABLES_ERROR_MESSAGE, |
| | 86 | | type = Type.GENERIC, |
| | 87 | | timer = 10f, |
| | 88 | | destroyOnFinish = true |
| | 89 | | }); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | private void Update() |
| | 93 | | { |
| 5548 | 94 | | if (!enableCameraCheck || repositioningWorld) |
| 5548 | 95 | | return; |
| | 96 | |
|
| 0 | 97 | | if (mainCamera == null) |
| | 98 | | { |
| 0 | 99 | | mainCamera = Camera.main; |
| | 100 | |
|
| 0 | 101 | | if (mainCamera == null) |
| 0 | 102 | | return; |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | bool shouldBeVisible = Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDea |
| 0 | 106 | | avatarVisibility.SetVisibility("PLAYER_AVATAR_CONTROLLER", shouldBeVisible); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public void SetAvatarVisibility(bool isVisible) |
| | 110 | | { |
| 43 | 111 | | VISIBILITY_CONSTRAIN = "own_player_invisible"; |
| 43 | 112 | | if (isVisible) |
| 5 | 113 | | avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN); |
| | 114 | | else |
| 38 | 115 | | avatar.AddVisibilityConstrain(VISIBILITY_CONSTRAIN); |
| 38 | 116 | | } |
| | 117 | |
|
| | 118 | | private void OnEnable() |
| | 119 | | { |
| 573 | 120 | | userProfile.OnUpdate += OnUserProfileOnUpdate; |
| 573 | 121 | | userProfile.OnAvatarEmoteSet += OnAvatarEmote; |
| 573 | 122 | | } |
| | 123 | |
|
| | 124 | | private void OnAvatarEmote(string id, long timestamp) |
| | 125 | | { |
| 2 | 126 | | avatar.PlayEmote(id, timestamp); |
| 2 | 127 | | playerAvatarAnalytics.ReportExpression(id); |
| 2 | 128 | | } |
| | 129 | |
|
| | 130 | | private void OnUserProfileOnUpdate(UserProfile profile) |
| | 131 | | { |
| 21 | 132 | | avatarLoadingCts?.Cancel(); |
| 21 | 133 | | avatarLoadingCts?.Dispose(); |
| 21 | 134 | | avatarLoadingCts = new CancellationTokenSource(); |
| 21 | 135 | | LoadingAvatarRoutine(profile, avatarLoadingCts.Token); |
| 21 | 136 | | } |
| | 137 | |
|
| | 138 | | private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct) |
| | 139 | | { |
| 21 | 140 | | if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null) |
| | 141 | | { |
| 21 | 142 | | avatar.Dispose(); |
| 21 | 143 | | return; |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | ct.ThrowIfCancellationRequested(); |
| 0 | 147 | | if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar)) |
| | 148 | | { |
| | 149 | | try |
| | 150 | | { |
| 0 | 151 | | currentAvatar.CopyFrom(profile.avatar); |
| | 152 | |
|
| 0 | 153 | | List<string> wearableItems = profile.avatar.wearables.ToList(); |
| 0 | 154 | | wearableItems.Add(profile.avatar.bodyShape); |
| | 155 | |
|
| | 156 | | //temporarily hardcoding the embedded emotes until the user profile provides the equipped ones |
| 0 | 157 | | var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes"); |
| 0 | 158 | | wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id)); |
| | 159 | |
|
| 0 | 160 | | await avatar.Load(wearableItems, new AvatarSettings |
| | 161 | | { |
| | 162 | | bodyshapeId = profile.avatar.bodyShape, |
| | 163 | | eyesColor = profile.avatar.eyeColor, |
| | 164 | | skinColor = profile.avatar.skinColor, |
| | 165 | | hairColor = profile.avatar.hairColor, |
| | 166 | | }, ct); |
| | 167 | |
|
| 0 | 168 | | if (avatar.lodLevel <= 1) |
| 0 | 169 | | AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab); |
| 0 | 170 | | } |
| 0 | 171 | | catch (OperationCanceledException) |
| | 172 | | { |
| 0 | 173 | | return; |
| | 174 | | } |
| | 175 | | catch (Exception e) |
| | 176 | | { |
| 0 | 177 | | Debug.LogException(e); |
| 0 | 178 | | WebInterface.ReportAvatarFatalError(); |
| 0 | 179 | | return; |
| | 180 | | } |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints(); |
| 0 | 184 | | anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), avatar.extents.y); |
| | 185 | |
|
| 0 | 186 | | var player = new Player() |
| | 187 | | { |
| | 188 | | id = userProfile.userId, |
| | 189 | | name = userProfile.name, |
| | 190 | | avatar = avatar, |
| | 191 | | anchorPoints = anchorPoints, |
| | 192 | | collider = avatarCollider |
| | 193 | | }; |
| 0 | 194 | | DataStore.i.player.ownPlayer.Set(player); |
| | 195 | |
|
| 0 | 196 | | enableCameraCheck = true; |
| 0 | 197 | | avatarCollider.gameObject.SetActive(true); |
| 0 | 198 | | CommonScriptableObjects.rendererState.RemoveLock(this); |
| 0 | 199 | | DataStore.i.common.isPlayerRendererLoaded.Set(true); |
| 21 | 200 | | } |
| | 201 | |
|
| | 202 | | private void OnDisable() |
| | 203 | | { |
| 573 | 204 | | userProfile.OnUpdate -= OnUserProfileOnUpdate; |
| 573 | 205 | | userProfile.OnAvatarEmoteSet -= OnAvatarEmote; |
| 573 | 206 | | } |
| | 207 | |
|
| | 208 | | private void OnDestroy() |
| | 209 | | { |
| 597 | 210 | | avatarLoadingCts?.Cancel(); |
| 597 | 211 | | avatarLoadingCts?.Dispose(); |
| 597 | 212 | | avatarLoadingCts = null; |
| 597 | 213 | | avatar?.Dispose(); |
| 572 | 214 | | } |
| | 215 | | } |