From 1afb4e8f6fdb75797de6e1ec392dc2ae3e109c8b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 1 Oct 2020 19:35:04 +0900 Subject: [PATCH] update gltfExporter.Export interface --- Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs | 64 +++++++------------ .../UniVRM/Editor/Format/VRMEditorExporter.cs | 19 +++--- 2 files changed, 32 insertions(+), 51 deletions(-) diff --git a/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs index c7790e9b6..53ebd20d7 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs @@ -129,17 +129,6 @@ namespace UniGLTF }; } - public static glTF Export(GameObject go, MeshExportSettings meshExportSettings) - { - var gltf = new glTF(); - using (var exporter = new gltfExporter(gltf)) - { - exporter.Prepare(go); - exporter.Export(meshExportSettings); - } - return gltf; - } - public virtual void Prepare(GameObject go) { // コピーを作って、Z軸を反転することで左手系を右手系に変換する @@ -147,11 +136,6 @@ namespace UniGLTF Copy.transform.ReverseZRecursive(); } - public void Export(MeshExportSettings meshExportSettings) - { - FromGameObject(glTF, Copy, meshExportSettings); - } - public void Dispose() { if (Application.isEditor) @@ -195,22 +179,22 @@ namespace UniGLTF return node; } - void FromGameObject(glTF gltf, GameObject go, MeshExportSettings meshExportSettings) + public virtual void Export(MeshExportSettings meshExportSettings) { var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]); - var bufferIndex = gltf.AddBuffer(bytesBuffer); + var bufferIndex = glTF.AddBuffer(bytesBuffer); GameObject tmpParent = null; - if (go.transform.childCount == 0) + if (Copy.transform.childCount == 0) { tmpParent = new GameObject("tmpParent"); - go.transform.SetParent(tmpParent.transform, true); - go = tmpParent; + Copy.transform.SetParent(tmpParent.transform, true); + Copy = tmpParent; } try { - Nodes = go.transform.Traverse() + Nodes = Copy.transform.Traverse() .Skip(1) // exclude root object for the symmetry with the importer .ToList(); @@ -221,12 +205,12 @@ namespace UniGLTF TextureManager = new TextureExportManager(unityTextures.Select(x => x.Texture)); var materialExporter = CreateMaterialExporter(); - gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); + glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); for (int i = 0; i < unityTextures.Count; ++i) { var unityTexture = unityTextures[i]; - TextureIO.ExportTexture(gltf, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.TextureType); + TextureIO.ExportTexture(glTF, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.TextureType); } #endregion @@ -255,9 +239,9 @@ namespace UniGLTF MeshBlendShapeIndexMap = new Dictionary>(); foreach (var (mesh, gltfMesh, blendShapeIndexMap) in MeshExporter.ExportMeshes( - gltf, bufferIndex, unityMeshes, Materials, meshExportSettings)) + glTF, bufferIndex, unityMeshes, Materials, meshExportSettings)) { - gltf.meshes.Add(gltfMesh); + glTF.meshes.Add(gltfMesh); if (!MeshBlendShapeIndexMap.ContainsKey(mesh)) { // 同じmeshが複数回現れた @@ -274,19 +258,19 @@ namespace UniGLTF && x.bones != null && x.bones.Length > 0) .ToList(); - gltf.nodes = Nodes.Select(x => ExportNode(x, Nodes, unityMeshes.Select(y => y.Renderer).ToList(), unitySkins)).ToList(); - gltf.scenes = new List + glTF.nodes = Nodes.Select(x => ExportNode(x, Nodes, unityMeshes.Select(y => y.Renderer).ToList(), unitySkins)).ToList(); + glTF.scenes = new List { new gltfScene { - nodes = go.transform.GetChildren().Select(x => Nodes.IndexOf(x)).ToArray(), + nodes = Copy.transform.GetChildren().Select(x => Nodes.IndexOf(x)).ToArray(), } }; foreach (var x in unitySkins) { var matrices = x.sharedMesh.bindposes.Select(y => y.ReverseZ()).ToArray(); - var accessor = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, matrices, glBufferTarget.NONE); + var accessor = glTF.ExtendBufferAndGetAccessorIndex(bufferIndex, matrices, glBufferTarget.NONE); var skin = new glTFSkin { @@ -294,13 +278,13 @@ namespace UniGLTF joints = x.bones.Select(y => Nodes.IndexOf(y)).ToArray(), skeleton = Nodes.IndexOf(x.rootBone), }; - var skinIndex = gltf.skins.Count; - gltf.skins.Add(skin); + var skinIndex = glTF.skins.Count; + glTF.skins.Add(skin); foreach (var z in Nodes.Where(y => y.Has(x))) { var nodeIndex = Nodes.IndexOf(z); - var node = gltf.nodes[nodeIndex]; + var node = glTF.nodes[nodeIndex]; node.skin = skinIndex; } } @@ -310,8 +294,8 @@ namespace UniGLTF #region Animations var clips = new List(); - var animator = go.GetComponent(); - var animation = go.GetComponent(); + var animator = Copy.GetComponent(); + var animation = Copy.GetComponent(); if (animator != null) { clips = AnimationExporter.GetAnimationClips(animator); @@ -325,20 +309,20 @@ namespace UniGLTF { foreach (AnimationClip clip in clips) { - var animationWithCurve = AnimationExporter.Export(clip, go.transform, Nodes); + var animationWithCurve = AnimationExporter.Export(clip, Copy.transform, Nodes); foreach (var kv in animationWithCurve.SamplerMap) { var sampler = animationWithCurve.Animation.samplers[kv.Key]; - var inputAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, kv.Value.Input); + var inputAccessorIndex = glTF.ExtendBufferAndGetAccessorIndex(bufferIndex, kv.Value.Input); sampler.input = inputAccessorIndex; - var outputAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, kv.Value.Output); + var outputAccessorIndex = glTF.ExtendBufferAndGetAccessorIndex(bufferIndex, kv.Value.Output); sampler.output = outputAccessorIndex; // modify accessors - var outputAccessor = gltf.accessors[outputAccessorIndex]; + var outputAccessor = glTF.accessors[outputAccessorIndex]; var channel = animationWithCurve.Animation.channels.First(x => x.sampler == kv.Key); switch (glTFAnimationTarget.GetElementCount(channel.target.path)) { @@ -361,7 +345,7 @@ namespace UniGLTF } } animationWithCurve.Animation.name = clip.name; - gltf.animations.Add(animationWithCurve.Animation); + glTF.animations.Add(animationWithCurve.Animation); } } #endregion diff --git a/Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs index 340a9074e..2d48de97c 100644 --- a/Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs @@ -217,19 +217,16 @@ namespace VRM } // 出力 + var sw = System.Diagnostics.Stopwatch.StartNew(); + var gltf = new UniGLTF.glTF(); + using (var exporter = new VRMExporter(gltf)) { - var sw = System.Diagnostics.Stopwatch.StartNew(); - var vrm = VRMExporter.Export(target, settings.MeshExportSettings); - // vrm.extensions.VRM.meta.title = settings.Title; - // vrm.extensions.VRM.meta.version = settings.Version; - // vrm.extensions.VRM.meta.author = settings.Author; - // vrm.extensions.VRM.meta.contactInformation = settings.ContactInformation; - // vrm.extensions.VRM.meta.reference = settings.Reference; - - var bytes = vrm.ToGlbBytes(); - File.WriteAllBytes(path, bytes); - Debug.LogFormat("Export elapsed {0}", sw.Elapsed); + exporter.Prepare(target); + exporter.Export(settings.MeshExportSettings); } + var bytes = gltf.ToGlbBytes(); + File.WriteAllBytes(path, bytes); + Debug.LogFormat("Export elapsed {0}", sw.Elapsed); if (path.StartsWithUnityAssetPath()) {