From d3243fcda738bc3cd7ad5268fbf2a6b894c3f0cc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 14 Jul 2022 15:32:38 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HasNormal が true にならない --- .../Runtime/UniGLTF/IO/MeshIO/MeshData.cs | 11 +- .../Runtime/UniGLTF/IO/MeshIO/MeshVertex.cs | 2 +- .../UniGLTF/IO/MeshIO/SkinnedMeshVertex.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs | 210 ++++++++++++++++++ .../Tests/UniGLTF/MeshDataTests.cs.meta | 11 + 5 files changed, 229 insertions(+), 7 deletions(-) create mode 100644 Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs create mode 100644 Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs index f73c6de45..cb54df9ca 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs @@ -8,7 +8,7 @@ using UnityEngine.Rendering; namespace UniGLTF { - internal class MeshData : IDisposable + public class MeshData : IDisposable { private NativeArray _vertices; public NativeArray Vertices => _vertices.GetSubArray(0, _currentVertexCount); @@ -63,12 +63,13 @@ namespace UniGLTF /// バッファ共有方式(vrm-0.x)の判定。 /// import の後方互換性のためで、vrm-1.0 export では使いません。 /// - /// * バッファ共用方式は VertexBuffer が同じでSubMeshの index buffer がスライドしていく方式 + /// バッファ共用方式は連結済みの VertexBuffer を共有して、SubMeshの index buffer による参照がスライドしていく方式 + /// /// * バッファがひとつのとき /// * すべての primitive の attribute が 同一の accessor を使用している時 /// /// - private static bool HasSharedVertexBuffer(glTFMesh gltfMesh) + public static bool HasSharedVertexBuffer(glTFMesh gltfMesh) { glTFAttributes lastAttributes = null; foreach (var prim in gltfMesh.primitives) @@ -324,7 +325,7 @@ namespace UniGLTF var texCoords1 = primitives.GetTexCoords1(data, positions.Length); var colors = primitives.GetColors(data, positions.Length); var skinning = SkinningInfo.Create(data, gltfMesh, primitives); - AssignBoneWeight = skinning.ShouldSetRendererNodeAsBone ; + AssignBoneWeight = skinning.ShouldSetRendererNodeAsBone; CheckAttributeUsages(primitives); @@ -456,7 +457,7 @@ namespace UniGLTF var texCoords1 = primitives.GetTexCoords1(data, positions.Length); var colors = primitives.GetColors(data, positions.Length); var skinning = SkinningInfo.Create(data, gltfMesh, primitives); - AssignBoneWeight = skinning.ShouldSetRendererNodeAsBone ; + AssignBoneWeight = skinning.ShouldSetRendererNodeAsBone; CheckAttributeUsages(primitives); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshVertex.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshVertex.cs index 5619fa84d..5a8bc0f45 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshVertex.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshVertex.cs @@ -11,7 +11,7 @@ namespace UniGLTF /// そのままGPUにアップロードされる /// [Serializable, StructLayout(LayoutKind.Sequential)] - internal readonly struct MeshVertex + public readonly struct MeshVertex { private readonly Vector3 _position; private readonly Vector3 _normal; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinnedMeshVertex.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinnedMeshVertex.cs index 5850aff65..a59d0fff9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinnedMeshVertex.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinnedMeshVertex.cs @@ -9,7 +9,7 @@ namespace UniGLTF /// そのままGPUにアップロードされる /// [Serializable, StructLayout(LayoutKind.Sequential)] - internal readonly struct SkinnedMeshVertex + public readonly struct SkinnedMeshVertex { private readonly float _boneWeight0; private readonly float _boneWeight1; diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs new file mode 100644 index 000000000..fa255e424 --- /dev/null +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs @@ -0,0 +1,210 @@ +using System; +using NUnit.Framework; +using UnityEngine; + +namespace UniGLTF +{ + public class MeshDataTests + { + /// + /// shared + /// 3 2 + /// +-+ + /// |/| + /// +-+ + /// 0 1 + /// + /// divided + /// 2 + /// + + /// /| + /// +-+ + /// 0 1 + /// 4 3 + /// +-+ + /// |/ + /// + + /// 5 + /// + static byte[] CreateTestData(bool shared, bool hasNormal) + { + var data = new ExportingGltfData(); + data.Gltf.asset.version = "2.0"; + var mesh = new glTFMesh(); + data.Gltf.meshes.Add(mesh); + + if (shared) + { + var positions = new Vector3[] + { + new Vector3(), + new Vector3(), + new Vector3(), + new Vector3(), + }; + var normals = new Vector3[] + { + new Vector3(), + new Vector3(), + new Vector3(), + new Vector3(), + }; + + var position = data.ExtendBufferAndGetAccessorIndex(positions); + var normal = data.ExtendBufferAndGetAccessorIndex(normals); + { + var prim = new glTFPrimitives + { + attributes = new glTFAttributes + { + POSITION = position, + }, + indices = data.ExtendBufferAndGetAccessorIndex(new uint[] { 0, 1, 2 }), + }; + mesh.primitives.Add(prim); + if (hasNormal) + { + prim.attributes.NORMAL = normal; + } + } + { + var prim = new glTFPrimitives + { + attributes = new glTFAttributes + { + POSITION = position, + }, + indices = data.ExtendBufferAndGetAccessorIndex(new uint[] { 2, 3, 0 }), + }; + mesh.primitives.Add(prim); + if (hasNormal) + { + prim.attributes.NORMAL = normal; + } + } + } + else + { + { + var positions = new Vector3[] + { + new Vector3(), + new Vector3(), + new Vector3(), + }; + var position = data.ExtendBufferAndGetAccessorIndex(positions); + var prim = new glTFPrimitives + { + attributes = new glTFAttributes + { + POSITION = position, + }, + indices = data.ExtendBufferAndGetAccessorIndex(new uint[] { 0, 1, 2 }), + }; + if (hasNormal) + { + var normals = new Vector3[] + { + new Vector3(), + new Vector3(), + new Vector3(), + }; + var normal = data.ExtendBufferAndGetAccessorIndex(normals); + prim.attributes.NORMAL = normal; + } + mesh.primitives.Add(prim); + } + { + var positions = new Vector3[] + { + new Vector3(), + new Vector3(), + new Vector3(), + }; + var position = data.ExtendBufferAndGetAccessorIndex(positions); + var prim = new glTFPrimitives + { + attributes = new glTFAttributes + { + POSITION = position, + }, + indices = data.ExtendBufferAndGetAccessorIndex(new uint[] { 0, 1, 2 }), + }; + if (hasNormal) + { + var normals = new Vector3[] + { + new Vector3(), + new Vector3(), + new Vector3(), + }; + var normal = data.ExtendBufferAndGetAccessorIndex(normals); + prim.attributes.NORMAL = normal; + } + mesh.primitives.Add(prim); + } + } + return data.ToGlbBytes(); + } + + [Test] + public void SharedHasNormalTest() + { + var glb = CreateTestData(true, true); + using (var parsed = new GlbBinaryParser(glb, "test").Parse()) + { + Assert.True(MeshData.HasSharedVertexBuffer(parsed.GLTF.meshes[0])); + using (var data = new MeshData(6, 6)) + { + data.LoadFromGltf(parsed, 0, new ReverseZ()); + Assert.True(data.HasNormal); + } + } + } + + [Test] + public void SharedNotHasNormalTest() + { + var glb = CreateTestData(true, false); + using (var parsed = new GlbBinaryParser(glb, "test").Parse()) + { + Assert.True(MeshData.HasSharedVertexBuffer(parsed.GLTF.meshes[0])); + using (var data = new MeshData(6, 6)) + { + data.LoadFromGltf(parsed, 0, new ReverseZ()); + Assert.False(data.HasNormal); + } + } + } + + [Test] + public void DividedHasNormalTest() + { + var glb = CreateTestData(false, true); + using (var parsed = new GlbBinaryParser(glb, "test").Parse()) + { + Assert.False(MeshData.HasSharedVertexBuffer(parsed.GLTF.meshes[0])); + using (var data = new MeshData(6, 6)) + { + data.LoadFromGltf(parsed, 0, new ReverseZ()); + Assert.True(data.HasNormal); + } + } + } + + [Test] + public void DividedNotHasNormalTest() + { + var glb = CreateTestData(false, false); + using (var parsed = new GlbBinaryParser(glb, "test").Parse()) + { + Assert.False(MeshData.HasSharedVertexBuffer(parsed.GLTF.meshes[0])); + using (var data = new MeshData(6, 6)) + { + data.LoadFromGltf(parsed, 0, new ReverseZ()); + Assert.False(data.HasNormal); + } + } + } + } +} diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs.meta b/Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs.meta new file mode 100644 index 000000000..abeec9770 --- /dev/null +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e1e46a1dd2717854194c1b10ec096bef +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: