public static void QuadAndCopyTest() { // Serialize a triangle g3d as bytes and read it back. var vertices = new[] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 1), new Vector3(1, 1, 1) }; var indices = new[] { 0, 1, 2, 3 }; var materialIndices = new[] { 5 }; var g3d = new G3DBuilder() .AddVertices(vertices.ToIArray()) .AddIndices(indices.ToIArray()) .Add(materialIndices.ToIArray().ToFaceMaterialAttribute()) .ToG3D(); var bytes = g3d.WriteToBytes(); var g = G3D.Read(bytes); Assert.IsNotNull(g); Assert.AreEqual(4, g.NumCornersPerFace); Assert.AreEqual(4, g.NumVertices); Assert.AreEqual(4, g.NumCorners); Assert.AreEqual(1, g.NumFaces); Assert.AreEqual(0, g.NumMeshes); Assert.AreEqual(0, g.NumInstances); Assert.AreEqual(vertices, g.Vertices.ToArray()); Assert.AreEqual(indices, g.Indices.ToArray()); Assert.AreEqual(materialIndices, g.FaceMaterials.ToArray()); var g2 = g.TriangulateQuadMesh(); Assert.AreEqual(3, g2.NumCornersPerFace); Assert.AreEqual(4, g2.NumVertices); Assert.AreEqual(6, g2.NumCorners); Assert.AreEqual(2, g2.NumFaces); Assert.AreEqual(0, g2.NumMeshes); Assert.AreEqual(0, g2.NumInstances); Assert.AreEqual(vertices, g2.GetAttributeDataPosition().ToArray()); Assert.AreEqual(new[] { 0, 1, 2, 0, 2, 3 }, g2.GetAttributeDataIndex().ToArray()); Assert.AreEqual(new[] { 5, 5 }, g2.GetAttributeDataFaceMaterial().ToArray()); g2 = g2.CopyFaces(1, 1); Assert.AreEqual(3, g2.NumCornersPerFace); Assert.AreEqual(4, g2.NumVertices); Assert.AreEqual(3, g2.NumCorners); Assert.AreEqual(1, g2.NumFaces); Assert.AreEqual(vertices, g2.GetAttributeDataPosition().ToArray()); Assert.AreEqual(new[] { 0, 2, 3 }, g2.GetAttributeDataIndex().ToArray()); Assert.AreEqual(new[] { 5 }, g2.GetAttributeDataFaceMaterial().ToArray()); }
public static void TriangleTest() { // Serialize a triangle g3d as bytes and read it back. var vertices = new[] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 1) }; var indices = new[] { 0, 1, 2 }; var materialIndices = new[] { 5 }; var g3d = new G3DBuilder() .AddVertices(vertices.ToIArray()) .AddIndices(indices.ToIArray()) .Add(materialIndices.ToIArray().ToFaceMaterialAttribute()) .ToG3D(); var bytes = g3d.WriteToBytes(); var g = G3D.Read(bytes); Assert.IsNotNull(g); Assert.AreEqual(3, g.NumVertices); Assert.AreEqual(3, g.NumCorners); Assert.AreEqual(1, g.NumFaces); Assert.AreEqual(3, g.NumCornersPerFace); Assert.AreEqual(0, g.NumMeshes); Assert.AreEqual(0, g.NumInstances); Assert.AreEqual(vertices, g.Vertices.ToArray()); Assert.AreEqual(indices, g.Indices.ToArray()); Assert.AreEqual(materialIndices, g.FaceMaterials.ToArray()); }
public static void ReadG3DFiles() { foreach (var f in Directory.GetFiles(TestOutputFolder)) { var g3d = G3D.Read(f); G3dTestUtils.OutputStats(g3d); } }
public override void OnImportAsset(AssetImportContext ctx) { Debug.Log($"Importing G3D file: {ctx.assetPath}"); ImportHelpers.TimeBlockingOperation(); var baseName = Path.GetFileNameWithoutExtension(ctx.assetPath); var g3d = G3D.Read(ctx.assetPath); g3d.OutputStats(); var obj = ctx.ImportG3D(g3d, baseName); ctx.AddRandomMaterial(obj); ctx.SetMainObject(obj); }
// NOTE: can't be run as part of NUnit because it requires the GC public static void BigFileTest() { var nVerts = (300 * 1000 * 1000); // 300 * 12 = 3.6 GB var vertices = nVerts.Select(i => new Vector3(i, i, i)); var bldr = new G3DBuilder(); bldr.AddVertices(vertices); var g3d = bldr.ToG3D(); Assert.AreEqual(nVerts, g3d.NumVertices); var tempFile = Path.Combine(Path.GetTempPath(), "bigfile.g3d"); g3d.Write(tempFile); var tmp = G3D.Read(tempFile); ValidateSameG3D(g3d, tmp); }
public static void TriangleSerializationTest() { // Serialize a triangle g3d to a bfast as bytes and read it back. var vertices = new[] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 1) }; var indices = new[] { 0, 1, 2 }; var materialIds = new[] { 0 }; var faceGroupIds = new[] { 0 }; var g3d = new G3DBuilder() .AddVertices(vertices.ToIArray()) .AddIndices(indices.ToIArray()) .Add(faceGroupIds.ToIArray().ToFaceGroupAttribute()) .Add(materialIds.ToIArray().ToFaceMaterialIdAttribute()) .ToG3D(); var bfastBytes = g3d.WriteToBytes(); var readG3d = G3D.Read(bfastBytes); Assert.IsNotNull(readG3d); var mesh = readG3d.ToIMesh(); ValidateGeometry(mesh); Assert.AreEqual(3, mesh.NumVertices); Assert.AreEqual(new Vector3(0, 0, 0), mesh.Vertices[0]); Assert.AreEqual(new Vector3(0, 1, 0), mesh.Vertices[1]); Assert.AreEqual(new Vector3(0, 1, 1), mesh.Vertices[2]); Assert.AreEqual(1, mesh.NumFaces); Assert.AreEqual(0, mesh.FaceGroups.First()); Assert.AreEqual(0, mesh.FaceMaterialIds.First()); }
public static SerializableDocument ReadBuffer(this SerializableDocument doc, Stream stream, string name, long numBytes) { Debug.WriteLine($"Reading buffer {name} of size {Util.BytesToString(numBytes)}"); switch (name) { case BufferNames.Header: var bytes = stream.ReadArray <byte>((int)numBytes); doc.Header = SerializableHeader.Parse(Encoding.UTF8.GetString(bytes)); return(doc); case BufferNames.Assets: if (doc.Options?.SkipAssets == true) { Debug.WriteLine("Skipping assets"); stream.Advance(numBytes); return(doc); } doc.Assets = stream.ReadBFast().ToArray(); return(doc); case BufferNames.Strings: var stringBytes = stream.ReadArray <byte>((int)numBytes); var joinedStringTable = Encoding.UTF8.GetString(stringBytes); doc.StringTable = joinedStringTable.Split('\0'); return(doc); case BufferNames.Geometry: if (doc.Options?.SkipGeometry == true) { Debug.WriteLine("Skipping geometry"); stream.Advance(numBytes); return(doc); } doc.Geometry = G3D.Read(stream); return(doc); case BufferNames.Nodes: if (doc.Options?.SkipGeometry == true) { Debug.WriteLine("Skipping nodes"); stream.Advance(numBytes); return(doc); } var cnt = (int)(numBytes / SerializableSceneNode.Size); Debug.Assert(numBytes % SerializableSceneNode.Size == 0, $"Number of bytes is not divisible by {SerializableSceneNode.Size}"); if (cnt < 0) { throw new Exception($"More than {int.MaxValue} items in array"); } doc.Nodes = stream.ReadArray <SerializableSceneNode>(cnt); return(doc); case BufferNames.Entities: doc.EntityTables = ReadEntityTables(stream); return(doc); } // NOTE: unrecognized buffers are not an error. Debug.WriteLine($"Unrecognized buffer {name}"); stream.ReadArray <byte>((int)numBytes); return(doc); }
public static void OpenAndConvertAssimpFiles() { var files = GetInputFiles() .Where(AssimpLoader.CanLoad) .Select(f => new FileLoadData(f)) .ToArray(); // Load all the files foreach (var f in files) { try { (f.MemoryConsumption, f.MSecToOpen) = Util.GetMemoryConsumptionAndMSecElapsed(() => f.Scene = AssimpLoader.Load(f.SourceFile.FullName)); } catch (Exception e) { f.Error = e; } } // Convert all the Assimp scenes to G3D foreach (var f in files) { if (f.Scene == null) { continue; } try { f.MSecToConvert = Util.GetMSecElapsed(() => f.G3d = f.Scene.ToG3d()); } catch (Exception e) { f.Error = e; } } // Save all the G3D scenes Util.CreateAndClearDirectory(TestOutputFolder); foreach (var f in files) { if (f.G3d == null) { continue; } try { var outputFilePath = Path.Combine(TestOutputFolder, f.ShortName + ".g3d"); f.G3DFile = new FileInfo(outputFilePath); f.MSecToSaveG3d = Util.GetMSecElapsed(() => f.G3d.Write(outputFilePath)); } catch (Exception e) { f.Error = e; } } // Try reading back in all of the G3D scenes, measure load times and the memory consumption foreach (var f in files) { if (f.G3DFile == null) { continue; } try { G3D localG3d = null; (f.MemoryConsumptionG3d, f.MSecToOpenG3d) = Util.GetMemoryConsumptionAndMSecElapsed(() => localG3d = G3D.Read(f.G3DFile.FullName)); ValidateSameG3D(f.G3d, localG3d); } catch (Exception e) { f.Error = e; } } // Output the header for data Console.WriteLine( "Importer," + "Extension," + "File Name," + "File Size(KB)," + "Load Time(s)," + "Memory(KB)," + "# Meshes," + "Time to Convert," + "Time to Write G3D," + "G3D File Size(KB)," + "G3D Memory(KB)", "G3D Load Time(s)", "Error"); // Output the data rows foreach (var f in files) { Console.WriteLine( "Assimp," + $"{Path.GetExtension(f.ShortName)}," + $"{f.ShortName}," + $"{f.SourceFile?.Length / 1000}," + $"{f.MSecToOpen / 100f}," + $"{f.MemoryConsumption / 1000}," + $"{f.NumMeshes}," + $"{f.MSecToConvert / 100f}," + $"{f.MSecToSaveG3d / 100f}," + $"{f.G3DFile?.Length / 1000}," + $"{f.MemoryConsumptionG3d / 1000}," + $"{f.MSecToOpenG3d / 100f}," + $"{f.Error}"); } Assert.AreEqual(0, files.Count(f => f.Error != null), "Errors occurred"); }
public static IMesh ReadG3D(string filePath) => G3D.Read(filePath).ToIMesh();