| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Assertions; |
| | 8 | | using UnityGLTF; |
| | 9 | | using UnityGLTF.Scripts; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 13 | | public class AssetPromise_GLTF : AssetPromise<Asset_GLTF> |
| | 14 | | { |
| 176 | 15 | | public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering(); |
| | 16 | | protected string assetDirectoryPath; |
| | 17 | |
|
| | 18 | | protected ContentProvider provider = null; |
| 0 | 19 | | public string fileName { get; private set; } |
| | 20 | |
|
| | 21 | | IGLTFComponent gltfComponent = null; |
| | 22 | | IWebRequestController webRequestController = null; |
| | 23 | |
|
| | 24 | | object id = null; |
| | 25 | | private List<int> texIdsCache; |
| | 26 | |
|
| | 27 | | public AssetPromise_GLTF(string url) |
| 0 | 28 | | : this(new ContentProvider_Dummy(), url, null, Environment.i.platform.webRequest) |
| | 29 | | { |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public AssetPromise_GLTF(string url, IWebRequestController webRequestController) |
| 11 | 33 | | : this(new ContentProvider_Dummy(), url, null, webRequestController) |
| | 34 | | { |
| 11 | 35 | | } |
| | 36 | |
|
| | 37 | | public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null) |
| 158 | 38 | | : this(provider, url, hash, Environment.i.platform.webRequest) |
| | 39 | | { |
| 158 | 40 | | } |
| | 41 | |
|
| | 42 | | public AssetPromise_GLTF(ContentProvider provider, string url, IWebRequestController webRequestController) |
| 7 | 43 | | : this(provider, url, null, webRequestController) |
| | 44 | | { |
| 7 | 45 | | } |
| | 46 | |
|
| 176 | 47 | | public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont |
| | 48 | | { |
| 176 | 49 | | this.provider = provider; |
| 176 | 50 | | this.fileName = url.Substring(url.LastIndexOf('/') + 1); |
| 176 | 51 | | this.id = hash ?? url; |
| 176 | 52 | | this.webRequestController = webRequestController; |
| | 53 | | // We separate the directory path of the GLB and its file name, to be able to use the directory path when |
| | 54 | | // fetching relative assets like textures in the ParseGLTFWebRequestedFile() event call |
| 176 | 55 | | assetDirectoryPath = URIHelper.GetDirectoryName(url); |
| 176 | 56 | | } |
| | 57 | |
|
| | 58 | | protected override void OnBeforeLoadOrReuse() |
| | 59 | | { |
| 273 | 60 | | settings.ApplyBeforeLoad(asset.container.transform); |
| 273 | 61 | | } |
| | 62 | |
|
| | 63 | | protected override void OnAfterLoadOrReuse() |
| | 64 | | { |
| 139 | 65 | | if (asset?.container != null) |
| | 66 | | { |
| 139 | 67 | | asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container); |
| 139 | 68 | | settings.ApplyAfterLoad(asset.renderers.ToList()); |
| | 69 | | } |
| 139 | 70 | | } |
| | 71 | |
|
| 1060 | 72 | | public override object GetId() { return id; } |
| | 73 | |
|
| | 74 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 75 | | { |
| | 76 | | try |
| | 77 | | { |
| 133 | 78 | | gltfComponent = asset.container.AddComponent<GLTFComponent>(); |
| | 79 | |
|
| 133 | 80 | | gltfComponent.Initialize(webRequestController, AssetPromiseKeeper_GLTF.i.throttlingCounter); |
| 133 | 81 | | gltfComponent.RegisterCallbacks(MeshCreated, RendererCreated); |
| | 82 | |
|
| 133 | 83 | | GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings |
| | 84 | | { |
| | 85 | | useVisualFeedback = settings.visibleFlags == |
| | 86 | | AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, |
| | 87 | | initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE, |
| | 88 | | shaderOverride = settings.shaderOverride, |
| | 89 | | addMaterialsToPersistentCaching = |
| | 90 | | (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) != 0, |
| | 91 | | forceGPUOnlyMesh = settings.forceGPUOnlyMesh |
| | 92 | | }; |
| | 93 | |
|
| 133 | 94 | | gltfComponent.OnSuccess += () => |
| | 95 | | { |
| | 96 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 107 | 97 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 98 | | return; |
| | 99 | | #endif |
| | 100 | |
|
| 107 | 101 | | if (asset != null) |
| | 102 | | { |
| 107 | 103 | | asset.totalTriangleCount = |
| | 104 | | MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCount); |
| 107 | 105 | | asset.materials = MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers); |
| 107 | 106 | | asset.textures = MeshesInfoUtils.ExtractUniqueTextures(asset.materials); |
| 107 | 107 | | asset.animationClipSize = gltfComponent.GetAnimationClipMemorySize(); |
| 107 | 108 | | asset.meshDataSize = gltfComponent.GetMeshesMemorySize(); |
| 107 | 109 | | var animations = MeshesInfoUtils.ExtractUniqueAnimations(asset.container); |
| 107 | 110 | | asset.animationClips = MeshesInfoUtils.ExtractUniqueAnimationClips(animations); |
| | 111 | | } |
| | 112 | |
|
| 107 | 113 | | OnSuccess.Invoke(); |
| 107 | 114 | | }; |
| | 115 | |
|
| 133 | 116 | | gltfComponent.OnFail += OnFail; |
| | 117 | |
|
| 133 | 118 | | gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string, |
| | 119 | | false, tmpSettings, FileToHash); |
| | 120 | |
|
| 133 | 121 | | asset.name = fileName; |
| 133 | 122 | | } |
| 0 | 123 | | catch (Exception error) |
| | 124 | | { |
| 0 | 125 | | OnFail?.Invoke(error); |
| 0 | 126 | | } |
| 133 | 127 | | } |
| | 128 | |
|
| | 129 | | private void RendererCreated(Renderer r) |
| | 130 | | { |
| 296 | 131 | | Assert.IsTrue(r != null, "Renderer is null?"); |
| | 132 | |
|
| | 133 | | // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation. |
| 296 | 134 | | if ( asset == null ) |
| 0 | 135 | | return; |
| | 136 | |
|
| 296 | 137 | | asset.renderers.Add(r); |
| 296 | 138 | | } |
| | 139 | |
|
| | 140 | | private void MeshCreated(Mesh mesh) |
| | 141 | | { |
| 315 | 142 | | Assert.IsTrue(mesh != null, "Mesh is null?"); |
| | 143 | |
|
| | 144 | | // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation. |
| 315 | 145 | | if ( asset == null ) |
| 0 | 146 | | return; |
| | 147 | |
|
| 315 | 148 | | asset.meshes.Add(mesh); |
| 315 | 149 | | asset.meshToTriangleCount[mesh] = mesh.triangles.Length; |
| 315 | 150 | | } |
| | 151 | |
|
| 295 | 152 | | bool FileToHash(string fileName, out string hash) { return provider.TryGetContentHash(assetDirectoryPath + fileN |
| | 153 | |
|
| | 154 | | protected override void OnReuse(Action OnSuccess) |
| | 155 | | { |
| | 156 | | // Materials and textures are reused, so they are not extracted again |
| 33 | 157 | | asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container); |
| | 158 | |
|
| | 159 | | //NOTE(Brian): Show the asset using the simple gradient feedback. |
| 33 | 160 | | asset.Show(settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, OnS |
| 33 | 161 | | } |
| | 162 | |
|
| | 163 | | protected override bool AddToLibrary() |
| | 164 | | { |
| 107 | 165 | | if (!library.Add(asset)) |
| 0 | 166 | | return false; |
| | 167 | |
|
| | 168 | | //NOTE(Brian): If the asset did load "in world" add it to library and then Get it immediately |
| | 169 | | // So it keeps being there. As master gltfs can't be in the world. |
| | 170 | | // |
| | 171 | | // ApplySettings will reposition the newly Get asset to the proper coordinates. |
| 107 | 172 | | if (settings.forceNewInstance) |
| | 173 | | { |
| 2 | 174 | | asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id); |
| 2 | 175 | | } |
| | 176 | | else |
| | 177 | | { |
| 105 | 178 | | asset = library.Get(asset.id); |
| | 179 | | } |
| | 180 | |
|
| | 181 | | //NOTE(Brian): Call again this method because we are replacing the asset. |
| 107 | 182 | | OnBeforeLoadOrReuse(); |
| 107 | 183 | | return true; |
| | 184 | | } |
| | 185 | |
|
| | 186 | | protected override void OnCancelLoading() |
| | 187 | | { |
| 27 | 188 | | if (asset != null) |
| | 189 | | { |
| 27 | 190 | | asset.CancelShow(); |
| | 191 | | } |
| 27 | 192 | | } |
| | 193 | |
|
| | 194 | | protected override Asset_GLTF GetAsset(object id) |
| | 195 | | { |
| 33 | 196 | | if (settings.forceNewInstance) |
| | 197 | | { |
| 9 | 198 | | return ((AssetLibrary_GLTF)library).GetCopyFromOriginal(id); |
| | 199 | | } |
| | 200 | | else |
| | 201 | | { |
| 24 | 202 | | return base.GetAsset(id); |
| | 203 | | } |
| | 204 | | } |
| | 205 | |
|
| | 206 | | // NOTE: master promise are silently forgotten. We should make sure that they are loaded anyway since |
| | 207 | | // other promises are waiting for them |
| | 208 | | internal void OnSilentForget() |
| | 209 | | { |
| 3 | 210 | | asset.Hide(); |
| 3 | 211 | | settings.parent = null; |
| | 212 | |
|
| 3 | 213 | | if (gltfComponent != null) |
| | 214 | | { |
| 3 | 215 | | gltfComponent.SetPrioritized(); |
| | 216 | | } |
| 3 | 217 | | } |
| | 218 | | } |
| | 219 | | } |