VRMExporterConfiguration to MeshExportSettings

This commit is contained in:
ousttrue
2020-10-01 17:04:27 +09:00
parent d721e9a8b1
commit d6623be68b
10 changed files with 49 additions and 91 deletions

View File

@@ -81,7 +81,7 @@ namespace VRM.Samples
*/
importedJson.RemoveValue(Utf8String.From("/bufferViews/*/byteStride"));
var vrm = VRMExporter.Export(context.Root);
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root);
// TODO: Check contents in JSON
/*var exportJson = */

View File

@@ -90,7 +90,7 @@ namespace VRM.Samples
return;
}
var vrm = VRMExporter.Export(m_model);
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model);
var bytes = vrm.ToGlbBytes();
File.WriteAllBytes(path, bytes);
Debug.LogFormat("export to {0}", path);

View File

@@ -57,7 +57,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(go);
exporter.Export();
exporter.Export(MeshExportSettings.Default);
// remove empty buffer
gltf.buffers.Clear();
@@ -307,7 +307,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(CreateSimpleScene());
exporter.Export();
exporter.Export(MeshExportSettings.Default);
}
var expected = gltf.ToJson().ParseAsJson();
@@ -629,7 +629,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(go);
exporter.Export();
exporter.Export(UniGLTF.MeshExportSettings.Default);
json = gltf.ToJson();
}

View File

@@ -13,6 +13,25 @@ namespace UniGLTF
public Renderer Renderer;
}
public struct MeshExportSettings
{
// MorphTarget に Sparse Accessor を使う
public bool UseSparseAccessorForMorphTarget;
// MorphTarget を Position だけにする(normal とか捨てる)
public bool ExportOnlyBlendShapePosition;
// VertexColor が存在していても捨てる
public bool RemoveVertexColor;
public static MeshExportSettings Default => new MeshExportSettings
{
UseSparseAccessorForMorphTarget = false,
ExportOnlyBlendShapePosition = false,
RemoveVertexColor = false,
};
}
public static class MeshExporter
{
static glTFMesh ExportPrimitives(glTF gltf, int bufferIndex,
@@ -234,9 +253,7 @@ namespace UniGLTF
public static IEnumerable<(Mesh, glTFMesh, Dictionary<int, int>)> ExportMeshes(glTF gltf, int bufferIndex,
List<MeshWithRenderer> unityMeshes, List<Material> unityMaterials,
bool useSparseAccessorForMorphTarget,
bool exportOnlyBlendShapePosition,
bool removeVertexColor)
MeshExportSettings settings)
{
for (int i = 0; i < unityMeshes.Count; ++i)
{
@@ -246,7 +263,7 @@ namespace UniGLTF
var gltfMesh = ExportPrimitives(gltf, bufferIndex,
x.Renderer.name,
mesh, materials, unityMaterials, removeVertexColor);
mesh, materials, unityMaterials, settings.RemoveVertexColor);
var blendShapeIndexMap = new Dictionary<int, int>();
int exportBlendShapes = 0;
@@ -254,8 +271,8 @@ namespace UniGLTF
{
var morphTarget = ExportMorphTarget(gltf, bufferIndex,
mesh, j,
useSparseAccessorForMorphTarget,
exportOnlyBlendShapePosition);
settings.UseSparseAccessorForMorphTarget,
settings.ExportOnlyBlendShapePosition);
if (morphTarget.POSITION < 0 && morphTarget.NORMAL < 0 && morphTarget.TANGENT < 0)
{
continue;

View File

@@ -41,7 +41,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(go);
exporter.Export();
exporter.Export(MeshExportSettings.Default);
}
var bytes = gltf.ToGlbBytes();
File.WriteAllBytes(path, bytes);
@@ -61,24 +61,6 @@ namespace UniGLTF
glTF glTF;
public bool UseSparseAccessorForBlendShape
{
get;
set;
}
public bool ExportOnlyBlendShapePosition
{
get;
set;
}
public bool RemoveVertexColor
{
get;
set;
}
public GameObject Copy
{
get;
@@ -147,13 +129,13 @@ namespace UniGLTF
};
}
public static glTF Export(GameObject go)
public static glTF Export(GameObject go, MeshExportSettings meshExportSettings)
{
var gltf = new glTF();
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(go);
exporter.Export();
exporter.Export(meshExportSettings);
}
return gltf;
}
@@ -165,9 +147,9 @@ namespace UniGLTF
Copy.transform.ReverseZRecursive();
}
public void Export()
public void Export(MeshExportSettings meshExportSettings)
{
FromGameObject(glTF, Copy, UseSparseAccessorForBlendShape, RemoveVertexColor);
FromGameObject(glTF, Copy, meshExportSettings);
}
public void Dispose()
@@ -213,8 +195,7 @@ namespace UniGLTF
return node;
}
void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMorphTarget = false,
bool removeVertexColor = false)
void FromGameObject(glTF gltf, GameObject go, MeshExportSettings meshExportSettings)
{
var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]);
var bufferIndex = gltf.AddBuffer(bytesBuffer);
@@ -229,7 +210,6 @@ namespace UniGLTF
try
{
Nodes = go.transform.Traverse()
.Skip(1) // exclude root object for the symmetry with the importer
.ToList();
@@ -250,7 +230,6 @@ namespace UniGLTF
}
#endregion
#region Meshes
var unityMeshes = Nodes
.Select(x => new MeshWithRenderer
@@ -276,8 +255,7 @@ namespace UniGLTF
MeshBlendShapeIndexMap = new Dictionary<Mesh, Dictionary<int, int>>();
foreach (var (mesh, gltfMesh, blendShapeIndexMap) in MeshExporter.ExportMeshes(
gltf, bufferIndex, unityMeshes, Materials, useSparseAccessorForMorphTarget,
ExportOnlyBlendShapePosition, removeVertexColor))
gltf, bufferIndex, unityMeshes, Materials, meshExportSettings))
{
gltf.meshes.Add(gltfMesh);
if (!MeshBlendShapeIndexMap.ContainsKey(mesh))

View File

@@ -219,12 +219,7 @@ namespace VRM
// 出力
{
var sw = System.Diagnostics.Stopwatch.StartNew();
var vrm = VRMExporter.Export(target, new VRMExporterConfiguration
{
UseSparseAccessorForBlendShape = settings.UseSparseAccessor,
ExportOnlyBlendShapePosition = settings.OnlyBlendshapePosition,
RemoveVertexColor = settings.RemoveVertexColor
});
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;

View File

@@ -47,5 +47,12 @@ namespace VRM
/// </summary>
[Tooltip("Remove vertex color")]
public bool RemoveVertexColor = false;
}
public UniGLTF.MeshExportSettings MeshExportSettings => new UniGLTF.MeshExportSettings
{
UseSparseAccessorForMorphTarget = UseSparseAccessor,
ExportOnlyBlendShapePosition = OnlyBlendshapePosition,
RemoveVertexColor = RemoveVertexColor,
};
}
}

