| | 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 |
| | 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 | |
|
| | 26 | | public static event Action<IDCLEntity, AvatarShape> OnAvatarShapeUpdated; |
| | 27 | |
|
| | 28 | | public GameObject avatarContainer; |
| | 29 | | public Collider avatarCollider; |
| | 30 | | public AvatarMovementController avatarMovementController; |
| | 31 | | [SerializeField] private GameObject onloadParticlePrefab; |
| | 32 | |
|
| | 33 | | [SerializeField] internal AvatarOnPointerDown onPointerDown; |
| | 34 | | internal IPlayerName playerName; |
| | 35 | | internal IAvatarReporterController avatarReporterController; |
| | 36 | |
|
| | 37 | | private StringVariable currentPlayerInfoCardId; |
| | 38 | |
|
| | 39 | | public bool everythingIsLoaded; |
| | 40 | |
|
| | 41 | | bool initializedPosition = false; |
| | 42 | |
|
| | 43 | | private Player player = null; |
| 0 | 44 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 45 | |
|
| 687 | 46 | | private IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints(); |
| | 47 | | private IAvatar avatar; |
| 687 | 48 | | private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() }; |
| | 49 | | private CancellationTokenSource loadingCts; |
| | 50 | | private ILazyTextureObserver currentLazyObserver; |
| 687 | 51 | | private bool isGlobalSceneAvatar = true; |
| | 52 | |
|
| | 53 | | private void Awake() |
| | 54 | | { |
| 686 | 55 | | model = new AvatarModel(); |
| 686 | 56 | | currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| 686 | 57 | | Visibility visibility = new Visibility(); |
| 686 | 58 | | LOD avatarLOD = new LOD(avatarContainer, visibility, avatarMovementController); |
| 686 | 59 | | AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>(); |
| 686 | 60 | | avatar = new Avatar( |
| | 61 | | new AvatarCurator(new WearableItemResolver()), |
| | 62 | | new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()), |
| | 63 | | animator, |
| | 64 | | visibility, |
| | 65 | | avatarLOD, |
| | 66 | | new SimpleGPUSkinning(), |
| | 67 | | new GPUSkinningThrottler(), |
| | 68 | | new EmoteAnimationEquipper(animator, DataStore.i.emotes)); |
| | 69 | |
|
| 686 | 70 | | if (avatarReporterController == null) |
| | 71 | | { |
| 686 | 72 | | avatarReporterController = new AvatarReporterController(Environment.i.world.state); |
| | 73 | | } |
| 686 | 74 | | } |
| | 75 | |
|
| | 76 | | private void Start() |
| | 77 | | { |
| 1 | 78 | | playerName = GetComponentInChildren<IPlayerName>(); |
| 1 | 79 | | playerName?.Hide(true); |
| 1 | 80 | | } |
| | 81 | |
|
| | 82 | | private void PlayerClicked() |
| | 83 | | { |
| 0 | 84 | | if (model == null) |
| 0 | 85 | | return; |
| 0 | 86 | | currentPlayerInfoCardId.Set(((AvatarModel) model).id); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public void OnDestroy() |
| | 90 | | { |
| 686 | 91 | | Cleanup(); |
| | 92 | |
|
| 686 | 93 | | if (poolableObject != null && poolableObject.isInsidePool) |
| 2 | 94 | | poolableObject.RemoveFromPool(); |
| 686 | 95 | | } |
| | 96 | |
|
| | 97 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 98 | | { |
| 2 | 99 | | isGlobalSceneAvatar = scene.sceneData.id == EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID; |
| | 100 | |
|
| 2 | 101 | | DisablePassport(); |
| | 102 | |
|
| 2 | 103 | | var model = (AvatarModel) newModel; |
| | 104 | |
|
| 2 | 105 | | bool needsLoading = !model.HaveSameWearablesAndColors(currentAvatar); |
| 2 | 106 | | currentAvatar.CopyFrom(model); |
| | 107 | |
|
| 2 | 108 | | if (string.IsNullOrEmpty(model.bodyShape) || model.wearables.Count == 0) |
| 0 | 109 | | yield break; |
| | 110 | | #if UNITY_EDITOR |
| 2 | 111 | | gameObject.name = $"Avatar Shape {model.name}"; |
| | 112 | | #endif |
| 2 | 113 | | everythingIsLoaded = false; |
| | 114 | |
|
| | 115 | | bool avatarDone = false; |
| | 116 | | bool avatarFailed = false; |
| | 117 | |
|
| 2 | 118 | | yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved. |
| | 119 | |
|
| | 120 | | // To deal with the cases in which the entity transform was configured before the AvatarShape |
| 1 | 121 | | if (!initializedPosition && entity.components.ContainsKey(DCL.Models.CLASS_ID_COMPONENT.TRANSFORM)) |
| | 122 | | { |
| 1 | 123 | | initializedPosition = true; |
| 1 | 124 | | OnEntityTransformChanged(entity.gameObject.transform.localPosition, |
| | 125 | | entity.gameObject.transform.localRotation, true); |
| | 126 | | } |
| | 127 | |
|
| | 128 | | // NOTE: we subscribe here to transform changes since we might "lose" the message |
| | 129 | | // if we subscribe after a any yield |
| 1 | 130 | | entity.OnTransformChange -= OnEntityTransformChanged; |
| 1 | 131 | | entity.OnTransformChange += OnEntityTransformChanged; |
| | 132 | |
|
| 1 | 133 | | var wearableItems = model.wearables.ToList(); |
| 1 | 134 | | wearableItems.Add(model.bodyShape); |
| | 135 | |
|
| | 136 | | //temporarily hardcoding the embedded emotes until the user profile provides the equipped ones |
| 1 | 137 | | var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes"); |
| 23 | 138 | | wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id)); |
| | 139 | |
|
| 1 | 140 | | if (avatar.status != IAvatar.Status.Loaded || needsLoading) |
| | 141 | | { |
| | 142 | | //TODO Add Collider to the AvatarSystem |
| | 143 | | //TODO Without this the collider could get triggered disabling the avatar container, |
| | 144 | | // this would stop the loading process due to the underlying coroutines of the AssetLoader not starting |
| 1 | 145 | | avatarCollider.gameObject.SetActive(false); |
| | 146 | |
|
| 1 | 147 | | SetImpostor(model.id); |
| 1 | 148 | | loadingCts?.Cancel(); |
| 1 | 149 | | loadingCts?.Dispose(); |
| 1 | 150 | | loadingCts = new CancellationTokenSource(); |
| | 151 | |
|
| 1 | 152 | | avatar.Load(wearableItems, new AvatarSettings |
| | 153 | | { |
| | 154 | | playerName = model.name, |
| | 155 | | bodyshapeId = model.bodyShape, |
| | 156 | | eyesColor = model.eyeColor, |
| | 157 | | skinColor = model.skinColor, |
| | 158 | | hairColor = model.hairColor, |
| | 159 | | }, loadingCts.Token); |
| | 160 | |
|
| | 161 | | // Yielding a UniTask doesn't do anything, we manually wait until the avatar is ready |
| 3 | 162 | | yield return new WaitUntil(() => avatar.status == IAvatar.Status.Loaded); |
| | 163 | |
|
| 0 | 164 | | if (avatar.lodLevel <= 1) |
| 0 | 165 | | AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, onloadParticlePrefab); |
| | 166 | | } |
| | 167 | |
|
| 0 | 168 | | avatar.PlayEmote(model.expressionTriggerId, model.expressionTriggerTimestamp); |
| | 169 | |
|
| 0 | 170 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 0 | 171 | | onPointerDown.OnPointerDownReport += PlayerClicked; |
| 0 | 172 | | onPointerDown.OnPointerEnterReport -= PlayerPointerEnter; |
| 0 | 173 | | onPointerDown.OnPointerEnterReport += PlayerPointerEnter; |
| 0 | 174 | | onPointerDown.OnPointerExitReport -= PlayerPointerExit; |
| 0 | 175 | | onPointerDown.OnPointerExitReport += PlayerPointerExit; |
| | 176 | |
|
| 0 | 177 | | UpdatePlayerStatus(model); |
| | 178 | |
|
| 0 | 179 | | onPointerDown.Initialize( |
| | 180 | | new OnPointerDown.Model() |
| | 181 | | { |
| | 182 | | type = OnPointerDown.NAME, |
| | 183 | | button = WebInterface.ACTION_BUTTON.POINTER.ToString(), |
| | 184 | | hoverText = "view profile" |
| | 185 | | }, |
| | 186 | | entity, player |
| | 187 | | ); |
| | 188 | |
|
| 0 | 189 | | avatarCollider.gameObject.SetActive(true); |
| | 190 | |
|
| 0 | 191 | | everythingIsLoaded = true; |
| 0 | 192 | | OnAvatarShapeUpdated?.Invoke(entity, this); |
| | 193 | |
|
| 0 | 194 | | EnablePassport(); |
| | 195 | |
|
| 0 | 196 | | onPointerDown.SetColliderEnabled(isGlobalSceneAvatar); |
| 0 | 197 | | onPointerDown.SetOnClickReportEnabled(isGlobalSceneAvatar); |
| 0 | 198 | | } |
| | 199 | |
|
| | 200 | | public void SetImpostor(string userId) |
| | 201 | | { |
| 1 | 202 | | currentLazyObserver?.RemoveListener(avatar.SetImpostorTexture); |
| 1 | 203 | | if (string.IsNullOrEmpty(userId)) |
| 1 | 204 | | return; |
| | 205 | |
|
| 0 | 206 | | UserProfile userProfile = UserProfileController.GetProfileByUserId(userId); |
| 0 | 207 | | if (userProfile == null) |
| 0 | 208 | | return; |
| | 209 | |
|
| 0 | 210 | | currentLazyObserver = userProfile.bodySnapshotObserver; |
| 0 | 211 | | currentLazyObserver.AddListener(avatar.SetImpostorTexture); |
| 0 | 212 | | } |
| | 213 | |
|
| 0 | 214 | | private void PlayerPointerExit() { playerName?.SetForceShow(false); } |
| 0 | 215 | | private void PlayerPointerEnter() { playerName?.SetForceShow(true); } |
| | 216 | |
|
| | 217 | | private void UpdatePlayerStatus(AvatarModel model) |
| | 218 | | { |
| | 219 | | // Remove the player status if the userId changes |
| 0 | 220 | | if (player != null && (player.id != model.id || player.name != model.name)) |
| 0 | 221 | | otherPlayers.Remove(player.id); |
| | 222 | |
|
| 0 | 223 | | if (isGlobalSceneAvatar && string.IsNullOrEmpty(model?.id)) |
| 0 | 224 | | return; |
| | 225 | |
|
| 0 | 226 | | bool isNew = player == null; |
| 0 | 227 | | if (isNew) |
| | 228 | | { |
| 0 | 229 | | player = new Player(); |
| | 230 | | } |
| | 231 | |
|
| 0 | 232 | | player.id = model.id; |
| 0 | 233 | | player.name = model.name; |
| 0 | 234 | | player.isTalking = model.talking; |
| 0 | 235 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 236 | | player.avatar = avatar; |
| 0 | 237 | | player.onPointerDownCollider = onPointerDown; |
| 0 | 238 | | player.collider = avatarCollider; |
| | 239 | |
|
| 0 | 240 | | if (isNew) |
| | 241 | | { |
| 0 | 242 | | player.playerName = playerName; |
| 0 | 243 | | player.playerName.SetName(player.name); |
| 0 | 244 | | player.playerName.Show(); |
| 0 | 245 | | player.anchorPoints = anchorPoints; |
| 0 | 246 | | if (isGlobalSceneAvatar) |
| | 247 | | { |
| 0 | 248 | | otherPlayers.Add(player.id, player); |
| | 249 | | } |
| 0 | 250 | | avatarReporterController.ReportAvatarRemoved(); |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | avatarReporterController.SetUp(entity.scene.sceneData.id, entity.entityId, player.id); |
| | 254 | |
|
| 0 | 255 | | float height = AvatarSystemUtils.AVATAR_Y_OFFSET + avatar.extents.y; |
| | 256 | |
|
| 0 | 257 | | anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), height); |
| | 258 | |
|
| 0 | 259 | | player.playerName.SetIsTalking(model.talking); |
| 0 | 260 | | player.playerName.SetYOffset(Mathf.Max(MINIMUM_PLAYERNAME_HEIGHT, height)); |
| 0 | 261 | | } |
| | 262 | |
|
| | 263 | | private void Update() |
| | 264 | | { |
| 2 | 265 | | if (player != null) |
| | 266 | | { |
| 0 | 267 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 268 | | player.forwardDirection = entity.gameObject.transform.forward; |
| 0 | 269 | | avatarReporterController.ReportAvatarPosition(player.worldPosition); |
| | 270 | | } |
| 2 | 271 | | } |
| | 272 | |
|
| | 273 | | public void DisablePassport() |
| | 274 | | { |
| 2 | 275 | | if (onPointerDown.collider == null) |
| 0 | 276 | | return; |
| | 277 | |
|
| 2 | 278 | | onPointerDown.SetPassportEnabled(false); |
| 2 | 279 | | } |
| | 280 | |
|
| | 281 | | public void EnablePassport() |
| | 282 | | { |
| 0 | 283 | | if (onPointerDown.collider == null) |
| 0 | 284 | | return; |
| | 285 | |
|
| 0 | 286 | | onPointerDown.SetPassportEnabled(true); |
| 0 | 287 | | } |
| | 288 | |
|
| | 289 | | private void OnEntityTransformChanged(object newModel) |
| | 290 | | { |
| 0 | 291 | | DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel; |
| 0 | 292 | | OnEntityTransformChanged(newTransformModel.position, newTransformModel.rotation, !initializedPosition); |
| 0 | 293 | | } |
| | 294 | |
|
| | 295 | | private void OnEntityTransformChanged(in Vector3 position, in Quaternion rotation, bool inmediate) |
| | 296 | | { |
| 1 | 297 | | if (isGlobalSceneAvatar) |
| | 298 | | { |
| 0 | 299 | | avatarMovementController.OnTransformChanged(position, rotation, inmediate); |
| 0 | 300 | | } |
| | 301 | | else |
| | 302 | | { |
| 1 | 303 | | var scenePosition = Utils.GridToWorldPosition(entity.scene.sceneData.basePosition.x, entity.scene.sceneD |
| 1 | 304 | | avatarMovementController.OnTransformChanged(scenePosition + position, rotation, inmediate); |
| | 305 | | } |
| 1 | 306 | | initializedPosition = true; |
| 1 | 307 | | } |
| | 308 | |
|
| | 309 | | public override void OnPoolGet() |
| | 310 | | { |
| 2 | 311 | | base.OnPoolGet(); |
| | 312 | |
|
| 2 | 313 | | everythingIsLoaded = false; |
| 2 | 314 | | initializedPosition = false; |
| 2 | 315 | | model = new AvatarModel(); |
| 2 | 316 | | player = null; |
| 2 | 317 | | } |
| | 318 | |
|
| | 319 | | public override void Cleanup() |
| | 320 | | { |
| 689 | 321 | | base.Cleanup(); |
| | 322 | |
|
| 689 | 323 | | playerName?.Hide(true); |
| 689 | 324 | | if (player != null) |
| | 325 | | { |
| 0 | 326 | | otherPlayers.Remove(player.id); |
| 0 | 327 | | player = null; |
| | 328 | | } |
| | 329 | |
|
| 689 | 330 | | loadingCts?.Cancel(); |
| 689 | 331 | | loadingCts?.Dispose(); |
| 689 | 332 | | loadingCts = null; |
| 689 | 333 | | currentLazyObserver?.RemoveListener(avatar.SetImpostorTexture); |
| 689 | 334 | | avatar.Dispose(); |
| | 335 | |
|
| 689 | 336 | | if (poolableObject != null) |
| | 337 | | { |
| 5 | 338 | | poolableObject.OnRelease -= Cleanup; |
| | 339 | | } |
| | 340 | |
|
| 689 | 341 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 689 | 342 | | onPointerDown.OnPointerEnterReport -= PlayerPointerEnter; |
| 689 | 343 | | onPointerDown.OnPointerExitReport -= PlayerPointerExit; |
| | 344 | |
|
| 689 | 345 | | if (entity != null) |
| | 346 | | { |
| 2 | 347 | | entity.OnTransformChange = null; |
| 2 | 348 | | entity = null; |
| | 349 | | } |
| | 350 | |
|
| 689 | 351 | | avatarReporterController.ReportAvatarRemoved(); |
| 689 | 352 | | } |
| | 353 | |
|
| 0 | 354 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; } |
| | 355 | |
|
| | 356 | | [ContextMenu("Print current profile")] |
| 0 | 357 | | private void PrintCurrentProfile() { Debug.Log(JsonUtility.ToJson(model)); } |
| | 358 | | } |
| | 359 | | } |