| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using GPUSkinning; |
| | 7 | | using UnityEngine; |
| | 8 | | using static WearableLiterals; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public class AvatarRenderer : MonoBehaviour, IAvatarRenderer |
| | 13 | | { |
| 1 | 14 | | private static readonly int BASE_COLOR_PROPERTY = Shader.PropertyToID("_BaseColor"); |
| | 15 | |
|
| | 16 | | private const int MAX_RETRIES = 5; |
| | 17 | |
|
| | 18 | | public Material defaultMaterial; |
| | 19 | | public Material eyeMaterial; |
| | 20 | | public Material eyebrowMaterial; |
| | 21 | | public Material mouthMaterial; |
| | 22 | |
|
| | 23 | | public MeshRenderer impostorRenderer; |
| | 24 | | public MeshFilter impostorMeshFilter; |
| | 25 | |
|
| | 26 | | private AvatarModel model; |
| | 27 | | private AvatarMeshCombinerHelper avatarMeshCombiner; |
| | 28 | | private SimpleGPUSkinning gpuSkinning = null; |
| | 29 | | private GPUSkinningThrottler gpuSkinningThrottler = null; |
| 1072 | 30 | | private int gpuSkinningFramesBetweenUpdates = 1; |
| | 31 | | private bool initializedImpostor = false; |
| | 32 | |
|
| | 33 | | private Renderer mainMeshRenderer |
| | 34 | | { |
| | 35 | | get |
| | 36 | | { |
| 0 | 37 | | if (gpuSkinning != null) |
| 0 | 38 | | return gpuSkinning.renderer; |
| 0 | 39 | | return avatarMeshCombiner.renderer; |
| | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public event Action<IAvatarRenderer.VisualCue> OnVisualCue; |
| | 44 | | public event Action OnSuccessEvent; |
| | 45 | | public event Action<float> OnImpostorAlphaValueUpdate; |
| | 46 | | public event Action<bool> OnFailEvent; |
| | 47 | |
|
| | 48 | | internal BodyShapeController bodyShapeController; |
| 1072 | 49 | | internal Dictionary<WearableItem, WearableController> wearableControllers = new Dictionary<WearableItem, Wearabl |
| | 50 | | internal FacialFeatureController eyesController; |
| | 51 | | internal FacialFeatureController eyebrowsController; |
| | 52 | | internal FacialFeatureController mouthController; |
| | 53 | | internal AvatarAnimatorLegacy animator; |
| | 54 | | internal StickersController stickersController; |
| | 55 | |
|
| 1072 | 56 | | private long lastStickerTimestamp = -1; |
| | 57 | |
|
| | 58 | | public bool isLoading; |
| 0 | 59 | | public bool isReady => bodyShapeController != null && bodyShapeController.isReady && wearableControllers != null |
| | 60 | |
|
| | 61 | | private Coroutine loadCoroutine; |
| | 62 | | private AssetPromise_Texture bodySnapshotTexturePromise; |
| 1072 | 63 | | private List<string> wearablesInUse = new List<string>(); |
| | 64 | | private bool isDestroyed = false; |
| | 65 | |
|
| | 66 | | private void Awake() |
| | 67 | | { |
| 844 | 68 | | animator = GetComponent<AvatarAnimatorLegacy>(); |
| 844 | 69 | | stickersController = GetComponent<StickersController>(); |
| 844 | 70 | | avatarMeshCombiner = new AvatarMeshCombinerHelper(); |
| 844 | 71 | | avatarMeshCombiner.prepareMeshForGpuSkinning = true; |
| 844 | 72 | | avatarMeshCombiner.uploadMeshToGpu = true; |
| | 73 | |
|
| 844 | 74 | | if (impostorRenderer != null) |
| 654 | 75 | | SetImpostorVisibility(false); |
| 844 | 76 | | } |
| | 77 | |
|
| | 78 | | public void ApplyModel(AvatarModel model, Action onSuccess, Action onFail) |
| | 79 | | { |
| 137 | 80 | | if ( this.model != null ) |
| | 81 | | { |
| 87 | 82 | | if (model != null && this.model.Equals(model)) |
| | 83 | | { |
| 56 | 84 | | onSuccess?.Invoke(); |
| 43 | 85 | | return; |
| | 86 | | } |
| | 87 | |
|
| 31 | 88 | | bool wearablesChanged = !this.model.HaveSameWearablesAndColors(model); |
| 31 | 89 | | bool expressionsChanged = !this.model.HaveSameExpressions(model); |
| | 90 | |
|
| 31 | 91 | | if (!wearablesChanged && expressionsChanged) |
| | 92 | | { |
| 0 | 93 | | this.model.expressionTriggerId = model.expressionTriggerId; |
| 0 | 94 | | this.model.expressionTriggerTimestamp = model.expressionTriggerTimestamp; |
| 0 | 95 | | this.model.stickerTriggerId = model.stickerTriggerId; |
| 0 | 96 | | this.model.stickerTriggerTimestamp = model.stickerTriggerTimestamp; |
| 0 | 97 | | UpdateExpression(); |
| 0 | 98 | | onSuccess?.Invoke(); |
| 0 | 99 | | return; |
| | 100 | | } |
| | 101 | | } |
| | 102 | |
|
| 81 | 103 | | this.model = new AvatarModel(); |
| 81 | 104 | | this.model.CopyFrom(model); |
| | 105 | |
|
| 81 | 106 | | ResetImpostor(); |
| | 107 | |
|
| | 108 | | // TODO(Brian): Find a better approach than overloading callbacks like this. This code is not readable. |
| | 109 | | void onSuccessWrapper() |
| | 110 | | { |
| 0 | 111 | | onSuccess?.Invoke(); |
| 0 | 112 | | this.OnSuccessEvent -= onSuccessWrapper; |
| 0 | 113 | | } |
| | 114 | |
|
| 81 | 115 | | this.OnSuccessEvent += onSuccessWrapper; |
| | 116 | |
|
| | 117 | | void onFailWrapper(bool isFatalError) |
| | 118 | | { |
| 6 | 119 | | onFail?.Invoke(); |
| 6 | 120 | | this.OnFailEvent -= onFailWrapper; |
| 6 | 121 | | } |
| | 122 | |
|
| 81 | 123 | | this.OnFailEvent += onFailWrapper; |
| | 124 | |
|
| 81 | 125 | | isLoading = false; |
| | 126 | |
|
| 81 | 127 | | if (model == null) |
| | 128 | | { |
| 0 | 129 | | CleanupAvatar(); |
| 0 | 130 | | this.OnSuccessEvent?.Invoke(); |
| 0 | 131 | | return; |
| | 132 | | } |
| | 133 | |
|
| 81 | 134 | | StopLoadingCoroutines(); |
| | 135 | |
|
| 81 | 136 | | isLoading = true; |
| 81 | 137 | | loadCoroutine = CoroutineStarter.Start(LoadAvatar()); |
| 81 | 138 | | } |
| | 139 | |
|
| | 140 | | public void InitializeImpostor() |
| | 141 | | { |
| 0 | 142 | | initializedImpostor = true; |
| | 143 | |
|
| | 144 | | // The fetched snapshot can take its time so it's better to assign a generic impostor first. |
| 0 | 145 | | AvatarRendererHelpers.RandomizeAndApplyGenericImpostor(impostorMeshFilter.mesh, impostorRenderer.material); |
| | 146 | |
|
| 0 | 147 | | UserProfile userProfile = null; |
| 0 | 148 | | if (!string.IsNullOrEmpty(model?.id)) |
| 0 | 149 | | userProfile = UserProfileController.GetProfileByUserId(model.id); |
| | 150 | |
|
| 0 | 151 | | if (userProfile != null) |
| | 152 | | { |
| 0 | 153 | | bodySnapshotTexturePromise = new AssetPromise_Texture(userProfile.bodySnapshotURL); |
| 0 | 154 | | bodySnapshotTexturePromise.OnSuccessEvent += asset => AvatarRendererHelpers.SetImpostorTexture(asset.tex |
| 0 | 155 | | AssetPromiseKeeper_Texture.i.Keep(bodySnapshotTexturePromise); |
| | 156 | | } |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | void StopLoadingCoroutines() |
| | 160 | | { |
| 1582 | 161 | | if (loadCoroutine != null) |
| 80 | 162 | | CoroutineStarter.Stop(loadCoroutine); |
| | 163 | |
|
| 1582 | 164 | | loadCoroutine = null; |
| 1582 | 165 | | } |
| | 166 | |
|
| | 167 | | public void CleanupAvatar() |
| | 168 | | { |
| 1501 | 169 | | StopLoadingCoroutines(); |
| 1501 | 170 | | if (!isDestroyed) |
| | 171 | | { |
| 658 | 172 | | SetGOVisibility(true); |
| 658 | 173 | | if (impostorRenderer != null) |
| 658 | 174 | | SetImpostorVisibility(false); |
| | 175 | | } |
| | 176 | |
|
| 1501 | 177 | | avatarMeshCombiner.Dispose(); |
| 1501 | 178 | | gpuSkinningThrottler = null; |
| 1501 | 179 | | gpuSkinning = null; |
| 1501 | 180 | | eyebrowsController?.CleanUp(); |
| 1501 | 181 | | eyebrowsController = null; |
| | 182 | |
|
| 1501 | 183 | | eyesController?.CleanUp(); |
| 1501 | 184 | | eyesController = null; |
| | 185 | |
|
| 1501 | 186 | | mouthController?.CleanUp(); |
| 1501 | 187 | | mouthController = null; |
| | 188 | |
|
| 1501 | 189 | | bodyShapeController?.CleanUp(); |
| 1501 | 190 | | bodyShapeController = null; |
| | 191 | |
|
| 1501 | 192 | | using (var iterator = wearableControllers.GetEnumerator()) |
| | 193 | | { |
| 1501 | 194 | | while (iterator.MoveNext()) |
| | 195 | | { |
| 0 | 196 | | iterator.Current.Value.CleanUp(); |
| | 197 | | } |
| 1501 | 198 | | } |
| | 199 | |
|
| 1501 | 200 | | wearableControllers.Clear(); |
| 1501 | 201 | | model = null; |
| 1501 | 202 | | isLoading = false; |
| 1501 | 203 | | OnFailEvent = null; |
| 1501 | 204 | | OnSuccessEvent = null; |
| | 205 | |
|
| 1501 | 206 | | CleanMergedAvatar(); |
| | 207 | |
|
| 1501 | 208 | | ResetImpostor(); |
| | 209 | |
|
| 1501 | 210 | | CatalogController.RemoveWearablesInUse(wearablesInUse); |
| 1501 | 211 | | wearablesInUse.Clear(); |
| 1501 | 212 | | OnVisualCue?.Invoke(IAvatarRenderer.VisualCue.CleanedUp); |
| 1457 | 213 | | } |
| | 214 | |
|
| | 215 | | void CleanUpUnusedItems() |
| | 216 | | { |
| 0 | 217 | | if (model.wearables == null) |
| 0 | 218 | | return; |
| | 219 | |
|
| 0 | 220 | | var ids = wearableControllers.Keys.ToArray(); |
| | 221 | |
|
| 0 | 222 | | for (var i = 0; i < ids.Length; i++) |
| | 223 | | { |
| 0 | 224 | | var currentId = ids[i]; |
| 0 | 225 | | var wearable = wearableControllers[currentId]; |
| | 226 | |
|
| 0 | 227 | | if (!model.wearables.Contains(wearable.id) || !wearable.IsLoadedForBodyShape(model.bodyShape)) |
| | 228 | | { |
| 0 | 229 | | wearable.CleanUp(); |
| 0 | 230 | | wearableControllers.Remove(currentId); |
| | 231 | | } |
| | 232 | | } |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | // TODO(Brian): Pure functions should be extracted from this big LoadAvatar() method and unit-test separately. |
| | 236 | | // The current approach has tech debt that's getting very expensive and is costing many hours of de |
| | 237 | | // Avatar Loading should be a self contained operation that doesn't depend on pool management and A |
| | 238 | | // lifecycle like it does now. |
| | 239 | | private IEnumerator LoadAvatar() |
| | 240 | | { |
| | 241 | | // TODO(Brian): This is an ugly hack, all the loading should be performed |
| | 242 | | // without being afraid of the gameObject active state. |
| 162 | 243 | | yield return new WaitUntil(() => gameObject.activeSelf); |
| | 244 | |
|
| 6 | 245 | | bool loadSoftFailed = false; |
| | 246 | |
|
| 6 | 247 | | WearableItem resolvedBody = null; |
| | 248 | |
|
| | 249 | | // TODO(Brian): Evaluate using UniTask<T> here instead of Helpers.Promise. |
| 6 | 250 | | Helpers.Promise<WearableItem> avatarBodyPromise = null; |
| 6 | 251 | | if (!string.IsNullOrEmpty(model.bodyShape)) |
| | 252 | | { |
| 0 | 253 | | avatarBodyPromise = CatalogController.RequestWearable(model.bodyShape); |
| 0 | 254 | | } |
| | 255 | | else |
| | 256 | | { |
| 6 | 257 | | OnFailEvent?.Invoke(true); |
| 6 | 258 | | yield break; |
| | 259 | | } |
| | 260 | |
|
| 0 | 261 | | List<WearableItem> resolvedWearables = new List<WearableItem>(); |
| | 262 | |
|
| | 263 | | // TODO(Brian): Evaluate using UniTask<T> here instead of Helpers.Promise. |
| 0 | 264 | | List<Helpers.Promise<WearableItem>> avatarWearablePromises = new List<Helpers.Promise<WearableItem>>(); |
| 0 | 265 | | if (model.wearables != null) |
| | 266 | | { |
| 0 | 267 | | for (int i = 0; i < model.wearables.Count; i++) |
| | 268 | | { |
| 0 | 269 | | avatarWearablePromises.Add(CatalogController.RequestWearable(model.wearables[i])); |
| | 270 | | } |
| | 271 | | } |
| | 272 | |
|
| | 273 | | // In this point, all the requests related to the avatar's wearables have been collected and sent to the Cat |
| | 274 | | // From here we wait for the response of the requested wearables and process them. |
| 0 | 275 | | if (avatarBodyPromise != null) |
| | 276 | | { |
| 0 | 277 | | yield return avatarBodyPromise; |
| | 278 | |
|
| 0 | 279 | | if (!string.IsNullOrEmpty(avatarBodyPromise.error)) |
| | 280 | | { |
| 0 | 281 | | Debug.LogError(avatarBodyPromise.error); |
| 0 | 282 | | loadSoftFailed = true; |
| 0 | 283 | | } |
| | 284 | | else |
| | 285 | | { |
| 0 | 286 | | resolvedBody = avatarBodyPromise.value; |
| 0 | 287 | | wearablesInUse.Add(avatarBodyPromise.value.id); |
| | 288 | | } |
| | 289 | | } |
| | 290 | |
|
| 0 | 291 | | if (resolvedBody == null) |
| | 292 | | { |
| 0 | 293 | | isLoading = false; |
| 0 | 294 | | OnFailEvent?.Invoke(true); |
| 0 | 295 | | yield break; |
| | 296 | | } |
| | 297 | |
|
| | 298 | | // TODO(Brian): Evaluate using UniTask<T> here instead of Helpers.Promise. |
| 0 | 299 | | List<Helpers.Promise<WearableItem>> replacementPromises = new List<Helpers.Promise<WearableItem>>(); |
| | 300 | |
|
| 0 | 301 | | foreach (var avatarWearablePromise in avatarWearablePromises) |
| | 302 | | { |
| 0 | 303 | | yield return avatarWearablePromise; |
| | 304 | |
|
| 0 | 305 | | if (!string.IsNullOrEmpty(avatarWearablePromise.error)) |
| | 306 | | { |
| 0 | 307 | | Debug.LogError(avatarWearablePromise.error); |
| 0 | 308 | | loadSoftFailed = true; |
| 0 | 309 | | } |
| | 310 | | else |
| | 311 | | { |
| 0 | 312 | | WearableItem wearableItem = avatarWearablePromise.value; |
| 0 | 313 | | wearablesInUse.Add(wearableItem.id); |
| | 314 | |
|
| 0 | 315 | | if (wearableItem.GetRepresentation(model.bodyShape) != null) |
| | 316 | | { |
| 0 | 317 | | resolvedWearables.Add(wearableItem); |
| 0 | 318 | | } |
| | 319 | | else |
| | 320 | | { |
| 0 | 321 | | model.wearables.Remove(wearableItem.id); |
| 0 | 322 | | string defaultReplacement = DefaultWearables.GetDefaultWearable(model.bodyShape, wearableItem.da |
| 0 | 323 | | if (!string.IsNullOrEmpty(defaultReplacement)) |
| | 324 | | { |
| 0 | 325 | | model.wearables.Add(defaultReplacement); |
| 0 | 326 | | replacementPromises.Add(CatalogController.RequestWearable(defaultReplacement)); |
| | 327 | | } |
| | 328 | | } |
| | 329 | | } |
| 0 | 330 | | } |
| | 331 | |
|
| 0 | 332 | | foreach (var wearablePromise in replacementPromises) |
| | 333 | | { |
| 0 | 334 | | yield return wearablePromise; |
| | 335 | |
|
| 0 | 336 | | if (!string.IsNullOrEmpty(wearablePromise.error)) |
| | 337 | | { |
| 0 | 338 | | Debug.LogError(wearablePromise.error); |
| 0 | 339 | | loadSoftFailed = true; |
| 0 | 340 | | } |
| | 341 | | else |
| | 342 | | { |
| 0 | 343 | | WearableItem wearableItem = wearablePromise.value; |
| 0 | 344 | | wearablesInUse.Add(wearableItem.id); |
| 0 | 345 | | resolvedWearables.Add(wearableItem); |
| | 346 | | } |
| 0 | 347 | | } |
| | 348 | |
|
| 0 | 349 | | bool bodyIsDirty = false; |
| 0 | 350 | | if (bodyShapeController != null && bodyShapeController.id != model?.bodyShape) |
| | 351 | | { |
| 0 | 352 | | bodyShapeController.CleanUp(); |
| 0 | 353 | | bodyShapeController = null; |
| 0 | 354 | | bodyIsDirty = true; |
| | 355 | | } |
| | 356 | |
|
| 0 | 357 | | if (bodyShapeController == null) |
| | 358 | | { |
| 0 | 359 | | HideAll(); |
| | 360 | |
|
| 0 | 361 | | bodyShapeController = new BodyShapeController(resolvedBody); |
| 0 | 362 | | eyesController = FacialFeatureController.CreateDefaultFacialFeature(bodyShapeController.bodyShapeId, Cat |
| 0 | 363 | | eyebrowsController = FacialFeatureController.CreateDefaultFacialFeature(bodyShapeController.bodyShapeId, |
| 0 | 364 | | mouthController = FacialFeatureController.CreateDefaultFacialFeature(bodyShapeController.bodyShapeId, Ca |
| | 365 | | } |
| | 366 | |
|
| | 367 | | //TODO(Brian): This logic should be performed in a testeable pure function instead of this inline approach. |
| | 368 | | // Moreover, this function should work against data, not wearableController instances. |
| 0 | 369 | | bool wearablesIsDirty = false; |
| 0 | 370 | | HashSet<string> unusedCategories = new HashSet<string>(Categories.ALL); |
| 0 | 371 | | int wearableCount = resolvedWearables.Count; |
| 0 | 372 | | for (int index = 0; index < wearableCount; index++) |
| | 373 | | { |
| 0 | 374 | | WearableItem wearable = resolvedWearables[index]; |
| 0 | 375 | | if (wearable == null) |
| | 376 | | continue; |
| | 377 | |
|
| 0 | 378 | | unusedCategories.Remove(wearable.data.category); |
| 0 | 379 | | if (wearableControllers.ContainsKey(wearable)) |
| | 380 | | { |
| 0 | 381 | | if (!wearableControllers[wearable].IsLoadedForBodyShape(bodyShapeController.bodyShapeId)) |
| 0 | 382 | | wearableControllers[wearable].CleanUp(); |
| 0 | 383 | | } |
| | 384 | | else |
| | 385 | | { |
| 0 | 386 | | AddWearableController(wearable); |
| 0 | 387 | | if (wearable.data.category != Categories.EYES && wearable.data.category != Categories.MOUTH && weara |
| 0 | 388 | | wearablesIsDirty = true; |
| | 389 | | } |
| | 390 | | } |
| | 391 | |
|
| 0 | 392 | | if ( eyesController == null && !unusedCategories.Contains(Categories.EYES)) |
| 0 | 393 | | unusedCategories.Add(Categories.EYES); |
| | 394 | |
|
| 0 | 395 | | if ( mouthController == null && !unusedCategories.Contains(Categories.MOUTH)) |
| 0 | 396 | | unusedCategories.Add(Categories.MOUTH); |
| | 397 | |
|
| 0 | 398 | | if ( eyebrowsController == null && !unusedCategories.Contains(Categories.EYEBROWS)) |
| 0 | 399 | | unusedCategories.Add(Categories.EYEBROWS); |
| | 400 | |
|
| 0 | 401 | | foreach (var category in unusedCategories) |
| | 402 | | { |
| | 403 | | switch (category) |
| | 404 | | { |
| | 405 | | case Categories.EYES: |
| 0 | 406 | | eyesController = FacialFeatureController.CreateDefaultFacialFeature(bodyShapeController.bodyShap |
| 0 | 407 | | break; |
| | 408 | | case Categories.MOUTH: |
| 0 | 409 | | mouthController = FacialFeatureController.CreateDefaultFacialFeature(bodyShapeController.bodySha |
| 0 | 410 | | break; |
| | 411 | | case Categories.EYEBROWS: |
| 0 | 412 | | eyebrowsController = FacialFeatureController.CreateDefaultFacialFeature(bodyShapeController.body |
| | 413 | | break; |
| | 414 | | } |
| | 415 | | } |
| | 416 | |
|
| 0 | 417 | | HashSet<string> hiddenList = WearableItem.CompoundHidesList(bodyShapeController.bodyShapeId, resolvedWearabl |
| 0 | 418 | | if (!bodyShapeController.isReady) |
| | 419 | | { |
| 0 | 420 | | bodyShapeController.Load(bodyShapeController.bodyShapeId, transform, OnWearableLoadingSuccess, OnBodySha |
| | 421 | | } |
| | 422 | |
|
| 0 | 423 | | foreach (WearableController wearable in wearableControllers.Values) |
| | 424 | | { |
| 0 | 425 | | if (bodyIsDirty) |
| 0 | 426 | | wearable.boneRetargetingDirty = true; |
| | 427 | |
|
| 0 | 428 | | wearable.Load(bodyShapeController.bodyShapeId, transform, OnWearableLoadingSuccess, x => OnWearableLoadi |
| 0 | 429 | | yield return null; |
| | 430 | | } |
| | 431 | |
|
| | 432 | | // TODO(Brian): Evaluate using UniTask<T> instead of this way. |
| 0 | 433 | | yield return new WaitUntil(() => bodyShapeController.isReady && wearableControllers.Values.All(x => x.isRead |
| | 434 | |
|
| 0 | 435 | | eyesController.Load(bodyShapeController, model.eyeColor); |
| 0 | 436 | | eyebrowsController.Load(bodyShapeController, model.hairColor); |
| 0 | 437 | | mouthController.Load(bodyShapeController, model.skinColor); |
| | 438 | |
|
| 0 | 439 | | yield return eyesController; |
| 0 | 440 | | yield return eyebrowsController; |
| 0 | 441 | | yield return mouthController; |
| | 442 | |
|
| 0 | 443 | | if (bodyIsDirty || wearablesIsDirty) |
| | 444 | | { |
| 0 | 445 | | OnVisualCue?.Invoke(IAvatarRenderer.VisualCue.Loaded); |
| | 446 | | } |
| | 447 | |
|
| | 448 | | // TODO(Brian): unusedCategories and hiddenList management is a double negative PITA. |
| | 449 | | // The load process should define how the avatar should look like before |
| | 450 | | // loading it and put this information in a positive list |
| | 451 | | // (i.e. not negative, because leads to double negative checks). |
| 0 | 452 | | bodyShapeController.SetActiveParts(unusedCategories.Contains(Categories.LOWER_BODY), unusedCategories.Contai |
| 0 | 453 | | bodyShapeController.UpdateVisibility(hiddenList); |
| 0 | 454 | | foreach (WearableController wearableController in wearableControllers.Values) |
| | 455 | | { |
| 0 | 456 | | wearableController.UpdateVisibility(hiddenList); |
| | 457 | | } |
| | 458 | |
|
| 0 | 459 | | CleanUpUnusedItems(); |
| | 460 | |
|
| 0 | 461 | | isLoading = false; |
| | 462 | |
|
| 0 | 463 | | SetupHairAndSkinColors(); |
| 0 | 464 | | SetWearableBones(); |
| | 465 | |
|
| | 466 | | // TODO(Brian): Expression and sticker update shouldn't be part of avatar loading code!!!! Refactor me pleas |
| 0 | 467 | | UpdateExpression(); |
| | 468 | |
|
| 0 | 469 | | var allRenderers = wearableControllers.SelectMany( x => x.Value.GetRenderers() ).ToList(); |
| 0 | 470 | | allRenderers.AddRange( bodyShapeController.GetRenderers() ); |
| 0 | 471 | | bool mergeSuccess = MergeAvatar(allRenderers); |
| | 472 | |
|
| 0 | 473 | | if (mergeSuccess) |
| 0 | 474 | | PrepareGpuSkinning(); |
| | 475 | | else |
| 0 | 476 | | loadSoftFailed = true; |
| | 477 | |
|
| | 478 | | // TODO(Brian): The loadSoftFailed flow is too convoluted--you never know which objects are nulled or empty |
| | 479 | | // before reaching this branching statement. The failure should be caught with a throw or other |
| | 480 | | // proper language feature. |
| 0 | 481 | | if (loadSoftFailed) |
| | 482 | | { |
| 0 | 483 | | OnFailEvent?.Invoke(false); |
| 0 | 484 | | } |
| | 485 | | else |
| | 486 | | { |
| 0 | 487 | | OnSuccessEvent?.Invoke(); |
| | 488 | | } |
| 0 | 489 | | } |
| | 490 | |
|
| | 491 | | private void PrepareGpuSkinning() |
| | 492 | | { |
| | 493 | | // Sample the animation manually and force an update in the GPUSkinning to avoid giant avatars |
| 0 | 494 | | animator.SetIdleFrame(); |
| 0 | 495 | | animator.animation.Sample(); |
| | 496 | |
|
| 0 | 497 | | gpuSkinning = new SimpleGPUSkinning( |
| | 498 | | avatarMeshCombiner.renderer, |
| | 499 | | false); // Bind poses are encoded by the AvatarMeshCombiner before making the mesh unreadable. |
| | 500 | |
|
| 0 | 501 | | gpuSkinningThrottler = new GPUSkinningThrottler(gpuSkinning); |
| 0 | 502 | | gpuSkinningThrottler.SetThrottling(gpuSkinningFramesBetweenUpdates); |
| 0 | 503 | | } |
| | 504 | |
|
| | 505 | | void SetupHairAndSkinColors() |
| | 506 | | { |
| 0 | 507 | | bodyShapeController.SetupHairAndSkinColors(model.skinColor, model.hairColor); |
| | 508 | |
|
| 0 | 509 | | foreach ( var wearable in wearableControllers ) |
| | 510 | | { |
| 0 | 511 | | wearable.Value.SetupHairAndSkinColors(model.skinColor, model.hairColor); |
| | 512 | | } |
| 0 | 513 | | } |
| | 514 | |
|
| | 515 | | void OnWearableLoadingSuccess(WearableController wearableController) |
| | 516 | | { |
| 0 | 517 | | if (wearableController == null || model == null) |
| | 518 | | { |
| 0 | 519 | | Debug.LogWarning($"WearableSuccess was called wrongly: IsWearableControllerNull=>{wearableController == |
| 0 | 520 | | OnWearableLoadingFail(wearableController, 0); |
| | 521 | | } |
| 0 | 522 | | } |
| | 523 | |
|
| | 524 | | void OnBodyShapeLoadingFail(WearableController wearableController) |
| | 525 | | { |
| 0 | 526 | | Debug.LogError($"Avatar: {model?.name} - Failed loading bodyshape: {wearableController?.id}"); |
| 0 | 527 | | CleanupAvatar(); |
| 0 | 528 | | OnFailEvent?.Invoke(true); |
| 0 | 529 | | } |
| | 530 | |
|
| | 531 | | void OnWearableLoadingFail(WearableController wearableController, int retriesCount = MAX_RETRIES) |
| | 532 | | { |
| 0 | 533 | | if (retriesCount <= 0) |
| | 534 | | { |
| 0 | 535 | | Debug.LogError($"Avatar: {model?.name} - Failed loading wearable: {wearableController?.id}"); |
| 0 | 536 | | CleanupAvatar(); |
| 0 | 537 | | OnFailEvent?.Invoke(false); |
| 0 | 538 | | return; |
| | 539 | | } |
| | 540 | |
|
| 0 | 541 | | wearableController.Load(bodyShapeController.id, transform, OnWearableLoadingSuccess, x => OnWearableLoadingF |
| 0 | 542 | | } |
| | 543 | |
|
| | 544 | | private void SetWearableBones() |
| | 545 | | { |
| | 546 | | // NOTE(Brian): Set bones/rootBone of all wearables to be the same of the baseBody, |
| | 547 | | // so all of them are animated together. |
| 0 | 548 | | using (var enumerator = wearableControllers.GetEnumerator()) |
| | 549 | | { |
| 0 | 550 | | while (enumerator.MoveNext()) |
| | 551 | | { |
| 0 | 552 | | enumerator.Current.Value.SetAnimatorBones(bodyShapeController.bones, bodyShapeController.rootBone); |
| | 553 | | } |
| 0 | 554 | | } |
| 0 | 555 | | } |
| | 556 | |
|
| | 557 | | private void UpdateExpression() |
| | 558 | | { |
| 0 | 559 | | SetExpression(model.expressionTriggerId, model.expressionTriggerTimestamp); |
| | 560 | |
|
| 0 | 561 | | if (lastStickerTimestamp != model.stickerTriggerTimestamp && model.stickerTriggerId != null) |
| | 562 | | { |
| 0 | 563 | | lastStickerTimestamp = model.stickerTriggerTimestamp; |
| | 564 | |
|
| 0 | 565 | | if ( stickersController != null ) |
| 0 | 566 | | stickersController.PlayEmote(model.stickerTriggerId); |
| | 567 | | } |
| 0 | 568 | | } |
| | 569 | |
|
| | 570 | | public void SetExpression(string id, long timestamp) |
| | 571 | | { |
| 2 | 572 | | model.expressionTriggerId = id; |
| 2 | 573 | | model.expressionTriggerTimestamp = timestamp; |
| 2 | 574 | | animator.SetExpressionValues(id, timestamp); |
| 2 | 575 | | } |
| | 576 | |
|
| | 577 | | private void AddWearableController(WearableItem wearable) |
| | 578 | | { |
| 0 | 579 | | if (wearable == null) |
| 0 | 580 | | return; |
| 0 | 581 | | switch (wearable.data.category) |
| | 582 | | { |
| | 583 | | case Categories.EYES: |
| 0 | 584 | | eyesController = new FacialFeatureController(wearable, eyeMaterial); |
| 0 | 585 | | break; |
| | 586 | | case Categories.EYEBROWS: |
| 0 | 587 | | eyebrowsController = new FacialFeatureController(wearable, eyebrowMaterial); |
| 0 | 588 | | break; |
| | 589 | | case Categories.MOUTH: |
| 0 | 590 | | mouthController = new FacialFeatureController(wearable, mouthMaterial); |
| 0 | 591 | | break; |
| | 592 | | case Categories.BODY_SHAPE: |
| | 593 | | break; |
| | 594 | |
|
| | 595 | | default: |
| 0 | 596 | | var wearableController = new WearableController(wearable); |
| 0 | 597 | | wearableControllers.Add(wearable, wearableController); |
| | 598 | | break; |
| | 599 | | } |
| 0 | 600 | | } |
| | 601 | |
|
| | 602 | | //TODO: Remove/replace once the class is easily mockable. |
| | 603 | | protected void CopyFrom(AvatarRenderer original) |
| | 604 | | { |
| 0 | 605 | | this.wearableControllers = original.wearableControllers; |
| 0 | 606 | | this.mouthController = original.mouthController; |
| 0 | 607 | | this.bodyShapeController = original.bodyShapeController; |
| 0 | 608 | | this.eyebrowsController = original.eyebrowsController; |
| 0 | 609 | | this.eyesController = original.eyesController; |
| 0 | 610 | | } |
| | 611 | |
|
| | 612 | | public void SetGOVisibility(bool newVisibility) |
| | 613 | | { |
| | 614 | | //NOTE(Brian): Avatar being loaded needs the renderer.enabled as false until the loading finishes. |
| | 615 | | // So we can' manipulate the values because it'd show an incomplete avatar. Its easier to just d |
| 715 | 616 | | if (gameObject.activeSelf != newVisibility) |
| 17 | 617 | | gameObject.SetActive(newVisibility); |
| 715 | 618 | | } |
| | 619 | |
|
| | 620 | | public void SetRendererEnabled(bool newVisibility) |
| | 621 | | { |
| 0 | 622 | | if (mainMeshRenderer == null) |
| 0 | 623 | | return; |
| | 624 | |
|
| 0 | 625 | | mainMeshRenderer.enabled = newVisibility; |
| 0 | 626 | | } |
| | 627 | |
|
| | 628 | | public void SetImpostorVisibility(bool impostorVisibility) |
| | 629 | | { |
| 1312 | 630 | | if (impostorVisibility && !initializedImpostor) |
| 0 | 631 | | InitializeImpostor(); |
| | 632 | |
|
| 1312 | 633 | | impostorRenderer.gameObject.SetActive(impostorVisibility); |
| 1312 | 634 | | } |
| | 635 | |
|
| 0 | 636 | | public void SetImpostorForward(Vector3 newForward) { impostorRenderer.transform.forward = newForward; } |
| | 637 | |
|
| 0 | 638 | | public void SetImpostorColor(Color newColor) { AvatarRendererHelpers.SetImpostorTintColor(impostorRenderer.mater |
| | 639 | |
|
| | 640 | | public void SetThrottling(int framesBetweenUpdates) |
| | 641 | | { |
| 0 | 642 | | gpuSkinningFramesBetweenUpdates = framesBetweenUpdates; |
| 0 | 643 | | gpuSkinningThrottler?.SetThrottling(gpuSkinningFramesBetweenUpdates); |
| 0 | 644 | | } |
| | 645 | |
|
| | 646 | | public void SetAvatarFade(float avatarFade) |
| | 647 | | { |
| 0 | 648 | | if (bodyShapeController == null || !bodyShapeController.isReady) |
| 0 | 649 | | return; |
| | 650 | |
|
| 0 | 651 | | Material[] mats = mainMeshRenderer.sharedMaterials; |
| 0 | 652 | | for (int j = 0; j < mats.Length; j++) |
| | 653 | | { |
| 0 | 654 | | mats[j].SetFloat(ShaderUtils.DitherFade, avatarFade); |
| | 655 | | } |
| 0 | 656 | | } |
| | 657 | |
|
| | 658 | | public void SetImpostorFade(float impostorFade) |
| | 659 | | { |
| | 660 | | //TODO implement dither in Unlit shader |
| 0 | 661 | | Color current = impostorRenderer.material.GetColor(BASE_COLOR_PROPERTY); |
| 0 | 662 | | current.a = impostorFade; |
| 0 | 663 | | impostorRenderer.material.SetColor(BASE_COLOR_PROPERTY, current); |
| | 664 | |
|
| 0 | 665 | | OnImpostorAlphaValueUpdate?.Invoke(impostorFade); |
| 0 | 666 | | } |
| | 667 | |
|
| | 668 | | private void HideAll() |
| | 669 | | { |
| | 670 | | // TODO: Cache this somewhere (maybe when the LoadAvatar finishes) instead of fetching this on every call |
| 0 | 671 | | Renderer[] renderers = gameObject.GetComponentsInChildren<Renderer>(); |
| | 672 | |
|
| 0 | 673 | | for (int i = 0; i < renderers.Length; i++) |
| | 674 | | { |
| 0 | 675 | | renderers[i].enabled = false; |
| | 676 | | } |
| 0 | 677 | | } |
| | 678 | |
|
| | 679 | | public void SetFacialFeaturesVisible(bool visible) |
| | 680 | | { |
| 0 | 681 | | if (bodyShapeController == null || !bodyShapeController.isReady) |
| 0 | 682 | | return; |
| | 683 | |
|
| 0 | 684 | | if (isLoading) |
| 0 | 685 | | return; |
| | 686 | |
|
| 0 | 687 | | bodyShapeController.SetFacialFeaturesVisible(visible, true); |
| 0 | 688 | | } |
| | 689 | |
|
| | 690 | | public void SetSSAOEnabled(bool ssaoEnabled) |
| | 691 | | { |
| 0 | 692 | | if ( isLoading ) |
| 0 | 693 | | return; |
| | 694 | |
|
| 0 | 695 | | Material[] mats = mainMeshRenderer.sharedMaterials; |
| | 696 | |
|
| 0 | 697 | | for (int j = 0; j < mats.Length; j++) |
| | 698 | | { |
| 0 | 699 | | if (ssaoEnabled) |
| 0 | 700 | | mats[j].DisableKeyword("_SSAO_OFF"); |
| | 701 | | else |
| 0 | 702 | | mats[j].EnableKeyword("_SSAO_OFF"); |
| | 703 | | } |
| 0 | 704 | | } |
| | 705 | |
|
| | 706 | | private bool MergeAvatar(IEnumerable<SkinnedMeshRenderer> allRenderers) |
| | 707 | | { |
| 0 | 708 | | var renderersToCombine = allRenderers.Where((r) => !r.transform.parent.gameObject.name.Contains("Mask")).ToL |
| | 709 | |
|
| 0 | 710 | | bool success = avatarMeshCombiner.Combine( |
| | 711 | | bodyShapeController.upperBodyRenderer, |
| | 712 | | renderersToCombine.ToArray(), |
| | 713 | | defaultMaterial); |
| | 714 | |
|
| 0 | 715 | | if ( success ) |
| | 716 | | { |
| 0 | 717 | | avatarMeshCombiner.container.transform.SetParent( transform, true ); |
| 0 | 718 | | avatarMeshCombiner.container.transform.localPosition = Vector3.zero; |
| | 719 | | } |
| | 720 | |
|
| 0 | 721 | | return success; |
| | 722 | | } |
| | 723 | |
|
| 3002 | 724 | | private void CleanMergedAvatar() { avatarMeshCombiner.Dispose(); } |
| | 725 | |
|
| | 726 | | private void ResetImpostor() |
| | 727 | | { |
| 1582 | 728 | | if (impostorRenderer == null) |
| 269 | 729 | | return; |
| | 730 | |
|
| 1313 | 731 | | if (bodySnapshotTexturePromise != null) |
| 0 | 732 | | AssetPromiseKeeper_Texture.i.Forget(bodySnapshotTexturePromise); |
| | 733 | |
|
| 1313 | 734 | | AvatarRendererHelpers.ResetImpostor(impostorMeshFilter.mesh, impostorRenderer.material); |
| | 735 | |
|
| 1313 | 736 | | initializedImpostor = false; |
| 1313 | 737 | | } |
| | 738 | |
|
| | 739 | | private void LateUpdate() |
| | 740 | | { |
| 20530 | 741 | | if (gpuSkinning != null && mainMeshRenderer.enabled) |
| 0 | 742 | | gpuSkinningThrottler.TryUpdate(); |
| 20530 | 743 | | } |
| | 744 | |
|
| | 745 | | protected virtual void OnDestroy() |
| | 746 | | { |
| 843 | 747 | | isDestroyed = true; |
| 843 | 748 | | CleanupAvatar(); |
| 843 | 749 | | } |
| | 750 | | } |
| | 751 | | } |