| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Interface; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | | using AvatarSystem; |
| | 9 | | using DCL.Configuration; |
| | 10 | | using DCL.Emotes; |
| | 11 | | using DCL.Helpers; |
| | 12 | | using DCL.Models; |
| | 13 | | using GPUSkinning; |
| | 14 | | using UnityEngine; |
| | 15 | | using Avatar = AvatarSystem.Avatar; |
| | 16 | | using LOD = AvatarSystem.LOD; |
| | 17 | |
|
| | 18 | | namespace DCL |
| | 19 | | { |
| | 20 | | public class AvatarShape : BaseComponent, IHideAvatarAreaHandler, IHidePassportAreaHandler |
| | 21 | | { |
| | 22 | | private const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 23 | | private const float MINIMUM_PLAYERNAME_HEIGHT = 2.7f; |
| | 24 | | private const float AVATAR_PASSPORT_TOGGLE_ALPHA_THRESHOLD = 0.9f; |
| | 25 | | private const string IN_HIDE_AREA = "IN_HIDE_AREA"; |
| | 26 | |
|
| | 27 | | public static event Action<IDCLEntity, AvatarShape> OnAvatarShapeUpdated; |
| | 28 | |
|
| | 29 | | public GameObject avatarContainer; |
| | 30 | | public Collider avatarCollider; |
| | 31 | | public AvatarMovementController avatarMovementController; |
| | 32 | | public StickersController stickersControllers; |
| | 33 | | [SerializeField] private Transform avatarRevealContainer; |
| | 34 | | [SerializeField] private GameObject armatureContainer; |
| | 35 | |
|
| | 36 | | [SerializeField] internal AvatarOnPointerDown onPointerDown; |
| | 37 | | [SerializeField] internal GameObject playerNameContainer; |
| | 38 | | internal IPlayerName playerName; |
| | 39 | | internal IAvatarReporterController avatarReporterController; |
| | 40 | |
|
| | 41 | | private StringVariable currentPlayerInfoCardId; |
| | 42 | |
|
| | 43 | | public bool everythingIsLoaded; |
| | 44 | |
|
| | 45 | | bool initializedPosition = false; |
| | 46 | |
|
| | 47 | | private Player player = null; |
| 0 | 48 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 49 | |
|
| 402 | 50 | | private IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints(); |
| | 51 | | internal IAvatar avatar; |
| 402 | 52 | | private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() }; |
| | 53 | | private CancellationTokenSource loadingCts; |
| | 54 | | private ILazyTextureObserver currentLazyObserver; |
| 402 | 55 | | private bool isGlobalSceneAvatar = true; |
| | 56 | | private BaseRefCounter<AvatarModifierAreaID> currentActiveModifiers; |
| | 57 | |
|
| 4 | 58 | | public override string componentName => "avatarShape"; |
| | 59 | |
|
| | 60 | | private void Awake() |
| | 61 | | { |
| 400 | 62 | | model = new AvatarModel(); |
| 400 | 63 | | currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| | 64 | |
|
| 400 | 65 | | if (DataStore.i.avatarConfig.useHologramAvatar.Get()) |
| 2 | 66 | | avatar = GetAvatarWithHologram(); |
| | 67 | | else |
| 398 | 68 | | avatar = GetStandardAvatar(); |
| | 69 | |
|
| 400 | 70 | | if (avatarReporterController == null) |
| | 71 | | { |
| 400 | 72 | | avatarReporterController = new AvatarReporterController(Environment.i.world.state); |
| | 73 | | } |
| 400 | 74 | | } |
| | 75 | |
|
| | 76 | | private Avatar GetStandardAvatar() |
| | 77 | | { |
| 398 | 78 | | Visibility visibility = new Visibility(); |
| 398 | 79 | | LOD avatarLOD = new LOD(avatarContainer, visibility, avatarMovementController); |
| 398 | 80 | | AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>(); |
| 398 | 81 | | return new Avatar( |
| | 82 | | new AvatarCurator(new WearableItemResolver()), |
| | 83 | | new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()), |
| | 84 | | animator, |
| | 85 | | visibility, |
| | 86 | | avatarLOD, |
| | 87 | | new SimpleGPUSkinning(), |
| | 88 | | new GPUSkinningThrottler(), |
| | 89 | | new EmoteAnimationEquipper(animator, DataStore.i.emotes)); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | private AvatarWithHologram GetAvatarWithHologram() |
| | 93 | | { |
| 2 | 94 | | Visibility visibility = new Visibility(); |
| 2 | 95 | | LOD avatarLOD = new LOD(avatarContainer, visibility, avatarMovementController); |
| 2 | 96 | | AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>(); |
| 2 | 97 | | BaseAvatar baseAvatar = new BaseAvatar(avatarRevealContainer, armatureContainer, avatarLOD); |
| 2 | 98 | | return new AvatarWithHologram( |
| | 99 | | baseAvatar, |
| | 100 | | new AvatarCurator(new WearableItemResolver()), |
| | 101 | | new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()), |
| | 102 | | animator, |
| | 103 | | visibility, |
| | 104 | | avatarLOD, |
| | 105 | | new SimpleGPUSkinning(), |
| | 106 | | new GPUSkinningThrottler(), |
| | 107 | | new EmoteAnimationEquipper(animator, DataStore.i.emotes)); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | private void Start() |
| | 111 | | { |
| 1 | 112 | | playerName = GetComponentInChildren<IPlayerName>(); |
| 1 | 113 | | playerName?.Hide(true); |
| 1 | 114 | | currentActiveModifiers ??= new BaseRefCounter<AvatarModifierAreaID>(); |
| 1 | 115 | | } |
| | 116 | |
|
| | 117 | | private void PlayerClicked() |
| | 118 | | { |
| 0 | 119 | | if (model == null) |
| 0 | 120 | | return; |
| 0 | 121 | | currentPlayerInfoCardId.Set(((AvatarModel) model).id); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public void OnDestroy() |
| | 125 | | { |
| 400 | 126 | | Cleanup(); |
| | 127 | |
|
| 400 | 128 | | if (poolableObject != null && poolableObject.isInsidePool) |
| 5 | 129 | | poolableObject.RemoveFromPool(); |
| 400 | 130 | | } |
| | 131 | |
|
| | 132 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 133 | | { |
| 5 | 134 | | isGlobalSceneAvatar = scene.sceneData.id == EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID; |
| 5 | 135 | | currentActiveModifiers ??= new BaseRefCounter<AvatarModifierAreaID>(); |
| | 136 | |
|
| 5 | 137 | | ApplyHidePassportModifier(); |
| | 138 | |
|
| 5 | 139 | | var model = (AvatarModel) newModel; |
| | 140 | |
|
| 5 | 141 | | bool needsLoading = !model.HaveSameWearablesAndColors(currentAvatar); |
| 5 | 142 | | currentAvatar.CopyFrom(model); |
| | 143 | |
|
| 5 | 144 | | if (string.IsNullOrEmpty(model.bodyShape) || model.wearables.Count == 0) |
| 1 | 145 | | yield break; |
| | 146 | | #if UNITY_EDITOR |
| 4 | 147 | | gameObject.name = $"Avatar Shape {model.name}"; |
| | 148 | | #endif |
| 4 | 149 | | everythingIsLoaded = false; |
| | 150 | |
|
| | 151 | | bool avatarDone = false; |
| | 152 | | bool avatarFailed = false; |
| | 153 | |
|
| 4 | 154 | | yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved. |
| | 155 | |
|
| | 156 | | // To deal with the cases in which the entity transform was configured before the AvatarShape |
| 0 | 157 | | if (!initializedPosition && scene.componentsManagerLegacy.HasComponent(entity, CLASS_ID_COMPONENT.TRANSFORM) |
| | 158 | | { |
| 0 | 159 | | initializedPosition = true; |
| 0 | 160 | | OnEntityTransformChanged(entity.gameObject.transform.localPosition, |
| | 161 | | entity.gameObject.transform.localRotation, true); |
| | 162 | | } |
| | 163 | |
|
| | 164 | | // NOTE: we subscribe here to transform changes since we might "lose" the message |
| | 165 | | // if we subscribe after a any yield |
| 0 | 166 | | entity.OnTransformChange -= OnEntityTransformChanged; |
| 0 | 167 | | entity.OnTransformChange += OnEntityTransformChanged; |
| | 168 | |
|
| 0 | 169 | | var wearableItems = model.wearables.ToList(); |
| 0 | 170 | | wearableItems.Add(model.bodyShape); |
| | 171 | |
|
| | 172 | | //temporarily hardcoding the embedded emotes until the user profile provides the equipped ones |
| 0 | 173 | | var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes"); |
| 0 | 174 | | wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id)); |
| | 175 | |
|
| 0 | 176 | | if (avatar.status != IAvatar.Status.Loaded || needsLoading) |
| | 177 | | { |
| | 178 | | //TODO Add Collider to the AvatarSystem |
| | 179 | | //TODO Without this the collider could get triggered disabling the avatar container, |
| | 180 | | // this would stop the loading process due to the underlying coroutines of the AssetLoader not starting |
| 0 | 181 | | avatarCollider.gameObject.SetActive(false); |
| | 182 | |
|
| 0 | 183 | | SetImpostor(model.id); |
| 0 | 184 | | loadingCts?.Cancel(); |
| 0 | 185 | | loadingCts?.Dispose(); |
| 0 | 186 | | loadingCts = new CancellationTokenSource(); |
| 0 | 187 | | if (DataStore.i.avatarConfig.useHologramAvatar.Get()) |
| | 188 | | { |
| 0 | 189 | | playerName.SetName(model.name); |
| 0 | 190 | | playerName.Show(true); |
| | 191 | | } |
| 0 | 192 | | avatar.Load(wearableItems, new AvatarSettings |
| | 193 | | { |
| | 194 | | playerName = model.name, |
| | 195 | | bodyshapeId = model.bodyShape, |
| | 196 | | eyesColor = model.eyeColor, |
| | 197 | | skinColor = model.skinColor, |
| | 198 | | hairColor = model.hairColor, |
| | 199 | | }, loadingCts.Token); |
| | 200 | |
|
| | 201 | | // Yielding a UniTask doesn't do anything, we manually wait until the avatar is ready |
| 0 | 202 | | yield return new WaitUntil(() => avatar.status == IAvatar.Status.Loaded); |
| | 203 | | } |
| | 204 | |
|
| 0 | 205 | | avatar.PlayEmote(model.expressionTriggerId, model.expressionTriggerTimestamp); |
| | 206 | |
|
| 0 | 207 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 0 | 208 | | onPointerDown.OnPointerDownReport += PlayerClicked; |
| 0 | 209 | | onPointerDown.OnPointerEnterReport -= PlayerPointerEnter; |
| 0 | 210 | | onPointerDown.OnPointerEnterReport += PlayerPointerEnter; |
| 0 | 211 | | onPointerDown.OnPointerExitReport -= PlayerPointerExit; |
| 0 | 212 | | onPointerDown.OnPointerExitReport += PlayerPointerExit; |
| | 213 | |
|
| 0 | 214 | | UpdatePlayerStatus(model); |
| | 215 | |
|
| 0 | 216 | | onPointerDown.Initialize( |
| | 217 | | new OnPointerDown.Model() |
| | 218 | | { |
| | 219 | | type = OnPointerDown.NAME, |
| | 220 | | button = WebInterface.ACTION_BUTTON.POINTER.ToString(), |
| | 221 | | hoverText = "view profile" |
| | 222 | | }, |
| | 223 | | entity, player |
| | 224 | | ); |
| | 225 | |
|
| 0 | 226 | | avatarCollider.gameObject.SetActive(true); |
| | 227 | |
|
| 0 | 228 | | everythingIsLoaded = true; |
| 0 | 229 | | OnAvatarShapeUpdated?.Invoke(entity, this); |
| | 230 | |
|
| 0 | 231 | | RemoveHidePassportModifier(); |
| | 232 | |
|
| 0 | 233 | | onPointerDown.SetColliderEnabled(isGlobalSceneAvatar); |
| 0 | 234 | | onPointerDown.SetOnClickReportEnabled(isGlobalSceneAvatar); |
| 0 | 235 | | } |
| | 236 | |
|
| | 237 | | public void SetImpostor(string userId) |
| | 238 | | { |
| 0 | 239 | | currentLazyObserver?.RemoveListener(avatar.SetImpostorTexture); |
| 0 | 240 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 241 | | return; |
| | 242 | |
|
| 0 | 243 | | UserProfile userProfile = UserProfileController.GetProfileByUserId(userId); |
| 0 | 244 | | if (userProfile == null) |
| 0 | 245 | | return; |
| | 246 | |
|
| 0 | 247 | | currentLazyObserver = userProfile.bodySnapshotObserver; |
| 0 | 248 | | currentLazyObserver.AddListener(avatar.SetImpostorTexture); |
| 0 | 249 | | } |
| | 250 | |
|
| 0 | 251 | | private void PlayerPointerExit() { playerName?.SetForceShow(false); } |
| 0 | 252 | | private void PlayerPointerEnter() { playerName?.SetForceShow(true); } |
| | 253 | |
|
| | 254 | | private void UpdatePlayerStatus(AvatarModel model) |
| | 255 | | { |
| | 256 | | // Remove the player status if the userId changes |
| 0 | 257 | | if (player != null && (player.id != model.id || player.name != model.name)) |
| 0 | 258 | | otherPlayers.Remove(player.id); |
| | 259 | |
|
| 0 | 260 | | if (isGlobalSceneAvatar && string.IsNullOrEmpty(model?.id)) |
| 0 | 261 | | return; |
| | 262 | |
|
| 0 | 263 | | bool isNew = player == null; |
| 0 | 264 | | if (isNew) |
| | 265 | | { |
| 0 | 266 | | player = new Player(); |
| | 267 | | } |
| | 268 | |
|
| 0 | 269 | | bool isNameDirty = player.name != model.name; |
| | 270 | |
|
| 0 | 271 | | player.id = model.id; |
| 0 | 272 | | player.name = model.name; |
| 0 | 273 | | player.isTalking = model.talking; |
| 0 | 274 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 275 | | player.avatar = avatar; |
| 0 | 276 | | player.onPointerDownCollider = onPointerDown; |
| 0 | 277 | | player.collider = avatarCollider; |
| | 278 | |
|
| 0 | 279 | | if (isNew) |
| | 280 | | { |
| 0 | 281 | | player.playerName = playerName; |
| 0 | 282 | | player.playerName.Show(); |
| 0 | 283 | | player.anchorPoints = anchorPoints; |
| 0 | 284 | | if (isGlobalSceneAvatar) |
| | 285 | | { |
| | 286 | | // TODO: Note: This is having a problem, sometimes the users has been detected as new 2 times and it |
| | 287 | | // we should investigate this |
| 0 | 288 | | if (otherPlayers.ContainsKey(player.id)) |
| 0 | 289 | | otherPlayers.Remove(player.id); |
| 0 | 290 | | otherPlayers.Add(player.id, player); |
| | 291 | | } |
| 0 | 292 | | avatarReporterController.ReportAvatarRemoved(); |
| | 293 | | } |
| | 294 | |
|
| 0 | 295 | | avatarReporterController.SetUp(entity.scene.sceneData.id, player.id); |
| | 296 | |
|
| 0 | 297 | | float height = AvatarSystemUtils.AVATAR_Y_OFFSET + avatar.extents.y; |
| | 298 | |
|
| 0 | 299 | | anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), height); |
| | 300 | |
|
| 0 | 301 | | player.playerName.SetIsTalking(model.talking); |
| 0 | 302 | | player.playerName.SetYOffset(Mathf.Max(MINIMUM_PLAYERNAME_HEIGHT, height)); |
| 0 | 303 | | if (isNameDirty) |
| 0 | 304 | | player.playerName.SetName(model.name); |
| 0 | 305 | | } |
| | 306 | |
|
| | 307 | | private void Update() |
| | 308 | | { |
| 1 | 309 | | if (player != null) |
| | 310 | | { |
| 0 | 311 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 312 | | player.forwardDirection = entity.gameObject.transform.forward; |
| 0 | 313 | | avatarReporterController.ReportAvatarPosition(player.worldPosition); |
| | 314 | | } |
| 1 | 315 | | } |
| | 316 | |
|
| | 317 | | private void OnEntityTransformChanged(object newModel) |
| | 318 | | { |
| 0 | 319 | | DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel; |
| 0 | 320 | | OnEntityTransformChanged(newTransformModel.position, newTransformModel.rotation, !initializedPosition); |
| 0 | 321 | | } |
| | 322 | |
|
| | 323 | | private void OnEntityTransformChanged(in Vector3 position, in Quaternion rotation, bool inmediate) |
| | 324 | | { |
| 0 | 325 | | if (isGlobalSceneAvatar) |
| | 326 | | { |
| 0 | 327 | | avatarMovementController.OnTransformChanged(position, rotation, inmediate); |
| 0 | 328 | | } |
| | 329 | | else |
| | 330 | | { |
| 0 | 331 | | var scenePosition = Utils.GridToWorldPosition(entity.scene.sceneData.basePosition.x, entity.scene.sceneD |
| 0 | 332 | | avatarMovementController.OnTransformChanged(scenePosition + position, rotation, inmediate); |
| | 333 | | } |
| 0 | 334 | | initializedPosition = true; |
| 0 | 335 | | } |
| | 336 | |
|
| | 337 | | public override void OnPoolGet() |
| | 338 | | { |
| 5 | 339 | | base.OnPoolGet(); |
| | 340 | |
|
| 5 | 341 | | everythingIsLoaded = false; |
| 5 | 342 | | initializedPosition = false; |
| 5 | 343 | | model = new AvatarModel(); |
| 5 | 344 | | player = null; |
| 5 | 345 | | } |
| | 346 | |
|
| | 347 | | public void ApplyHideAvatarModifier() |
| | 348 | | { |
| 0 | 349 | | if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR)) |
| | 350 | | { |
| 0 | 351 | | avatar.AddVisibilityConstrain(IN_HIDE_AREA); |
| 0 | 352 | | onPointerDown.gameObject.SetActive(false); |
| 0 | 353 | | playerNameContainer.SetActive(false); |
| 0 | 354 | | stickersControllers.ToggleHideArea(true); |
| | 355 | | } |
| 0 | 356 | | currentActiveModifiers.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR); |
| 0 | 357 | | } |
| | 358 | |
|
| | 359 | | public void RemoveHideAvatarModifier() |
| | 360 | | { |
| 0 | 361 | | currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR); |
| 0 | 362 | | if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR)) |
| | 363 | | { |
| 0 | 364 | | avatar.RemoveVisibilityConstrain(IN_HIDE_AREA); |
| 0 | 365 | | onPointerDown.gameObject.SetActive(true); |
| 0 | 366 | | playerNameContainer.SetActive(true); |
| 0 | 367 | | stickersControllers.ToggleHideArea(false); |
| | 368 | | } |
| 0 | 369 | | } |
| | 370 | |
|
| | 371 | | public void ApplyHidePassportModifier() |
| | 372 | | { |
| 5 | 373 | | if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.DISABLE_PASSPORT)) |
| | 374 | | { |
| 5 | 375 | | if (onPointerDown.collider == null) |
| 0 | 376 | | return; |
| | 377 | |
|
| 5 | 378 | | onPointerDown.SetPassportEnabled(false); |
| | 379 | | } |
| 5 | 380 | | currentActiveModifiers.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT); |
| 5 | 381 | | } |
| | 382 | |
|
| | 383 | | public void RemoveHidePassportModifier() |
| | 384 | | { |
| 0 | 385 | | currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT); |
| 0 | 386 | | if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.DISABLE_PASSPORT)) |
| | 387 | | { |
| 0 | 388 | | if (onPointerDown.collider == null) |
| 0 | 389 | | return; |
| | 390 | |
|
| 0 | 391 | | onPointerDown.SetPassportEnabled(true); |
| | 392 | | } |
| 0 | 393 | | } |
| | 394 | |
|
| | 395 | | public override void Cleanup() |
| | 396 | | { |
| 410 | 397 | | base.Cleanup(); |
| | 398 | |
|
| 410 | 399 | | playerName?.Hide(true); |
| 410 | 400 | | if (player != null) |
| | 401 | | { |
| 0 | 402 | | otherPlayers.Remove(player.id); |
| 0 | 403 | | player = null; |
| | 404 | | } |
| | 405 | |
|
| 410 | 406 | | loadingCts?.Cancel(); |
| 410 | 407 | | loadingCts?.Dispose(); |
| 410 | 408 | | loadingCts = null; |
| 410 | 409 | | currentLazyObserver?.RemoveListener(avatar.SetImpostorTexture); |
| 410 | 410 | | avatar.Dispose(); |
| | 411 | |
|
| 410 | 412 | | if (poolableObject != null) |
| | 413 | | { |
| 15 | 414 | | poolableObject.OnRelease -= Cleanup; |
| | 415 | | } |
| | 416 | |
|
| 410 | 417 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 410 | 418 | | onPointerDown.OnPointerEnterReport -= PlayerPointerEnter; |
| 410 | 419 | | onPointerDown.OnPointerExitReport -= PlayerPointerExit; |
| | 420 | |
|
| 410 | 421 | | if (entity != null) |
| | 422 | | { |
| 5 | 423 | | entity.OnTransformChange = null; |
| 5 | 424 | | entity = null; |
| | 425 | | } |
| | 426 | |
|
| 410 | 427 | | avatarReporterController.ReportAvatarRemoved(); |
| 410 | 428 | | } |
| | 429 | |
|
| 1 | 430 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; } |
| | 431 | |
|
| | 432 | | [ContextMenu("Print current profile")] |
| 0 | 433 | | private void PrintCurrentProfile() { Debug.Log(JsonUtility.ToJson(model)); } |
| | 434 | |
|
| | 435 | | } |
| | 436 | | } |