public static ImporterContext Parse(string path, Byte[] bytes) { var ext = Path.GetExtension(path).ToLower(); var context = new ImporterContext(UnityPath.FromFullpath(path)); switch (ext) { case ".gltf": context.ParseJson(Encoding.UTF8.GetString(bytes), new FileSystemStorage(Path.GetDirectoryName(path))); break; case ".zip": { var zipArchive = Zip.ZipArchiveStorage.Parse(bytes); var gltf = zipArchive.Entries.FirstOrDefault(x => x.FileName.ToLower().EndsWith(".gltf")); if (gltf == null) { throw new Exception("no gltf in archive"); } var jsonBytes = zipArchive.Extract(gltf); var json = Encoding.UTF8.GetString(jsonBytes); context.ParseJson(json, zipArchive); } break; case ".glb": context.ParseGlb(bytes); break; default: throw new NotImplementedException(); } return(context); }
public async void Setup(SyncObject obj) { // TODO: dynamic model change handling if (!obj.HasField("model") || !(obj.GetField("model") is BlobHandle)) { // TODO: obj.WriteErrorLog("Model", $"This object has no model field or not a blob handle. Ignoring."); return; } BlobHandle handle = (BlobHandle)obj.GetField("model"); Blob blob = await obj.Node.ReadBlob(handle); obj.WriteDebugLog("Model", $"Blob {handle} loaded"); // Because UniGLTF.ImporterContext is the parent class of VRMImporterContext, // ( https://github.com/vrm-c/UniVRM/blob/3b68eb7f99bfe78ea9c83ea75511282ef1782f1a/Assets/VRM/UniVRM/Scripts/Format/VRMImporterContext.cs#L11 ) // loading procedure is probably almost same (See PlyayerAvatar.cs for VRM loading). // https://github.com/vrm-c/UniVRM/blob/3b68eb7f99bfe78ea9c83ea75511282ef1782f1a/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs#L46 ctx = new UniGLTF.ImporterContext(); // ParseGlb parses GLB file. // https://github.com/vrm-c/UniVRM/blob/3b68eb7f99bfe78ea9c83ea75511282ef1782f1a/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs#L239 // Currently, only GLB (glTF binary format) is supported because it is self-contained ctx.ParseGlb(blob.Data); ctx.Root = gameObject; await ctx.LoadAsyncTask(); // UniGLTF also has ShowMeshes https://github.com/ousttrue/UniGLTF/wiki/Rutime-API#import ctx.ShowMeshes(); obj.WriteDebugLog("Model", "Model load completed"); LoadComplete?.Invoke(this); }
public static void Import(UnityPath gltfPath) { if (!gltfPath.IsUnderAssetsFolder) { throw new Exception(); } ImporterContext context = new ImporterContext(gltfPath); var ext = gltfPath.Extension.ToLower(); try { var prefabPath = gltfPath.Parent.Child(gltfPath.FileNameWithoutExtension + ".prefab"); if (ext == ".gltf") { context.ParseJson(File.ReadAllText(gltfPath.FullPath, System.Text.Encoding.UTF8), new FileSystemStorage(gltfPath.Parent.FullPath)); gltfImporter.Load(context); context.SaveAsAsset(prefabPath); context.Destroy(false); } else if (ext == ".glb") { context.ParseGlb(File.ReadAllBytes(gltfPath.FullPath)); context.SaveTexturesAsPng(prefabPath); EditorApplication.delayCall += () => { // delay and can import png texture gltfImporter.Load(context); context.SaveAsAsset(prefabPath); context.Destroy(false); }; } else { return; } } catch (UniGLTFNotSupportedException ex) { Debug.LogWarningFormat("{0}: {1}", gltfPath, ex.Message ); } catch (Exception ex) { Debug.LogErrorFormat("import error: {0}", gltfPath); Debug.LogErrorFormat("{0}", ex); if (context != null) { context.Destroy(true); } } }
GameObject Load(string path) { var bytes = File.ReadAllBytes(path); Debug.LogFormat("[OnClick] {0}", path); var context = new ImporterContext(); var ext = Path.GetExtension(path).ToLower(); switch (ext) { case ".gltf": context.ParseJson(Encoding.UTF8.GetString(bytes), new FileSystemStorage(Path.GetDirectoryName(path))); break; case ".zip": { var zipArchive = Zip.ZipArchiveStorage.Parse(bytes); var gltf = zipArchive.Entries.FirstOrDefault(x => x.FileName.ToLower().EndsWith(".gltf")); if (gltf == null) { throw new Exception("no gltf in archive"); } var jsonBytes = zipArchive.Extract(gltf); var json = Encoding.UTF8.GetString(jsonBytes); context.ParseJson(json, zipArchive); } break; case ".glb": context.ParseGlb(bytes); break; default: throw new NotImplementedException(); } context.Load(); context.Root.name = Path.GetFileNameWithoutExtension(path); context.ShowMeshes(); return(context.Root); }
public static void ImportMenu() { var path = UnityEditor.EditorUtility.OpenFilePanel("open gltf", "", "gltf,glb"); if (!string.IsNullOrEmpty(path)) { Debug.Log(path); var context = new ImporterContext { Path = path, }; var bytes = File.ReadAllBytes(path); var ext = Path.GetExtension(path).ToLower(); switch (ext) { case ".gltf": { context.ParseJson <glTF>(Encoding.UTF8.GetString(bytes), new FileSystemStorage(Path.GetDirectoryName(path))); gltfImporter.Import <glTF>(context); context.Root.name = Path.GetFileNameWithoutExtension(path); context.ShowMeshes(); Selection.activeGameObject = context.Root; } break; case ".glb": { context.ParseGlb <glTF>(bytes); gltfImporter.Import <glTF>(context); context.Root.name = Path.GetFileNameWithoutExtension(path); context.ShowMeshes(); Selection.activeGameObject = context.Root; } break; default: Debug.LogWarningFormat("unknown ext: {0}", path); break; } } }
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (string path in importedAssets) { ImporterContext context = new ImporterContext { Path = path, }; var ext = Path.GetExtension(path).ToLower(); try { if (ext == ".gltf") { context.ParseJson <glTF>(File.ReadAllText(context.Path, System.Text.Encoding.UTF8), new FileSystemStorage(Path.GetDirectoryName(path))); gltfImporter.Import <glTF>(context); context.SaveAsAsset(); context.Destroy(false); } else if (ext == ".glb") { context.ParseGlb <glTF>(File.ReadAllBytes(context.Path)); // // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html // for (int i = 0; i < context.GLTF.textures.Count; ++i) { var x = context.GLTF.textures[i]; var image = context.GLTF.images[x.source]; if (string.IsNullOrEmpty(image.uri)) { // glb buffer var folder = context.GetAssetFolder(".Textures").AssetPathToFullPath(); if (!Directory.Exists(folder)) { UnityEditor.AssetDatabase.CreateFolder(context.GLTF.baseDir, Path.GetFileNameWithoutExtension(context.Path) + ".Textures"); //Directory.CreateDirectory(folder); } // name & bytes var textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#GLB", i); var byteSegment = context.GLTF.GetViewBytes(image.bufferView); // path var png = Path.Combine(folder, textureName + ".png"); File.WriteAllBytes(png, byteSegment.ToArray()); var assetPath = png.ToUnityRelativePath(); //Debug.LogFormat("import asset {0}", assetPath); UnityEditor.AssetDatabase.ImportAsset(assetPath); image.uri = assetPath.Substring(context.GLTF.baseDir.Length + 1); } } UnityEditor.AssetDatabase.Refresh(); EditorApplication.delayCall += () => { // delay and can import png texture gltfImporter.Import <glTF>(context); context.SaveAsAsset(); context.Destroy(false); }; } else { continue; } } catch (UniGLTFNotSupportedException ex) { Debug.LogWarningFormat("{0}: {1}", path, ex.Message ); } catch (Exception ex) { Debug.LogErrorFormat("import error: {0}", path); Debug.LogErrorFormat("{0}", ex); if (context != null) { context.Destroy(true); } } } }