Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/rename mesh exporter #1000

Merged
merged 2 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace UniGLTF
{
public static class MeshExporterDivided
public static class MeshExporter_DividedVertexBuffer
{
public static glTFMesh Export(glTF gltf, int bufferIndex,
public static (glTFMesh, Dictionary<int, int>) Export(glTF gltf, int bufferIndex,
MeshWithRenderer unityMesh, List<Material> unityMaterials,
IAxisInverter axisInverter, MeshExportSettings settings)
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public static glTFMesh Export(glTF gltf, int bufferIndex,
var targetNames = Enumerable.Range(0, mesh.blendShapeCount).Select(x => mesh.GetBlendShapeName(x)).ToArray();
gltf_mesh_extras_targetNames.Serialize(gltfMesh, targetNames);

return gltfMesh;
return (gltfMesh, Enumerable.Range(0, mesh.blendShapeCount).ToDictionary(x => x, x => x));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

namespace UniGLTF
{
public static class MeshExporter
public static class MeshExporter_SharedVertexBuffer
{

/// <summary>
/// primitive 間で vertex を共有する形で Export する。
///
/// UniVRM-0.71.0 までの挙動
///
/// UniVRM-0.71.0 以降は、MeshExporterDivided.Export もある
///
/// /// </summary>
/// <param name="gltf"></param>
Expand All @@ -22,7 +23,7 @@ public static class MeshExporter
/// <param name="axisInverter"></param>
/// <param name="settings"></param>
/// <returns></returns>
static glTFMesh ExportSharedVertexBuffer(glTF gltf, int bufferIndex,
public static (glTFMesh, Dictionary<int, int> blendShapeIndexMap) Export(glTF gltf, int bufferIndex,
MeshWithRenderer unityMesh, List<Material> unityMaterials,
IAxisInverter axisInverter, MeshExportSettings settings)
{
Expand Down Expand Up @@ -146,7 +147,41 @@ static glTFMesh ExportSharedVertexBuffer(glTF gltf, int bufferIndex,
material = unityMaterials.IndexOf(materials[j])
});
}
return gltfMesh;

var blendShapeIndexMap = new Dictionary<int, int>();
{
var targetNames = new List<string>();

int exportBlendShapes = 0;
for (int j = 0; j < unityMesh.Mesh.blendShapeCount; ++j)
{
var morphTarget = ExportMorphTarget(gltf, bufferIndex,
unityMesh.Mesh, j,
settings.UseSparseAccessorForMorphTarget,
settings.ExportOnlyBlendShapePosition, axisInverter);
if (morphTarget.POSITION < 0 && morphTarget.NORMAL < 0 && morphTarget.TANGENT < 0)
{
continue;
}

// maybe skip
var blendShapeName = unityMesh.Mesh.GetBlendShapeName(j);
blendShapeIndexMap.Add(j, exportBlendShapes++);
targetNames.Add(blendShapeName);

//
// all primitive has same blendShape
//
for (int k = 0; k < gltfMesh.primitives.Count; ++k)
{
gltfMesh.primitives[k].targets.Add(morphTarget);
}
}

gltf_mesh_extras_targetNames.Serialize(gltfMesh, targetNames);
}

return (gltfMesh, blendShapeIndexMap);
}

static bool UseSparse(
Expand Down Expand Up @@ -207,16 +242,6 @@ static gltfMorphTarget ExportMorphTarget(glTF gltf, int bufferIndex,
{
Debug.LogFormat("Sparse {0}/{1}", sparseIndices.Length, mesh.vertexCount);
}
/*
var vertexSize = 12;
if (useNormal) vertexSize += 12;
if (useTangent) vertexSize += 24;
var sparseBytes = (4 + vertexSize) * sparseIndices.Length;
var fullBytes = (vertexSize) * blendShapeVertices.Length;
Debug.LogFormat("Export sparse: {0}/{1}bytes({2}%)",
sparseBytes, fullBytes, (int)((float)sparseBytes / fullBytes)
);
*/

var sparseIndicesViewIndex = -1;
if (usePosition)
Expand Down Expand Up @@ -287,64 +312,5 @@ static gltfMorphTarget ExportMorphTarget(glTF gltf, int bufferIndex,
TANGENT = blendShapeTangentAccessorIndex,
};
}

/// <summary>
///
/// </summary>
/// <param name="mesh"></param>
/// <param name="gltf"></param>
/// <param name="bufferIndex"></param>
/// <param name="unityMesh"></param>
/// <param name="unityMaterials"></param>
/// <param name="settings"></param>
/// <param name="axisInverter"></param>
/// <returns></returns>
public static (glTFMesh mesh, Dictionary<int, int> blendShapeIndexMap) ExportMesh(glTF gltf, int bufferIndex,
MeshWithRenderer unityMesh, List<Material> unityMaterials,
MeshExportSettings settings, IAxisInverter axisInverter)
{
glTFMesh gltfMesh = default;
var blendShapeIndexMap = new Dictionary<int, int>();
if (settings.DivideVertexBuffer)
{
gltfMesh = MeshExporterDivided.Export(gltf, bufferIndex, unityMesh, unityMaterials, axisInverter, settings);
}
else
{
gltfMesh = ExportSharedVertexBuffer(gltf, bufferIndex, unityMesh, unityMaterials, axisInverter, settings);

var targetNames = new List<string>();

int exportBlendShapes = 0;
for (int j = 0; j < unityMesh.Mesh.blendShapeCount; ++j)
{
var morphTarget = ExportMorphTarget(gltf, bufferIndex,
unityMesh.Mesh, j,
settings.UseSparseAccessorForMorphTarget,
settings.ExportOnlyBlendShapePosition, axisInverter);
if (morphTarget.POSITION < 0 && morphTarget.NORMAL < 0 && morphTarget.TANGENT < 0)
{
continue;
}

// maybe skip
var blendShapeName = unityMesh.Mesh.GetBlendShapeName(j);
blendShapeIndexMap.Add(j, exportBlendShapes++);
targetNames.Add(blendShapeName);

//
// all primitive has same blendShape
//
for (int k = 0; k < gltfMesh.primitives.Count; ++k)
{
gltfMesh.primitives[k].targets.Add(morphTarget);
}
}

gltf_mesh_extras_targetNames.Serialize(gltfMesh, targetNames);
}

return (gltfMesh, blendShapeIndexMap);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ static glTFNode ExportNode(Transform x, List<Transform> nodes, List<MeshWithRend
if (meshRenderer != null)
{
var meshFilter = x.GetComponent<MeshFilter>();
if(meshFilter != null)
if (meshFilter != null)
{
var mesh = meshFilter.sharedMesh;
var materials = meshRenderer.sharedMaterials;
if (TryGetSameMeshIndex(meshWithRenderers, mesh, materials, out int meshIndex))
{
node.mesh = meshIndex;
}
else if(mesh != null && !mesh.vertices.Any())
else if (mesh != null && !mesh.vertices.Any())
{
// 頂点データが無い場合
node.mesh = -1;
Expand All @@ -182,7 +182,7 @@ static glTFNode ExportNode(Transform x, List<Transform> nodes, List<MeshWithRend
{
var mesh = skinnedMeshRenderer.sharedMesh;
var materials = skinnedMeshRenderer.sharedMaterials;
if(TryGetSameMeshIndex(meshWithRenderers, mesh, materials, out int meshIndex))
if (TryGetSameMeshIndex(meshWithRenderers, mesh, materials, out int meshIndex))
{
node.mesh = meshIndex;
node.skin = skins.IndexOf(skinnedMeshRenderer);
Expand Down Expand Up @@ -252,12 +252,15 @@ public virtual void Export(MeshExportSettings meshExportSettings, ITextureSerial
MeshBlendShapeIndexMap = new Dictionary<Mesh, Dictionary<int, int>>();
foreach (var unityMesh in uniqueUnityMeshes)
{
var (gltfMesh, blendShapeIndexMap) = MeshExporter.ExportMesh(glTF, bufferIndex, unityMesh, Materials, meshExportSettings, m_axisInverter);
var (gltfMesh, blendShapeIndexMap) = meshExportSettings.DivideVertexBuffer
? MeshExporter_DividedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials, m_axisInverter, meshExportSettings)
: MeshExporter_SharedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials, m_axisInverter, meshExportSettings)
;
glTF.meshes.Add(gltfMesh);
Meshes.Add(unityMesh.Mesh);
if (!MeshBlendShapeIndexMap.ContainsKey(unityMesh.Mesh))
{
// 同じmeshが複数回現れた
// 重複防止
MeshBlendShapeIndexMap.Add(unityMesh.Mesh, blendShapeIndexMap);
}
}
Expand Down
16 changes: 12 additions & 4 deletions Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ public void SharedVertexBufferTest()
new Material(Shader.Find("Standard")), // B
};

var (go, unityMesh) = CreateMesh(Materials.ToArray());
var (go, mesh) = CreateMesh(Materials.ToArray());
var meshExportSettings = new MeshExportSettings
{
DivideVertexBuffer = false
};
var axisInverter = Axes.X.Create();

var (gltfMesh, blendShapeIndexMap) = MeshExporter.ExportMesh(glTF, bufferIndex, new MeshWithRenderer(go.transform), Materials, meshExportSettings, axisInverter);
var unityMesh = new MeshWithRenderer(go.transform);
var (gltfMesh, blendShapeIndexMap) = meshExportSettings.DivideVertexBuffer
? MeshExporter_DividedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials, axisInverter, meshExportSettings)
: MeshExporter_SharedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials,axisInverter, meshExportSettings)
;

{
var indices = glTF.GetIndices(gltfMesh.primitives[0].indices);
Expand Down Expand Up @@ -171,14 +175,18 @@ public void DividedVertexBufferTest()
new Material(Shader.Find("Standard")), // B
};

var (go, unityMesh) = CreateMesh(Materials.ToArray());
var (go, mesh) = CreateMesh(Materials.ToArray());
var meshExportSettings = new MeshExportSettings
{
DivideVertexBuffer = true
};
var axisInverter = Axes.X.Create();

var (gltfMesh, blendShapeIndexMap) = MeshExporter.ExportMesh(glTF, bufferIndex, new MeshWithRenderer(go.transform), Materials, meshExportSettings, axisInverter);
var unityMesh = new MeshWithRenderer(go.transform);
var (gltfMesh, blendShapeIndexMap) = meshExportSettings.DivideVertexBuffer
? MeshExporter_DividedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials, axisInverter, meshExportSettings)
: MeshExporter_SharedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials,axisInverter, meshExportSettings)
;

{
var indices = glTF.GetIndices(gltfMesh.primitives[0].indices);
Expand Down