View File

@@ -19,34 +19,22 @@ namespace VRM
gltf.extensions.VRM = new glTF_VRM_extensions();
}
public static glTF Export(GameObject go, bool exportOnlyBlendShapePosition = false)
{
var config = VRMExporterConfiguration.Default;
config.ExportOnlyBlendShapePosition = exportOnlyBlendShapePosition;
return Export(go, config);
}
public static glTF Export(GameObject go, VRMExporterConfiguration configuration)
public static glTF Export(MeshExportSettings configuration, GameObject go)
{
var gltf = new glTF();
using (var exporter = new VRMExporter(gltf)
using (var exporter = new VRMExporter(gltf))
{
UseSparseAccessorForBlendShape = configuration.UseSparseAccessorForBlendShape,
ExportOnlyBlendShapePosition = configuration.ExportOnlyBlendShapePosition,
RemoveVertexColor = configuration.RemoveVertexColor,
})
{
_Export(gltf, exporter, go);
_Export(gltf, exporter, go, configuration);
}
return gltf;
}
public static void _Export(glTF gltf, VRMExporter exporter, GameObject go)
static void _Export(glTF gltf, VRMExporter exporter, GameObject go, MeshExportSettings meshExportSettings)
{
exporter.Prepare(go);
exporter.Export();
exporter.Export(meshExportSettings);
// avatar
var animator = go.GetComponent<Animator>();

View File

@@ -1,16 +0,0 @@
namespace VRM
{
public struct VRMExporterConfiguration
{
public bool UseSparseAccessorForBlendShape;
public bool ExportOnlyBlendShapePosition;
public bool RemoveVertexColor;
public static VRMExporterConfiguration Default => new VRMExporterConfiguration
{
UseSparseAccessorForBlendShape = true,
ExportOnlyBlendShapePosition = false,
RemoveVertexColor = false,
};
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 974aea88b487e5543b1c39e5a528f751
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: