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