private static MgPrimitiveTopology DetermineTopology(MeshPrimitive.ModeEnum mode) { switch (mode) { case MeshPrimitive.ModeEnum.LINES: return(MgPrimitiveTopology.LINE_LIST); case MeshPrimitive.ModeEnum.LINE_STRIP: return(MgPrimitiveTopology.LINE_STRIP); case MeshPrimitive.ModeEnum.POINTS: return(MgPrimitiveTopology.POINT_LIST); case MeshPrimitive.ModeEnum.TRIANGLES: return(MgPrimitiveTopology.TRIANGLE_LIST); case MeshPrimitive.ModeEnum.TRIANGLE_FAN: return(MgPrimitiveTopology.TRIANGLE_FAN); case MeshPrimitive.ModeEnum.TRIANGLE_STRIP: return(MgPrimitiveTopology.TRIANGLE_STRIP); default: throw new NotSupportedException(); } }
internal static int AddLineLoop(this Gltf gltf, string name, List <byte> buffer, double[] vertices, ushort[] indices, double[] vMin, double[] vMax, ushort iMin, ushort iMax, int materialId, MeshPrimitive.ModeEnum mode, Transform transform = null) { var m = new glTFLoader.Schema.Mesh(); m.Name = name; var vBuff = gltf.AddBufferView(0, buffer.Count, vertices.Length * sizeof(float), null, null); var iBuff = gltf.AddBufferView(0, buffer.Count + vertices.Length * sizeof(float), indices.Length * sizeof(ushort), null, null); foreach (var v in vertices) { buffer.AddRange(BitConverter.GetBytes((float)v)); } foreach (var i in indices) { buffer.AddRange(BitConverter.GetBytes(i)); } while (buffer.Count % 4 != 0) { // Console.WriteLine("Padding..."); buffer.Add(0); } var vAccess = gltf.AddAccessor(vBuff, 0, Accessor.ComponentTypeEnum.FLOAT, vertices.Length / 3, new[] { (float)vMin[0], (float)vMin[1], (float)vMin[2] }, new[] { (float)vMax[0], (float)vMax[1], (float)vMax[2] }, Accessor.TypeEnum.VEC3); var iAccess = gltf.AddAccessor(iBuff, 0, Accessor.ComponentTypeEnum.UNSIGNED_SHORT, indices.Length, new[] { (float)iMin }, new[] { (float)iMax }, Accessor.TypeEnum.SCALAR); var prim = new MeshPrimitive(); prim.Indices = iAccess; prim.Material = materialId; prim.Mode = mode; prim.Attributes = new Dictionary <string, int> { { "POSITION", vAccess } }; m.Primitives = new[] { prim }; // Add mesh to gltf if (gltf.Meshes != null) { // TODO: Get rid of this resizing. var meshes = gltf.Meshes.ToList(); meshes.Add(m); gltf.Meshes = meshes.ToArray(); } else { gltf.Meshes = new[] { m }; } var parentId = 0; if (transform != null) { var a = transform.XAxis; var b = transform.YAxis; var c = transform.ZAxis; var transNode = new Node(); transNode.Matrix = new[] { (float)a.X, (float)a.Y, (float)a.Z, 0.0f, (float)b.X, (float)b.Y, (float)b.Z, 0.0f, (float)c.X, (float)c.Y, (float)c.Z, 0.0f, (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f }; parentId = gltf.AddNode(transNode, 0); } // Add mesh node to gltf var node = new Node(); node.Mesh = gltf.Meshes.Length - 1; gltf.AddNode(node, parentId); return(gltf.Meshes.Length - 1); }
internal static int AddLineLoop(this Gltf gltf, string name, List <byte> buffer, List <BufferView> bufferViews, List <Accessor> accessors, byte[] vertices, byte[] indices, double[] vMin, double[] vMax, ushort iMin, ushort iMax, int materialId, MeshPrimitive.ModeEnum mode, List <glTFLoader.Schema.Mesh> meshes, List <glTFLoader.Schema.Node> nodes, Transform transform = null) { var m = new glTFLoader.Schema.Mesh(); m.Name = name; var vBuff = AddBufferView(bufferViews, 0, buffer.Count, vertices.Length, null, null); var iBuff = AddBufferView(bufferViews, 0, buffer.Count + vertices.Length, indices.Length, null, null); buffer.AddRange(vertices); buffer.AddRange(indices); while (buffer.Count % 4 != 0) { // Console.WriteLine("Padding..."); buffer.Add(0); } var vAccess = AddAccessor(accessors, vBuff, 0, Accessor.ComponentTypeEnum.FLOAT, vertices.Length / sizeof(float) / 3, new[] { (float)vMin[0], (float)vMin[1], (float)vMin[2] }, new[] { (float)vMax[0], (float)vMax[1], (float)vMax[2] }, Accessor.TypeEnum.VEC3); var iAccess = AddAccessor(accessors, iBuff, 0, Accessor.ComponentTypeEnum.UNSIGNED_SHORT, indices.Length / sizeof(ushort), new[] { (float)iMin }, new[] { (float)iMax }, Accessor.TypeEnum.SCALAR); var prim = new MeshPrimitive(); prim.Indices = iAccess; prim.Material = materialId; prim.Mode = mode; prim.Attributes = new Dictionary <string, int> { { "POSITION", vAccess } }; m.Primitives = new[] { prim }; // Add mesh to gltf meshes.Add(m); var parentId = 0; if (transform != null) { var a = transform.XAxis; var b = transform.YAxis; var c = transform.ZAxis; var transNode = new Node(); transNode.Matrix = new[] { (float)a.X, (float)a.Y, (float)a.Z, 0.0f, (float)b.X, (float)b.Y, (float)b.Z, 0.0f, (float)c.X, (float)c.Y, (float)c.Z, 0.0f, (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f }; parentId = gltf.AddNode(nodes, transNode, 0); } // Add mesh node to gltf var node = new Node(); node.Mesh = meshes.Count - 1; gltf.AddNode(nodes, node, parentId); return(meshes.Count - 1); }