public void DividedVertexBufferTest() { var data = new ExportingGltfData(50 * 1024 * 1024); var Materials = new List <Material> { new Material(Shader.Find("Standard")), // A new Material(Shader.Find("Standard")), // B }; var(go, mesh) = CreateMesh(Materials.ToArray()); var meshExportSettings = new GltfExportSettings { DivideVertexBuffer = true }; var axisInverter = Axes.X.Create(); var unityMesh = MeshExportList.Create(go); var(gltfMesh, blendShapeIndexMap) = meshExportSettings.DivideVertexBuffer ? MeshExporter_DividedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) : MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) ; var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes); { var indices = parsed.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 = parsed.GetArrayFromAccessor <Vector3>(gltfMesh.primitives[0].attributes.POSITION); Assert.AreEqual(4, positions.Length); } { var indices = parsed.GetIndices(gltfMesh.primitives[1].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 = parsed.GetArrayFromAccessor <Vector3>(gltfMesh.primitives[1].attributes.POSITION); Assert.AreEqual(4, positions.Length); } }
public void AccessorTest() { byte[] bytes = default; using (var ms = new MemoryStream()) using (var w = new BinaryWriter(ms)) { w.Write(1.0f); w.Write(2.0f); w.Write(3.0f); w.Write(4.0f); w.Write(5.0f); w.Write(6.0f); w.Write(7.0f); w.Write(8.0f); bytes = ms.ToArray(); } var gltf = new glTF { buffers = new List <glTFBuffer> { new glTFBuffer { } }, bufferViews = new List <glTFBufferView> { new glTFBufferView { buffer = 0, byteLength = 32, byteOffset = 0, } }, accessors = new List <glTFAccessor> { new glTFAccessor { bufferView = 0, componentType = glComponentType.FLOAT, count = 2, byteOffset = 0, type = "VEC4", } } }; using (var data = GltfData.CreateFromGltfDataForTest(gltf, new ArraySegment <byte>(bytes))) { var(getter, len) = WeightsAccessor.GetAccessor(data, 0); Assert.AreEqual((1.0f, 2.0f, 3.0f, 4.0f), getter(0)); Assert.AreEqual((5.0f, 6.0f, 7.0f, 8.0f), getter(1)); } }
private static Texture2D AssignTextureToMaterialPropertyAndExportAndExtract(Texture2D srcTex, string srcImageName, string propertyName) { // Prepare var root = GameObject.CreatePrimitive(PrimitiveType.Cube); var mat = new Material(Shader.Find("Standard")); mat.SetTexture(propertyName, srcTex); root.GetComponent <MeshRenderer>().sharedMaterial = mat; // Export glTF var data = new ExportingGltfData(); using (var exporter = new gltfExporter(data, new GltfExportSettings { InverseAxis = Axes.X, ExportOnlyBlendShapePosition = false, UseSparseAccessorForMorphTarget = false, DivideVertexBuffer = false, })) { exporter.Prepare(root); exporter.Export(new EditorTextureSerializer()); } var gltf = data.GLTF; Assert.AreEqual(1, gltf.images.Count); var exportedImage = gltf.images[0]; Assert.AreEqual("image/png", exportedImage.mimeType); Assert.AreEqual(srcImageName, exportedImage.name); UnityEngine.Object.DestroyImmediate(mat); UnityEngine.Object.DestroyImmediate(root); var parsed = GltfData.CreateFromGltfDataForTest(gltf, data.BinBytes); // Extract Image to Texture2D var exportedBytes = parsed.GetBytesFromBufferView(exportedImage.bufferView).ToArray(); var exportedTexture = new Texture2D(2, 2, TextureFormat.ARGB32, mipChain: false, linear: false); Assert.IsTrue(exportedTexture.LoadImage(exportedBytes)); // Always true ? Assert.AreEqual(srcTex.width, exportedTexture.width); Assert.AreEqual(srcTex.height, exportedTexture.height); return(exportedTexture); }