mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-24 23:18:04 -05:00
gltfExporter.m_axisInverter
This commit is contained in:
parent
e6a167b906
commit
d96f47bd2a
|
|
@ -20,7 +20,7 @@ namespace UniGLTF
|
|||
window.Show();
|
||||
}
|
||||
|
||||
private static void Export(GameObject go, string path, MeshExportSettings settings)
|
||||
private static void Export(GameObject go, string path, MeshExportSettings settings, Axises inverseAxis)
|
||||
{
|
||||
var ext = Path.GetExtension(path).ToLower();
|
||||
var isGlb = false;
|
||||
|
|
@ -32,7 +32,7 @@ namespace UniGLTF
|
|||
}
|
||||
|
||||
var gltf = new glTF();
|
||||
using (var exporter = new gltfExporter(gltf))
|
||||
using (var exporter = new gltfExporter(gltf, inverseAxis))
|
||||
{
|
||||
exporter.Prepare(go);
|
||||
exporter.Export(settings);
|
||||
|
|
@ -240,10 +240,9 @@ namespace UniGLTF
|
|||
// export
|
||||
Export(root, path, new MeshExportSettings
|
||||
{
|
||||
InvertAxis = settings.InverseAxis,
|
||||
ExportOnlyBlendShapePosition = settings.DropNormal,
|
||||
UseSparseAccessorForMorphTarget = settings.Sparse,
|
||||
});
|
||||
}, settings.InverseAxis);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -286,14 +286,14 @@ namespace UniGLTF
|
|||
return new float[] { c.r, c.g, c.b, c.a };
|
||||
}
|
||||
|
||||
public static void ReverseZRecursive(this Transform root)
|
||||
public static void ReverseRecursive(this Transform root, IAxisInverter axisInverter)
|
||||
{
|
||||
var globalMap = root.Traverse().ToDictionary(x => x, x => PosRot.FromGlobalTransform(x));
|
||||
|
||||
foreach (var x in root.Traverse())
|
||||
{
|
||||
x.position = globalMap[x].Position.ReverseZ();
|
||||
x.rotation = globalMap[x].Rotation.ReverseZ();
|
||||
x.position = axisInverter.InvertVector3(globalMap[x].Position);
|
||||
x.rotation = axisInverter.InvertQuaternion(globalMap[x].Rotation);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,9 +8,6 @@ namespace UniGLTF
|
|||
[Serializable]
|
||||
public struct MeshExportSettings
|
||||
{
|
||||
// 反転軸
|
||||
public Axises InvertAxis;
|
||||
|
||||
// MorphTarget に Sparse Accessor を使う
|
||||
public bool UseSparseAccessorForMorphTarget;
|
||||
|
||||
|
|
@ -19,7 +16,6 @@ namespace UniGLTF
|
|||
|
||||
public static MeshExportSettings Default => new MeshExportSettings
|
||||
{
|
||||
InvertAxis = Axises.Z,
|
||||
UseSparseAccessorForMorphTarget = false,
|
||||
ExportOnlyBlendShapePosition = false,
|
||||
};
|
||||
|
|
@ -28,18 +24,19 @@ namespace UniGLTF
|
|||
public static class MeshExporter
|
||||
{
|
||||
static glTFMesh ExportPrimitives(glTF gltf, int bufferIndex,
|
||||
MeshWithRenderer unityMesh, List<Material> unityMaterials)
|
||||
MeshWithRenderer unityMesh, List<Material> unityMaterials,
|
||||
IAxisInverter axisInverter)
|
||||
{
|
||||
var mesh = unityMesh.Mesh;
|
||||
var materials = unityMesh.Renderer.sharedMaterials;
|
||||
var positions = mesh.vertices.Select(y => y.ReverseZ()).ToArray();
|
||||
var positions = mesh.vertices.Select(axisInverter.InvertVector3).ToArray();
|
||||
var positionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, positions, glBufferTarget.ARRAY_BUFFER);
|
||||
gltf.accessors[positionAccessorIndex].min = positions.Aggregate(positions[0], (a, b) => new Vector3(Mathf.Min(a.x, b.x), Math.Min(a.y, b.y), Mathf.Min(a.z, b.z))).ToArray();
|
||||
gltf.accessors[positionAccessorIndex].max = positions.Aggregate(positions[0], (a, b) => new Vector3(Mathf.Max(a.x, b.x), Math.Max(a.y, b.y), Mathf.Max(a.z, b.z))).ToArray();
|
||||
|
||||
var normalAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.normals.Select(y => y.normalized.ReverseZ()).ToArray(), glBufferTarget.ARRAY_BUFFER);
|
||||
var normalAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.normals.Select(y => axisInverter.InvertVector3(y.normalized)).ToArray(), glBufferTarget.ARRAY_BUFFER);
|
||||
#if GLTF_EXPORT_TANGENTS
|
||||
var tangentAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.tangents.Select(y => y.ReverseZ()).ToArray(), glBufferTarget.ARRAY_BUFFER);
|
||||
var tangentAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.tangents.Select(axisInverter.InvertVector4()).ToArray(), glBufferTarget.ARRAY_BUFFER);
|
||||
#endif
|
||||
var uvAccessorIndex0 = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.uv.Select(y => y.ReverseUV()).ToArray(), glBufferTarget.ARRAY_BUFFER);
|
||||
var uvAccessorIndex1 = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.uv2.Select(y => y.ReverseUV()).ToArray(), glBufferTarget.ARRAY_BUFFER);
|
||||
|
|
@ -166,7 +163,8 @@ namespace UniGLTF
|
|||
static gltfMorphTarget ExportMorphTarget(glTF gltf, int bufferIndex,
|
||||
Mesh mesh, int j,
|
||||
bool useSparseAccessorForMorphTarget,
|
||||
bool exportOnlyBlendShapePosition)
|
||||
bool exportOnlyBlendShapePosition,
|
||||
IAxisInverter axisInverter)
|
||||
{
|
||||
var blendShapeVertices = mesh.vertices;
|
||||
var usePosition = blendShapeVertices != null && blendShapeVertices.Length > 0;
|
||||
|
|
@ -222,7 +220,7 @@ namespace UniGLTF
|
|||
{
|
||||
sparseIndicesViewIndex = gltf.ExtendBufferAndGetViewIndex(bufferIndex, sparseIndices);
|
||||
|
||||
blendShapeVertices = sparseIndices.Select(x => blendShapeVertices[x].ReverseZ()).ToArray();
|
||||
blendShapeVertices = sparseIndices.Select(x => axisInverter.InvertVector3(blendShapeVertices[x])).ToArray();
|
||||
blendShapePositionAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, accessorCount,
|
||||
blendShapeVertices,
|
||||
sparseIndices, sparseIndicesViewIndex,
|
||||
|
|
@ -231,7 +229,7 @@ namespace UniGLTF
|
|||
|
||||
if (useNormal)
|
||||
{
|
||||
blendShapeNormals = sparseIndices.Select(x => blendShapeNormals[x].ReverseZ()).ToArray();
|
||||
blendShapeNormals = sparseIndices.Select(x => axisInverter.InvertVector3(blendShapeNormals[x])).ToArray();
|
||||
blendShapeNormalAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, accessorCount,
|
||||
blendShapeNormals,
|
||||
sparseIndices, sparseIndicesViewIndex,
|
||||
|
|
@ -240,7 +238,7 @@ namespace UniGLTF
|
|||
|
||||
if (useTangent)
|
||||
{
|
||||
blendShapeTangents = sparseIndices.Select(x => blendShapeTangents[x].ReverseZ()).ToArray();
|
||||
blendShapeTangents = sparseIndices.Select(x => axisInverter.InvertVector3(blendShapeTangents[x])).ToArray();
|
||||
blendShapeTangentAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, accessorCount,
|
||||
blendShapeTangents, sparseIndices, sparseIndicesViewIndex,
|
||||
glBufferTarget.NONE);
|
||||
|
|
@ -248,7 +246,7 @@ namespace UniGLTF
|
|||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < blendShapeVertices.Length; ++i) blendShapeVertices[i] = blendShapeVertices[i].ReverseZ();
|
||||
for (int i = 0; i < blendShapeVertices.Length; ++i) blendShapeVertices[i] = axisInverter.InvertVector3(blendShapeVertices[i]);
|
||||
if (usePosition)
|
||||
{
|
||||
blendShapePositionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex,
|
||||
|
|
@ -258,7 +256,7 @@ namespace UniGLTF
|
|||
|
||||
if (useNormal)
|
||||
{
|
||||
for (int i = 0; i < blendShapeNormals.Length; ++i) blendShapeNormals[i] = blendShapeNormals[i].ReverseZ();
|
||||
for (int i = 0; i < blendShapeNormals.Length; ++i) blendShapeNormals[i] = axisInverter.InvertVector3(blendShapeNormals[i]);
|
||||
blendShapeNormalAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex,
|
||||
blendShapeNormals,
|
||||
glBufferTarget.ARRAY_BUFFER);
|
||||
|
|
@ -266,7 +264,7 @@ namespace UniGLTF
|
|||
|
||||
if (useTangent)
|
||||
{
|
||||
for (int i = 0; i < blendShapeTangents.Length; ++i) blendShapeTangents[i] = blendShapeTangents[i].ReverseZ();
|
||||
for (int i = 0; i < blendShapeTangents.Length; ++i) blendShapeTangents[i] = axisInverter.InvertVector3(blendShapeTangents[i]);
|
||||
blendShapeTangentAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex,
|
||||
blendShapeTangents,
|
||||
glBufferTarget.ARRAY_BUFFER);
|
||||
|
|
@ -289,12 +287,12 @@ namespace UniGLTF
|
|||
|
||||
public static IEnumerable<(Mesh, glTFMesh, Dictionary<int, int>)> ExportMeshes(glTF gltf, int bufferIndex,
|
||||
List<MeshWithRenderer> unityMeshes, List<Material> unityMaterials,
|
||||
MeshExportSettings settings)
|
||||
MeshExportSettings settings, IAxisInverter axisInverter)
|
||||
{
|
||||
foreach (var unityMesh in unityMeshes)
|
||||
{
|
||||
var gltfMesh = ExportPrimitives(gltf, bufferIndex,
|
||||
unityMesh, unityMaterials);
|
||||
unityMesh, unityMaterials, axisInverter);
|
||||
|
||||
var targetNames = new List<string>();
|
||||
|
||||
|
|
@ -305,7 +303,7 @@ namespace UniGLTF
|
|||
var morphTarget = ExportMorphTarget(gltf, bufferIndex,
|
||||
unityMesh.Mesh, j,
|
||||
settings.UseSparseAccessorForMorphTarget,
|
||||
settings.ExportOnlyBlendShapePosition);
|
||||
settings.ExportOnlyBlendShapePosition, axisInverter);
|
||||
if (morphTarget.POSITION < 0 && morphTarget.NORMAL < 0 && morphTarget.TANGENT < 0)
|
||||
{
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -88,7 +88,9 @@ namespace UniGLTF
|
|||
}
|
||||
}
|
||||
|
||||
public gltfExporter(glTF gltf)
|
||||
IAxisInverter m_axisInverter;
|
||||
|
||||
public gltfExporter(glTF gltf, Axises invertAxis = Axises.Z)
|
||||
{
|
||||
glTF = gltf;
|
||||
|
||||
|
|
@ -99,15 +101,17 @@ namespace UniGLTF
|
|||
generator = "UniGLTF-" + UniGLTFVersion.VERSION,
|
||||
version = "2.0",
|
||||
};
|
||||
|
||||
m_axisInverter = invertAxis.Create();
|
||||
}
|
||||
|
||||
GameObject m_tmpParent = null;
|
||||
|
||||
public virtual void Prepare(GameObject go)
|
||||
{
|
||||
// コピーを作って、Z軸を反転することで左手系を右手系に変換する
|
||||
// コピーを作って左手系を右手系に変換する
|
||||
Copy = GameObject.Instantiate(go);
|
||||
Copy.transform.ReverseZRecursive();
|
||||
Copy.transform.ReverseRecursive(m_axisInverter);
|
||||
|
||||
// Export の root は gltf の scene になるので、
|
||||
// エクスポート対象が単一の GameObject の場合に、
|
||||
|
|
@ -216,7 +220,7 @@ namespace UniGLTF
|
|||
|
||||
MeshBlendShapeIndexMap = new Dictionary<Mesh, Dictionary<int, int>>();
|
||||
foreach (var (mesh, gltfMesh, blendShapeIndexMap) in MeshExporter.ExportMeshes(
|
||||
glTF, bufferIndex, unityMeshes, Materials, meshExportSettings))
|
||||
glTF, bufferIndex, unityMeshes, Materials, meshExportSettings, m_axisInverter))
|
||||
{
|
||||
glTF.meshes.Add(gltfMesh);
|
||||
if (!MeshBlendShapeIndexMap.ContainsKey(mesh))
|
||||
|
|
@ -243,7 +247,7 @@ namespace UniGLTF
|
|||
|
||||
foreach (var x in unitySkins)
|
||||
{
|
||||
var matrices = x.GetBindPoses().Select(y => y.ReverseZ()).ToArray();
|
||||
var matrices = x.GetBindPoses().Select(m_axisInverter.InvertMat4).ToArray();
|
||||
var accessor = glTF.ExtendBufferAndGetAccessorIndex(bufferIndex, matrices, glBufferTarget.NONE);
|
||||
|
||||
var renderer = x.Renderer as SkinnedMeshRenderer;
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace VRM
|
|||
|
||||
public readonly VRM.glTF_VRM_extensions VRM = new glTF_VRM_extensions();
|
||||
|
||||
public VRMExporter(glTF gltf) : base(gltf)
|
||||
public VRMExporter(glTF gltf) : base(gltf, Axises.Z)
|
||||
{
|
||||
gltf.extensionsUsed.Add(glTF_VRM_extensions.ExtensionName);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user