| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.ExceptionServices; |
| | 5 | | using System.Threading; |
| | 6 | | using AvatarSystem; |
| | 7 | | using Cysharp.Threading.Tasks; |
| | 8 | | using DCL.Components; |
| | 9 | | using DCL.Configuration; |
| | 10 | | using DCL.Controllers; |
| | 11 | | using DCL.Emotes; |
| | 12 | | using DCL.Helpers; |
| | 13 | | using DCL.Interface; |
| | 14 | | using DCL.Models; |
| | 15 | | using GPUSkinning; |
| | 16 | | using UnityEngine; |
| | 17 | | using LOD = AvatarSystem.LOD; |
| | 18 | |
|
| | 19 | | namespace DCL.ECSComponents |
| | 20 | | { |
| | 21 | | public interface IAvatarShape |
| | 22 | | { |
| | 23 | | /// <summary> |
| | 24 | | /// This will initialize the AvatarShape and set |
| | 25 | | /// </summary> |
| | 26 | | void Init(); |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Clean up the avatar shape so we can reutilizate it using the pool |
| | 30 | | /// </summary> |
| | 31 | | void Cleanup(); |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Apply the model of the avatar, it will reload if necessary |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="scene"></param> |
| | 37 | | /// <param name="entity"></param> |
| | 38 | | /// <param name="newModel"></param> |
| | 39 | | void ApplyModel(IParcelScene scene, IDCLEntity entity, PBAvatarShape newModel); |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Get the transform of the avatar shape |
| | 43 | | /// </summary> |
| | 44 | | Transform transform { get; } |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public class AvatarShape : MonoBehaviour, IHideAvatarAreaHandler, IPoolableObjectContainer, IAvatarShape, IPoolLifec |
| | 48 | | { |
| | 49 | | private const float AVATAR_Y_AXIS_OFFSET = -0.72f; |
| | 50 | | private const float MINIMUM_PLAYERNAME_HEIGHT = 2.7f; |
| | 51 | | private const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 52 | | internal const string IN_HIDE_AREA = "IN_HIDE_AREA"; |
| | 53 | |
|
| | 54 | | [SerializeField] private GameObject avatarContainer; |
| | 55 | | [SerializeField] internal Collider avatarCollider; |
| | 56 | | [SerializeField] private Transform avatarRevealContainer; |
| | 57 | | [SerializeField] private GameObject armatureContainer; |
| | 58 | |
|
| | 59 | | [SerializeField] internal AvatarOnPointerDown onPointerDown; |
| | 60 | | [SerializeField] internal GameObject playerNameContainer; |
| | 61 | |
|
| | 62 | | internal IAvatarMovementController avatarMovementController; |
| | 63 | | internal IPlayerName playerName; |
| | 64 | | internal IAvatarReporterController avatarReporterController; |
| | 65 | |
|
| | 66 | | private StringVariable currentPlayerInfoCardId; |
| | 67 | |
|
| | 68 | | internal bool initializedPosition = false; |
| | 69 | |
|
| | 70 | | internal Player player = null; |
| 8 | 71 | | internal BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 72 | |
|
| 20 | 73 | | private IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints(); |
| | 74 | | internal IAvatar avatar; |
| | 75 | | internal CancellationTokenSource loadingCts; |
| | 76 | | private ILazyTextureObserver currentLazyObserver; |
| 20 | 77 | | private bool isGlobalSceneAvatar = true; |
| | 78 | |
|
| 0 | 79 | | public IPoolableObject poolableObject { get; set; } |
| | 80 | | internal PBAvatarShape model; |
| | 81 | | internal IDCLEntity entity; |
| | 82 | |
|
| | 83 | | private void Awake() |
| | 84 | | { |
| 18 | 85 | | avatarMovementController = GetComponent<IAvatarMovementController>(); |
| | 86 | |
|
| 18 | 87 | | currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| 18 | 88 | | Visibility visibility = new Visibility(); |
| 18 | 89 | | avatarMovementController.SetAvatarTransform(transform); |
| | 90 | |
|
| | 91 | | // Right now the avatars that are not part of the global scene of avatar are not using LOD since |
| | 92 | | // AvatarsLodController are no taking them into account. It needs product definition and a refactor to inclu |
| 18 | 93 | | LOD avatarLOD = new LOD(avatarContainer, visibility, avatarMovementController); |
| 18 | 94 | | AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>(); |
| 18 | 95 | | BaseAvatar baseAvatar = new BaseAvatar(avatarRevealContainer, armatureContainer, avatarLOD); |
| 18 | 96 | | avatar = new AvatarWithHologram( |
| | 97 | | baseAvatar, |
| | 98 | | new AvatarCurator(new WearableItemResolver(), Environment.i.serviceLocator.Get<IEmotesCatalogService>()) |
| | 99 | | new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()), |
| | 100 | | animator, |
| | 101 | | visibility, |
| | 102 | | avatarLOD, |
| | 103 | | new SimpleGPUSkinning(), |
| | 104 | | new GPUSkinningThrottler(), |
| | 105 | | new EmoteAnimationEquipper(animator, DataStore.i.emotes)); |
| | 106 | |
|
| 18 | 107 | | if (avatarReporterController == null) |
| 18 | 108 | | avatarReporterController = new AvatarReporterController(Environment.i.world.state); |
| | 109 | |
|
| 18 | 110 | | onPointerDown.OnPointerDownReport += PlayerClicked; |
| 18 | 111 | | onPointerDown.OnPointerEnterReport += PlayerPointerEnter; |
| 18 | 112 | | onPointerDown.OnPointerExitReport += PlayerPointerExit; |
| 18 | 113 | | } |
| | 114 | |
|
| | 115 | | public void Init() |
| | 116 | | { |
| | 117 | | // The avatars have an offset in the Y axis, so we set the offset after the avatar has been restored from th |
| 0 | 118 | | transform.position = new UnityEngine.Vector3(transform.position.x, AVATAR_Y_AXIS_OFFSET, transform.position. |
| 0 | 119 | | SetPlayerNameReference(); |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | private void SetPlayerNameReference() |
| | 123 | | { |
| 5 | 124 | | if (playerName != null) |
| 0 | 125 | | return; |
| 5 | 126 | | playerName = GetComponentInChildren<IPlayerName>(); |
| 5 | 127 | | playerName?.Hide(true); |
| 5 | 128 | | } |
| | 129 | |
|
| | 130 | | private void PlayerClicked() |
| | 131 | | { |
| 0 | 132 | | if (model == null) |
| 0 | 133 | | return; |
| 0 | 134 | | currentPlayerInfoCardId.Set(model.Id); |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | public void OnDestroy() |
| | 138 | | { |
| 18 | 139 | | Cleanup(); |
| | 140 | |
|
| 18 | 141 | | if (poolableObject != null && poolableObject.isInsidePool) |
| 0 | 142 | | poolableObject.RemoveFromPool(); |
| 18 | 143 | | } |
| | 144 | |
|
| | 145 | | public async void ApplyModel(IParcelScene scene, IDCLEntity entity, PBAvatarShape newModel) |
| | 146 | | { |
| 4 | 147 | | this.entity = entity; |
| | 148 | |
|
| 4 | 149 | | isGlobalSceneAvatar = scene.sceneData.id == EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID; |
| | 150 | |
|
| 4 | 151 | | DisablePassport(); |
| | 152 | |
|
| 4 | 153 | | bool needsLoading = !AvatarUtils.HaveSameWearablesAndColors(model,newModel); |
| 4 | 154 | | model = newModel; |
| | 155 | |
|
| | 156 | | #if UNITY_EDITOR |
| 4 | 157 | | gameObject.name = $"Avatar Shape {model.GetName()}"; |
| | 158 | | #endif |
| | 159 | |
|
| | 160 | | // To deal with the cases in which the entity transform was configured before the AvatarShape |
| 4 | 161 | | if (!initializedPosition) |
| | 162 | | { |
| 4 | 163 | | OnEntityTransformChanged(entity.gameObject.transform.localPosition, |
| | 164 | | entity.gameObject.transform.localRotation, true); |
| | 165 | | } |
| | 166 | |
|
| | 167 | | // NOTE: we subscribe here to transform changes since we might "lose" the message |
| | 168 | | // if we subscribe after a any yield |
| 4 | 169 | | entity.OnTransformChange -= OnEntityTransformChanged; |
| 4 | 170 | | entity.OnTransformChange += OnEntityTransformChanged; |
| | 171 | |
|
| 4 | 172 | | var wearableItems = model.GetWereables().ToList(); |
| 4 | 173 | | wearableItems.Add(model.GetBodyShape()); |
| | 174 | |
|
| | 175 | | // temporarily hardcoding the embedded emotes until the user profile provides the equipped ones |
| 4 | 176 | | var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes"); |
| 80 | 177 | | wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id)); |
| 4 | 178 | | HashSet<string> emotes = new HashSet<string>(); |
| 80 | 179 | | emotes.UnionWith(embeddedEmotesSo.emotes.Select(x => x.id)); |
| | 180 | |
|
| 4 | 181 | | if (avatar.status != IAvatar.Status.Loaded || needsLoading) |
| | 182 | | { |
| 4 | 183 | | loadingCts?.Cancel(); |
| 4 | 184 | | loadingCts?.Dispose(); |
| 4 | 185 | | loadingCts = new CancellationTokenSource(); |
| | 186 | | try |
| | 187 | | { |
| 4 | 188 | | await LoadAvatar(wearableItems,emotes); |
| 4 | 189 | | } |
| 0 | 190 | | catch (Exception e) |
| | 191 | | { |
| | 192 | | // If the load of the avatar fails, we do it silently so the scene continue to operate. |
| | 193 | | // The LoadAvatar function will show the error in console already so in order to avoid noise, we jus |
| 0 | 194 | | } |
| | 195 | | } |
| | 196 | |
|
| | 197 | | // If the model contains a value for expressionTriggerId then we try it, if value doesn't exist, we skip |
| 4 | 198 | | if(model.HasExpressionTriggerId) |
| 4 | 199 | | avatar.PlayEmote(model.ExpressionTriggerId, model.GetExpressionTriggerTimestamp()); |
| | 200 | |
|
| 4 | 201 | | UpdatePlayerStatus(entity, model); |
| | 202 | |
|
| 4 | 203 | | onPointerDown.Initialize( |
| | 204 | | new OnPointerDown.Model() |
| | 205 | | { |
| | 206 | | type = OnPointerDown.NAME, |
| | 207 | | button = WebInterface.ACTION_BUTTON.POINTER.ToString(), |
| | 208 | | hoverText = "view profile" |
| | 209 | | }, |
| | 210 | | entity, player |
| | 211 | | ); |
| | 212 | |
|
| 4 | 213 | | avatarCollider.gameObject.SetActive(true); |
| | 214 | |
|
| 4 | 215 | | EnablePassport(); |
| | 216 | |
|
| 4 | 217 | | onPointerDown.SetColliderEnabled(isGlobalSceneAvatar); |
| 4 | 218 | | onPointerDown.SetOnClickReportEnabled(isGlobalSceneAvatar); |
| 4 | 219 | | } |
| | 220 | |
|
| | 221 | | private async UniTask LoadAvatar(List<string> wearableItems, HashSet<string> emotes) |
| | 222 | | { |
| | 223 | | try |
| | 224 | | { |
| | 225 | | //TODO Add Collider to the AvatarSystem |
| | 226 | | //TODO Without this the collider could get triggered disabling the avatar container, |
| | 227 | | // this would stop the loading process due to the underlying coroutines of the AssetLoader not starting |
| 4 | 228 | | avatarCollider.gameObject.SetActive(false); |
| | 229 | |
|
| 4 | 230 | | SetImpostor(model.Id); |
| 4 | 231 | | playerName.SetName(model.GetName()); |
| 4 | 232 | | playerName.Show(true); |
| 4 | 233 | | await avatar.Load(wearableItems, emotes.ToList(),new AvatarSettings |
| | 234 | | { |
| | 235 | | playerName = model.GetName(), |
| | 236 | | bodyshapeId = model.GetBodyShape(), |
| | 237 | | eyesColor = model.GetEyeColor().ToUnityColor(), |
| | 238 | | skinColor = model.GetSkinColor().ToUnityColor(), |
| | 239 | | hairColor = model.GetHairColor().ToUnityColor(), |
| | 240 | | }, loadingCts.Token); |
| 4 | 241 | | } |
| 0 | 242 | | catch (OperationCanceledException) |
| | 243 | | { |
| 0 | 244 | | Cleanup(); |
| 0 | 245 | | throw; |
| | 246 | | } |
| 0 | 247 | | catch (Exception e) |
| | 248 | | { |
| 0 | 249 | | Cleanup(); |
| 0 | 250 | | Debug.Log($"Avatar.Load failed with wearables:[{string.Join(",", wearableItems)}] for bodyshape:{model.B |
| 0 | 251 | | if (e.InnerException != null) |
| 0 | 252 | | ExceptionDispatchInfo.Capture(e.InnerException).Throw(); |
| | 253 | | else |
| 0 | 254 | | throw; |
| 0 | 255 | | } |
| | 256 | | finally |
| | 257 | | { |
| 4 | 258 | | loadingCts?.Dispose(); |
| 4 | 259 | | loadingCts = null; |
| | 260 | | } |
| 4 | 261 | | } |
| | 262 | |
|
| | 263 | | public void SetImpostor(string userId) |
| | 264 | | { |
| 4 | 265 | | currentLazyObserver?.RemoveListener(avatar.SetImpostorTexture); |
| 4 | 266 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 267 | | return; |
| | 268 | |
|
| 4 | 269 | | UserProfile userProfile = UserProfileController.GetProfileByUserId(userId); |
| 4 | 270 | | if (userProfile == null) |
| 4 | 271 | | return; |
| | 272 | |
|
| 0 | 273 | | currentLazyObserver = userProfile.bodySnapshotObserver; |
| 0 | 274 | | currentLazyObserver.AddListener(avatar.SetImpostorTexture); |
| 0 | 275 | | } |
| | 276 | |
|
| 0 | 277 | | private void PlayerPointerExit() { playerName?.SetForceShow(false); } |
| | 278 | |
|
| 0 | 279 | | private void PlayerPointerEnter() { playerName?.SetForceShow(true); } |
| | 280 | |
|
| | 281 | | internal void UpdatePlayerStatus(IDCLEntity entity, PBAvatarShape model) |
| | 282 | | { |
| | 283 | | // Remove the player status if the userId changes |
| 5 | 284 | | if (player != null && (player.id != model.Id || player.name != model.GetName())) |
| 0 | 285 | | otherPlayers.Remove(player.id); |
| | 286 | |
|
| 5 | 287 | | if (isGlobalSceneAvatar && string.IsNullOrEmpty(model?.GetName())) |
| 0 | 288 | | return; |
| | 289 | |
|
| 5 | 290 | | bool isNew = player == null; |
| 5 | 291 | | if (isNew) |
| 5 | 292 | | player = new Player(); |
| | 293 | |
|
| 5 | 294 | | bool isNameDirty = player.name != model.GetName(); |
| | 295 | |
|
| 5 | 296 | | player.id = model.Id; |
| 5 | 297 | | player.name = model.GetName(); |
| 5 | 298 | | player.isTalking = model.Talking; |
| 5 | 299 | | player.worldPosition = entity.gameObject.transform.position; |
| 5 | 300 | | player.avatar = avatar; |
| 5 | 301 | | player.onPointerDownCollider = onPointerDown; |
| 5 | 302 | | player.collider = avatarCollider; |
| | 303 | |
|
| 5 | 304 | | if (isNew) |
| | 305 | | { |
| 5 | 306 | | player.playerName = playerName; |
| 5 | 307 | | player.playerName.Show(); |
| 5 | 308 | | player.anchorPoints = anchorPoints; |
| 5 | 309 | | if (isGlobalSceneAvatar) |
| | 310 | | { |
| | 311 | | // TODO: Note: This is having a problem, sometimes the users has been detected as new 2 times and it |
| | 312 | | // we should investigate this |
| 1 | 313 | | if (otherPlayers.ContainsKey(player.id)) |
| 0 | 314 | | otherPlayers.Remove(player.id); |
| 1 | 315 | | otherPlayers.Add(player.id, player); |
| | 316 | | } |
| 5 | 317 | | avatarReporterController.ReportAvatarRemoved(); |
| | 318 | | } |
| | 319 | |
|
| 5 | 320 | | avatarReporterController.SetUp(entity.scene.sceneData.id, player.id); |
| | 321 | |
|
| 5 | 322 | | float height = AvatarSystemUtils.AVATAR_Y_OFFSET + avatar.extents.y; |
| | 323 | |
|
| 5 | 324 | | anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), height); |
| | 325 | |
|
| 5 | 326 | | player.playerName.SetIsTalking(model.Talking); |
| 5 | 327 | | player.playerName.SetYOffset(Mathf.Max(MINIMUM_PLAYERNAME_HEIGHT, height)); |
| 5 | 328 | | if (isNameDirty) |
| 5 | 329 | | player.playerName.SetName(model.GetName()); |
| 5 | 330 | | } |
| | 331 | |
|
| | 332 | | private void Update() |
| | 333 | | { |
| 0 | 334 | | if (player == null || entity == null) |
| 0 | 335 | | return; |
| | 336 | |
|
| 0 | 337 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 338 | | player.forwardDirection = entity.gameObject.transform.forward; |
| 0 | 339 | | avatarReporterController.ReportAvatarPosition(player.worldPosition); |
| 0 | 340 | | } |
| | 341 | |
|
| | 342 | | public void DisablePassport() |
| | 343 | | { |
| 4 | 344 | | if (onPointerDown.collider == null) |
| 0 | 345 | | return; |
| | 346 | |
|
| 4 | 347 | | onPointerDown.SetPassportEnabled(false); |
| 4 | 348 | | } |
| | 349 | |
|
| | 350 | | public void EnablePassport() |
| | 351 | | { |
| 4 | 352 | | if (onPointerDown.collider == null) |
| 0 | 353 | | return; |
| | 354 | |
|
| 4 | 355 | | onPointerDown.SetPassportEnabled(true); |
| 4 | 356 | | } |
| | 357 | |
|
| | 358 | | private void OnEntityTransformChanged(object newModel) |
| | 359 | | { |
| 0 | 360 | | DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel; |
| 0 | 361 | | OnEntityTransformChanged(newTransformModel.position, newTransformModel.rotation, !initializedPosition); |
| 0 | 362 | | } |
| | 363 | |
|
| | 364 | | private void OnEntityTransformChanged(in UnityEngine.Vector3 position, in Quaternion rotation, bool inmediate) |
| | 365 | | { |
| 4 | 366 | | if (entity == null) |
| 0 | 367 | | return; |
| | 368 | |
|
| 4 | 369 | | if (isGlobalSceneAvatar) |
| | 370 | | { |
| 0 | 371 | | avatarMovementController.OnTransformChanged(position, rotation, inmediate); |
| 0 | 372 | | } |
| | 373 | | else |
| | 374 | | { |
| 4 | 375 | | var scenePosition = DCL.Helpers.Utils.GridToWorldPosition(entity.scene.sceneData.basePosition.x, entity. |
| 4 | 376 | | avatarMovementController.OnTransformChanged(scenePosition + position, rotation, inmediate); |
| | 377 | | } |
| 4 | 378 | | initializedPosition = true; |
| 4 | 379 | | } |
| | 380 | |
|
| | 381 | | public void OnPoolRelease() |
| | 382 | | { |
| 0 | 383 | | Cleanup(); |
| 0 | 384 | | } |
| | 385 | |
|
| | 386 | | public void OnPoolGet() |
| | 387 | | { |
| 5 | 388 | | initializedPosition = false; |
| 5 | 389 | | model = new PBAvatarShape(); |
| 5 | 390 | | player = null; |
| 5 | 391 | | SetPlayerNameReference(); |
| 5 | 392 | | } |
| | 393 | |
|
| | 394 | | public void ApplyHideAvatarModifier() |
| | 395 | | { |
| 1 | 396 | | avatar.AddVisibilityConstrain(IN_HIDE_AREA); |
| 1 | 397 | | onPointerDown.gameObject.SetActive(false); |
| 1 | 398 | | playerNameContainer.SetActive(false); |
| 1 | 399 | | } |
| | 400 | |
|
| | 401 | | public void RemoveHideAvatarModifier() |
| | 402 | | { |
| 1 | 403 | | avatar.RemoveVisibilityConstrain(IN_HIDE_AREA); |
| 1 | 404 | | onPointerDown.gameObject.SetActive(true); |
| 1 | 405 | | playerNameContainer.SetActive(true); |
| 1 | 406 | | } |
| | 407 | |
|
| | 408 | | public void Cleanup() |
| | 409 | | { |
| 19 | 410 | | playerName?.Hide(true); |
| 19 | 411 | | if (player != null) |
| | 412 | | { |
| 5 | 413 | | otherPlayers.Remove(player.id); |
| 5 | 414 | | player = null; |
| | 415 | | } |
| | 416 | |
|
| 19 | 417 | | loadingCts?.Cancel(); |
| 19 | 418 | | loadingCts?.Dispose(); |
| 19 | 419 | | loadingCts = null; |
| | 420 | |
|
| 19 | 421 | | currentLazyObserver?.RemoveListener(avatar.SetImpostorTexture); |
| 19 | 422 | | avatar.Dispose(); |
| | 423 | |
|
| 19 | 424 | | if (poolableObject != null) |
| | 425 | | { |
| 0 | 426 | | poolableObject.OnRelease -= Cleanup; |
| | 427 | | } |
| | 428 | |
|
| 19 | 429 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 19 | 430 | | onPointerDown.OnPointerEnterReport -= PlayerPointerEnter; |
| 19 | 431 | | onPointerDown.OnPointerExitReport -= PlayerPointerExit; |
| | 432 | |
|
| 19 | 433 | | if (entity != null) |
| | 434 | | { |
| 4 | 435 | | entity.OnTransformChange = null; |
| 4 | 436 | | entity = null; |
| | 437 | | } |
| | 438 | |
|
| 19 | 439 | | avatarReporterController.ReportAvatarRemoved(); |
| 19 | 440 | | } |
| | 441 | |
|
| | 442 | | [ContextMenu("Print current profile")] |
| 0 | 443 | | private void PrintCurrentProfile() { Debug.Log(JsonUtility.ToJson(model)); } |
| | 444 | | } |
| | 445 | | } |
| | 446 | |
|