From 6b9ab0db3795d2c03b7bbadb0f9b13769ba52eed Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 12 Apr 2021 14:45:17 +0900 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E8=A4=87=E9=A0=82=E7=82=B9=E3=81=8C?= =?UTF-8?q?=E5=A2=97=E3=81=88=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/UniGLTF/IO/MeshExporterDivided.cs | 50 ++++++++++++------- Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs | 12 ++--- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshExporterDivided.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshExporterDivided.cs index d54f9186e..3b876f87a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshExporterDivided.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshExporterDivided.cs @@ -42,7 +42,6 @@ namespace UniGLTF class VertexBuffer { - readonly int m_indexCount; readonly List m_positions; readonly List m_normals; readonly List m_uv; @@ -53,7 +52,6 @@ namespace UniGLTF public VertexBuffer(int reserve, Func getJointIndex) { - m_indexCount = reserve; m_positions = new List(reserve); m_normals = new List(reserve); m_uv = new List(); @@ -79,18 +77,8 @@ namespace UniGLTF m_weights.Add(new Vector4(boneWeight.weight0, boneWeight.weight1, boneWeight.weight2, boneWeight.weight3)); } - public glTFPrimitives ToGltf(glTF gltf, int bufferIndex, int materialIndex) + public glTFPrimitives ToGltf(glTF gltf, int bufferIndex, int materialIndex, IReadOnlyList indices) { - var indices = new List(m_indexCount); - // flip triangles - for (int i = 0; i < m_indexCount; i += 3) - { - // 2, 1, 0, 5, 4, 3, ... - indices.Add((uint)(i + 2)); - indices.Add((uint)(i + 1)); - indices.Add((uint)(i + 0)); - } - var indicesAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, indices.ToArray(), glBufferTarget.ELEMENT_ARRAY_BUFFER); var positionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, m_positions.ToArray(), glBufferTarget.ARRAY_BUFFER); var normalAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, m_normals.ToArray(), glBufferTarget.ARRAY_BUFFER); @@ -152,6 +140,7 @@ namespace UniGLTF Vector3[] blendShapePositions = new Vector3[mesh.vertexCount]; Vector3[] blendShapeNormals = new Vector3[mesh.vertexCount]; + var usedIndices = new List(); for (int i = 0; i < mesh.subMeshCount; ++i) { var indices = mesh.GetIndices(i); @@ -159,12 +148,26 @@ namespace UniGLTF // mesh // index の順に attributes を蓄える var buffer = new VertexBuffer(indices.Length, getJointIndex); - foreach (var k in indices) + // foreach (var k in indices) + // { + // buffer.Push(axisInverter.InvertVector3(positions[k]), axisInverter.InvertVector3(normals[k]), uv[k].ReverseUV()); + // if (getJointIndex != null) + // { + // buffer.Push(boneWeights[k]); + // } + // } + // indices から参照される頂点だけを蓄える + usedIndices.Clear(); + for (int k = 0; k < positions.Length; ++k) { - buffer.Push(axisInverter.InvertVector3(positions[k]), axisInverter.InvertVector3(normals[k]), uv[k].ReverseUV()); - if (getJointIndex != null) + if (indices.Contains(k)) { - buffer.Push(boneWeights[k]); + usedIndices.Add(k); + buffer.Push(axisInverter.InvertVector3(positions[k]), axisInverter.InvertVector3(normals[k]), uv[k].ReverseUV()); + if (getJointIndex != null) + { + buffer.Push(boneWeights[k]); + } } } @@ -174,7 +177,16 @@ namespace UniGLTF { materialIndex = unityMaterials.IndexOf(material); } - var gltfPrimitive = buffer.ToGltf(gltf, bufferIndex, materialIndex); + var indexMap = usedIndices.Select((used, index) => (used, index)).ToDictionary(x => x.used, x => (uint)x.index); + + var flipped = new List(); + for (int j = 0; j < indices.Length; j += 3) + { + flipped.Add((uint)indexMap[indices[j + 2]]); + flipped.Add((uint)indexMap[indices[j + 1]]); + flipped.Add((uint)indexMap[indices[j]]); + } + var gltfPrimitive = buffer.ToGltf(gltf, bufferIndex, materialIndex, flipped); // blendShape for (int j = 0; j < mesh.blendShapeCount; ++j) @@ -183,7 +195,7 @@ namespace UniGLTF // index の順に attributes を蓄える mesh.GetBlendShapeFrameVertices(j, 0, blendShapePositions, blendShapeNormals, null); - foreach (var k in indices) + foreach (var k in usedIndices) { blendShape.Push( axisInverter.InvertVector3(blendShapePositions[k]), diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs index 8742a1f29..f8e30b21e 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs @@ -196,12 +196,12 @@ namespace UniGLTF { var indices = glTF.GetIndices(gltfMesh.primitives[1].indices); - Assert.AreEqual(1, indices[0]); - Assert.AreEqual(2, indices[1]); - Assert.AreEqual(4, indices[2]); - Assert.AreEqual(4, indices[3]); - Assert.AreEqual(2, indices[4]); - Assert.AreEqual(3, indices[5]); + Assert.AreEqual(0, indices[0]); + Assert.AreEqual(1, indices[1]); + Assert.AreEqual(3, indices[2]); + Assert.AreEqual(3, indices[3]); + Assert.AreEqual(1, indices[4]); + Assert.AreEqual(2, indices[5]); } { var positions = glTF.GetArrayFromAccessor(gltfMesh.primitives[1].attributes.POSITION);