From 7845f24edfa6c82d835622aa20a7432fd673642a Mon Sep 17 00:00:00 2001 From: notargs Date: Wed, 10 Nov 2021 13:45:10 +0900 Subject: [PATCH 1/2] =?UTF-8?q?List=E3=82=92IReadOnlyList=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=83=BB=E3=81=9D=E3=81=AE=E4=BB=96=E3=83=95?= =?UTF-8?q?=E3=82=A9=E3=83=BC=E3=83=9E=E3=83=83=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 231 ++++++++---------- .../Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs | 34 ++- 2 files changed, 124 insertions(+), 141 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index 7f6f774f7..d2044c136 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -1,83 +1,58 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices; using UnityEngine; namespace UniGLTF { public class MeshContext { - [Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)] - struct Float4 - { - public float x; - public float y; - public float z; - public float w; - - public Float4 One() - { - var sum = x + y + z + w; - var f = 1.0f / sum; - return new Float4 - { - x = x * f, - y = y * f, - z = z * f, - w = w * f, - }; - } - } - - string m_name; - public string name => m_name; - - readonly List m_positions = new List(); - public List Positions => m_positions; - - readonly List m_normals = new List(); - public List Normals => m_normals; + private readonly List _positions = new List(); + private readonly List _normals = new List(); + [Obsolete] private readonly List _tangents = new List(); + private readonly List _uv = new List(); + private readonly List _uv2 = new List(); + private readonly List _colors = new List(); + private readonly List _boneWeights = new List(); + private readonly List _subMeshes = new List(); + private readonly List _materialIndices = new List(); + private readonly List _blendShapes = new List(); + + public IReadOnlyList Positions => _positions; + public IReadOnlyList Normals => _normals; [Obsolete] - readonly List m_tangents = new List(); - [Obsolete] - public List Tangetns => m_tangents; + public IReadOnlyList Tangetns => _tangents; - readonly List m_uv = new List(); - public List UV => m_uv; + public IReadOnlyList UV => _uv; - readonly List m_uv2 = new List(); - public List UV2 => m_uv2; + public IReadOnlyList UV2 => _uv2; + public IReadOnlyList Colors => _colors; - readonly List m_colors = new List(); - public List Colors => m_colors; + public IReadOnlyList BoneWeights => _boneWeights; - readonly List m_boneWeights = new List(); - public List BoneWeights => m_boneWeights; + public IReadOnlyList SubMeshes => _subMeshes; - readonly List m_subMeshes = new List(); - public List SubMeshes => m_subMeshes; + public IReadOnlyList MaterialIndices => _materialIndices; - readonly List m_materialIndices = new List(); - public List MaterialIndices => m_materialIndices; + public IReadOnlyList BlendShapes => _blendShapes; - readonly List m_blendShapes = new List(); - public List BlendShapes => m_blendShapes; - BlendShape GetOrCreateBlendShape(int i) + public string Name { get; } + + private BlendShape GetOrCreateBlendShape(int i) { - if (i < m_blendShapes.Count && m_blendShapes[i] != null) + if (i < _blendShapes.Count && _blendShapes[i] != null) { - return m_blendShapes[i]; + return _blendShapes[i]; } - while (m_blendShapes.Count <= i) + while (_blendShapes.Count <= i) { - m_blendShapes.Add(null); + _blendShapes.Add(null); } var blendShape = new BlendShape(i.ToString()); - m_blendShapes[i] = blendShape; + _blendShapes[i] = blendShape; return blendShape; } @@ -85,9 +60,9 @@ namespace UniGLTF { if (string.IsNullOrEmpty(name)) { - name = string.Format("UniGLTF import#{0}", meshIndex); + name = $"UniGLTF import#{meshIndex}"; } - m_name = name; + this.Name = name; } /// @@ -96,7 +71,7 @@ namespace UniGLTF /// /// /// - static void FillZero(IList list, int fillLength) + private static void FillZero(ICollection list, int fillLength) { if (list.Count > fillLength) { @@ -108,7 +83,7 @@ namespace UniGLTF } } - public static BoneWeight NormalizeBoneWeight(BoneWeight src) + private static BoneWeight NormalizeBoneWeight(BoneWeight src) { var sum = src.weight0 + src.weight1 + src.weight2 + src.weight3; if (sum == 0) @@ -136,13 +111,13 @@ namespace UniGLTF { foreach (var prim in gltfMesh.primitives) { - var indexOffset = m_positions.Count; + var indexOffset = _positions.Count; var indexBuffer = prim.indices; // position は必ずある var positions = data.GetArrayFromAccessor(prim.attributes.POSITION); - m_positions.AddRange(positions.Select(inverter.InvertVector3)); - var fillLength = m_positions.Count; + _positions.AddRange(positions.Select(inverter.InvertVector3)); + var fillLength = _positions.Count; // normal if (prim.attributes.NORMAL != -1) @@ -152,8 +127,8 @@ namespace UniGLTF { throw new Exception("different length"); } - m_normals.AddRange(normals.Select(inverter.InvertVector3)); - FillZero(m_normals, fillLength); + _normals.AddRange(normals.Select(inverter.InvertVector3)); + FillZero(_normals, fillLength); } // uv @@ -168,14 +143,14 @@ namespace UniGLTF { #pragma warning disable 0612 // backward compatibility - m_uv.AddRange(uvs.Select(x => x.ReverseY())); - FillZero(m_uv, fillLength); + _uv.AddRange(uvs.Select(x => x.ReverseY())); + FillZero(_uv, fillLength); #pragma warning restore 0612 } else { - m_uv.AddRange(uvs.Select(x => x.ReverseUV())); - FillZero(m_uv, fillLength); + _uv.AddRange(uvs.Select(x => x.ReverseUV())); + FillZero(_uv, fillLength); } } @@ -187,8 +162,8 @@ namespace UniGLTF { throw new Exception("different length"); } - m_uv2.AddRange(uvs.Select(x => x.ReverseUV())); - FillZero(m_uv2, fillLength); + _uv2.AddRange(uvs.Select(x => x.ReverseUV())); + FillZero(_uv2, fillLength); } // color @@ -199,8 +174,8 @@ namespace UniGLTF { throw new Exception("different length"); } - m_colors.AddRange(colors); - FillZero(m_colors, fillLength); + _colors.AddRange(colors); + FillZero(_colors, fillLength); } // skin @@ -216,7 +191,7 @@ namespace UniGLTF { throw new Exception("different length"); } - for (int j = 0; j < jointsLength; ++j) + for (var j = 0; j < jointsLength; ++j) { var bw = new BoneWeight(); @@ -237,15 +212,15 @@ namespace UniGLTF bw = NormalizeBoneWeight(bw); - m_boneWeights.Add(bw); + _boneWeights.Add(bw); } - FillZero(m_boneWeights, fillLength); + FillZero(_boneWeights, fillLength); } // blendshape if (prim.targets != null && prim.targets.Count > 0) { - for (int i = 0; i < prim.targets.Count; ++i) + for (var i = 0; i < prim.targets.Count; ++i) { var primTarget = prim.targets[i]; var blendShape = GetOrCreateBlendShape(i); @@ -285,17 +260,17 @@ namespace UniGLTF var indices = (indexBuffer >= 0) ? data.GetIndices(indexBuffer) - : TriangleUtil.FlipTriangle(Enumerable.Range(0, m_positions.Count)).ToArray() // without index array + : TriangleUtil.FlipTriangle(Enumerable.Range(0, _positions.Count)).ToArray() // without index array ; - for (int i = 0; i < indices.Length; ++i) + for (var i = 0; i < indices.Length; ++i) { indices[i] += indexOffset; } - m_subMeshes.Add(indices); + _subMeshes.Add(indices); // material - m_materialIndices.Add(prim.material); + _materialIndices.Add(prim.material); } } @@ -312,12 +287,12 @@ namespace UniGLTF { // 同じVertexBufferを共有しているので先頭のモノを使う var prim = gltfMesh.primitives.First(); - m_positions.AddRange(data.GetArrayFromAccessor(prim.attributes.POSITION).SelectInplace(inverter.InvertVector3)); + _positions.AddRange(data.GetArrayFromAccessor(prim.attributes.POSITION).SelectInplace(inverter.InvertVector3)); // normal if (prim.attributes.NORMAL != -1) { - m_normals.AddRange(data.GetArrayFromAccessor(prim.attributes.NORMAL).SelectInplace(inverter.InvertVector3)); + _normals.AddRange(data.GetArrayFromAccessor(prim.attributes.NORMAL).SelectInplace(inverter.InvertVector3)); } #if false @@ -335,42 +310,45 @@ namespace UniGLTF { #pragma warning disable 0612 // backward compatibility - m_uv.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_0).SelectInplace(x => x.ReverseY())); + _uv.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_0).SelectInplace(x => x.ReverseY())); #pragma warning restore 0612 } else { - m_uv.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_0).SelectInplace(x => x.ReverseUV())); + _uv.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_0).SelectInplace(x => x.ReverseUV())); } } // uv2 if (prim.attributes.TEXCOORD_1 != -1) { - m_uv2.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_1).SelectInplace(x => x.ReverseUV())); + _uv2.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_1).SelectInplace(x => x.ReverseUV())); } // color if (prim.attributes.COLOR_0 != -1) { - if (data.GLTF.accessors[prim.attributes.COLOR_0].TypeCount == 3) + switch (data.GLTF.accessors[prim.attributes.COLOR_0].TypeCount) { - var vec3Color = data.GetArrayFromAccessor(prim.attributes.COLOR_0); - m_colors.AddRange(new Color[vec3Color.Length]); - - for (int i = 0; i < vec3Color.Length; i++) + case 3: { - Vector3 color = vec3Color[i]; - m_colors[i] = new Color(color.x, color.y, color.z); + var vec3Color = data.GetArrayFromAccessor(prim.attributes.COLOR_0); + _colors.AddRange(new Color[vec3Color.Length]); + + for (var i = 0; i < vec3Color.Length; i++) + { + var color = vec3Color[i]; + _colors[i] = new Color(color.x, color.y, color.z); + } + + break; } - } - else if (data.GLTF.accessors[prim.attributes.COLOR_0].TypeCount == 4) - { - m_colors.AddRange(data.GetArrayFromAccessor(prim.attributes.COLOR_0)); - } - else - { - throw new NotImplementedException(string.Format("unknown color type {0}", data.GLTF.accessors[prim.attributes.COLOR_0].type)); + case 4: + _colors.AddRange(data.GetArrayFromAccessor(prim.attributes.COLOR_0)); + break; + default: + throw new NotImplementedException( + $"unknown color type {data.GLTF.accessors[prim.attributes.COLOR_0].type}"); } } @@ -380,7 +358,7 @@ namespace UniGLTF var (joints0, jointsLength) = JointsAccessor.GetAccessor(data, prim.attributes.JOINTS_0); var (weights0, weightsLength) = WeightsAccessor.GetAccessor(data, prim.attributes.WEIGHTS_0); - for (int j = 0; j < jointsLength; ++j) + for (var j = 0; j < jointsLength; ++j) { var bw = new BoneWeight(); @@ -401,19 +379,19 @@ namespace UniGLTF bw = NormalizeBoneWeight(bw); - m_boneWeights.Add(bw); + _boneWeights.Add(bw); } } // blendshape if (prim.targets != null && prim.targets.Count > 0) { - m_blendShapes.AddRange(prim.targets.Select((x, i) => new BlendShape(i.ToString()))); + _blendShapes.AddRange(prim.targets.Select((x, i) => new BlendShape(i.ToString()))); for (int i = 0; i < prim.targets.Count; ++i) { //var name = string.Format("target{0}", i++); var primTarget = prim.targets[i]; - var blendShape = m_blendShapes[i]; + var blendShape = _blendShapes[i]; if (primTarget.POSITION != -1) { @@ -438,36 +416,34 @@ namespace UniGLTF { if (prim.indices == -1) { - m_subMeshes.Add(TriangleUtil.FlipTriangle(Enumerable.Range(0, m_positions.Count)).ToArray()); + _subMeshes.Add(TriangleUtil.FlipTriangle(Enumerable.Range(0, _positions.Count)).ToArray()); } else { var indices = data.GetIndices(prim.indices); - m_subMeshes.Add(indices); + _subMeshes.Add(indices); } // material - m_materialIndices.Add(prim.material); + _materialIndices.Add(prim.material); } } public void RenameBlendShape(glTFMesh gltfMesh) { - if (gltf_mesh_extras_targetNames.TryGet(gltfMesh, out List targetNames)) + if (!gltf_mesh_extras_targetNames.TryGet(gltfMesh, out var targetNames)) return; + for (var i = 0; i < BlendShapes.Count; i++) { - for (var i = 0; i < BlendShapes.Count; i++) + if (i >= targetNames.Count) { - if (i >= targetNames.Count) - { - Debug.LogWarning($"invalid primitive.extras.targetNames length"); - break; - } - BlendShapes[i].Name = targetNames[i]; + Debug.LogWarning($"invalid primitive.extras.targetNames length"); + break; } + BlendShapes[i].Name = targetNames[i]; } } - static void Truncate(List list, int maxIndex) + private static void Truncate(List list, int maxIndex) { if (list == null) { @@ -481,6 +457,15 @@ namespace UniGLTF } } + public void AddDefaultMaterial() + { + if (!_materialIndices.Any()) + { + // add default material + _materialIndices.Add(0); + } + } + // // https://github.com/vrm-c/UniVRM/issues/610 // @@ -488,17 +473,17 @@ namespace UniGLTF // public void DropUnusedVertices() { - var maxIndex = m_subMeshes.SelectMany(x => x).Max(); - Truncate(m_positions, maxIndex); - Truncate(m_normals, maxIndex); - Truncate(m_uv, maxIndex); - Truncate(m_uv2, maxIndex); - Truncate(m_colors, maxIndex); - Truncate(m_boneWeights, maxIndex); + var maxIndex = _subMeshes.SelectMany(x => x).Max(); + Truncate(_positions, maxIndex); + Truncate(_normals, maxIndex); + Truncate(_uv, maxIndex); + Truncate(_uv2, maxIndex); + Truncate(_colors, maxIndex); + Truncate(_boneWeights, maxIndex); #if false Truncate(m_tangents, maxIndex); #endif - foreach (var blendshape in m_blendShapes) + foreach (var blendshape in _blendShapes) { Truncate(blendshape.Positions, maxIndex); Truncate(blendshape.Normals, maxIndex); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs index be4350524..6656ba53d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs @@ -9,9 +9,9 @@ namespace UniGLTF { public class MeshImporter { - const float FRAME_WEIGHT = 100.0f; + private const float FrameWeight = 100.0f; - static bool HasSharedVertexBuffer(glTFMesh gltfMesh) + private static bool HasSharedVertexBuffer(glTFMesh gltfMesh) { glTFAttributes lastAttributes = null; var sharedAttributes = true; @@ -49,25 +49,23 @@ namespace UniGLTF return meshContext; } - static (Mesh, bool) _BuildMesh(MeshContext meshContext) + private static (Mesh, bool) _BuildMesh(MeshContext meshContext) { - if (!meshContext.MaterialIndices.Any()) - { - // add default material - meshContext.MaterialIndices.Add(0); - } + meshContext.AddDefaultMaterial(); //Debug.Log(prims.ToJson()); - var mesh = new Mesh(); - mesh.name = meshContext.name; + var mesh = new Mesh + { + name = meshContext.Name + }; - if (meshContext.Positions.Count > UInt16.MaxValue) + if (meshContext.Positions.Count > ushort.MaxValue) { mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; } mesh.vertices = meshContext.Positions.ToArray(); - bool recalculateNormals = false; + var recalculateNormals = false; if (meshContext.Normals != null && meshContext.Normals.Count > 0) { mesh.normals = meshContext.Normals.ToArray(); @@ -86,7 +84,7 @@ namespace UniGLTF mesh.uv2 = meshContext.UV2.ToArray(); } - bool recalculateTangents = true; + var recalculateTangents = true; #if UNIGLTF_IMPORT_TANGENTS if (meshContext.Tangents.Length > 0) { @@ -104,7 +102,7 @@ namespace UniGLTF mesh.boneWeights = meshContext.BoneWeights.ToArray(); } mesh.subMeshCount = meshContext.SubMeshes.Count; - for (int i = 0; i < meshContext.SubMeshes.Count; ++i) + for (var i = 0; i < meshContext.SubMeshes.Count; ++i) { mesh.SetTriangles(meshContext.SubMeshes[i], i); } @@ -117,7 +115,7 @@ namespace UniGLTF return (mesh, recalculateTangents); } - static async Task BuildBlendShapeAsync(IAwaitCaller awaitCaller, Mesh mesh, MeshContext meshContext, BlendShape blendShape, Vector3[] emptyVertices) + private static async Task BuildBlendShapeAsync(IAwaitCaller awaitCaller, Mesh mesh, BlendShape blendShape, Vector3[] emptyVertices) { Vector3[] positions = null; Vector3[] normals = null; @@ -132,7 +130,7 @@ namespace UniGLTF { if (blendShape.Positions.Count == mesh.vertexCount) { - mesh.AddBlendShapeFrame(blendShape.Name, FRAME_WEIGHT, + mesh.AddBlendShapeFrame(blendShape.Name, FrameWeight, blendShape.Positions.ToArray(), normals.Length == mesh.vertexCount && normals.Length == positions.Length ? normals : null, null @@ -147,7 +145,7 @@ namespace UniGLTF { // Debug.LogFormat("empty blendshape: {0}.{1}", mesh.name, blendShape.Name); // add empty blend shape for keep blend shape index - mesh.AddBlendShapeFrame(blendShape.Name, FRAME_WEIGHT, + mesh.AddBlendShapeFrame(blendShape.Name, FrameWeight, emptyVertices, null, null @@ -182,7 +180,7 @@ namespace UniGLTF var emptyVertices = new Vector3[mesh.vertexCount]; foreach (var blendShape in meshContext.BlendShapes) { - await BuildBlendShapeAsync(awaitCaller, mesh, meshContext, blendShape, emptyVertices); + await BuildBlendShapeAsync(awaitCaller, mesh, blendShape, emptyVertices); } } From da4a7035234e9c1337a64074b5d9f6928ba3a739 Mon Sep 17 00:00:00 2001 From: notargs Date: Wed, 10 Nov 2021 14:54:35 +0900 Subject: [PATCH 2/2] format --- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 44 ++++++++++++++----- .../Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs | 21 ++++++--- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index d2044c136..e223014e7 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -17,12 +17,11 @@ namespace UniGLTF private readonly List _subMeshes = new List(); private readonly List _materialIndices = new List(); private readonly List _blendShapes = new List(); - + public IReadOnlyList Positions => _positions; public IReadOnlyList Normals => _normals; - [Obsolete] - public IReadOnlyList Tangetns => _tangents; + [Obsolete] public IReadOnlyList Tangetns => _tangents; public IReadOnlyList UV => _uv; @@ -62,6 +61,7 @@ namespace UniGLTF { name = $"UniGLTF import#{meshIndex}"; } + this.Name = name; } @@ -77,6 +77,7 @@ namespace UniGLTF { throw new Exception("Impossible"); } + while (list.Count < fillLength) { list.Add(default); @@ -90,6 +91,7 @@ namespace UniGLTF { return src; } + var f = 1.0f / sum; src.weight0 *= f; src.weight1 *= f; @@ -127,6 +129,7 @@ namespace UniGLTF { throw new Exception("different length"); } + _normals.AddRange(normals.Select(inverter.InvertVector3)); FillZero(_normals, fillLength); } @@ -139,6 +142,7 @@ namespace UniGLTF { throw new Exception("different length"); } + if (data.GLTF.IsGeneratedUniGLTFAndOlder(1, 16)) { #pragma warning disable 0612 @@ -162,6 +166,7 @@ namespace UniGLTF { throw new Exception("different length"); } + _uv2.AddRange(uvs.Select(x => x.ReverseUV())); FillZero(_uv2, fillLength); } @@ -174,6 +179,7 @@ namespace UniGLTF { throw new Exception("different length"); } + _colors.AddRange(colors); FillZero(_colors, fillLength); } @@ -187,10 +193,12 @@ namespace UniGLTF { throw new Exception("different length"); } + if (weightsLength != positions.Length) { throw new Exception("different length"); } + for (var j = 0; j < jointsLength; ++j) { var bw = new BoneWeight(); @@ -214,6 +222,7 @@ namespace UniGLTF _boneWeights.Add(bw); } + FillZero(_boneWeights, fillLength); } @@ -231,9 +240,11 @@ namespace UniGLTF { throw new Exception("different length"); } + blendShape.Positions.AddRange(array.Select(inverter.InvertVector3).ToArray()); FillZero(blendShape.Positions, fillLength); } + if (primTarget.NORMAL != -1) { var array = data.GetArrayFromAccessor(primTarget.NORMAL); @@ -241,9 +252,11 @@ namespace UniGLTF { throw new Exception("different length"); } + blendShape.Normals.AddRange(array.Select(inverter.InvertVector3).ToArray()); FillZero(blendShape.Normals, fillLength); } + if (primTarget.TANGENT != -1) { var array = data.GetArrayFromAccessor(primTarget.TANGENT); @@ -251,6 +264,7 @@ namespace UniGLTF { throw new Exception("different length"); } + blendShape.Tangents.AddRange(array.Select(inverter.InvertVector3).ToArray()); FillZero(blendShape.Tangents, fillLength); } @@ -260,7 +274,8 @@ namespace UniGLTF var indices = (indexBuffer >= 0) ? data.GetIndices(indexBuffer) - : TriangleUtil.FlipTriangle(Enumerable.Range(0, _positions.Count)).ToArray() // without index array + : TriangleUtil.FlipTriangle(Enumerable.Range(0, _positions.Count)) + .ToArray() // without index array ; for (var i = 0; i < indices.Length; ++i) { @@ -287,12 +302,14 @@ namespace UniGLTF { // 同じVertexBufferを共有しているので先頭のモノを使う var prim = gltfMesh.primitives.First(); - _positions.AddRange(data.GetArrayFromAccessor(prim.attributes.POSITION).SelectInplace(inverter.InvertVector3)); + _positions.AddRange(data.GetArrayFromAccessor(prim.attributes.POSITION) + .SelectInplace(inverter.InvertVector3)); // normal if (prim.attributes.NORMAL != -1) { - _normals.AddRange(data.GetArrayFromAccessor(prim.attributes.NORMAL).SelectInplace(inverter.InvertVector3)); + _normals.AddRange(data.GetArrayFromAccessor(prim.attributes.NORMAL) + .SelectInplace(inverter.InvertVector3)); } #if false @@ -310,19 +327,22 @@ namespace UniGLTF { #pragma warning disable 0612 // backward compatibility - _uv.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_0).SelectInplace(x => x.ReverseY())); + _uv.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_0) + .SelectInplace(x => x.ReverseY())); #pragma warning restore 0612 } else { - _uv.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_0).SelectInplace(x => x.ReverseUV())); + _uv.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_0) + .SelectInplace(x => x.ReverseUV())); } } // uv2 if (prim.attributes.TEXCOORD_1 != -1) { - _uv2.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_1).SelectInplace(x => x.ReverseUV())); + _uv2.AddRange(data.GetArrayFromAccessor(prim.attributes.TEXCOORD_1) + .SelectInplace(x => x.ReverseUV())); } // color @@ -398,11 +418,13 @@ namespace UniGLTF blendShape.Positions.Assign( data.GetArrayFromAccessor(primTarget.POSITION), inverter.InvertVector3); } + if (primTarget.NORMAL != -1) { blendShape.Normals.Assign( data.GetArrayFromAccessor(primTarget.NORMAL), inverter.InvertVector3); } + if (primTarget.TANGENT != -1) { blendShape.Tangents.Assign( @@ -439,6 +461,7 @@ namespace UniGLTF Debug.LogWarning($"invalid primitive.extras.targetNames length"); break; } + BlendShapes[i].Name = targetNames[i]; } } @@ -449,6 +472,7 @@ namespace UniGLTF { return; } + var count = maxIndex + 1; if (list.Count > count) { @@ -480,7 +504,7 @@ namespace UniGLTF Truncate(_uv2, maxIndex); Truncate(_colors, maxIndex); Truncate(_boneWeights, maxIndex); -#if false +#if false Truncate(m_tangents, maxIndex); #endif foreach (var blendshape in _blendShapes) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs index 6656ba53d..8b9727eec 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs @@ -25,6 +25,7 @@ namespace UniGLTF lastAttributes = prim.attributes; } + return sharedAttributes; } @@ -79,6 +80,7 @@ namespace UniGLTF { mesh.uv = meshContext.UV.ToArray(); } + if (meshContext.UV2.Count == mesh.vertexCount) { mesh.uv2 = meshContext.UV2.ToArray(); @@ -97,10 +99,12 @@ namespace UniGLTF { mesh.colors = meshContext.Colors.ToArray(); } + if (meshContext.BoneWeights.Count > 0) { mesh.boneWeights = meshContext.BoneWeights.ToArray(); } + mesh.subMeshCount = meshContext.SubMeshes.Count; for (var i = 0; i < meshContext.SubMeshes.Count; ++i) { @@ -115,7 +119,8 @@ namespace UniGLTF return (mesh, recalculateTangents); } - private static async Task BuildBlendShapeAsync(IAwaitCaller awaitCaller, Mesh mesh, BlendShape blendShape, Vector3[] emptyVertices) + private static async Task BuildBlendShapeAsync(IAwaitCaller awaitCaller, Mesh mesh, BlendShape blendShape, + Vector3[] emptyVertices) { Vector3[] positions = null; Vector3[] normals = null; @@ -134,11 +139,13 @@ namespace UniGLTF blendShape.Positions.ToArray(), normals.Length == mesh.vertexCount && normals.Length == positions.Length ? normals : null, null - ); + ); } else { - Debug.LogWarningFormat("May be partial primitive has blendShape. Require separate mesh or extend blend shape, but not implemented: {0}", blendShape.Name); + Debug.LogWarningFormat( + "May be partial primitive has blendShape. Require separate mesh or extend blend shape, but not implemented: {0}", + blendShape.Name); } } else @@ -149,12 +156,14 @@ namespace UniGLTF emptyVertices, null, null - ); + ); } + Profiler.EndSample(); } - public static async Task BuildMeshAsync(IAwaitCaller awaitCaller, Func ctx, MeshContext meshContext) + public static async Task BuildMeshAsync(IAwaitCaller awaitCaller, Func ctx, + MeshContext meshContext) { Profiler.BeginSample("MeshImporter._BuildMesh"); var (mesh, recalculateTangents) = _BuildMesh(meshContext); @@ -191,4 +200,4 @@ namespace UniGLTF return result; } } -} +} \ No newline at end of file