| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | | using MainScripts.DCL.Analytics.PerformanceAnalytics; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class AssetPromise_GLTFast_Instance : AssetPromise_WithUrl<Asset_GLTFast_Instance> |
| | 12 | | { |
| | 13 | | private readonly IWebRequestController webRequestController; |
| | 14 | | private readonly ContentProvider contentProvider; |
| | 15 | | private readonly AssetPromiseSettings_Rendering settings; |
| | 16 | | private AssetPromise_GLTFast_Loader subPromise; |
| | 17 | | private Coroutine loadingCoroutine; |
| | 18 | |
|
| | 19 | | public AssetPromise_GLTFast_Instance(string contentUrl, string hash, IWebRequestController webRequestController, |
| 110 | 20 | | : base(contentUrl, hash) |
| | 21 | | { |
| 110 | 22 | | this.webRequestController = webRequestController; |
| 110 | 23 | | this.contentProvider = contentProvider ?? new ContentProvider_Dummy(); |
| 110 | 24 | | this.settings = settings ?? new AssetPromiseSettings_Rendering(); |
| 110 | 25 | | } |
| | 26 | |
|
| | 27 | | protected override void OnLoad(Action onSuccess, Action<Exception> onFail) |
| | 28 | | { |
| 103 | 29 | | loadingCoroutine = CoroutineStarter.Start(LoadingCoroutine(onSuccess, onFail)); |
| 103 | 30 | | } |
| | 31 | |
|
| | 32 | | protected override bool AddToLibrary() |
| | 33 | | { |
| 94 | 34 | | if (!library.Add(asset)) |
| 0 | 35 | | return false; |
| | 36 | |
|
| 94 | 37 | | asset = settings.forceNewInstance ? ((AssetLibrary_GLTFast_Instance)library).GetCopyFromOriginal(asset.id) : |
| | 38 | |
|
| 94 | 39 | | settings.ApplyBeforeLoad(asset.container.transform); |
| | 40 | |
|
| 94 | 41 | | return true; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | protected override void OnReuse(Action onSuccess) |
| | 45 | | { |
| 7 | 46 | | asset.Show(onSuccess); |
| 7 | 47 | | } |
| | 48 | |
|
| | 49 | | protected override void OnAfterLoadOrReuse() |
| | 50 | | { |
| 101 | 51 | | settings.ApplyAfterLoad(asset.container.transform); |
| 101 | 52 | | } |
| | 53 | |
|
| | 54 | | protected override void OnBeforeLoadOrReuse() |
| | 55 | | { |
| 110 | 56 | | settings.ApplyBeforeLoad(asset.container.transform); |
| 110 | 57 | | } |
| | 58 | |
|
| | 59 | | protected override void OnCancelLoading() |
| | 60 | | { |
| 9 | 61 | | if (loadingCoroutine != null) |
| | 62 | | { |
| 9 | 63 | | PerformanceAnalytics.GLTFTracker.TrackCancelled(); |
| 9 | 64 | | CoroutineStarter.Stop(loadingCoroutine); |
| 9 | 65 | | loadingCoroutine = null; |
| | 66 | | } |
| | 67 | |
|
| 9 | 68 | | if (asset != null) |
| 9 | 69 | | Object.Destroy(asset.container); |
| | 70 | |
|
| 9 | 71 | | AssetPromiseKeeper_GLTFast.i.Forget(subPromise); |
| 9 | 72 | | } |
| | 73 | |
|
| | 74 | | public override string ToString() |
| | 75 | | { |
| 0 | 76 | | if (subPromise != null) |
| 0 | 77 | | return $"{subPromise.ToString()} ... GLTFast_GameObject state = {state}"; |
| | 78 | | else |
| 0 | 79 | | return $"subPromise == null? state = {state}"; |
| | 80 | | } |
| | 81 | |
|
| 219 | 82 | | public override bool keepWaiting => loadingCoroutine != null; |
| | 83 | |
|
| | 84 | | private IEnumerator LoadingCoroutine(Action OnSuccess, Action<Exception> OnFail) |
| | 85 | | { |
| 103 | 86 | | PerformanceAnalytics.GLTFTracker.TrackLoading(); |
| | 87 | |
|
| | 88 | | // Since GLTFast give us an "instantiator" we create another asset promise to create the main and only insta |
| 103 | 89 | | subPromise = new AssetPromise_GLTFast_Loader(contentUrl, hash, webRequestController, contentProvider); |
| | 90 | |
|
| 103 | 91 | | var success = false; |
| 103 | 92 | | Exception loadingException = null; |
| | 93 | |
|
| 203 | 94 | | subPromise.OnSuccessEvent += (x) => success = true; |
| | 95 | |
|
| 103 | 96 | | subPromise.OnFailEvent += (ab, exception) => |
| | 97 | | { |
| 0 | 98 | | loadingException = exception; |
| 0 | 99 | | success = false; |
| 0 | 100 | | }; |
| | 101 | |
|
| 103 | 102 | | asset.ownerPromise = subPromise; |
| 103 | 103 | | AssetPromiseKeeper_GLTFast.i.Keep(subPromise); |
| | 104 | |
|
| 103 | 105 | | asset.container.SetActive(false); |
| | 106 | |
|
| 103 | 107 | | yield return subPromise; |
| | 108 | |
|
| 100 | 109 | | if (success) |
| | 110 | | { |
| 100 | 111 | | if (!asset.container) |
| | 112 | | { |
| 0 | 113 | | OnFail?.Invoke(new Exception("Object was destroyed during loading")); |
| 0 | 114 | | yield break; |
| | 115 | | } |
| | 116 | |
|
| 100 | 117 | | yield return subPromise.asset.InstantiateAsync(asset.container.transform).ToCoroutine(e => |
| | 118 | | { |
| 0 | 119 | | if (e is not OperationCanceledException) |
| 0 | 120 | | throw e; |
| 0 | 121 | | }); |
| 98 | 122 | | yield return RemoveCollidersFromRenderers(asset.container.transform); |
| | 123 | | } |
| | 124 | |
|
| 94 | 125 | | SetupAssetSettings(); |
| | 126 | |
|
| 94 | 127 | | asset.container.SetActive(true); |
| | 128 | |
|
| 94 | 129 | | loadingCoroutine = null; |
| | 130 | |
|
| 94 | 131 | | if (success) |
| | 132 | | { |
| 94 | 133 | | PerformanceAnalytics.GLTFTracker.TrackLoaded(); |
| 94 | 134 | | OnSuccess?.Invoke(); |
| | 135 | | } |
| | 136 | | else |
| | 137 | | { |
| 0 | 138 | | PerformanceAnalytics.GLTFTracker.TrackFailed(); |
| 0 | 139 | | loadingException ??= new Exception($"GLTFast sub-promise asset or container is null. Asset: {subPromise. |
| 0 | 140 | | OnFail?.Invoke(loadingException); |
| | 141 | | } |
| 94 | 142 | | } |
| | 143 | |
|
| | 144 | | private void SetupAssetSettings() |
| | 145 | | { |
| 94 | 146 | | if (settings.visibleFlags is AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE) |
| | 147 | | { |
| 1 | 148 | | var renderers = asset.container.GetComponentsInChildren<Renderer>(true); |
| | 149 | |
|
| 6 | 150 | | foreach (Renderer renderer in renderers) |
| 2 | 151 | | renderer.enabled = false; |
| | 152 | | } |
| 94 | 153 | | } |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// Our GLTFs come with visible colliders that we should get rid of, this is a requirement for our systems |
| | 157 | | /// </summary> |
| | 158 | | /// <param name="rootGameObject"></param> |
| | 159 | | /// <returns></returns> |
| | 160 | | private IEnumerator RemoveCollidersFromRenderers(Transform rootGameObject) |
| | 161 | | { |
| 98 | 162 | | Utils.InverseTransformChildTraversal<Renderer>(renderer => |
| | 163 | | { |
| 269 | 164 | | if (IsCollider(renderer.transform)) |
| 40 | 165 | | Utils.SafeDestroy(renderer); |
| 269 | 166 | | }, rootGameObject.transform); |
| | 167 | |
|
| | 168 | | // we wait a single frame until the collider renderers are deleted because some systems might be able to get |
| 98 | 169 | | yield return null; |
| 94 | 170 | | } |
| | 171 | |
|
| | 172 | | private static bool IsCollider(Transform transform) |
| | 173 | | { |
| 269 | 174 | | bool transformName = transform.name.ToLower().Contains("_collider"); |
| 269 | 175 | | bool parentName = transform.parent.name.ToLower().Contains("_collider"); |
| | 176 | |
|
| 269 | 177 | | return parentName || transformName; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | protected override Asset_GLTFast_Instance GetAsset(object id) => |
| 7 | 181 | | settings.forceNewInstance ? ((AssetLibrary_GLTFast_Instance)library).GetCopyFromOriginal(id) : base.GetAsset |
| | 182 | |
|
| | 183 | | public void OverrideInitialPosition(Vector3 pos) |
| | 184 | | { |
| 0 | 185 | | settings.initialLocalPosition = pos; |
| 0 | 186 | | } |
| | 187 | | } |
| | 188 | | } |