private static string CreateWeightList(IOVertex v, int Count) { StringBuilder WeightList = new StringBuilder(); for (int i = 0; i < Count; i++) { WeightList.Append($"{v.BoneIndices[i]} {v.BoneWeights[i]} "); } return(WeightList.ToString()); }
public static void ExportIOModelAsSMD(string FileName, IOModel Model) { StringBuilder o = new StringBuilder(); o.AppendLine("version 1"); //skeleton if (Model.HasSkeleton) { o.AppendLine("nodes"); foreach (RBone bone in Model.Skeleton.Bones) { o.AppendLine($"{bone.Id} \"{bone.Name}\" {bone.ParentId}"); } o.AppendLine("end"); o.AppendLine("skeleton"); o.AppendLine("time 0"); foreach (RBone bone in Model.Skeleton.Bones) { Vector3 Position = bone.Position; Vector3 Rotation = bone.EulerRotation; o.AppendLine($"{bone.Id} {Position.X} {Position.Y} {Position.Z} {Rotation.X} {Rotation.Y} {Rotation.Z}"); } o.AppendLine("end"); } //meshes if (Model.HasMeshes) { // keep track of used names so we don't have name overlap Dictionary <string, int> UsedNames = new Dictionary <string, int>(); //begin triangles o.AppendLine("triangles"); // go through each mesh foreach (IOMesh mesh in Model.Meshes) { // append the index number for this mesh name string Meshname = mesh.Name; if (UsedNames.ContainsKey(mesh.Name)) { UsedNames[mesh.Name] += 1; Meshname += $"_{UsedNames[mesh.Name]}"; } else { UsedNames.Add(mesh.Name, 0); } // add a triangle strip // IOMesh are assumed to be triangles only for (int i = 0; i < mesh.Indices.Count; i += 3) { o.AppendLine(Meshname); { IOVertex v = mesh.Vertices[(int)mesh.Indices[i]]; o.AppendLine($"0 {v.Position.X} {v.Position.Y} {v.Position.Z} {v.Normal.X} {v.Normal.Y} {v.Normal.Z} {v.UV0.X} {v.UV0.Y} " + CountWeights(v.BoneWeights) + " " + CreateWeightList(v, CountWeights(v.BoneWeights))); } { IOVertex v = mesh.Vertices[(int)mesh.Indices[i + 1]]; o.AppendLine($"0 {v.Position.X} {v.Position.Y} {v.Position.Z} {v.Normal.X} {v.Normal.Y} {v.Normal.Z} {v.UV0.X} {v.UV0.Y} " + CountWeights(v.BoneWeights) + " " + CreateWeightList(v, CountWeights(v.BoneWeights))); } { IOVertex v = mesh.Vertices[(int)mesh.Indices[i + 2]]; o.AppendLine($"0 {v.Position.X} {v.Position.Y} {v.Position.Z} {v.Normal.X} {v.Normal.Y} {v.Normal.Z} {v.UV0.X} {v.UV0.Y} " + CountWeights(v.BoneWeights) + " " + CreateWeightList(v, CountWeights(v.BoneWeights))); } } } // done with triangle section o.AppendLine("end"); } File.WriteAllText(FileName, o.ToString()); }