diff --git a/Assets/Scripts/API/ApiMethods.EditableModels.cs b/Assets/Scripts/API/ApiMethods.EditableModels.cs index 06d1ef7411..de8e80ab40 100644 --- a/Assets/Scripts/API/ApiMethods.EditableModels.cs +++ b/Assets/Scripts/API/ApiMethods.EditableModels.cs @@ -13,10 +13,12 @@ // limitations under the License. using System; +using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; using System.Threading.Tasks; +using Newtonsoft.Json.Linq; namespace TiltBrush { @@ -39,6 +41,30 @@ public static void ImportWebModel(string url) } var destinationPath = Path.Combine("Models", uri.Host); string filename = _DownloadMediaFileFromUrl(uri, destinationPath); + // Very basic workaround for dependent files like .bin and textures + // TODO + // 1. Handle GLB + // 2. Handle other formats + // 3. Handle zip files + // 4. Create subdirectories for each model + if (ext == "gltf") + { + // Split the url into a base uri and the filename + var baseUri = new Uri(uri, "."); + + var fullLocalPath = Path.Combine(App.ModelLibraryPath(), uri.Host); + + var jsonString = File.ReadAllText(Path.Combine(fullLocalPath, filename)); + JObject jsonObject = JObject.Parse(jsonString); + List externalFiles = jsonObject["buffers"].Select(j => j["uri"].Value()).ToList(); + externalFiles.AddRange(jsonObject["images"].Select(j => j["uri"].Value()).ToList()); + foreach (var externalFile in externalFiles) + { + var newUri = new Uri(baseUri, externalFile); + var subdir = Path.GetDirectoryName(externalFile); + _DownloadMediaFileFromUrl(newUri, Path.Combine(fullLocalPath, subdir)); + } + } ImportModel(Path.Combine(uri.Host, filename)); }