1
0
mirror of synced 2024-11-28 01:10:51 +01:00

Add my own obj libary since assimp is broken

This commit is contained in:
KillzXGaming 2019-04-10 18:42:45 -04:00
parent 3a153b5ed1
commit a21eead9da
10 changed files with 127 additions and 20 deletions

Binary file not shown.

View File

@ -452,32 +452,39 @@ namespace Bfres.Structs
else
Model.Export(FileName, GetResFile());
break;
case ".obj":
OBJ.ExportModel(FileName, this, GetTextures());
break;
default:
List<STGenericTexture> textures = new List<STGenericTexture>();
foreach (var mat in materials)
{
foreach (var texref in mat.Value.TextureMaps)
{
foreach (var bntx in PluginRuntime.bntxContainers)
{
if (bntx.Textures.ContainsKey(texref.Name))
textures.Add(bntx.Textures[texref.Name]);
}
foreach (var ftexCont in PluginRuntime.ftexContainers)
{
if (ftexCont.ResourceNodes.ContainsKey(texref.Name))
textures.Add((FTEX)ftexCont.ResourceNodes[texref.Name]);
}
}
}
AssimpSaver assimp = new AssimpSaver();
assimp.SaveFromModel(this, FileName, textures, Skeleton, Skeleton.Node_Array.ToList());
assimp.SaveFromModel(this, FileName, GetTextures(), Skeleton, Skeleton.Node_Array.ToList());
break;
}
}
private List<STGenericTexture> GetTextures()
{
List<STGenericTexture> textures = new List<STGenericTexture>();
foreach (var mat in materials)
{
foreach (var texref in mat.Value.TextureMaps)
{
foreach (var bntx in PluginRuntime.bntxContainers)
{
if (bntx.Textures.ContainsKey(texref.Name))
textures.Add(bntx.Textures[texref.Name]);
}
foreach (var ftexCont in PluginRuntime.ftexContainers)
{
if (ftexCont.ResourceNodes.ContainsKey(texref.Name))
textures.Add((FTEX)ftexCont.ResourceNodes[texref.Name]);
}
}
}
return textures;
}
public override void Replace(string FileName) {
AddOjects(FileName, GetResFile(), GetResFileU());
}

View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using OpenTK;
namespace Switch_Toolbox.Library
{
public class OBJ
{
public static void ExportModel(string FileName, STGenericModel Model, List<STGenericTexture> Textures)
{
string fileNoExt = Path.GetFileNameWithoutExtension(FileName);
//Write model
StringBuilder writer = new StringBuilder();
SaveMeshes(writer, Model, $"{fileNoExt}.mtl");
File.WriteAllText(FileName, writer.ToString());
//Write materials
StringBuilder writerMtl = new StringBuilder();
SaveMaterials(writer, Model);
File.WriteAllText($"{fileNoExt}.mtl", writerMtl.ToString());
}
private static void SaveMeshes(StringBuilder writer, STGenericModel Model, string MtlName)
{
writer.AppendLine($"mtllib {MtlName}");
int VertexID = 1;
foreach (STGenericObject mesh in Model.Nodes[0].Nodes)
{
writer.AppendLine($"o {mesh.Text}");
writer.AppendLine($"g {mesh.Text}");
foreach (var v in mesh.vertices)
{
writer.AppendLine($"v {v.pos.X} {v.pos.Y} {v.pos.Z}");
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}");
for (int i = 0; i < mesh.faces.Count; i += 3)
{
int FaceInd0 = VertexID + mesh.faces[i];
int FaceInd1 = VertexID + mesh.faces[i + 1];
int FaceInd2 = VertexID + mesh.faces[i + 2];
writer.AppendLine($"f {FaceInd0}/{FaceInd0}/{FaceInd0}" +
$" {FaceInd1}/{FaceInd1}/{FaceInd1}" +
$" {FaceInd2}/{FaceInd2}/{FaceInd2}");
}
}
}
private static STGenericMaterial GetMaterial(int MaterialIndex, STGenericModel Model)
{
if (MaterialIndex < Model.Nodes[1].Nodes.Count)
return (STGenericMaterial)Model.Nodes[1].Nodes[MaterialIndex];
else
return null;
}
public static float Ns = -3.921569f;
public static Vector3 Ka = new Vector3(1.000000f, 1.000000f, 1.000000f);
public static Vector3 Kd = new Vector3(0.640000f, 0.640000f, 0.640000f);
public static Vector3 Ks = new Vector3(0.500000f, 0.500000f, 0.500000f);
public static Vector3 Ke = new Vector3(0.000000f, 0.000000f, 0.000000f);
public static float Ni = -1f;
public static float d = -1f;
public static float illum = 2;
private static void SaveMaterials(StringBuilder writer, STGenericModel Model)
{
foreach (STGenericMaterial mat in Model.Nodes[1].Nodes)
{
writer.AppendLine($"newmtl {mat.Text}");
writer.AppendLine($"Ns {Ns}");
writer.AppendLine($"Ka {Ka}");
writer.AppendLine($"Kd {Kd}");
writer.AppendLine($"Ks {Ks}");
writer.AppendLine($"Ke {Ke}");
writer.AppendLine($"Ni {Ni}");
writer.AppendLine($"d {d}");
writer.AppendLine($"illum {illum}");
foreach (var tex in mat.TextureMaps)
{
if (tex.Type == STGenericMatTexture.TextureType.Diffuse)
writer.AppendLine($"map_Kd {tex.Name}.png");
}
}
}
}
}

View File

@ -204,6 +204,7 @@
<Compile Include="FileFormats\APNG\CRC.cs" />
<Compile Include="FileFormats\Assimp\AssimpSaver.cs" />
<Compile Include="FileFormats\Animation\SEANIM.cs" />
<Compile Include="FileFormats\OBJ.cs" />
<Compile Include="Forms\BatchFormatExport.cs">
<SubType>Form</SubType>
</Compile>