public static void CompareBinaryFiles(string filename) { var testFile = Path.Combine(PathHelper.FilesPath, filename); var originalData = File.ReadAllBytes(testFile); var isBinary = FbxIO.IsBinaryFbx(testFile); Assert.True(isBinary); var documentNode = FbxIO.Read(testFile); using (var newStream = new MemoryStream()) { FbxIO.WriteBinary(documentNode, newStream); var newData = newStream.ToArray(); Assert.True(newData.Length <= originalData.Length, $"Unexpected size comparisson"); var identical = true; for (var i = 0; i < newData.Length; i++) { if (originalData[i] != newData[i]) { identical = false; break; } } Assert.True(identical, $"Files data did not match as expected"); } }
public static void CompareAsciiFiles(string filename) { var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); var testFile = Path.Combine(path, "Files", filename); var isBinary = FbxIO.IsBinaryFbx(testFile); Assert.False(isBinary); var documentNode = FbxIO.Read(testFile); using (var tempStream = new MemoryStream()) { FbxIO.WriteAscii(documentNode, tempStream); tempStream.Position = 0; var originalBuffer = string.Empty; using (StreamReader originalStream = new StreamReader(testFile)) { while (originalStream.EndOfStream) { originalBuffer += FilterLine(originalStream.ReadLine()); } } var newBuffer = string.Empty; using (StreamReader newStream = new StreamReader(tempStream)) { while (newStream.EndOfStream) { newBuffer += FilterLine(newStream.ReadLine()); } } Assert.True(originalBuffer.Length == newBuffer.Length, $"Unexpected size comparisson"); var identical = true; for (var i = 0; i < newBuffer.Length; i++) { if (originalBuffer[i] != newBuffer[i]) { identical = false; break; } } Assert.True(identical, $"Files data did not match as expected"); } }
public static void LoadFbx(string path, ref Vector3[] positions, ref Vector3[] normals, ref Vector2[] texCoords, ref int[] vertexIndices) { var isBinary = FbxIO.IsBinaryFbx(path); var documentNode = FbxIO.Read(path); // Scale factor usually 1 or 2.54 var scaleFactor = documentNode.GetScaleFactor(); var geometryIds = documentNode.GetGeometryIds(); var fbxIndexer = new FbxIndexer(); foreach (var geometryId in geometryIds) { vertexIndices = documentNode.GetVertexIndices(geometryId); long[] normalLayerIndices = documentNode.GetLayerIndices(geometryId, FbxLayerElementType.Normal); long[] tangentLayerIndices = documentNode.GetLayerIndices(geometryId, FbxLayerElementType.Tangent); long[] binormalLayerIndices = documentNode.GetLayerIndices(geometryId, FbxLayerElementType.Binormal); long[] texCoordLayerIndices = documentNode.GetLayerIndices(geometryId, FbxLayerElementType.TexCoord); long[] materialLayerIndices = documentNode.GetLayerIndices(geometryId, FbxLayerElementType.Material); positions = documentNode.GetPositions(geometryId, vertexIndices); normals = documentNode.GetNormals(geometryId, vertexIndices, normalLayerIndices[0]); texCoords = documentNode.GetTexCoords(geometryId, vertexIndices, texCoordLayerIndices[0]); int[] materials = documentNode.GetMaterials(geometryId, vertexIndices, materialLayerIndices[0]); bool hasNormals = documentNode.GetGeometryHasNormals(geometryId); bool hasTexCoords = documentNode.GetGeometryHasTexCoords(geometryId); bool hasTangents = documentNode.GetGeometryHasTangents(geometryId); bool hasBinormals = documentNode.GetGeometryHasBinormals(geometryId); bool hasMaterials = documentNode.GetGeometryHasMaterials(geometryId); Vector3[] tangents = Array.Empty <Vector3>(); Vector3[] binormals = Array.Empty <Vector3>(); if (hasTangents) { tangents = documentNode.GetTangents(geometryId, vertexIndices, tangentLayerIndices[0]); } if (hasBinormals) { binormals = documentNode.GetBinormals(geometryId, vertexIndices, binormalLayerIndices[0]); } for (var i = 0; i < positions.Length; i++) { var vertex = new FbxVertex { Position = positions[i], Normal = hasNormals ? normals[i] : new Vector3(), Tangent = hasTangents ? tangents[i] : new Vector3(), Binormal = hasBinormals ? binormals[i] : new Vector3(), TexCoord = hasTexCoords ? texCoords[i] : new Vector2() }; var materialId = hasMaterials ? materials[i] : 0; fbxIndexer.AddVertex(vertex, materialId, 0); } } }
public void TestFbx(string filename, bool expectedIsBinary, double expectedScaleFacor, bool expectedHasTexCoord, bool expectedHasTangent, bool expectedHasBinormal) { var testFile = Path.Combine(PathHelper.FilesPath, filename); Assert.True(expectedIsBinary == FbxIO.IsBinaryFbx(testFile), $"IsBinaryFbx expected {expectedIsBinary}"); var documentNode = FbxIO.Read(testFile, ErrorLevel.Strict); var scaleFactor = documentNode.GetScaleFactor(); Assert.True(expectedScaleFacor == scaleFactor, $"ScaleFactor expected {expectedScaleFacor}"); var materialIds = documentNode.GetMaterialIds(); var geometryIds = documentNode.GetGeometryIds(); Assert.True(geometryIds.Length > 0); var fbxIndexer = new FbxIndexer(); foreach (var geometryId in geometryIds) { var vertexIndices = documentNode.GetVertexIndices(geometryId); var positions = documentNode.GetPositions(geometryId, vertexIndices); var normals = documentNode.GetNormals(geometryId, vertexIndices); var tangents = documentNode.GetTangents(geometryId, vertexIndices); var binormals = documentNode.GetBinormals(geometryId, vertexIndices); var texCoords = documentNode.GetTexCoords(geometryId, vertexIndices); var materials = documentNode.GetMaterials(geometryId, vertexIndices); var hasNormals = documentNode.GetGeometryHasNormals(geometryId); var hasTexCoords = documentNode.GetGeometryHasTexCoords(geometryId); Assert.True(expectedHasTexCoord == hasTexCoords, $"HasTexCoord expected {expectedHasTexCoord}"); var hasTangents = documentNode.GetGeometryHasTangents(geometryId); Assert.True(expectedHasTangent == hasTangents, $"HasTangent expected {expectedHasTangent}"); var hasBinormals = documentNode.GetGeometryHasBinormals(geometryId); Assert.True(expectedHasBinormal == hasBinormals, $"HasBinormal expected {expectedHasBinormal}"); var hasMaterials = documentNode.GetGeometryHasMaterials(geometryId); for (var i = 0; i < positions.Length; i++) { var vertex = new FbxVertex { Position = positions[i], Normal = hasNormals ? normals[i] : new Vector3(), Tangent = hasTangents ? tangents[i] : new Vector3(), Binormal = hasBinormals ? binormals[i] : new Vector3(), TexCoord = hasTexCoords ? texCoords[i] : new Vector2() }; var materialId = hasMaterials ? materials[i] : 0; fbxIndexer.AddVertex(vertex, materialId); } } foreach (var materialId in materialIds) { var materialName = documentNode.GetMaterialName(materialId); var diffuseColor = documentNode.GetMaterialDiffuseColor(materialId); fbxIndexer.Index(materialId, out var indexedVertices, out var indexedIndices); } }