コード例 #1
0
ファイル: G3dTests.cs プロジェクト: dmk-rib/Open.Vim.Sdk
        // 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);
        }
コード例 #2
0
        public static G3D ToG3D(this Mesh mesh)
        {
            // The Assimp mesh must be triangulated
            if (mesh.FaceCount == 0)
            {
                return(G3D.Empty);
            }

            var bldr = new G3DBuilder();

            // Note: should be at most 3 for meses, but could 2 for lines, or 1 for point clouds
            var numCornersPerFace = mesh.Faces[0].IndexCount;

            if (numCornersPerFace > 3)
            {
                throw new Exception("The Assimp mesh must be triangulated as a post-process");
            }
            if (numCornersPerFace <= 0)
            {
                throw new Exception("The Assimp mesh has faces without indices");
            }
            foreach (var f in mesh.Faces)
            {
                if (f.IndexCount != numCornersPerFace)
                {
                    throw new Exception($"Each face of the assimp mesh must have {numCornersPerFace} corners, but found one with {f.IndexCount}");
                }
            }
            bldr.SetObjectFaceSize(numCornersPerFace);

            var indices = mesh.GetIndices();

            if (indices.Length % numCornersPerFace != 0)
            {
                throw new Exception($"The mesh index buffer length {indices.Length} is not divisible by {numCornersPerFace}");
            }

            bldr.AddVertices(mesh.Vertices.ToIArray().Select(ToMath3D));
            bldr.AddIndices(indices);

            if (mesh.HasTangentBasis)
            {
                bldr.Add(mesh.BiTangents.ToIArray().Select(ToMath3D).ToVertexBitangentAttribute());
            }

            if (mesh.HasTangentBasis)
            {
                bldr.Add(mesh.Tangents.ToIArray().Select(x => ToMath3D(x).ToVector4()).ToVertexTangentAttribute());
            }

            if (mesh.HasNormals)
            {
                bldr.Add(mesh.Normals.ToIArray().Select(ToMath3D).ToVertexNormalAttribute());
            }

            for (var i = 0; i < mesh.TextureCoordinateChannelCount; ++i)
            {
                var uvChannel = mesh.TextureCoordinateChannels[i];
                bldr.Add(uvChannel.ToIArray().Select(ToMath3D).ToVertexUvwAttribute(i));
            }

            for (var i = 0; i < mesh.VertexColorChannelCount; ++i)
            {
                var vcChannel = mesh.VertexColorChannels[i];
                bldr.Add(vcChannel.ToIArray().Select(ToMath3D).ToVertexColorAttribute(i));
            }

            return(bldr.ToG3D());
        }