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