diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs index 840fd67f7..8742a1f29 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using NUnit.Framework; +using UnityEngine; namespace UniGLTF { @@ -11,8 +12,8 @@ namespace UniGLTF public void AccessorTest() { byte[] bytes = default; - using(var ms = new MemoryStream()) - using(var w = new BinaryWriter(ms)) + using (var ms = new MemoryStream()) + using (var w = new BinaryWriter(ms)) { w.Write(1.0f); w.Write(2.0f); @@ -28,7 +29,7 @@ namespace UniGLTF var gltf = new glTF { - buffers=new List + buffers = new List { new glTFBuffer { @@ -39,7 +40,7 @@ namespace UniGLTF new glTFBufferView{ buffer=0, byteLength=32, - byteOffset=0, + byteOffset=0, } }, accessors = new List @@ -59,5 +60,153 @@ namespace UniGLTF Assert.AreEqual((1.0f, 2.0f, 3.0f, 4.0f), getter(0)); Assert.AreEqual((5.0f, 6.0f, 7.0f, 8.0f), getter(1)); } + + /// + /// 0 1 2 + /// +-+-+ + /// |A|B| + /// +-+-+ + /// 5 4 3 + /// + /// 6 vertices + /// 4 triangles + /// 2 materials + /// + /// + static (GameObject, Mesh) CreateMesh(Material[] materials) + { + var unityMesh = new Mesh(); + unityMesh.vertices = new Vector3[] + { + new Vector3(0, 1, 0), + new Vector3(1, 1, 0), + new Vector3(2, 1, 0), + new Vector3(2, 0, 0), + new Vector3(1, 0, 0), + new Vector3(0, 0, 0), + }; + unityMesh.uv = new Vector2[] + { + new Vector2(0, 0), + new Vector2(1, 0), + new Vector2(2, 0), + new Vector2(2, 1), + new Vector2(1, 1), + new Vector2(0, 1), + }; + + unityMesh.subMeshCount = 2; + unityMesh.SetTriangles(new int[]{ + 0, 1, 5, + 5, 1, 4, + }, 0); + unityMesh.SetTriangles(new int[]{ + 1, 2, 4, + 4, 2, 3, + }, 1); + + unityMesh.RecalculateNormals(); + unityMesh.RecalculateTangents(); + + var go = new GameObject(); + go.AddComponent().sharedMaterials = materials; + go.AddComponent().sharedMesh = unityMesh; + + return (go, unityMesh); + } + + [Test] + public void SharedVertexBufferTest() + { + var glTF = new glTF(); + var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]); + var bufferIndex = glTF.AddBuffer(bytesBuffer); + + var Materials = new List{ + new Material(Shader.Find("Standard")), // A + new Material(Shader.Find("Standard")), // B + }; + + var (go, unityMesh) = CreateMesh(Materials.ToArray()); + var meshExportSettings = new MeshExportSettings + { + UseSharingVertexBuffer = true + }; + var axisInverter = Axises.X.Create(); + + var (gltfMesh, blendShapeIndexMap) = MeshExporter.ExportMesh(glTF, bufferIndex, new MeshWithRenderer(go.transform), Materials, meshExportSettings, axisInverter); + + { + var indices = glTF.GetIndices(gltfMesh.primitives[0].indices); + Assert.AreEqual(0, indices[0]); + Assert.AreEqual(1, indices[1]); + Assert.AreEqual(5, indices[2]); + Assert.AreEqual(5, indices[3]); + Assert.AreEqual(1, indices[4]); + Assert.AreEqual(4, indices[5]); + } + { + 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]); + } + + var positions = glTF.GetArrayFromAccessor(gltfMesh.primitives[0].attributes.POSITION); + Assert.AreEqual(6, positions.Length); + } + + [Test] + public void DividedVertexBufferTest() + { + var glTF = new glTF(); + var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]); + var bufferIndex = glTF.AddBuffer(bytesBuffer); + + var Materials = new List{ + new Material(Shader.Find("Standard")), // A + new Material(Shader.Find("Standard")), // B + }; + + var (go, unityMesh) = CreateMesh(Materials.ToArray()); + var meshExportSettings = new MeshExportSettings + { + UseSharingVertexBuffer = false + }; + var axisInverter = Axises.X.Create(); + + var (gltfMesh, blendShapeIndexMap) = MeshExporter.ExportMesh(glTF, bufferIndex, new MeshWithRenderer(go.transform), Materials, meshExportSettings, axisInverter); + + { + var indices = glTF.GetIndices(gltfMesh.primitives[0].indices); + 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[0].attributes.POSITION); + Assert.AreEqual(4, positions.Length); + } + + { + 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]); + } + { + var positions = glTF.GetArrayFromAccessor(gltfMesh.primitives[1].attributes.POSITION); + Assert.AreEqual(4, positions.Length); + } + } } }