2019-03-30 02:57:31 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using System.Text;
|
2020-06-16 03:07:26 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Globalization;
|
2019-03-30 02:57:31 +01:00
|
|
|
|
|
2019-07-16 23:35:21 +02:00
|
|
|
|
namespace Toolbox.Library.Animations
|
2019-03-30 02:57:31 +01:00
|
|
|
|
{
|
|
|
|
|
//Todo rewrite this
|
|
|
|
|
//Currently from forge
|
|
|
|
|
//https://raw.githubusercontent.com/jam1garner/Smash-Forge/master/Smash%20Forge/Filetypes/SMD.cs
|
|
|
|
|
public class SMD
|
|
|
|
|
{
|
|
|
|
|
public STSkeleton Bones;
|
|
|
|
|
public Animation Animation; // todo
|
|
|
|
|
|
|
|
|
|
public SMD()
|
|
|
|
|
{
|
|
|
|
|
Bones = new STSkeleton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SMD(string fname)
|
|
|
|
|
{
|
|
|
|
|
Read(fname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Read(string fname)
|
|
|
|
|
{
|
2020-06-16 03:07:26 +02:00
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2019-03-30 02:57:31 +01:00
|
|
|
|
StreamReader reader = File.OpenText(fname);
|
|
|
|
|
string line;
|
|
|
|
|
|
|
|
|
|
string current = "";
|
|
|
|
|
|
|
|
|
|
Bones = new STSkeleton();
|
|
|
|
|
Dictionary<int, STBone> BoneList = new Dictionary<int, STBone>();
|
|
|
|
|
|
|
|
|
|
int time = 0;
|
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
line = Regex.Replace(line, @"\s+", " ");
|
|
|
|
|
string[] args = line.Replace(";", "").TrimStart().Split(' ');
|
|
|
|
|
|
|
|
|
|
if (args[0].Equals("triangles") || args[0].Equals("end") || args[0].Equals("skeleton") || args[0].Equals("nodes"))
|
|
|
|
|
{
|
|
|
|
|
current = args[0];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (current.Equals("nodes"))
|
|
|
|
|
{
|
|
|
|
|
int id = int.Parse(args[0]);
|
|
|
|
|
STBone b = new STBone(Bones);
|
|
|
|
|
b.Text = args[1].Replace('"', ' ').Trim();
|
|
|
|
|
int s = 2;
|
|
|
|
|
while (args[s].Contains("\""))
|
|
|
|
|
b.Text += args[s++];
|
|
|
|
|
b.parentIndex = int.Parse(args[s]);
|
|
|
|
|
BoneList.Add(id, b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (current.Equals("skeleton"))
|
|
|
|
|
{
|
|
|
|
|
if (args[0].Contains("time"))
|
|
|
|
|
time = int.Parse(args[1]);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (time == 0)
|
|
|
|
|
{
|
|
|
|
|
STBone b = BoneList[int.Parse(args[0])];
|
2020-02-07 01:47:32 +01:00
|
|
|
|
b.Position = new Vector3(
|
|
|
|
|
float.Parse(args[1]),
|
|
|
|
|
float.Parse(args[2]),
|
|
|
|
|
float.Parse(args[3]));
|
|
|
|
|
b.EulerRotation = new Vector3(
|
|
|
|
|
float.Parse(args[4]),
|
|
|
|
|
float.Parse(args[5]),
|
|
|
|
|
float.Parse(args[6]));
|
|
|
|
|
b.Scale = Vector3.One;
|
2019-03-30 02:57:31 +01:00
|
|
|
|
|
|
|
|
|
b.pos = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3]));
|
|
|
|
|
b.rot = STSkeleton.FromEulerAngles(float.Parse(args[6]), float.Parse(args[5]), float.Parse(args[4]));
|
|
|
|
|
|
|
|
|
|
Bones.bones.Add(b);
|
|
|
|
|
|
|
|
|
|
if (b.parentIndex != -1)
|
|
|
|
|
b.parentIndex = Bones.bones.IndexOf(BoneList[b.parentIndex]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Bones.reset();
|
2020-06-16 03:07:26 +02:00
|
|
|
|
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = CultureInfo.DefaultThreadCurrentCulture;
|
2019-03-30 02:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save(string FileName)
|
|
|
|
|
{
|
2020-10-24 01:36:18 +02:00
|
|
|
|
var culture = new CultureInfo("en-US");
|
2020-07-18 23:11:37 +02:00
|
|
|
|
|
2019-03-30 02:57:31 +01:00
|
|
|
|
StringBuilder o = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
o.AppendLine("version 1");
|
|
|
|
|
|
|
|
|
|
if (Bones != null)
|
|
|
|
|
{
|
|
|
|
|
o.AppendLine("nodes");
|
|
|
|
|
for (int i = 0; i < Bones.bones.Count; i++)
|
|
|
|
|
o.AppendLine(" " + i + " \"" + Bones.bones[i].Text + "\" " + Bones.bones[i].parentIndex);
|
|
|
|
|
o.AppendLine("end");
|
|
|
|
|
|
|
|
|
|
o.AppendLine("skeleton");
|
|
|
|
|
o.AppendLine("time 0");
|
|
|
|
|
for (int i = 0; i < Bones.bones.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
STBone b = Bones.bones[i];
|
2020-10-24 01:36:18 +02:00
|
|
|
|
o.AppendFormat(culture, "{0} {1} {2} {3} {4} {5} {6}\n", i,
|
2020-02-07 01:47:32 +01:00
|
|
|
|
b.Position.X,
|
|
|
|
|
b.Position.Y,
|
|
|
|
|
b.Position.Z,
|
|
|
|
|
b.EulerRotation.X,
|
|
|
|
|
b.EulerRotation.Y,
|
|
|
|
|
b.EulerRotation.Z);
|
2019-03-30 02:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
o.AppendLine("end");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(FileName, o.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-26 16:56:23 +02:00
|
|
|
|
public static Animation Read(string fname,STSkeleton v)
|
2019-03-30 02:57:31 +01:00
|
|
|
|
{
|
2019-05-26 16:56:23 +02:00
|
|
|
|
Animation a = new Animation();
|
|
|
|
|
|
2019-03-30 02:57:31 +01:00
|
|
|
|
StreamReader reader = File.OpenText(fname);
|
|
|
|
|
string line;
|
|
|
|
|
|
|
|
|
|
string current = "";
|
|
|
|
|
bool readBones = false;
|
|
|
|
|
int frame = 0, prevframe = 0;
|
|
|
|
|
Animation.KeyFrame k = new Animation.KeyFrame();
|
|
|
|
|
|
|
|
|
|
STSkeleton vbn = v;
|
|
|
|
|
if (v != null && v.bones.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
readBones = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
vbn = new STSkeleton();
|
|
|
|
|
|
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
line = Regex.Replace(line, @"\s+", " ");
|
|
|
|
|
string[] args = line.Replace(";", "").TrimStart().Split(' ');
|
|
|
|
|
|
|
|
|
|
if (args[0].Equals("nodes") || args[0].Equals("skeleton") || args[0].Equals("end") || args[0].Equals("time"))
|
|
|
|
|
{
|
|
|
|
|
current = args[0];
|
|
|
|
|
if (args.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
prevframe = frame;
|
|
|
|
|
frame = int.Parse(args[1]);
|
|
|
|
|
|
|
|
|
|
/*if (frame != prevframe + 1) {
|
|
|
|
|
Console.WriteLine ("Needs interpolation " + frame);
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
k = new Animation.KeyFrame();
|
|
|
|
|
k.Frame = frame;
|
|
|
|
|
//a.addKeyframe(k);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (current.Equals("nodes"))
|
|
|
|
|
{
|
|
|
|
|
STBone b = new STBone(vbn);
|
|
|
|
|
b.Text = args[1].Replace("\"", "");
|
|
|
|
|
b.parentIndex = int.Parse(args[2]);
|
|
|
|
|
//b.children = new System.Collections.Generic.List<int> ();
|
|
|
|
|
vbn.bones.Add(b);
|
|
|
|
|
Animation.KeyNode node = new Animation.KeyNode(b.Text);
|
|
|
|
|
a.Bones.Add(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (current.Equals("time"))
|
|
|
|
|
{
|
|
|
|
|
// reading the skeleton if this isn't an animation
|
|
|
|
|
if (readBones && frame == 0)
|
|
|
|
|
{
|
|
|
|
|
STBone b = vbn.bones[int.Parse(args[0])];
|
2020-02-07 01:47:32 +01:00
|
|
|
|
b.Position = new Vector3(
|
|
|
|
|
float.Parse(args[1]),
|
|
|
|
|
float.Parse(args[2]),
|
|
|
|
|
float.Parse(args[3]));
|
|
|
|
|
b.EulerRotation = new Vector3(
|
|
|
|
|
float.Parse(args[4]),
|
|
|
|
|
float.Parse(args[5]),
|
|
|
|
|
float.Parse(args[6]));
|
|
|
|
|
b.Scale = Vector3.One;
|
2019-03-30 02:57:31 +01:00
|
|
|
|
|
|
|
|
|
b.pos = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3]));
|
|
|
|
|
b.rot = STSkeleton.FromEulerAngles(float.Parse(args[6]), float.Parse(args[5]), float.Parse(args[4]));
|
|
|
|
|
|
|
|
|
|
if (b.parentIndex != -1)
|
|
|
|
|
vbn.bones[b.parentIndex].Nodes.Add(b);
|
|
|
|
|
}
|
|
|
|
|
Animation.KeyNode bone = a.GetBone(vbn.bones[int.Parse(args[0])].Text);
|
|
|
|
|
bone.RotType = Animation.RotationType.EULER;
|
|
|
|
|
|
|
|
|
|
Animation.KeyFrame n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[1]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.XPOS.Keys.Add(n);
|
|
|
|
|
|
|
|
|
|
n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[2]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.YPOS.Keys.Add(n);
|
|
|
|
|
|
|
|
|
|
n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[3]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.ZPOS.Keys.Add(n);
|
|
|
|
|
|
|
|
|
|
n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[4]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.XROT.Keys.Add(n);
|
|
|
|
|
|
|
|
|
|
n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[5]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.YROT.Keys.Add(n);
|
|
|
|
|
|
|
|
|
|
n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[6]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.ZROT.Keys.Add(n);
|
|
|
|
|
|
|
|
|
|
if (args.Length > 7)
|
|
|
|
|
{
|
|
|
|
|
n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[7]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.XSCA.Keys.Add(n);
|
|
|
|
|
|
|
|
|
|
n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[8]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.YSCA.Keys.Add(n);
|
|
|
|
|
|
|
|
|
|
n = new Animation.KeyFrame();
|
|
|
|
|
n.Value = float.Parse(args[9]);
|
|
|
|
|
n.Frame = frame;
|
|
|
|
|
bone.ZSCA.Keys.Add(n);
|
|
|
|
|
}
|
2019-05-26 16:56:23 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bone.XSCA.Keys.Add(new Animation.KeyFrame()
|
|
|
|
|
{
|
|
|
|
|
Value = 1.0f,
|
|
|
|
|
Frame = frame,
|
|
|
|
|
});
|
|
|
|
|
bone.YSCA.Keys.Add(new Animation.KeyFrame()
|
|
|
|
|
{
|
|
|
|
|
Value = 1.0f,
|
|
|
|
|
Frame = frame,
|
|
|
|
|
});
|
|
|
|
|
bone.ZSCA.Keys.Add(new Animation.KeyFrame()
|
|
|
|
|
{
|
|
|
|
|
Value = 1.0f,
|
|
|
|
|
Frame = frame,
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-03-30 02:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.FrameCount = frame;
|
|
|
|
|
vbn.update();
|
2019-05-26 16:56:23 +02:00
|
|
|
|
|
|
|
|
|
return a;
|
2019-03-30 02:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-10 17:44:52 +02:00
|
|
|
|
public static void Save(STSkeletonAnimation anim, String Fname)
|
|
|
|
|
{
|
2021-03-06 17:12:39 +01:00
|
|
|
|
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
|
2020-05-10 17:44:52 +02:00
|
|
|
|
|
|
|
|
|
STSkeleton Skeleton = anim.GetActiveSkeleton();
|
|
|
|
|
|
|
|
|
|
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@Fname))
|
|
|
|
|
{
|
|
|
|
|
file.WriteLine("version 1");
|
|
|
|
|
|
|
|
|
|
file.WriteLine("nodes");
|
|
|
|
|
foreach (STBone b in Skeleton.bones)
|
|
|
|
|
{
|
|
|
|
|
file.WriteLine(Skeleton.bones.IndexOf(b) + " \"" + b.Text + "\" " + b.parentIndex);
|
|
|
|
|
}
|
|
|
|
|
file.WriteLine("end");
|
|
|
|
|
|
|
|
|
|
file.WriteLine("skeleton");
|
|
|
|
|
anim.SetFrame(0);
|
|
|
|
|
for (int i = 0; i <= anim.FrameCount; i++)
|
|
|
|
|
{
|
|
|
|
|
anim.SetFrame(i);
|
|
|
|
|
anim.NextFrame();
|
|
|
|
|
|
|
|
|
|
file.WriteLine($"time {i}");
|
|
|
|
|
|
|
|
|
|
foreach (var sb in anim.AnimGroups)
|
|
|
|
|
{
|
|
|
|
|
STBone b = Skeleton.GetBone(sb.Name);
|
|
|
|
|
if (b == null) continue;
|
|
|
|
|
Vector3 eul = STMath.ToEulerAngles(b.rot);
|
|
|
|
|
Vector3 scale = b.GetScale();
|
|
|
|
|
Vector3 translate = b.GetPosition();
|
|
|
|
|
|
|
|
|
|
file.WriteLine($"{ Skeleton.bones.IndexOf(b)} {translate.X} {translate.Y} {translate.Z} {eul.X} {eul.Y} {eul.Z}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
file.WriteLine("end");
|
|
|
|
|
|
|
|
|
|
file.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 02:57:31 +01:00
|
|
|
|
public static void Save(Animation anim, STSkeleton Skeleton, String Fname)
|
|
|
|
|
{
|
2021-03-06 17:12:39 +01:00
|
|
|
|
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
|
2020-02-14 00:55:57 +01:00
|
|
|
|
|
2019-03-30 02:57:31 +01:00
|
|
|
|
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@Fname))
|
|
|
|
|
{
|
|
|
|
|
file.WriteLine("version 1");
|
|
|
|
|
|
|
|
|
|
file.WriteLine("nodes");
|
|
|
|
|
foreach (STBone b in Skeleton.bones)
|
|
|
|
|
{
|
|
|
|
|
file.WriteLine(Skeleton.bones.IndexOf(b) + " \"" + b.Text + "\" " + b.parentIndex);
|
|
|
|
|
}
|
|
|
|
|
file.WriteLine("end");
|
|
|
|
|
|
|
|
|
|
file.WriteLine("skeleton");
|
|
|
|
|
anim.SetFrame(0);
|
|
|
|
|
for (int i = 0; i <= anim.FrameCount; i++)
|
|
|
|
|
{
|
2019-05-15 21:35:05 +02:00
|
|
|
|
anim.NextFrame(Skeleton, false, true);
|
2019-03-30 02:57:31 +01:00
|
|
|
|
|
2019-05-26 17:02:07 +02:00
|
|
|
|
file.WriteLine($"time {i}");
|
2019-03-30 02:57:31 +01:00
|
|
|
|
|
|
|
|
|
foreach (Animation.KeyNode sb in anim.Bones)
|
|
|
|
|
{
|
|
|
|
|
STBone b = Skeleton.GetBone(sb.Text);
|
|
|
|
|
if (b == null) continue;
|
2019-05-26 16:56:23 +02:00
|
|
|
|
Vector3 eul = STMath.ToEulerAngles(b.rot);
|
2019-05-26 17:02:07 +02:00
|
|
|
|
Vector3 scale = b.GetScale();
|
|
|
|
|
Vector3 translate = b.GetPosition();
|
|
|
|
|
|
2021-03-06 17:15:24 +01:00
|
|
|
|
file.WriteLine($"{ Skeleton.bones.IndexOf(b)} {translate.X} {translate.Y} {translate.Z} {eul.X} {eul.Y} {eul.Z}");
|
2019-03-30 02:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
file.WriteLine("end");
|
|
|
|
|
|
|
|
|
|
file.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|