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