public Matrix4 CalculateMatrix(Vector3 Rotation, Vector3 Position, float twist) { if (settings.HasFlag(Settings.EulerZXY)) { return(Matrix4.CreateFromQuaternion(STSkeleton.FromEulerAngles(Rotation.Z, Rotation.Y, Rotation.X))); } else { float rad2deg = (float)(180.0 / Math.PI); var c = Matrix4.CreateRotationZ(twist); var rotation = LookAtAngles(Position) * rad2deg; return(Matrix4.CreateFromQuaternion(STSkeleton.FromEulerAngles(rotation.Z, rotation.Y, rotation.X)) * c); } }
public void Read(string fname) { 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])]; 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; 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(); }
public static Animation Read(string fname, STSkeleton v) { Animation a = new Animation(); 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])]; 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; 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); } 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, }); } } } a.FrameCount = frame; vbn.update(); return(a); }