1
0
mirror of synced 2024-12-01 02:27:22 +01:00

More exporting fixes

This commit is contained in:
KillzXGaming 2019-05-02 19:45:55 -04:00
parent 48814b4643
commit fc7f208af6
8 changed files with 125 additions and 134 deletions

Binary file not shown.

View File

@ -618,12 +618,7 @@ namespace Bfres.Structs
public void Export(object sender, EventArgs args)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Supported Formats|*.bfobj;*.fbx;*.dae; *.obj;|" +
"Bfres Object (shape/vertices) |*.bfobj|" +
"FBX |*.fbx|" +
"DAE |*.dae|" +
"OBJ |*.obj|" +
"All files(*.*)|*.*";
sfd.Filter = FileFilters.FSHP;
sfd.DefaultExt = ".bfobj";
sfd.FileName = Text;
@ -637,9 +632,12 @@ namespace Bfres.Structs
case ".bfobj":
ExportBinaryObject(sfd.FileName);
break;
case ".obj":
OBJ.ExportMesh(sfd.FileName, this);
break;
default:
AssimpSaver assimp = new AssimpSaver();
assimp.SaveFromObject(vertices, lodMeshes[DisplayLODIndex].faces, Text, sfd.FileName);
assimp.SaveFromObject(this, sfd.FileName);
break;
}
}

View File

@ -21,7 +21,11 @@ namespace Switch_Toolbox.Library
SaveSkeleton(skeleton, scene.RootNode);
SaveMaterials(scene, model, FileName, Textures);
SaveMeshes(scene, model, skeleton, FileName, NodeArray);
SaveScene(FileName, scene);
}
private void SaveScene(string FileName, Scene scene)
{
using (var v = new AssimpContext())
{
string ext = System.IO.Path.GetExtension(FileName);
@ -40,8 +44,6 @@ namespace Switch_Toolbox.Library
MessageBox.Show($"Exported {FileName} Successfuly!");
else
MessageBox.Show($"Failed to export {FileName}!");
Scene newScene = v.ImportFile(FileName);
}
}
@ -50,8 +52,21 @@ namespace Switch_Toolbox.Library
int MeshIndex = 0;
foreach (var obj in model.Nodes[0].Nodes)
{
var genericObj = (STGenericObject)obj;
var mesh = SaveMesh((STGenericObject)obj, skeleton, NodeArray);
scene.Meshes.Add(mesh);
MeshIndex++;
}
Node geomNode = new Node(Path.GetFileNameWithoutExtension(FileName), scene.RootNode);
for (int ob = 0; ob < scene.MeshCount; ob++)
{
geomNode.MeshIndices.Add(ob);
}
scene.RootNode.Children.Add(geomNode);
}
private Mesh SaveMesh(STGenericObject genericObj, STSkeleton skeleton, List<int> NodeArray)
{
Mesh mesh = new Mesh(genericObj.Text, PrimitiveType.Triangle);
mesh.MaterialIndex = genericObj.MaterialIndex;
@ -122,17 +137,7 @@ namespace Switch_Toolbox.Library
mesh.TextureCoordinateChannels.SetValue(textureCoords0, 0);
scene.Meshes.Add(mesh);
MeshIndex++;
}
Node geomNode = new Node(Path.GetFileNameWithoutExtension(FileName), scene.RootNode);
for (int ob = 0; ob < scene.MeshCount; ob++)
{
geomNode.MeshIndices.Add(ob);
}
scene.RootNode.Children.Add(geomNode);
return mesh;
}
private void SaveMaterials(Scene scene, STGenericModel model, string FileName, List<STGenericTexture> Textures)
@ -222,48 +227,20 @@ namespace Switch_Toolbox.Library
}
public void SaveFromObject(List<Vertex> vertices, List<int> faces, string MeshName, string FileName)
public void SaveFromObject(STGenericObject genericObject, string FileName)
{
Scene scene = new Scene();
scene.RootNode = new Node("Root");
Mesh mesh = new Mesh(MeshName, PrimitiveType.Triangle);
List<Vector3D> textureCoords0 = new List<Vector3D>();
List<Vector3D> textureCoords1 = new List<Vector3D>();
List<Vector3D> textureCoords2 = new List<Vector3D>();
List<Color4D> vertexColors = new List<Color4D>();
foreach (Vertex v in vertices)
{
mesh.Vertices.Add(new Vector3D(v.pos.X, v.pos.Y, v.pos.Z));
mesh.Normals.Add(new Vector3D(v.nrm.X, v.nrm.Y, v.nrm.Z));
textureCoords0.Add(new Vector3D(v.uv0.X, v.uv0.Y, 0));
textureCoords1.Add(new Vector3D(v.uv1.X, v.uv1.Y, 0));
textureCoords2.Add(new Vector3D(v.uv2.X, v.uv2.Y, 0));
vertexColors.Add(new Color4D(v.col.X, v.col.Y, v.col.Z, v.col.W));
mesh.TextureCoordinateChannels[0] = textureCoords0;
mesh.TextureCoordinateChannels[1] = textureCoords1;
mesh.TextureCoordinateChannels[2] = textureCoords2;
mesh.VertexColorChannels[0] = vertexColors;
}
for (int f = 0; f < faces.Count; f++)
{
mesh.Faces.Add(new Face(new int[] { faces[f++], faces[f++], faces[f] }));
}
var mesh = SaveMesh(genericObject, null, null);
mesh.MaterialIndex = 0;
mesh.TextureCoordinateChannels.SetValue(textureCoords0, 0);
scene.Meshes.Add(mesh);
Material material = new Material();
material.Name = "NewMaterial";
scene.Materials.Add(material);
using (var v = new AssimpContext())
{
v.ExportFile(scene, FileName, "obj");
}
SaveScene(FileName, scene);
}
private void SaveSkeleton(STSkeleton skeleton, Node parentNode)

View File

@ -24,12 +24,30 @@ namespace Switch_Toolbox.Library
File.WriteAllText(fileMtlPath, writerMtl.ToString());
}
public static void ExportMesh(string FileName, STGenericObject genericMesh)
{
string fileNoExt = Path.GetFileNameWithoutExtension(FileName);
string fileMtlPath = FileName.Replace("obj", "mtl");
//Write mesh
StringBuilder writer = new StringBuilder();
SaveMesh(writer, genericMesh, null, 0);
File.WriteAllText(FileName, writer.ToString());
}
private static void SaveMeshes(StringBuilder writer, STGenericModel Model, string MtlName)
{
writer.AppendLine($"mtllib {MtlName}");
int VertexCount = 1;
foreach (STGenericObject mesh in Model.Nodes[0].Nodes)
{
var mat = GetMaterial(mesh.MaterialIndex, Model);
SaveMesh(writer, mesh, mat, VertexCount);
}
}
private static void SaveMesh(StringBuilder writer, STGenericObject mesh, STGenericMaterial mat, int VertexCount)
{
writer.AppendLine($"o {mesh.Text}");
writer.AppendLine($"g {mesh.Text}");
@ -40,7 +58,6 @@ namespace Switch_Toolbox.Library
writer.AppendLine($"vn {v.nrm.X} {v.nrm.Y} {v.nrm.Z}");
writer.AppendLine($"vt {v.uv0.X} {v.uv0.Y}");
}
var mat = GetMaterial(mesh.MaterialIndex, Model);
if (mat != null)
writer.AppendLine($"usemtl {mat.Text}");
@ -61,7 +78,6 @@ namespace Switch_Toolbox.Library
VertexCount += mesh.vertices.Count;
}
}
private static STGenericMaterial GetMaterial(int MaterialIndex, STGenericModel Model)
{