public KeyFrame[] GetFrame(float frame) { if (keys.Count == 0) { return(null); } KeyFrame k1 = (KeyFrame)keys[0], k2 = (KeyFrame)keys[0]; foreach (KeyFrame k in keys) { if (k.Frame < frame) { k1 = k; } else { k2 = k; break; } } return(new KeyFrame[] { k1, k2 }); }
public float GetValue(float frame) { KeyFrame k1 = (KeyFrame)keys[0], k2 = (KeyFrame)keys[0]; int i = 0; if (frame < lastFrame) { lastFound = 0; } for (i = lastFound; i < keys.Count; i++) { lastFound = i % (keys.Count); KeyFrame k = keys[lastFound]; if (k.Frame < frame) { k1 = k; } else { k2 = k; break; } } lastFound -= 1; if (lastFound < 0) { lastFound = 0; } if (lastFound >= keys.Count - 2) { lastFound = 0; } lastFrame = frame; if (k1.interType == InterpolationType.Constant) { return(k1.Value); } if (k1.interType == InterpolationType.Step) { return(k1.Value); } if (k1.interType == InterpolationType.Linear) { return(Lerp(k1.Value, k2.Value, k1.Frame, k2.Frame, frame)); } if (k1.interType == InterpolationType.Hermite) { float val = Hermite(frame, k1.Frame, k2.Frame, k1.In, k1.Out != -1 ? k1.Out : k2.In, k1.Value, k2.Value) * (k1.degrees ? (float)Math.PI / 180 : 1); if (Parent != null && Text.Equals("XROT")) { Console.WriteLine(Text + " " + k1.Value + " " + k2.Value + " " + k1.Frame + " " + k2.Frame + " " + (val * 180 / (float)Math.PI)); } if (float.IsNaN(val)) { val = k1.value; } return(val);//k1.Out != -1 ? k1.Out : } return(k1.Value); }
public static void Read(string fname, Animation a, VBN v) { StreamReader reader = File.OpenText(fname); string line; string current = ""; bool readBones = false; int frame = 0, prevframe = 0; KeyFrame k = new KeyFrame(); VBN vbn = v; if (v != null && v.bones.Count == 0) { readBones = true; } else { vbn = new VBN(); } 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 KeyFrame(); k.frame = frame; //a.addKeyframe(k); } continue; } if (current.Equals("nodes")) { Bone b = new Bone(vbn); b.Text = args[1].Replace("\"", ""); b.parentIndex = int.Parse(args[2]); //b.children = new System.Collections.Generic.List<int> (); vbn.totalBoneCount++; vbn.bones.Add(b); Animation.KeyNode node = new Animation.KeyNode(b.Text); a.bones.Add(node); } if (current.Equals("time")) { //Animation.KeyFrame n = new Animation.KeyFrame(); /*n.id = v.boneIndex(vbn.bones[int.Parse(args[0])].Text); * if (n.id == -1) * { * continue; * } * else * n.hash = v.bones[n.id].boneId;*/ // only if it finds the node //k.addNode(n); // reading the skeleton if this isn't an animation if (readBones && frame == 0) { Bone b = vbn.bones[int.Parse(args[0])]; b.position = new float[3]; b.rotation = new float[3]; b.scale = new float[3]; b.position[0] = float.Parse(args[1]); b.position[1] = float.Parse(args[2]); b.position[2] = float.Parse(args[3]); b.rotation[0] = float.Parse(args[4]); b.rotation[1] = float.Parse(args[5]); b.rotation[2] = float.Parse(args[6]); b.scale[0] = 1f; b.scale[1] = 1f; b.scale[2] = 1f; b.pos = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3])); b.rot = VBN.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); } } } a.frameCount = frame; vbn.boneCountPerType[0] = (uint)vbn.bones.Count; vbn.update(); }
/* * TODO: Fix this * the key frame needs to check if it occurs within a time frame AND * it has the node it is looking for */ public void BakeFramesLinear() { List <int> nodeids = GetNodes(false); List <KeyFrame> baseFrames = frames; frames = new List <KeyFrame>(); int fCount = 0; foreach (KeyFrame k in baseFrames) { if (k.frame > fCount) { fCount = k.frame; } } for (int i = 0; i < fCount; i++) { KeyFrame k = new KeyFrame(); k.frame = i; frames.Add(k); // add all the nodes at this frame foreach (int id in nodeids) { KeyFrame f1 = baseFrames[0], f2 = baseFrames[0]; if (baseFrames.Count > 1) { for (int j = 0; j < baseFrames.Count - 1; j++) { if (baseFrames[j].frame <= i && baseFrames[j + 1].frame >= i && baseFrames[j].Contains(id) && baseFrames[j + 1].Contains(id)) { f1 = baseFrames[j]; f2 = baseFrames[j + 1]; break; } } } // interpolate the values KeyNode n = new KeyNode(); n.id = id; KeyNode k1 = f1.GetNodeid(id), k2 = f2.GetNodeid(id); n.hash = k1.hash; n.tType = k1.tType; n.rType = k1.rType; n.sType = k1.sType; n.t.X = Lerp(k1.t.X, k2.t.X, f1.frame, f2.frame, i); n.t.Y = Lerp(k1.t.Y, k2.t.Y, f1.frame, f2.frame, i); n.t.Z = Lerp(k1.t.Z, k2.t.Z, f1.frame, f2.frame, i); n.r.X = Lerp(k1.r.X, k2.r.X, f1.frame, f2.frame, i); n.r.Y = Lerp(k1.r.Y, k2.r.Y, f1.frame, f2.frame, i); n.r.Z = Lerp(k1.r.Z, k2.r.Z, f1.frame, f2.frame, i); n.r.W = Lerp(k1.r.W, k2.r.W, f1.frame, f2.frame, i); //n.s.X = lerp (k1.s.X, k2.s.X, f1.frame, f2.frame, i); //n.s.Y = lerp (k1.s.Y, k2.s.Y, f1.frame, f2.frame, i); //n.s.Z = lerp (k1.s.Z, k2.s.Z, f1.frame, f2.frame, i); k.AddNode(n); } } }
public void NextFrame(VBN vbn, bool isChild = false) { if (frame >= frames.Count) { return; } if (frame == 0 && (!isChild || children.Count > 0)) { vbn.reset(); /*foreach (ModelContainer con in Runtime.ModelContainers) * { * if (con.NUD != null && con.mta != null) * { * con.NUD.applyMTA(con.mta, 0); * } * }*/ } foreach (object child in children) { if (child is SkelAnimation) { ((SkelAnimation)child).SetFrame(frame); ((SkelAnimation)child).NextFrame(vbn, isChild: true); } if (child is MTA) { /*foreach(ModelContainer con in Runtime.ModelContainers) * { * if(con.NUD != null) * { * con.NUD.applyMTA(((MTA)child), frame); * } * }*/ } } KeyFrame key = frames[frame]; foreach (KeyNode n in key.nodes) { //if (n.id == -1) // continue; //if (n.hash == 0) { //continue; //n.hash = vbn.bones [n.id].boneId; //} int id = -1; foreach (Bone bo in vbn.bones) { if (bo.boneId == n.hash) { id = vbn.bones.IndexOf(bo); n.id = id; break; } } if (id == -1) { continue; } Bone b = vbn.bones[id]; if (n.tType != -1)// !b.isSwingBone) { b.pos = n.t; } // We don't do the same swingBone check on rotation because as of yet // I have not seen an example of the rotation data being garbage, and it's // actually used properly in the animations - Struz if (n.rType != -1) { if (b.Text.Equals("HeadN")) { //Console.WriteLine(b.transform.ExtractRotation().ToString()); //Console.WriteLine(VBN.FromEulerAngles(b.rotation[0], b.rotation[1], b.rotation[2]).ToString()); //Console.WriteLine(n.r.ToString() + " " + Math.Sqrt(n.r.X* n.r.X + n.r.Y* n.r.Y + n.r.Z* n.r.Z + n.r.W*n.r.W) + " " + n.r.Normalized().ToString()); } //Console.WriteLine(new string(b.boneName) + " " + b.rot.ToString() + " " + n.r.ToString() + "\n" + (n.r.X + n.r.Y + n.r.X + n.r.W)); b.rot = n.r; } if (n.sType != -1) { b.sca = n.s; } else { b.sca = new Vector3(b.scale[0], b.scale[1], b.scale[2]); } } frame++; if (frame >= frames.Count) { frame = 0; } vbn.update(); }
public void AddKeyframe(KeyFrame k) { frames.Add(k); }