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