From 06608a7aa61ecf06b77e9d5ddd8c64bc0d8dad84 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 4 Jan 2021 16:42:38 +0900 Subject: [PATCH 1/2] =?UTF-8?q?primitives=20attributes=20=E3=81=8C?= =?UTF-8?q?=E6=9C=89=E3=81=A3=E3=81=9F=E3=82=8A=E7=84=A1=E3=81=8B=E3=81=A3?= =?UTF-8?q?=E3=81=9F=E3=82=8A=E3=81=99=E3=82=8B=E5=A0=B4=E5=90=88=E3=81=AB?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C(=E4=BE=8B=E3=81=88=E3=81=B0NORMAL)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #642 --- .../Runtime/UniGLTF/IO/MeshImporter.cs | 112 +++++++++++++----- 1 file changed, 83 insertions(+), 29 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs index b56fb644ef..c04ec52903 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs @@ -80,11 +80,21 @@ public MeshContext(string name, int meshIndex) m_name = name; } - void FillZero(IList list) + /// + /// 指定した長さを0で埋める + /// + /// + /// + /// + static void FillZero(IList list, int fillLength) { - if (list.Count != m_positions.Count) + if (list.Count > fillLength) { - throw new NotImplementedException(); + throw new Exception("ありえない"); + } + while (list.Count < fillLength) + { + list.Add(default); } } @@ -105,64 +115,96 @@ public void ImportMeshIndependentVertexBuffer(ImporterContext ctx, glTFMesh gltf var indexBuffer = prim.indices; // position は必ずある - var positionCount = m_positions.Count; - m_positions.AddRange(ctx.GLTF.GetArrayFromAccessor(prim.attributes.POSITION).Select(x => x.ReverseZ())); - positionCount = m_positions.Count - positionCount; + var positions = ctx.GLTF.GetArrayFromAccessor(prim.attributes.POSITION); + var fillLength = m_positions.Count; + m_positions.AddRange(positions.Select(x => x.ReverseZ())); // normal if (prim.attributes.NORMAL != -1) { - FillZero(m_normals); - m_normals.AddRange(ctx.GLTF.GetArrayFromAccessor(prim.attributes.NORMAL).Select(x => x.ReverseZ())); + var normals = ctx.GLTF.GetArrayFromAccessor(prim.attributes.NORMAL); + if (normals.Length != positions.Length) + { + throw new Exception("different length"); + } + FillZero(m_normals, fillLength); + m_normals.AddRange(normals.Select(x => x.ReverseZ())); } #if false if (prim.attributes.TANGENT != -1) { - FillZero(tangetns); - tangents.AddRange(ctx.GLTF.GetArrayFromAccessor(prim.attributes.TANGENT).Select(x => x.ReverseZ())); + var tangents = ctx.GLTF.GetArrayFromAccessor(prim.attributes.TANGENT); + if (tangents.Length != positions.Length) + { + throw new Exception("different length"); + } + FillZero(tangetns, fillLength); + tangents.AddRange(.Select(x => x.ReverseZ())); } #endif // uv if (prim.attributes.TEXCOORD_0 != -1) { - FillZero(m_uv); + var uvs = ctx.GLTF.GetArrayFromAccessor(prim.attributes.TEXCOORD_0); + if (uvs.Length != positions.Length) + { + throw new Exception("different length"); + } if (ctx.IsGeneratedUniGLTFAndOlder(1, 16)) { #pragma warning disable 0612 // backward compatibility - m_uv.AddRange(ctx.GLTF.GetArrayFromAccessor(prim.attributes.TEXCOORD_0).Select(x => x.ReverseY())); + FillZero(m_uv, fillLength); + m_uv.AddRange(uvs.Select(x => x.ReverseY())); #pragma warning restore 0612 } else { - m_uv.AddRange(ctx.GLTF.GetArrayFromAccessor(prim.attributes.TEXCOORD_0).Select(x => x.ReverseUV())); + FillZero(m_uv, fillLength); + m_uv.AddRange(uvs.Select(x => x.ReverseUV())); } } // uv2 if (prim.attributes.TEXCOORD_1 != -1) { - FillZero(m_uv2); - m_uv2.AddRange(ctx.GLTF.GetArrayFromAccessor(prim.attributes.TEXCOORD_1).Select(x => x.ReverseUV())); + var uvs = ctx.GLTF.GetArrayFromAccessor(prim.attributes.TEXCOORD_1); + if (uvs.Length != positions.Length) + { + throw new Exception("different length"); + } + FillZero(m_uv2, fillLength); + m_uv2.AddRange(uvs.Select(x => x.ReverseUV())); } // color if (prim.attributes.COLOR_0 != -1) { - FillZero(m_colors); - m_colors.AddRange(ctx.GLTF.GetArrayFromAccessor(prim.attributes.COLOR_0)); + var colors = ctx.GLTF.GetArrayFromAccessor(prim.attributes.COLOR_0); + if (colors.Length != positions.Length) + { + throw new Exception("different length"); + } + FillZero(m_colors, fillLength); + m_colors.AddRange(colors); } // skin if (prim.attributes.JOINTS_0 != -1 && prim.attributes.WEIGHTS_0 != -1) { - FillZero(m_boneWeights); - var joints0 = ctx.GLTF.GetArrayFromAccessor(prim.attributes.JOINTS_0); // uint4 var weights0 = ctx.GLTF.GetArrayFromAccessor(prim.attributes.WEIGHTS_0).Select(x => x.One()).ToArray(); - + if (joints0.Length != positions.Length) + { + throw new Exception("different length"); + } + if (weights0.Length != positions.Length) + { + throw new Exception("different length"); + } + FillZero(m_boneWeights, fillLength); for (int j = 0; j < joints0.Length; ++j) { var bw = new BoneWeight(); @@ -192,21 +234,33 @@ public void ImportMeshIndependentVertexBuffer(ImporterContext ctx, glTFMesh gltf var blendShape = new BlendShape(i.ToString()); if (primTarget.POSITION != -1) { - FillZero(blendShape.Positions); - blendShape.Positions.AddRange( - ctx.GLTF.GetArrayFromAccessor(primTarget.POSITION).Select(x => x.ReverseZ()).ToArray()); + var array = ctx.GLTF.GetArrayFromAccessor(primTarget.POSITION); + if (array.Length != positions.Length) + { + throw new Exception("different length"); + } + FillZero(blendShape.Positions, fillLength); + blendShape.Positions.AddRange(array.Select(x => x.ReverseZ()).ToArray()); } if (primTarget.NORMAL != -1) { - FillZero(blendShape.Normals); - blendShape.Normals.AddRange( - ctx.GLTF.GetArrayFromAccessor(primTarget.NORMAL).Select(x => x.ReverseZ()).ToArray()); + var array = ctx.GLTF.GetArrayFromAccessor(primTarget.NORMAL); + if (array.Length != positions.Length) + { + throw new Exception("different length"); + } + FillZero(blendShape.Normals, fillLength); + blendShape.Normals.AddRange(array.Select(x => x.ReverseZ()).ToArray()); } if (primTarget.TANGENT != -1) { - FillZero(blendShape.Tangents); - blendShape.Tangents.AddRange( - ctx.GLTF.GetArrayFromAccessor(primTarget.TANGENT).Select(x => x.ReverseZ()).ToArray()); + var array = ctx.GLTF.GetArrayFromAccessor(primTarget.TANGENT); + if (array.Length != positions.Length) + { + throw new Exception("different length"); + } + FillZero(blendShape.Tangents, fillLength); + blendShape.Tangents.AddRange(array.Select(x => x.ReverseZ()).ToArray()); } m_blendShapes.Add(blendShape); } From f5452e6c139baa44e7251a348e67560db120943d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 4 Jan 2021 17:25:26 +0900 Subject: [PATCH 2/2] English comment --- Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs index c04ec52903..cc2667b458 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs @@ -81,7 +81,7 @@ public MeshContext(string name, int meshIndex) } /// - /// 指定した長さを0で埋める + /// Fill list with 0s with the specified length /// /// /// @@ -90,7 +90,7 @@ static void FillZero(IList list, int fillLength) { if (list.Count > fillLength) { - throw new Exception("ありえない"); + throw new Exception("Impossible"); } while (list.Count < fillLength) {