| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using DCL.Models; |
| | 8 | | using MainScripts.DCL.Analytics.PerformanceAnalytics; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public class AssetPromise_AB_GameObject : AssetPromise_WithUrl<Asset_AB_GameObject> |
| | 13 | | { |
| 44 | 14 | | public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering(); |
| | 15 | | AssetPromise_AB subPromise; |
| | 16 | | Coroutine loadingCoroutine; |
| | 17 | |
|
| 132 | 18 | | private BaseVariable<FeatureFlag> featureFlags => DataStore.i.featureFlags.flags; |
| | 19 | | private const string AB_LOAD_ANIMATION = "ab_load_animation"; |
| | 20 | | private const string GPU_ONLY_MESHES = "use_gpu_only_meshes"; |
| | 21 | | private bool doTransitionAnimation; |
| | 22 | |
|
| 44 | 23 | | public AssetPromise_AB_GameObject(string contentUrl, string hash) : base(contentUrl, hash) |
| | 24 | | { |
| 44 | 25 | | featureFlags.OnChange += OnFeatureFlagChange; |
| 44 | 26 | | OnFeatureFlagChange(featureFlags.Get(), null); |
| 44 | 27 | | } |
| | 28 | |
|
| 88 | 29 | | private void OnFeatureFlagChange(FeatureFlag current, FeatureFlag previous) { doTransitionAnimation = current.Is |
| | 30 | |
|
| 38 | 31 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) { loadingCoroutine = CoroutineStarter |
| | 32 | |
|
| | 33 | | protected override bool AddToLibrary() |
| | 34 | | { |
| 12 | 35 | | if (!library.Add(asset)) |
| | 36 | | { |
| 0 | 37 | | return false; |
| | 38 | | } |
| | 39 | |
|
| 12 | 40 | | if (settings.forceNewInstance) |
| | 41 | | { |
| 2 | 42 | | asset = (library as AssetLibrary_AB_GameObject).GetCopyFromOriginal(asset.id); |
| 2 | 43 | | } |
| | 44 | | else |
| | 45 | | { |
| 10 | 46 | | asset = library.Get(asset.id); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | //NOTE(Brian): Call again this method because we are replacing the asset. |
| 12 | 50 | | settings.ApplyBeforeLoad(asset.container.transform); |
| | 51 | |
|
| 12 | 52 | | return true; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | protected override void OnReuse(Action OnSuccess) |
| | 56 | | { |
| 25 | 57 | | asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container); |
| 25 | 58 | | asset.Show(OnSuccess); |
| 25 | 59 | | } |
| | 60 | |
|
| | 61 | | protected override void OnAfterLoadOrReuse() |
| | 62 | | { |
| 37 | 63 | | asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container); |
| 37 | 64 | | settings.ApplyAfterLoad(asset.container.transform); |
| 37 | 65 | | } |
| | 66 | |
|
| 88 | 67 | | protected override void OnBeforeLoadOrReuse() { settings.ApplyBeforeLoad(asset.container.transform); } |
| | 68 | |
|
| | 69 | | protected override void OnCancelLoading() |
| | 70 | | { |
| 7 | 71 | | if (loadingCoroutine != null) |
| | 72 | | { |
| 3 | 73 | | PerformanceAnalytics.ABTracker.TrackCancelled(); |
| 3 | 74 | | CoroutineStarter.Stop(loadingCoroutine); |
| 3 | 75 | | loadingCoroutine = null; |
| | 76 | | } |
| | 77 | |
|
| 7 | 78 | | if (asset != null) |
| 7 | 79 | | UnityEngine.Object.Destroy(asset.container); |
| | 80 | |
|
| 7 | 81 | | AssetPromiseKeeper_AB.i.Forget(subPromise); |
| 7 | 82 | | } |
| | 83 | |
|
| | 84 | | public override string ToString() |
| | 85 | | { |
| 0 | 86 | | if (subPromise != null) |
| 0 | 87 | | return $"{subPromise.ToString()} ... AB_GameObject state = {state}"; |
| | 88 | | else |
| 0 | 89 | | return $"subPromise == null? state = {state}"; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | public IEnumerator LoadingCoroutine(Action OnSuccess, Action<Exception> OnFail) |
| | 93 | | { |
| 19 | 94 | | PerformanceAnalytics.ABTracker.TrackLoading(); |
| 19 | 95 | | subPromise = new AssetPromise_AB(contentUrl, hash, asset.container.transform); |
| 19 | 96 | | bool success = false; |
| 19 | 97 | | Exception loadingException = null; |
| 34 | 98 | | subPromise.OnSuccessEvent += (x) => success = true; |
| | 99 | |
|
| 19 | 100 | | subPromise.OnFailEvent += ( ab, exception) => |
| | 101 | | { |
| 1 | 102 | | loadingException = exception; |
| 1 | 103 | | success = false; |
| 1 | 104 | | }; |
| | 105 | |
|
| 19 | 106 | | asset.ownerPromise = subPromise; |
| 19 | 107 | | AssetPromiseKeeper_AB.i.Keep(subPromise); |
| | 108 | |
|
| 19 | 109 | | yield return subPromise; |
| | 110 | |
|
| 16 | 111 | | if (success) |
| | 112 | | { |
| 15 | 113 | | yield return InstantiateABGameObjects(); |
| | 114 | |
|
| 15 | 115 | | if (subPromise.asset == null || asset.container == null) |
| 3 | 116 | | success = false; |
| | 117 | | } |
| | 118 | |
|
| 16 | 119 | | loadingCoroutine = null; |
| | 120 | |
|
| 16 | 121 | | if (success) |
| | 122 | | { |
| 12 | 123 | | PerformanceAnalytics.ABTracker.TrackLoaded(); |
| 12 | 124 | | OnSuccess?.Invoke(); |
| 12 | 125 | | } |
| | 126 | | else |
| | 127 | | { |
| 4 | 128 | | PerformanceAnalytics.ABTracker.TrackFailed(); |
| 4 | 129 | | loadingException ??= new Exception($"AB sub-promise asset or container is null. Asset: {subPromise.asset |
| 4 | 130 | | Debug.LogException(loadingException); |
| 4 | 131 | | OnFail?.Invoke(loadingException); |
| | 132 | | } |
| 16 | 133 | | } |
| | 134 | |
|
| | 135 | | public IEnumerator InstantiateABGameObjects() |
| | 136 | | { |
| 15 | 137 | | var goList = subPromise.asset.GetAssetsByExtensions<GameObject>("glb", "ltf"); |
| | 138 | |
|
| 15 | 139 | | if (goList.Count == 0) |
| | 140 | | { |
| 2 | 141 | | if (asset.container != null) |
| 2 | 142 | | UnityEngine.Object.Destroy(asset.container); |
| | 143 | |
|
| 2 | 144 | | asset.container = null; |
| | 145 | |
|
| 2 | 146 | | AssetPromiseKeeper_AB.i.Forget(subPromise); |
| | 147 | |
|
| 2 | 148 | | yield break; |
| | 149 | | } |
| | 150 | |
|
| 50 | 151 | | for (int i = 0; i < goList.Count; i++) |
| | 152 | | { |
| 13 | 153 | | if (loadingCoroutine == null) |
| | 154 | | break; |
| | 155 | |
|
| 13 | 156 | | if (asset.container == null) |
| | 157 | | break; |
| | 158 | |
|
| 12 | 159 | | GameObject assetBundleModelGO = UnityEngine.Object.Instantiate(goList[i], asset.container.transform); |
| | 160 | |
|
| 12 | 161 | | asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(assetBundleModelGO); |
| 12 | 162 | | asset.materials = MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers); |
| 12 | 163 | | asset.SetTextures(MeshesInfoUtils.ExtractUniqueTextures(asset.materials)); |
| | 164 | |
|
| 12 | 165 | | UploadMeshesToGPU(MeshesInfoUtils.ExtractUniqueMeshes(asset.renderers)); |
| 12 | 166 | | asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCo |
| | 167 | |
|
| | 168 | | //NOTE(Brian): Renderers are enabled in settings.ApplyAfterLoad |
| 12 | 169 | | yield return MaterialCachingHelper.Process(asset.renderers.ToList(), enableRenderers: false, settings.ca |
| | 170 | |
|
| 12 | 171 | | var animators = MeshesInfoUtils.ExtractUniqueAnimations(assetBundleModelGO); |
| 12 | 172 | | asset.animationClipSize = subPromise.asset.metrics.animationsEstimatedSize; |
| 12 | 173 | | asset.meshDataSize = subPromise.asset.metrics.meshesEstimatedSize; |
| | 174 | |
|
| 24 | 175 | | foreach (var animator in animators) |
| | 176 | | { |
| 0 | 177 | | animator.cullingType = AnimationCullingType.AlwaysAnimate; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | #if UNITY_EDITOR |
| 12 | 181 | | assetBundleModelGO.name = subPromise.asset.GetName(); |
| | 182 | | #endif |
| 12 | 183 | | assetBundleModelGO.transform.ResetLocalTRS(); |
| | 184 | |
|
| 12 | 185 | | yield return null; |
| | 186 | |
|
| 12 | 187 | | yield return SetMaterialTransition(); |
| 12 | 188 | | } |
| 13 | 189 | | } |
| | 190 | |
|
| | 191 | | private void UploadMeshesToGPU(HashSet<Mesh> meshesList) |
| | 192 | | { |
| 12 | 193 | | var uploadToGPU = featureFlags.Get().IsFeatureEnabled(GPU_ONLY_MESHES); |
| | 194 | |
|
| 48 | 195 | | foreach ( Mesh mesh in meshesList ) |
| | 196 | | { |
| 12 | 197 | | if ( !mesh.isReadable ) |
| | 198 | | continue; |
| | 199 | |
|
| 12 | 200 | | asset.meshToTriangleCount[mesh] = mesh.triangles.Length; |
| 12 | 201 | | asset.meshes.Add(mesh); |
| | 202 | |
|
| 12 | 203 | | if (uploadToGPU) |
| | 204 | | { |
| 0 | 205 | | Physics.BakeMesh(mesh.GetInstanceID(), false); |
| 0 | 206 | | mesh.UploadMeshData(true); |
| | 207 | | } |
| | 208 | | } |
| 12 | 209 | | } |
| | 210 | |
|
| | 211 | | protected override Asset_AB_GameObject GetAsset(object id) |
| | 212 | | { |
| 25 | 213 | | if (settings.forceNewInstance) |
| | 214 | | { |
| 9 | 215 | | return ((AssetLibrary_AB_GameObject) library).GetCopyFromOriginal(id); |
| | 216 | | } |
| | 217 | | else |
| | 218 | | { |
| 16 | 219 | | return base.GetAsset(id); |
| | 220 | | } |
| | 221 | | } |
| | 222 | |
|
| | 223 | | internal override void OnForget() |
| | 224 | | { |
| 32 | 225 | | base.OnForget(); |
| 32 | 226 | | featureFlags.OnChange -= OnFeatureFlagChange; |
| 32 | 227 | | } |
| | 228 | |
|
| | 229 | | IEnumerator SetMaterialTransition(Action OnSuccess = null) |
| | 230 | | { |
| 12 | 231 | | if (settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE && doTransitionAnimation) |
| | 232 | | { |
| 0 | 233 | | MaterialTransitionController[] materialTransitionControllers = new MaterialTransitionController[asset.re |
| 0 | 234 | | int index = 0; |
| 0 | 235 | | foreach (Renderer assetRenderer in asset.renderers) |
| | 236 | | { |
| 0 | 237 | | MaterialTransitionController transition = assetRenderer.gameObject.AddComponent<MaterialTransitionCo |
| 0 | 238 | | materialTransitionControllers[index] = transition; |
| 0 | 239 | | transition.delay = 0; |
| 0 | 240 | | transition.OnDidFinishLoading(assetRenderer.sharedMaterial); |
| | 241 | |
|
| 0 | 242 | | index++; |
| | 243 | | } |
| | 244 | | // Wait until MaterialTransitionController finishes its effect |
| 0 | 245 | | yield return new WaitUntil(() => IsTransitionFinished(materialTransitionControllers)); |
| | 246 | | } |
| 12 | 247 | | OnSuccess?.Invoke(); |
| 12 | 248 | | } |
| | 249 | |
|
| | 250 | | private bool IsTransitionFinished(MaterialTransitionController[] matTransitions) |
| | 251 | | { |
| 0 | 252 | | bool finishedTransition = true; |
| | 253 | |
|
| 0 | 254 | | for (int i = 0; i < matTransitions.Length; i++) |
| | 255 | | { |
| 0 | 256 | | if (matTransitions[i] != null) |
| | 257 | | { |
| 0 | 258 | | finishedTransition = false; |
| | 259 | |
|
| 0 | 260 | | break; |
| | 261 | | } |
| | 262 | | } |
| | 263 | |
|
| 0 | 264 | | return finishedTransition; |
| | 265 | | } |
| | 266 | |
|
| | 267 | | } |
| | 268 | |
|
| | 269 | | } |