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