| | 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 (); |
| | 15 | | AssetPromise_AB subPromise; |
| | 16 | | Coroutine loadingCoroutine; |
| | 17 | |
|
| 12 | 18 | | private BaseVariable<FeatureFlag> featureFlags => DataStore.i.featureFlags.flags; |
| | 19 | | private const string FEATURE_AB_MESH_GPU = "ab-mesh-gpu"; |
| | 20 | |
|
| 44 | 21 | | public AssetPromise_AB_GameObject(string contentUrl, string hash) : base(contentUrl, hash) |
| | 22 | | { |
| | 23 | |
|
| 44 | 24 | | } |
| | 25 | |
|
| | 26 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 27 | | { |
| 19 | 28 | | loadingCoroutine = CoroutineStarter.Start(LoadingCoroutine(OnSuccess, OnFail)); |
| 19 | 29 | | } |
| | 30 | |
|
| | 31 | | protected override bool AddToLibrary() |
| | 32 | | { |
| 12 | 33 | | if (!library.Add(asset)) |
| 0 | 34 | | return false; |
| | 35 | |
|
| 12 | 36 | | asset = settings.forceNewInstance |
| | 37 | | ? ((AssetLibrary_AB_GameObject)library).GetCopyFromOriginal(asset.id) |
| | 38 | | : library.Get(asset.id); |
| | 39 | |
|
| | 40 | | //NOTE(Brian): Call again this method because we are replacing the asset. |
| 12 | 41 | | settings.ApplyBeforeLoad(asset.container.transform); |
| | 42 | |
|
| 12 | 43 | | return true; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | protected override void OnReuse(Action OnSuccess) |
| | 47 | | { |
| 25 | 48 | | asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container); |
| 25 | 49 | | asset.Show(OnSuccess); |
| 25 | 50 | | } |
| | 51 | |
|
| | 52 | | protected override void OnAfterLoadOrReuse() |
| | 53 | | { |
| 37 | 54 | | asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container); |
| 37 | 55 | | settings.ApplyAfterLoad(asset.container.transform); |
| 37 | 56 | | } |
| | 57 | |
|
| | 58 | | protected override void OnBeforeLoadOrReuse() |
| | 59 | | { |
| 44 | 60 | | settings.ApplyBeforeLoad(asset.container.transform); |
| 44 | 61 | | } |
| | 62 | |
|
| | 63 | | protected override void OnCancelLoading() |
| | 64 | | { |
| 7 | 65 | | if (loadingCoroutine != null) |
| | 66 | | { |
| 3 | 67 | | PerformanceAnalytics.ABTracker.TrackCancelled(); |
| 3 | 68 | | CoroutineStarter.Stop(loadingCoroutine); |
| 3 | 69 | | loadingCoroutine = null; |
| | 70 | | } |
| | 71 | |
|
| 7 | 72 | | if (asset != null) |
| 7 | 73 | | UnityEngine.Object.Destroy(asset.container); |
| | 74 | |
|
| 7 | 75 | | AssetPromiseKeeper_AB.i.Forget(subPromise); |
| 7 | 76 | | } |
| | 77 | |
|
| | 78 | | public override string ToString() |
| | 79 | | { |
| 0 | 80 | | if (subPromise != null) |
| 0 | 81 | | return $"{subPromise} ... AB_GameObject state = {state}"; |
| | 82 | |
|
| 0 | 83 | | return $"subPromise == null? state = {state}"; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | private IEnumerator LoadingCoroutine(Action OnSuccess, Action<Exception> OnFail) |
| | 87 | | { |
| 19 | 88 | | PerformanceAnalytics.ABTracker.TrackLoading(); |
| 19 | 89 | | subPromise = new AssetPromise_AB(contentUrl, hash, asset.container.transform); |
| 19 | 90 | | bool success = false; |
| 19 | 91 | | Exception loadingException = null; |
| 34 | 92 | | subPromise.OnSuccessEvent += (x) => success = true; |
| | 93 | |
|
| 19 | 94 | | subPromise.OnFailEvent += (ab, exception) => |
| | 95 | | { |
| 1 | 96 | | loadingException = exception; |
| 1 | 97 | | success = false; |
| 1 | 98 | | }; |
| | 99 | |
|
| 19 | 100 | | asset.ownerPromise = subPromise; |
| 19 | 101 | | AssetPromiseKeeper_AB.i.Keep(subPromise); |
| | 102 | |
|
| 19 | 103 | | yield return subPromise; |
| | 104 | |
|
| 16 | 105 | | if (success) |
| | 106 | | { |
| 15 | 107 | | yield return InstantiateABGameObjects(); |
| | 108 | |
|
| 15 | 109 | | if (subPromise.asset == null || asset.container == null) |
| 3 | 110 | | success = false; |
| | 111 | | } |
| | 112 | |
|
| 16 | 113 | | loadingCoroutine = null; |
| | 114 | |
|
| 16 | 115 | | if (success) |
| | 116 | | { |
| 12 | 117 | | PerformanceAnalytics.ABTracker.TrackLoaded(); |
| 12 | 118 | | OnSuccess?.Invoke(); |
| | 119 | | } |
| | 120 | | else |
| | 121 | | { |
| 4 | 122 | | PerformanceAnalytics.ABTracker.TrackFailed(); |
| 4 | 123 | | loadingException ??= new Exception($"AB sub-promise asset or container is null. Asset: {subPromise.asset |
| 4 | 124 | | OnFail?.Invoke(loadingException); |
| | 125 | | } |
| 16 | 126 | | } |
| | 127 | |
|
| | 128 | | private IEnumerator InstantiateABGameObjects() |
| | 129 | | { |
| 15 | 130 | | var goList = subPromise.asset.GetAssetsByExtensions<GameObject>("glb", "ltf"); |
| | 131 | |
|
| 15 | 132 | | if (goList.Count == 0) |
| | 133 | | { |
| 2 | 134 | | if (asset.container != null) |
| 2 | 135 | | UnityEngine.Object.Destroy(asset.container); |
| | 136 | |
|
| 2 | 137 | | asset.container = null; |
| | 138 | |
|
| 2 | 139 | | AssetPromiseKeeper_AB.i.Forget(subPromise); |
| | 140 | |
|
| 2 | 141 | | yield break; |
| | 142 | | } |
| | 143 | |
|
| 50 | 144 | | for (int i = 0; i < goList.Count; i++) |
| | 145 | | { |
| 13 | 146 | | if (loadingCoroutine == null) |
| | 147 | | break; |
| | 148 | |
|
| 13 | 149 | | if (asset.container == null) |
| | 150 | | break; |
| | 151 | |
|
| 12 | 152 | | GameObject assetBundleModelGO = UnityEngine.Object.Instantiate(goList[i], asset.container.transform); |
| | 153 | |
|
| 12 | 154 | | asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(assetBundleModelGO); |
| 12 | 155 | | asset.materials = MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers); |
| 12 | 156 | | asset.SetTextures(MeshesInfoUtils.ExtractUniqueTextures(asset.materials)); |
| 12 | 157 | | OptimizeMaterials(MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers)); |
| 12 | 158 | | UploadMeshesToGPU(MeshesInfoUtils.ExtractUniqueMeshes(asset.renderers)); |
| 12 | 159 | | asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCo |
| | 160 | |
|
| 12 | 161 | | var animators = MeshesInfoUtils.ExtractUniqueAnimations(assetBundleModelGO); |
| 12 | 162 | | asset.animationClipSize = subPromise.asset.metrics.animationsEstimatedSize; |
| 12 | 163 | | asset.meshDataSize = subPromise.asset.metrics.meshesEstimatedSize; |
| | 164 | |
|
| 24 | 165 | | foreach (var animator in animators) { animator.cullingType = AnimationCullingType.AlwaysAnimate; } |
| | 166 | |
|
| | 167 | | #if UNITY_EDITOR |
| 12 | 168 | | assetBundleModelGO.name = subPromise.asset.GetName(); |
| | 169 | | #endif |
| | 170 | |
|
| | 171 | | } |
| 13 | 172 | | } |
| | 173 | |
|
| | 174 | | private void OptimizeMaterials(HashSet<Material> materials) |
| | 175 | | { |
| 48 | 176 | | foreach (Material material in materials) |
| 12 | 177 | | SRPBatchingHelper.OptimizeMaterial(material); |
| 12 | 178 | | } |
| | 179 | |
|
| | 180 | | private void UploadMeshesToGPU(HashSet<Mesh> meshesList) |
| | 181 | | { |
| 12 | 182 | | bool uploadMesh = IsUploadMeshToGPUEnabled(); |
| | 183 | |
|
| 48 | 184 | | foreach (Mesh mesh in meshesList) |
| | 185 | | { |
| 12 | 186 | | if (!mesh.isReadable) |
| | 187 | | continue; |
| | 188 | |
|
| 12 | 189 | | asset.meshToTriangleCount[mesh] = mesh.triangles.Length; |
| 12 | 190 | | asset.meshes.Add(mesh); |
| | 191 | |
|
| 12 | 192 | | if (!uploadMesh) continue; |
| | 193 | |
|
| 0 | 194 | | bool isCollider = mesh.name.Contains("_collider", StringComparison.InvariantCultureIgnoreCase); |
| | 195 | |
|
| | 196 | | // colliders will fail to be created if they are not readable on WebGL |
| 0 | 197 | | if (!isCollider) |
| | 198 | | { |
| 0 | 199 | | Physics.BakeMesh(mesh.GetInstanceID(), false); |
| 0 | 200 | | mesh.UploadMeshData(true); |
| | 201 | | } |
| | 202 | | } |
| 12 | 203 | | } |
| | 204 | |
|
| | 205 | | protected override Asset_AB_GameObject GetAsset(object id) |
| | 206 | | { |
| 25 | 207 | | if (settings.forceNewInstance) |
| 9 | 208 | | return ((AssetLibrary_AB_GameObject)library).GetCopyFromOriginal(id); |
| | 209 | |
|
| 16 | 210 | | return base.GetAsset(id); |
| | 211 | | } |
| | 212 | |
|
| | 213 | | private bool IsUploadMeshToGPUEnabled() => |
| 12 | 214 | | featureFlags.Get().IsFeatureEnabled(FEATURE_AB_MESH_GPU); |
| | 215 | | } |
| | 216 | | } |