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