Skip to content

Commit

Permalink
Merge pull request #811 from ousttrue/fix/export_reimport
Browse files Browse the repository at this point in the history
VRM texture export の修正
  • Loading branch information
ousttrue authored Mar 22, 2021
2 parents d09f012 + 2691d3a commit 87398e0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using UniGLTF.UniUnlit;
using UnityEngine;

Expand Down Expand Up @@ -65,7 +66,9 @@ static void Export_OcclusionMetallicRoughness(Material m, TextureExporter textur
{
Texture metallicSmoothTexture = default;
float smoothness = 1.0f;
if (m.HasProperty("_MetallicGlossMap"))

var textuerNames = m.GetTexturePropertyNames();
if (textuerNames.Contains("_MetallicGlossMap"))
{
if (m.HasProperty("_GlossMapScale"))
{
Expand All @@ -76,7 +79,7 @@ static void Export_OcclusionMetallicRoughness(Material m, TextureExporter textur

Texture occlusionTexture = default;
var occlusionStrength = 1.0f;
if (m.HasProperty("_OcclusionMap"))
if (textuerNames.Contains("_OcclusionMap"))
{
occlusionTexture = m.GetTexture("_OcclusionMap");
if (occlusionTexture != null && m.HasProperty("_OcclusionStrength"))
Expand Down
20 changes: 14 additions & 6 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ static glTFNode ExportNode(Transform x, List<Transform> nodes, List<Renderer> re
return node;
}

public virtual void ExportExtensions()
{

}

public virtual void Export(MeshExportSettings meshExportSettings)
{
var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]);
Expand All @@ -184,12 +189,6 @@ public virtual void Export(MeshExportSettings meshExportSettings)

var materialExporter = CreateMaterialExporter();
glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList();

for (int i = 0; i < TextureManager.Exported.Count; ++i)
{
var unityTexture = TextureManager.Exported[i];
glTF.PushGltfTexture(bufferIndex, unityTexture);
}
#endregion

#region Meshes
Expand Down Expand Up @@ -306,6 +305,15 @@ public virtual void Export(MeshExportSettings meshExportSettings)
}
#endregion
#endif

ExportExtensions();

// Extension で Texture が増える場合があるので最後に呼ぶ
for (int i = 0; i < TextureManager.Exported.Count; ++i)
{
var unityTexture = TextureManager.Exported[i];
glTF.PushGltfTexture(bufferIndex, unityTexture);
}
}
#endregion
}
Expand Down
4 changes: 1 addition & 3 deletions Assets/VRM/Runtime/IO/VRMExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ public VRMExporter(glTF gltf) : base(gltf, Axises.Z)
gltf.extensionsUsed.Add(glTF_VRM_extensions.ExtensionName);
}

public override void Export(MeshExportSettings configuration)
public override void ExportExtensions()
{
base.Export(configuration);

// avatar
var animator = Copy.GetComponent<Animator>();
if (animator != null)
Expand Down
32 changes: 29 additions & 3 deletions Assets/VRM/Tests/VRMLoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,39 @@ static void Message(string path, Exception exception)
}
}

static void Load(FileInfo gltf, DirectoryInfo root)
static GameObject Load(FileInfo gltf, DirectoryInfo root, byte[] bytes = null)
{
var parser = new GltfParser();
try
{
parser.ParsePath(gltf.FullName);
if (bytes != null)
{
parser.Parse(gltf.FullName, bytes);
}
else
{
parser.ParsePath(gltf.FullName);
}
}
catch (Exception ex)
{
Debug.LogError($"ParseError: {gltf}");
Debug.LogException(ex);
return null;
}

try
{
using (var importer = new VRMImporterContext(parser))
{
importer.Load();
return importer.DisposeOnGameObjectDestroyed().gameObject;
}
}
catch (Exception ex)
{
Message(gltf.FullName.Substring(root.FullName.Length), ex);
return null;
}
}

Expand All @@ -90,7 +100,23 @@ public void VrmTestModelsTests()

foreach (var gltf in EnumerateGltfFiles(root))
{
Load(gltf, root);
// import
var go = Load(gltf, root);
try
{
// export
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go);

// re import
if (vrm != null)
{
Load(gltf, root, vrm.ToGlbBytes());
}
}
finally
{
GameObject.DestroyImmediate(go);
}
}
}
}
Expand Down

0 comments on commit 87398e0

Please sign in to comment.