| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using DCL; |
| | 6 | |
|
| | 7 | | public class FacialFeatureController : CustomYieldInstruction |
| | 8 | | { |
| 0 | 9 | | public bool isReady { get; private set; } |
| 0 | 10 | | public string wearableId => wearableItem?.id; |
| | 11 | |
|
| | 12 | | private readonly WearableItem wearableItem; |
| | 13 | | private readonly Material baseMaterial; |
| | 14 | |
|
| | 15 | | internal Texture mainTexture = null; |
| | 16 | | internal Texture maskTexture = null; |
| | 17 | | internal AssetPromise_Texture mainTexturePromise = null; |
| | 18 | | internal AssetPromise_Texture maskTexturePromise = null; |
| | 19 | |
|
| | 20 | | private Color color; |
| | 21 | | private IBodyShapeController bodyShape; |
| | 22 | | internal Material baseMaterialCopy; |
| | 23 | | private Coroutine fetchTextureCoroutine; |
| | 24 | |
|
| 4 | 25 | | public FacialFeatureController(WearableItem wearableItem, Material baseMaterial) |
| | 26 | | { |
| 4 | 27 | | isReady = false; |
| 4 | 28 | | this.baseMaterial = baseMaterial; |
| 4 | 29 | | this.wearableItem = wearableItem; |
| 4 | 30 | | } |
| | 31 | |
|
| | 32 | | public void Load(IBodyShapeController loadedBody, Color color) |
| | 33 | | { |
| 4 | 34 | | this.color = color; |
| | 35 | |
|
| 4 | 36 | | if (isReady) |
| | 37 | | { |
| 0 | 38 | | PrepareWearable(); |
| 0 | 39 | | return; |
| | 40 | | } |
| | 41 | |
|
| 4 | 42 | | this.bodyShape = loadedBody; |
| 4 | 43 | | fetchTextureCoroutine = CoroutineStarter.Start(FetchTextures()); |
| 4 | 44 | | } |
| | 45 | |
|
| | 46 | | void PrepareWearable() |
| | 47 | | { |
| 4 | 48 | | if (baseMaterialCopy == null) |
| 4 | 49 | | baseMaterialCopy = new Material(baseMaterial); |
| | 50 | |
|
| 4 | 51 | | switch (wearableItem.data.category) |
| | 52 | | { |
| | 53 | | case WearableLiterals.Categories.EYES: |
| 3 | 54 | | bodyShape.SetupEyes(baseMaterialCopy, mainTexture, maskTexture, color); |
| 3 | 55 | | break; |
| | 56 | | case WearableLiterals.Categories.EYEBROWS: |
| 0 | 57 | | bodyShape.SetupEyebrows(baseMaterialCopy, mainTexture, color); |
| 0 | 58 | | break; |
| | 59 | | case WearableLiterals.Categories.MOUTH: |
| 1 | 60 | | bodyShape.SetupMouth(baseMaterialCopy, mainTexture, maskTexture, color); |
| | 61 | | break; |
| | 62 | | } |
| | 63 | |
|
| 4 | 64 | | isReady = true; |
| 4 | 65 | | } |
| | 66 | |
|
| | 67 | | private IEnumerator FetchTextures() |
| | 68 | | { |
| 4 | 69 | | if (mainTexturePromise != null) |
| 0 | 70 | | AssetPromiseKeeper_Texture.i.Forget(mainTexturePromise); |
| | 71 | |
|
| 4 | 72 | | if (maskTexturePromise != null) |
| 0 | 73 | | AssetPromiseKeeper_Texture.i.Forget(maskTexturePromise); |
| | 74 | |
|
| 4 | 75 | | mainTexture = null; |
| 4 | 76 | | maskTexture = null; |
| | 77 | |
|
| 4 | 78 | | var representation = wearableItem.GetRepresentation(bodyShape.bodyShapeId); |
| | 79 | |
|
| 8 | 80 | | string mainTextureHash = representation?.contents?.FirstOrDefault(x => x.key == representation?.mainFile)?.hash; |
| 4 | 81 | | if (mainTextureHash == null) |
| 3 | 82 | | mainTextureHash = representation?.contents?.FirstOrDefault(x => !x.key.ToLower().Contains("_mask.png"))?.has |
| 10 | 83 | | string maskhash = representation?.contents?.FirstOrDefault(x => x.key.ToLower().Contains("_mask.png"))?.hash; |
| | 84 | |
|
| 4 | 85 | | if (!string.IsNullOrEmpty(mainTextureHash)) |
| | 86 | | { |
| 3 | 87 | | mainTexturePromise = new AssetPromise_Texture(wearableItem.baseUrl + mainTextureHash); |
| 5 | 88 | | mainTexturePromise.OnSuccessEvent += (x) => mainTexture = x.texture; |
| 4 | 89 | | mainTexturePromise.OnFailEvent += (x) => mainTexture = null; |
| | 90 | |
|
| 3 | 91 | | AssetPromiseKeeper_Texture.i.Keep(mainTexturePromise); |
| | 92 | | } |
| | 93 | |
|
| 4 | 94 | | if (!string.IsNullOrEmpty(maskhash)) |
| | 95 | | { |
| 3 | 96 | | maskTexturePromise = new AssetPromise_Texture(wearableItem.baseUrl + maskhash); |
| 5 | 97 | | maskTexturePromise.OnSuccessEvent += (x) => maskTexture = x.texture; |
| 4 | 98 | | maskTexturePromise.OnFailEvent += (x) => maskTexture = null; |
| | 99 | |
|
| 3 | 100 | | AssetPromiseKeeper_Texture.i.Keep(maskTexturePromise); |
| | 101 | | } |
| | 102 | |
|
| 4 | 103 | | yield return mainTexturePromise; |
| 4 | 104 | | yield return maskTexturePromise; |
| | 105 | |
|
| 4 | 106 | | PrepareWearable(); |
| 4 | 107 | | } |
| | 108 | |
|
| | 109 | | public void CleanUp() |
| | 110 | | { |
| 0 | 111 | | if (mainTexturePromise != null) |
| 0 | 112 | | AssetPromiseKeeper_Texture.i.Forget(mainTexturePromise); |
| | 113 | |
|
| 0 | 114 | | if (maskTexturePromise != null) |
| 0 | 115 | | AssetPromiseKeeper_Texture.i.Forget(maskTexturePromise); |
| | 116 | |
|
| 0 | 117 | | if ( baseMaterialCopy != null ) |
| | 118 | | { |
| 0 | 119 | | Object.Destroy(baseMaterialCopy); |
| 0 | 120 | | baseMaterialCopy = null; |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | CoroutineStarter.Stop(fetchTextureCoroutine); |
| | 124 | |
|
| 0 | 125 | | mainTexture = null; |
| 0 | 126 | | maskTexture = null; |
| 0 | 127 | | isReady = false; |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | public static FacialFeatureController CreateDefaultFacialFeature(string bodyShape, string category, Material materia |
| | 131 | | { |
| 0 | 132 | | string defaultId = WearableLiterals.DefaultWearables.GetDefaultWearable(bodyShape, category); |
| 0 | 133 | | CatalogController.wearableCatalog.TryGetValue(defaultId, out WearableItem wearable); |
| 0 | 134 | | if (wearable == null) |
| | 135 | | { |
| 0 | 136 | | Debug.LogError($"Couldn't resolve wearable {defaultId}"); |
| 0 | 137 | | return null; |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | return new FacialFeatureController(wearable, material); |
| | 141 | | } |
| | 142 | |
|
| 0 | 143 | | public override bool keepWaiting => !isReady; |
| | 144 | | } |