diff --git a/.vs/Switch_Toolbox/v15/.suo b/.vs/Switch_Toolbox/v15/.suo index 0cfc56fe..d55469b4 100644 Binary files a/.vs/Switch_Toolbox/v15/.suo and b/.vs/Switch_Toolbox/v15/.suo differ diff --git a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide index dfb42dd5..970126d0 100644 Binary files a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide and b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide differ diff --git a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal index e2387f00..08fd5f05 100644 Binary files a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal and b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal differ diff --git a/Switch_FileFormatsMain/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL.cs b/Switch_FileFormatsMain/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL.cs index b85a36a7..9846700e 100644 --- a/Switch_FileFormatsMain/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL.cs +++ b/Switch_FileFormatsMain/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL.cs @@ -472,7 +472,7 @@ namespace Bfres.Structs } } - AssimpData assimp = new AssimpData(); + AssimpSaver assimp = new AssimpSaver(); assimp.SaveFromModel(this, FileName, textures, Skeleton, Skeleton.Node_Array.ToList()); break; } diff --git a/Switch_FileFormatsMain/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs b/Switch_FileFormatsMain/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs index 78a6e692..494d10f8 100644 --- a/Switch_FileFormatsMain/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs +++ b/Switch_FileFormatsMain/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs @@ -590,7 +590,7 @@ namespace Bfres.Structs ExportBinaryObject(sfd.FileName); break; default: - AssimpData assimp = new AssimpData(); + AssimpSaver assimp = new AssimpSaver(); assimp.SaveFromObject(vertices, lodMeshes[DisplayLODIndex].faces, Text, sfd.FileName); break; } diff --git a/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache b/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache index c61bc207..c67e5d44 100644 Binary files a/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache and b/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache differ diff --git a/Switch_FileFormatsMain/obj/Release/FirstPlugin.Plg.dll b/Switch_FileFormatsMain/obj/Release/FirstPlugin.Plg.dll index 6913e22b..57e46e64 100644 Binary files a/Switch_FileFormatsMain/obj/Release/FirstPlugin.Plg.dll and b/Switch_FileFormatsMain/obj/Release/FirstPlugin.Plg.dll differ diff --git a/Switch_FileFormatsMain/obj/Release/FirstPlugin.Plg.pdb b/Switch_FileFormatsMain/obj/Release/FirstPlugin.Plg.pdb index 721d5814..416fc03f 100644 Binary files a/Switch_FileFormatsMain/obj/Release/FirstPlugin.Plg.pdb and b/Switch_FileFormatsMain/obj/Release/FirstPlugin.Plg.pdb differ diff --git a/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache b/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache index 1a9a621b..1e828859 100644 Binary files a/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache and b/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache differ diff --git a/Switch_Toolbox_Library/FileFormats/Assimp/Assimp.cs b/Switch_Toolbox_Library/FileFormats/Assimp/Assimp.cs index 9d646458..c17cb6fa 100644 --- a/Switch_Toolbox_Library/FileFormats/Assimp/Assimp.cs +++ b/Switch_Toolbox_Library/FileFormats/Assimp/Assimp.cs @@ -357,275 +357,7 @@ namespace Switch_Toolbox.Library return obj; } - private void SaveSkeleton(STSkeleton skeleton, Node parentNode) - { - Node root = new Node("skeleton_root"); - parentNode.Children.Add(root); - if (skeleton.bones.Count > 0) - { - Node boneNode = new Node(skeleton.bones[0].Text); - boneNode.Transform = GetBoneMatrix(skeleton.bones[0]); - root.Children.Add(boneNode); - - foreach (STBone child in skeleton.bones[0].GetChildren()) - SaveBones(boneNode, child, skeleton); - } - } - private void SaveBones(Node parentBone, STBone bone, STSkeleton skeleton) - { - Node boneNode = new Node(bone.Text); - parentBone.Children.Add(boneNode); - - boneNode.Transform = GetBoneMatrix(bone); - - foreach (STBone child in bone.GetChildren()) - SaveBones(boneNode, child, skeleton); - } - private Matrix4x4 GetBoneMatrix(STBone bone) - { - var pos = Matrix4x4.FromTranslation(new Vector3D(bone.position[0], bone.position[1], bone.position[2])); - var rotx = Matrix4x4.FromRotationX(bone.rotation[0]); - var roty = Matrix4x4.FromRotationY(bone.rotation[1]); - var rotz = Matrix4x4.FromRotationZ(bone.rotation[2]); - var sca = Matrix4x4.FromScaling(new Vector3D(bone.scale[0], bone.scale[1], bone.scale[2])); - - return sca * (rotx * roty * rotz) * pos; - } - public void SaveFromModel(STGenericModel model, string FileName, List Textures, STSkeleton skeleton = null, List NodeArray = null) - { - Scene scene = new Scene(); - scene.RootNode = new Node("RootNode"); - - SaveSkeleton(skeleton, scene.RootNode); - - int MeshIndex = 0; - foreach (var obj in model.Nodes[0].Nodes) - { - var genericObj = (STGenericObject)obj; - - Mesh mesh = new Mesh(genericObj.Text, PrimitiveType.Triangle); - mesh.MaterialIndex = genericObj.MaterialIndex; - - List textureCoords0 = new List(); - List textureCoords1 = new List(); - List textureCoords2 = new List(); - List vertexColors = new List(); - - int vertexID = 0; - foreach (Vertex v in genericObj.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 j = 0; j < v.boneIds.Count; j++) - { - if (j < genericObj.VertexSkinCount) - { - //Get the bone via the node array and bone index from the vertex - STBone STbone = skeleton.bones[NodeArray[v.boneIds[j]]]; - - //Find the index of a bone. If it doesn't exist then we add it - int boneInd = mesh.Bones.FindIndex(x => x.Name == STbone.Text); - - if (boneInd == -1) - { - var matrices = Switch_Toolbox.Library.IO.MatrixExenstion.CalculateInverseMatrix(STbone); - - //Set the inverse matrix - Matrix4x4 transform = matrices.inverse.FromNumerics(); - - //Create a new assimp bone - Bone bone = new Bone(); - bone.Name = STbone.Text; - bone.OffsetMatrix = transform; - - mesh.Bones.Add(bone); - BoneNames.Add(bone.Name); - - boneInd = mesh.Bones.IndexOf(bone); //Set the index of the bone for the vertex weight - } - - //Check if the max amount of weights is higher than the current bone id - if (v.boneWeights.Count > j) - { - if (v.boneWeights[j] <= 1) - mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, v.boneWeights[j])); - else - mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, 1)); - } - else - mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, 1)); - } - } - vertexID++; - } - List faces = genericObj.lodMeshes[genericObj.DisplayLODIndex].faces; - for (int f = 0; f < faces.Count; f++) - mesh.Faces.Add(new Face(new int[] { faces[f++], faces[f++], faces[f] })); - - 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); - - string TextureExtension = ".png"; - string TexturePath = System.IO.Path.GetDirectoryName(FileName); - - foreach (var tex in Textures) - { - string path = System.IO.Path.Combine(TexturePath, tex.Text + TextureExtension); - - var bitmap = tex.GetBitmap(); - bitmap.Save(path); - bitmap.Dispose(); - - } - - foreach (var mat in model.Nodes[1].Nodes) - { - var genericMat = (STGenericMaterial)mat; - - Material material = new Material(); - material.Name = genericMat.Text; - - foreach (var tex in genericMat.TextureMaps) - { - TextureSlot slot = new TextureSlot(); - string path = System.IO.Path.Combine(TexturePath, tex.Name + TextureExtension); - slot.FilePath = path; - slot.UVIndex = 0; - slot.Flags = 0; - slot.TextureIndex = 0; - slot.BlendFactor = 1.0f; - slot.Mapping = TextureMapping.FromUV; - slot.Operation = TextureOperation.Add; - - if (tex.Type == STGenericMatTexture.TextureType.Diffuse) - slot.TextureType = TextureType.Diffuse; - else if (tex.Type == STGenericMatTexture.TextureType.Normal) - slot.TextureType = TextureType.Normals; - else if (tex.Type == STGenericMatTexture.TextureType.Specular) - slot.TextureType = TextureType.Specular; - else if (tex.Type == STGenericMatTexture.TextureType.Emission) - slot.TextureType = TextureType.Emissive; - else if (tex.Type == STGenericMatTexture.TextureType.Light) - { - slot.TextureType = TextureType.Lightmap; - slot.UVIndex = 2; - } - else if (tex.Type == STGenericMatTexture.TextureType.Shadow) - { - slot.TextureType = TextureType.Ambient; - slot.UVIndex = 1; - } - else - slot.TextureType = TextureType.Unknown; - - if (tex.wrapModeS == 0) - slot.WrapModeU = TextureWrapMode.Wrap; - if (tex.wrapModeS == 1) - slot.WrapModeU = TextureWrapMode.Mirror; - if (tex.wrapModeS == 2) - slot.WrapModeU = TextureWrapMode.Clamp; - if (tex.wrapModeT == 0) - slot.WrapModeV = TextureWrapMode.Wrap; - if (tex.wrapModeT == 1) - slot.WrapModeV = TextureWrapMode.Mirror; - if (tex.wrapModeT == 2) - slot.WrapModeV = TextureWrapMode.Clamp; - else - { - slot.WrapModeU = TextureWrapMode.Wrap; - slot.WrapModeV = TextureWrapMode.Wrap; - } - - material.AddMaterialTexture(ref slot); - } - scene.Materials.Add(material); - } - - using (var v = new AssimpContext()) - { - string ext = System.IO.Path.GetExtension(FileName); - - string formatID = "collada"; - if (ext == ".obj") - formatID = "obj"; - if (ext == ".3ds") - formatID = "3ds"; - if (ext == ".dae") - formatID = "collada"; - if (ext == ".ply") - formatID = "ply"; - - if (v.ExportFile(scene, FileName, formatID, PostProcessSteps.ValidateDataStructure)) - MessageBox.Show($"Exported {FileName} Successfuly!"); - else - MessageBox.Show($"Failed to export {FileName}!"); - } - } - public void SaveFromObject(List vertices, List faces, string MeshName, string FileName) - { - Scene scene = new Scene(); - scene.RootNode = new Node("Root"); - - Mesh mesh = new Mesh(MeshName, PrimitiveType.Triangle); - - List textureCoords0 = new List(); - List textureCoords1 = new List(); - List textureCoords2 = new List(); - List vertexColors = new List(); - - 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] })); - } - 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"); - } - } public List GetFaces(Mesh msh) { List faces = new List(); diff --git a/Switch_Toolbox_Library/FileFormats/Assimp/AssimpHelper.cs b/Switch_Toolbox_Library/FileFormats/Assimp/AssimpHelper.cs index 930b5bbe..1edc39a7 100644 --- a/Switch_Toolbox_Library/FileFormats/Assimp/AssimpHelper.cs +++ b/Switch_Toolbox_Library/FileFormats/Assimp/AssimpHelper.cs @@ -11,6 +11,17 @@ namespace Switch_Toolbox.Library { public static class AssimpHelper { + public static Matrix4x4 GetBoneMatrix(STBone bone) + { + var pos = Matrix4x4.FromTranslation(new Vector3D(bone.position[0], bone.position[1], bone.position[2])); + var rotx = Matrix4x4.FromRotationX(bone.rotation[0]); + var roty = Matrix4x4.FromRotationY(bone.rotation[1]); + var rotz = Matrix4x4.FromRotationZ(bone.rotation[2]); + var sca = Matrix4x4.FromScaling(new Vector3D(bone.scale[0], bone.scale[1], bone.scale[2])); + + return sca * (rotx * roty * rotz) * pos; + } + public static string GetSaveFilter() { return "Supported Formats|*.dae;*.stl;*.obj; *.ply; *.x;*.3ds;*.json;|" + diff --git a/Switch_Toolbox_Library/FileFormats/Assimp/AssimpSaver.cs b/Switch_Toolbox_Library/FileFormats/Assimp/AssimpSaver.cs new file mode 100644 index 00000000..51028ce0 --- /dev/null +++ b/Switch_Toolbox_Library/FileFormats/Assimp/AssimpSaver.cs @@ -0,0 +1,287 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Assimp; +using Switch_Toolbox.Library; +using Switch_Toolbox.Library.Animations; +using Switch_Toolbox.Library.Rendering; +using System.Windows.Forms; + +namespace Switch_Toolbox.Library +{ + public class AssimpSaver + { + public List BoneNames = new List(); + + public void SaveFromModel(STGenericModel model, string FileName, List Textures, STSkeleton skeleton = null, List NodeArray = null) + { + Scene scene = new Scene(); + scene.RootNode = new Node("RootNode"); + + SaveSkeleton(skeleton, scene.RootNode); + SaveMaterials(scene, model, FileName, Textures); + SaveMeshes(scene, model, skeleton, FileName, NodeArray); + + using (var v = new AssimpContext()) + { + string ext = System.IO.Path.GetExtension(FileName); + + string formatID = "collada"; + if (ext == ".obj") + formatID = "obj"; + if (ext == ".3ds") + formatID = "3ds"; + if (ext == ".dae") + formatID = "collada"; + if (ext == ".ply") + formatID = "ply"; + + if (v.ExportFile(scene, FileName, formatID, PostProcessSteps.ValidateDataStructure)) + MessageBox.Show($"Exported {FileName} Successfuly!"); + else + MessageBox.Show($"Failed to export {FileName}!"); + } + } + + private void SaveMeshes(Scene scene, STGenericModel model, STSkeleton skeleton, string FileName, List NodeArray) + { + int MeshIndex = 0; + foreach (var obj in model.Nodes[0].Nodes) + { + var genericObj = (STGenericObject)obj; + + Mesh mesh = new Mesh(genericObj.Text, PrimitiveType.Triangle); + mesh.MaterialIndex = genericObj.MaterialIndex; + + List textureCoords0 = new List(); + List textureCoords1 = new List(); + List textureCoords2 = new List(); + List vertexColors = new List(); + + int vertexID = 0; + foreach (Vertex v in genericObj.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 j = 0; j < v.boneIds.Count; j++) + { + if (j < genericObj.VertexSkinCount) + { + //Get the bone via the node array and bone index from the vertex + STBone STbone = skeleton.bones[NodeArray[v.boneIds[j]]]; + + //Find the index of a bone. If it doesn't exist then we add it + int boneInd = mesh.Bones.FindIndex(x => x.Name == STbone.Text); + + if (boneInd == -1) + { + var matrices = Switch_Toolbox.Library.IO.MatrixExenstion.CalculateInverseMatrix(STbone); + + //Set the inverse matrix + Matrix4x4 transform = matrices.inverse.FromNumerics(); + + //Create a new assimp bone + Bone bone = new Bone(); + bone.Name = STbone.Text; + bone.OffsetMatrix = transform; + + mesh.Bones.Add(bone); + BoneNames.Add(bone.Name); + + boneInd = mesh.Bones.IndexOf(bone); //Set the index of the bone for the vertex weight + } + + //Check if the max amount of weights is higher than the current bone id + if (v.boneWeights.Count > j) + { + if (v.boneWeights[j] <= 1) + mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, v.boneWeights[j])); + else + mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, 1)); + } + else + mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, 1)); + } + } + vertexID++; + } + List faces = genericObj.lodMeshes[genericObj.DisplayLODIndex].faces; + for (int f = 0; f < faces.Count; f++) + mesh.Faces.Add(new Face(new int[] { faces[f++], faces[f++], faces[f] })); + + 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); + } + + private void SaveMaterials(Scene scene, STGenericModel model, string FileName, List Textures) + { + + string TextureExtension = ".png"; + string TexturePath = System.IO.Path.GetDirectoryName(FileName); + + foreach (var tex in Textures) + { + string path = System.IO.Path.Combine(TexturePath, tex.Text + TextureExtension); + + var bitmap = tex.GetBitmap(); + bitmap.Save(path); + bitmap.Dispose(); + + } + + foreach (var mat in model.Nodes[1].Nodes) + { + var genericMat = (STGenericMaterial)mat; + + Material material = new Material(); + material.Name = genericMat.Text; + + foreach (var tex in genericMat.TextureMaps) + { + TextureSlot slot = new TextureSlot(); + string path = System.IO.Path.Combine(TexturePath, tex.Name + TextureExtension); + slot.FilePath = path; + slot.UVIndex = 0; + slot.Flags = 0; + slot.TextureIndex = 0; + slot.BlendFactor = 1.0f; + slot.Mapping = TextureMapping.FromUV; + slot.Operation = TextureOperation.Add; + + if (tex.Type == STGenericMatTexture.TextureType.Diffuse) + slot.TextureType = TextureType.Diffuse; + else if (tex.Type == STGenericMatTexture.TextureType.Normal) + slot.TextureType = TextureType.Normals; + else if (tex.Type == STGenericMatTexture.TextureType.Specular) + slot.TextureType = TextureType.Specular; + else if (tex.Type == STGenericMatTexture.TextureType.Emission) + slot.TextureType = TextureType.Emissive; + else if (tex.Type == STGenericMatTexture.TextureType.Light) + { + slot.TextureType = TextureType.Lightmap; + slot.UVIndex = 2; + } + else if (tex.Type == STGenericMatTexture.TextureType.Shadow) + { + slot.TextureType = TextureType.Ambient; + slot.UVIndex = 1; + } + else + slot.TextureType = TextureType.Unknown; + + if (tex.wrapModeS == 0) + slot.WrapModeU = TextureWrapMode.Wrap; + if (tex.wrapModeS == 1) + slot.WrapModeU = TextureWrapMode.Mirror; + if (tex.wrapModeS == 2) + slot.WrapModeU = TextureWrapMode.Clamp; + if (tex.wrapModeT == 0) + slot.WrapModeV = TextureWrapMode.Wrap; + if (tex.wrapModeT == 1) + slot.WrapModeV = TextureWrapMode.Mirror; + if (tex.wrapModeT == 2) + slot.WrapModeV = TextureWrapMode.Clamp; + else + { + slot.WrapModeU = TextureWrapMode.Wrap; + slot.WrapModeV = TextureWrapMode.Wrap; + } + + material.AddMaterialTexture(ref slot); + } + scene.Materials.Add(material); + } + + } + + public void SaveFromObject(List vertices, List faces, string MeshName, string FileName) + { + Scene scene = new Scene(); + scene.RootNode = new Node("Root"); + + Mesh mesh = new Mesh(MeshName, PrimitiveType.Triangle); + + List textureCoords0 = new List(); + List textureCoords1 = new List(); + List textureCoords2 = new List(); + List vertexColors = new List(); + + 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] })); + } + 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"); + } + } + + private void SaveSkeleton(STSkeleton skeleton, Node parentNode) + { + Node root = new Node("skeleton_root"); + parentNode.Children.Add(root); + + if (skeleton.bones.Count > 0) + { + Node boneNode = new Node(skeleton.bones[0].Text); + boneNode.Transform = AssimpHelper.GetBoneMatrix(skeleton.bones[0]); + root.Children.Add(boneNode); + + foreach (STBone child in skeleton.bones[0].GetChildren()) + SaveBones(boneNode, child, skeleton); + } + } + private void SaveBones(Node parentBone, STBone bone, STSkeleton skeleton) + { + Node boneNode = new Node(bone.Text); + parentBone.Children.Add(boneNode); + + boneNode.Transform = AssimpHelper.GetBoneMatrix(bone); + + foreach (STBone child in bone.GetChildren()) + SaveBones(boneNode, child, skeleton); + } + } +} diff --git a/Switch_Toolbox_Library/Switch_Toolbox_Library.csproj b/Switch_Toolbox_Library/Switch_Toolbox_Library.csproj index 3ab01280..6d8507e3 100644 --- a/Switch_Toolbox_Library/Switch_Toolbox_Library.csproj +++ b/Switch_Toolbox_Library/Switch_Toolbox_Library.csproj @@ -199,6 +199,7 @@ + Form