From dc2ebb369fd289e5c277c0be82b88bb531a1763a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 23 Jul 2018 14:39:40 +0900 Subject: [PATCH] Updated UniGLTF --- .../Format/Editor/vrmAssetPostprocessor.cs | 58 ++++++------------- Scripts/Format/VRMFormat.cs | 2 +- Scripts/Format/VRMImporter.cs | 40 +++---------- Scripts/Format/VRMImporterContext.cs | 9 +-- UniGLTF | 2 +- 5 files changed, 29 insertions(+), 82 deletions(-) diff --git a/Scripts/Format/Editor/vrmAssetPostprocessor.cs b/Scripts/Format/Editor/vrmAssetPostprocessor.cs index f3bc97c8d..34bc5ad24 100644 --- a/Scripts/Format/Editor/vrmAssetPostprocessor.cs +++ b/Scripts/Format/Editor/vrmAssetPostprocessor.cs @@ -16,49 +16,27 @@ namespace VRM var ext = Path.GetExtension(path).ToLower(); if (ext == ".vrm") { - var context = new VRMImporterContext(path); - - context.ParseGlb(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); - } - - var textureName = !string.IsNullOrEmpty(image.name) ? image.name: string.Format("buffer#{0:00}", i); - var png = Path.Combine(folder, textureName + ".png"); - var byteSegment = context.GLTF.GetViewBytes(image.bufferView); - File.WriteAllBytes(png, byteSegment.ToArray()); - var assetPath = png.ToUnityRelativePath(); - //Debug.LogFormat("import asset {0}", assetPath); - UnityEditor.AssetDatabase.ImportAsset(assetPath, UnityEditor.ImportAssetOptions.ForceUpdate); - UnityEditor.AssetDatabase.Refresh(); - image.uri = assetPath.Substring(context.GLTF.baseDir.Length + 1); - } - } - - EditorApplication.delayCall += () => - { - // delay and can import png texture - VRMImporter.LoadFromBytes(context); - context.SaveAsAsset(); - context.Destroy(false); - }; + ImportVrm(path); } } } + + static void ImportVrm(string path) + { + var context = new VRMImporterContext(path); + context.ParseGlb(File.ReadAllBytes(path)); + + var prefabPath = (Path.GetDirectoryName(path) + "/" + Path.GetFileNameWithoutExtension(path) + ".prefab").Replace("\\", "/"); + context.SaveTexturesAsPng(prefabPath); + + EditorApplication.delayCall += () => + { + // delay and can import png texture + VRMImporter.LoadFromBytes(context); + context.SaveAsAsset(prefabPath); + context.Destroy(false); + }; + } } #endif } diff --git a/Scripts/Format/VRMFormat.cs b/Scripts/Format/VRMFormat.cs index 554fbfbc7..6408d4d2b 100644 --- a/Scripts/Format/VRMFormat.cs +++ b/Scripts/Format/VRMFormat.cs @@ -17,7 +17,7 @@ namespace UniGLTF } } - public partial class glTF_extensions : ExtensionBase + public partial class glTF_extensions : ExtensionsBase { public VRM.glTF_VRM_extensions VRM = new VRM.glTF_VRM_extensions(); diff --git a/Scripts/Format/VRMImporter.cs b/Scripts/Format/VRMImporter.cs index 838fa6c6d..97ae4e1bc 100644 --- a/Scripts/Format/VRMImporter.cs +++ b/Scripts/Format/VRMImporter.cs @@ -28,38 +28,17 @@ namespace VRM public static GameObject LoadFromBytes(Byte[] bytes) { - var context = new VRMImporterContext(); + var context = new VRMImporterContext(null); context.ParseGlb(bytes); LoadFromBytes(context); return context.Root; } - public static void LoadFromPath(VRMImporterContext context) - { - context.ParseGlb(File.ReadAllBytes(context.Path)); - LoadFromBytes(context); - } - public static void LoadFromBytes(VRMImporterContext context) { context.CreateMaterial = VRMImporter.GetMaterialFunc(glTF_VRM_Material.Parse(context.Json)); gltfImporter.Load(context); - if (string.IsNullOrEmpty(context.Path)) - { - if (string.IsNullOrEmpty(context.GLTF.extensions.VRM.meta.title)) - { - context.Root.name = "VRM_LOADED"; - } - else - { - context.Root.name = context.GLTF.extensions.VRM.meta.title; - } - } - else - { - context.Root.name = Path.GetFileNameWithoutExtension(context.Path); - } OnLoadModel(context); @@ -368,7 +347,7 @@ namespace VRM }; context.HumanoidAvatar = AvatarBuilder.BuildHumanAvatar(context.Root, description); - context.HumanoidAvatar.name = Path.GetFileNameWithoutExtension(context.Path); + context.HumanoidAvatar.name = "VrmAvatar"; context.AvatarDescription = UniHumanoid.AvatarDescription.CreateFrom(description); context.AvatarDescription.name = "AvatarDescription"; @@ -389,7 +368,7 @@ namespace VRM context.AvatarDescription = context.GLTF.extensions.VRM.humanoid.ToDescription(context.Nodes); context.AvatarDescription.name = "AvatarDescription"; context.HumanoidAvatar = context.AvatarDescription.CreateAvatar(context.Root.transform); - context.HumanoidAvatar.name = Path.GetFileNameWithoutExtension(context.Path); + context.HumanoidAvatar.name = "VrmAvatar"; var humanoid = context.Root.AddComponent(); humanoid.Avatar = context.HumanoidAvatar; @@ -409,7 +388,7 @@ namespace VRM { for (int i = 0; i < context.GLTF.textures.Count; ++i) { - var x = new TextureItem(context.GLTF, i); + var x = new TextureItem(context.GLTF, i, null); x.Process(context.GLTF, storage); context.Textures.Add(x); yield return null; @@ -518,7 +497,7 @@ namespace VRM public static void LoadVrmAsync(Byte[] bytes, Action onLoaded, Action onError = null, bool show = true) { - var context = new VRMImporterContext(); + var context = new VRMImporterContext(null); context.ParseGlb(bytes); LoadVrmAsync(context, onLoaded, onError, show); } @@ -539,11 +518,6 @@ namespace VRM return schedulable .AddTask(Scheduler.ThreadPool, () => - { - ctx.GLTF.baseDir = Path.GetDirectoryName(ctx.Path); - return Unit.Default; - }) - .ContinueWith(Scheduler.CurrentThread, _ => { return glTF_VRM_Material.Parse(ctx.Json); }) @@ -561,7 +535,7 @@ namespace VRM parent.AddTask(Scheduler.MainThread, () => { - var texture = new TextureItem(ctx.GLTF, index); + var texture = new TextureItem(ctx.GLTF, index, null); texture.Process(ctx.GLTF, ctx.Storage); return texture; }) @@ -588,7 +562,7 @@ namespace VRM .ContinueWith(Scheduler.CurrentThread, _ => { - ctx.Root.name = Path.GetFileNameWithoutExtension(ctx.Path); + ctx.Root.name = "VRM"; if (show) { diff --git a/Scripts/Format/VRMImporterContext.cs b/Scripts/Format/VRMImporterContext.cs index 924cf8693..fa2b50d79 100644 --- a/Scripts/Format/VRMImporterContext.cs +++ b/Scripts/Format/VRMImporterContext.cs @@ -7,13 +7,8 @@ namespace VRM { public class VRMImporterContext : ImporterContext { - public VRMImporterContext() + public VRMImporterContext(string path):base(path) { - - } - public VRMImporterContext(string path) - { - Path = path; } public UniHumanoid.AvatarDescription AvatarDescription; @@ -44,7 +39,7 @@ namespace VRM // 作成する(先行ロード用) if(gltfMeta.texture >= 0 && gltfMeta.texture < GLTF.textures.Count) { - var t = new TextureItem(GLTF, gltfMeta.texture); + var t = new TextureItem(GLTF, gltfMeta.texture, null); t.Process(GLTF, Storage); meta.Thumbnail=t.Texture; } diff --git a/UniGLTF b/UniGLTF index 7c5646a08..d65954fa5 160000 --- a/UniGLTF +++ b/UniGLTF @@ -1 +1 @@ -Subproject commit 7c5646a0813adde070c1387630ac733a6dcd0c24 +Subproject commit d65954fa54388b15273293b9c87d97370195426e