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