public static MeshGroup FromFbx(string filePath) { const float Scale = 1.0f; var assimp = new Assimp.AssimpContext(); var scene = assimp.ImportFile(filePath, Assimp.PostProcessSteps.PreTransformVertices); var baseFilePath = Path.GetDirectoryName(filePath); TexList = new List <string>(); TextureData = new List <Tm2>(); foreach (Assimp.Material mat in scene.Materials) { TexList.Add(Path.GetFileName(mat.TextureDiffuse.FilePath)); Stream str = File.OpenRead(TexList[TexList.Count - 1]); PngImage png = new PngImage(str); Tm2 tmImage = Tm2.Create(png); TextureData.Add(tmImage); } int childCount = scene.RootNode.ChildCount; return(new MeshGroup() { MeshDescriptors = scene.Meshes .Select(x => { var vertices = new PositionColoredTextured[x.Vertices.Count]; for (var i = 0; i < vertices.Length; i++) { vertices[i].X = x.Vertices[i].X * Scale; vertices[i].Y = x.Vertices[i].Y * Scale; vertices[i].Z = x.Vertices[i].Z * Scale; vertices[i].Tu = x.TextureCoordinateChannels[0][i].X; vertices[i].Tv = 1.0f - x.TextureCoordinateChannels[0][i].Y; vertices[i].R = 1.0f; vertices[i].G = 1.0f; vertices[i].B = 1.0f; vertices[i].A = 1.0f; } return new MeshDescriptor { Vertices = vertices, Indices = x.GetIndices(), IsOpaque = true, TextureIndex = x.MaterialIndex }; }).ToList() }); }
public static List <MeshGroup> FromFbx(string filePath) { List <MeshGroup> group = new List <MeshGroup>(); const float Scale = 1.0f; var assimp = new Assimp.AssimpContext(); var scene = assimp.ImportFile(filePath, Assimp.PostProcessSteps.PreTransformVertices); var baseFilePath = Path.GetDirectoryName(filePath); TexList = new List <string>(); TextureData = new List <Tm2>(); foreach (Assimp.Material mat in scene.Materials) { TexList.Add(Path.GetFileName(mat.TextureDiffuse.FilePath)); Stream str = File.OpenRead(TexList[TexList.Count - 1]); PngImage png = new PngImage(str); Tm2 tmImage = Tm2.Create(png); TextureData.Add(tmImage); } for (int i = 0; i < scene.RootNode.ChildCount; i++) { Node child = scene.RootNode.Children[i]; MeshGroup currentMeshGroup = new MeshGroup(); currentMeshGroup.MeshDescriptors = new List <MeshDescriptor>(); // Get meshes by ID. foreach (int j in child.MeshIndices) { MeshDescriptor meshDescriptor = new MeshDescriptor(); Mesh x = scene.Meshes[j]; var vertices = new PositionColoredTextured[x.Vertices.Count]; for (var k = 0; k < vertices.Length; k++) { vertices[k].X = x.Vertices[k].X * Scale; vertices[k].Y = x.Vertices[k].Y * Scale; vertices[k].Z = x.Vertices[k].Z * Scale; vertices[k].Tu = x.TextureCoordinateChannels[0][k].X; vertices[k].Tv = 1.0f - x.TextureCoordinateChannels[0][k].Y; vertices[k].R = x.VertexColorChannels[0][i].R; vertices[k].G = x.VertexColorChannels[0][i].G; vertices[k].B = x.VertexColorChannels[0][i].B; vertices[k].A = x.VertexColorChannels[0][i].A; } meshDescriptor.Vertices = vertices; meshDescriptor.Indices = x.GetIndices(); meshDescriptor.IsOpaque = false; meshDescriptor.TextureIndex = x.MaterialIndex; currentMeshGroup.MeshDescriptors.Add(meshDescriptor); } group.Add(currentMeshGroup); } return(group); }
public static MeshGroup FromFbx(string filePath) { const float Scale = 1.0f; var assimp = new Assimp.AssimpContext(); var scene = assimp.ImportFile(filePath, Assimp.PostProcessSteps.PreTransformVertices); var BoneScene = assimp.ImportFile(filePath); var baseFilePath = Path.GetDirectoryName(filePath); TexList = new List <string>(); TextureData = new List <Tm2>(); BoneData = new List <Assimp.Bone>(); NodeData = new List <Assimp.Node>(); foreach (Assimp.Material mat in scene.Materials) { Stream str = null; var name = Path.GetFileName(mat.TextureDiffuse.FilePath); if (name != "" || name != null) { str = File.OpenRead(name); } if (str != null) { TexList.Add(Path.GetFileName(mat.TextureDiffuse.FilePath)); PngImage png = new PngImage(str); Tm2 tmImage = Tm2.Create(png); TextureData.Add(tmImage); } } Assimp.Bone rBone = new Assimp.Bone(); foreach (var m in BoneScene.Meshes) { foreach (var bn in m.Bones) { if (!BoneData.Contains(bn)) { BoneData.Add(bn); } } } NodeData.AddRange(BoneScene.RootNode.Children.ToList()); return(new MeshGroup() { MeshDescriptors = scene.Meshes .Select(x => { var vertices = new PositionColoredTextured[x.Vertices.Count]; for (var i = 0; i < vertices.Length; i++) { vertices[i].X = x.Vertices[i].X * Scale; vertices[i].Y = x.Vertices[i].Y * Scale; vertices[i].Z = x.Vertices[i].Z * Scale; vertices[i].Tu = x.TextureCoordinateChannels[0][i].X; vertices[i].Tv = 1.0f - x.TextureCoordinateChannels[0][i].Y; vertices[i].R = x.VertexColorChannels[0][i].R; vertices[i].G = x.VertexColorChannels[0][i].G; vertices[i].B = x.VertexColorChannels[0][i].B; vertices[i].A = x.VertexColorChannels[0][i].A; } return new MeshDescriptor { Vertices = vertices, Indices = x.GetIndices(), IsOpaque = true, TextureIndex = x.MaterialIndex }; }).ToList() }); }