public static void Save(Animation anim, VBN skeleton, String fname) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(fname)) { file.WriteLine("version 1"); file.WriteLine("nodes"); foreach (Bone 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.NextFrame(skeleton); file.WriteLine("time " + i); foreach (Animation.KeyNode sb in anim.bones) { Bone b = skeleton.getBone(sb.Text); if (b == null) { continue; } Vector3 eul = ANIM.quattoeul(b.rot); file.WriteLine(skeleton.bones.IndexOf(b) + " " + b.pos.X + " " + b.pos.Y + " " + b.pos.Z + " " + eul.X + " " + eul.Y + " " + eul.Z); } } file.WriteLine("end"); file.Close(); } }
public void NextFrame(VBN skeleton, bool isChild = false) { if (frame >= frameCount) { return; } if (frame == 0 && !isChild) { skeleton.reset(); } foreach (object child in children) { if (child is Animation) { ((Animation)child).SetFrame(frame); ((Animation)child).NextFrame(skeleton, isChild: true); } if (child is MTA) { //foreach (ModelContainer con in Runtime.ModelContainers) { if (((ModelContainer)skeleton.Parent).NUD != null) { ((ModelContainer)skeleton.Parent).NUD.ApplyMta(((MTA)child), (int)frame); } } } if (child is BFRES.MTA) //For BFRES { { if (((ModelContainer)skeleton.Parent).Bfres != null) { ((ModelContainer)skeleton.Parent).Bfres.ApplyMta(((BFRES.MTA)child), (int)frame); } } } } bool updated = false; // no need to update skeleton of animations that didn't change foreach (KeyNode node in bones) { // Get Skeleton Node Bone b = null; if (node.hash == -1) { b = skeleton.getBone(node.Text); } else { b = skeleton.GetBone((uint)node.hash); } if (b == null) { continue; } updated = true; if (node.xpos.HasAnimation() && b.boneType != 3) { b.pos.X = node.xpos.GetValue(frame); } if (node.ypos.HasAnimation() && b.boneType != 3) { b.pos.Y = node.ypos.GetValue(frame); } if (node.zpos.HasAnimation() && b.boneType != 3) { b.pos.Z = node.zpos.GetValue(frame); } if (node.xsca.HasAnimation()) { b.sca.X = node.xsca.GetValue(frame); } else { b.sca.X = 1; } if (node.ysca.HasAnimation()) { b.sca.Y = node.ysca.GetValue(frame); } else { b.sca.Y = 1; } if (node.zsca.HasAnimation()) { b.sca.Z = node.zsca.GetValue(frame); } else { b.sca.Z = 1; } if (node.xrot.HasAnimation() || node.yrot.HasAnimation() || node.zrot.HasAnimation()) { if (node.rotType == RotationType.Quaternion) { KeyFrame[] x = node.xrot.GetFrame(frame); KeyFrame[] y = node.yrot.GetFrame(frame); KeyFrame[] z = node.zrot.GetFrame(frame); KeyFrame[] w = node.wrot.GetFrame(frame); Quaternion q1 = new Quaternion(x[0].Value, y[0].Value, z[0].Value, w[0].Value); Quaternion q2 = new Quaternion(x[1].Value, y[1].Value, z[1].Value, w[1].Value); if (x[0].Frame == frame) { b.rot = q1; } else if (x[1].Frame == frame) { b.rot = q2; } else { b.rot = Quaternion.Slerp(q1, q2, (frame - x[0].Frame) / (x[1].Frame - x[0].Frame)); } } else if (node.rotType == RotationType.Euler) { float x = node.xrot.HasAnimation() ? node.xrot.GetValue(frame) : b.rotation[0]; float y = node.yrot.HasAnimation() ? node.yrot.GetValue(frame) : b.rotation[1]; float z = node.zrot.HasAnimation() ? node.zrot.GetValue(frame) : b.rotation[2]; b.rot = EulerToQuat(z, y, x); } } } frame += 1f; if (frame >= frameCount) { frame = 0; } if (!isChild && updated) { skeleton.update(); } }
public static byte[] CreateOMOFromAnimation(Animation a, VBN vbn) { if (vbn == null || a == null) { return new byte[] { } } ; // Test Actual Bones //------------------------- List <Animation.KeyNode> toRem = new List <Animation.KeyNode>(); for (int j = 0; j < a.bones.Count; j++) { Animation.KeyNode keynode = ((Animation.KeyNode)a.bones[j]); Bone b = vbn.getBone(keynode.Text); if (b == null) { toRem.Add(keynode); } } foreach (Animation.KeyNode r in toRem) { Console.WriteLine("Removing " + r.Text); a.bones.Remove(r); } //------------------------- FileOutput o = new FileOutput(); o.endian = Endianness.Big; FileOutput t1 = new FileOutput(); t1.endian = Endianness.Big; FileOutput t2 = new FileOutput(); t2.endian = Endianness.Big; o.WriteString("OMO "); o.WriteShort(1); //idk o.WriteShort(3); //idk o.WriteInt(0x091E100C); //flags?? o.WriteShort(0); //padding o.WriteShort(a.bones.Count); // numOfNodes o.WriteShort(a.frameCount); // frame size o.WriteShort(0); // frame start ?? o.WriteInt(0); o.WriteInt(0); o.WriteInt(0); o.WriteIntAt(o.Size(), 0x14); // ASSESSMENT Vector3[] maxT = new Vector3[a.bones.Count], minT = new Vector3[a.bones.Count]; Vector4[] maxR = new Vector4[a.bones.Count], minR = new Vector4[a.bones.Count]; Vector3[] maxS = new Vector3[a.bones.Count], minS = new Vector3[a.bones.Count]; bool[] hasScale = new bool[a.bones.Count]; bool[] hasTrans = new bool[a.bones.Count]; bool[] hasRot = new bool[a.bones.Count]; bool[] conScale = new bool[a.bones.Count]; bool[] conTrans = new bool[a.bones.Count]; bool[] conRot = new bool[a.bones.Count]; a.SetFrame(0); List <List <Bone> > Frames = new List <List <Bone> >(); { for (int j = 0; j < a.bones.Count; j++) { Animation.KeyNode keynode = ((Animation.KeyNode)a.bones[j]); if (keynode.xpos.HasAnimation() || keynode.ypos.HasAnimation() || keynode.zpos.HasAnimation()) { hasTrans[j] = true; } if (keynode.xrot.HasAnimation()) { hasRot[j] = true; } if (keynode.xsca.HasAnimation() || keynode.ysca.HasAnimation() || keynode.zsca.HasAnimation()) { hasScale[j] = true; } maxT[j] = new Vector3(-999f, -999f, -999f); minT[j] = new Vector3(999f, 999f, 999f); maxS[j] = new Vector3(-999f, -999f, -999f); minS[j] = new Vector3(999f, 999f, 999f); maxR[j] = new Vector4(-999f, -999f, -999f, -999f); minR[j] = new Vector4(999f, 999f, 999f, 999f); foreach (Animation.KeyFrame key in keynode.xpos.keys) { maxT[j].X = Math.Max(maxT[j].X, key.Value); minT[j].X = Math.Min(minT[j].X, key.Value); } foreach (Animation.KeyFrame key in keynode.ypos.keys) { maxT[j].Y = Math.Max(maxT[j].Y, key.Value); minT[j].Y = Math.Min(minT[j].Y, key.Value); } foreach (Animation.KeyFrame key in keynode.zpos.keys) { maxT[j].Z = Math.Max(maxT[j].Z, key.Value); minT[j].Z = Math.Min(minT[j].Z, key.Value); } foreach (Animation.KeyFrame key in keynode.xsca.keys) { maxS[j].X = Math.Max(maxS[j].X, key.Value); minS[j].X = Math.Min(minS[j].X, key.Value); } foreach (Animation.KeyFrame key in keynode.ysca.keys) { maxS[j].Y = Math.Max(maxS[j].Y, key.Value); minS[j].Y = Math.Min(minS[j].Y, key.Value); } foreach (Animation.KeyFrame key in keynode.zsca.keys) { maxS[j].Z = Math.Max(maxS[j].Z, key.Value); minS[j].Z = Math.Min(minS[j].Z, key.Value); } Bone b = vbn.getBone(keynode.Text); for (int i = 0; i < a.frameCount; i++) { Quaternion r = new Quaternion(); if (keynode.rotType == Animation.RotationType.Quaternion) { Animation.KeyFrame[] x = keynode.xrot.GetFrame(i); Animation.KeyFrame[] y = keynode.yrot.GetFrame(i); Animation.KeyFrame[] z = keynode.zrot.GetFrame(i); Animation.KeyFrame[] w = keynode.wrot.GetFrame(i); Quaternion q1 = new Quaternion(x[0].Value, y[0].Value, z[0].Value, w[0].Value); Quaternion q2 = new Quaternion(x[1].Value, y[1].Value, z[1].Value, w[1].Value); if (x[0].Frame == i) { r = q1; } else if (x[1].Frame == i) { r = q2; } else { r = Quaternion.Slerp(q1, q2, (i - x[0].Frame) / (x[1].Frame - x[0].Frame)); } } else if (keynode.rotType == Animation.RotationType.Euler) { float x = keynode.xrot.HasAnimation() ? keynode.xrot.GetValue(i) : b.rotation[0]; float y = keynode.yrot.HasAnimation() ? keynode.yrot.GetValue(i) : b.rotation[1]; float z = keynode.zrot.HasAnimation() ? keynode.zrot.GetValue(i) : b.rotation[2]; r = Animation.EulerToQuat(z, y, x); } r.Normalize(); maxR[j].X = Math.Max(maxR[j].X, r.X); minR[j].X = Math.Min(minR[j].X, r.X); maxR[j].Y = Math.Max(maxR[j].Y, r.Y); minR[j].Y = Math.Min(minR[j].Y, r.Y); maxR[j].Z = Math.Max(maxR[j].Z, r.Z); minR[j].Z = Math.Min(minR[j].Z, r.Z); } //if (b == null)continue; if (b != null) { if (maxT[j].X == -999) { maxT[j].X = b.position[0]; } if (maxT[j].Y == -999) { maxT[j].Y = b.position[1]; } if (maxT[j].Z == -999) { maxT[j].Z = b.position[2]; } if (minT[j].X == -999) { minT[j].X = b.position[0]; } if (minT[j].Y == -999) { minT[j].Y = b.position[1]; } if (minT[j].Z == -999) { minT[j].Z = b.position[2]; } if (maxS[j].X == -999) { maxS[j].X = b.scale[0]; } if (maxS[j].Y == -999) { maxS[j].Y = b.scale[1]; } if (maxS[j].Z == -999) { maxS[j].Z = b.scale[2]; } if (minS[j].X == -999) { minS[j].X = b.scale[0]; } if (minS[j].Y == -999) { minS[j].Y = b.scale[1]; } if (minS[j].Z == -999) { minS[j].Z = b.scale[2]; } } } } //TODO: Euler Rotation Values /*VBN tempvbn = new VBN(); * a.SetFrame(0); * for (int i = 0; i < a.FrameCount; i++) * { * //Frames.Add(new List<Bone>()); * for (int j = 0; j < a.Bones.Count; j++) * { * Animation.KeyNode keynode = a.Bones[j]; * Bone b = vbn.getBone(keynode.Text); * //if(b == null) continue; * maxR[j].X = Math.Max(maxR[j].X, b.rot.X); * minR[j].X = Math.Min(minR[j].X, b.rot.X); * maxR[j].Y = Math.Max(maxR[j].Y, b.rot.Y); * minR[j].Y = Math.Min(minR[j].Y, b.rot.Y); * maxR[j].Z = Math.Max(maxR[j].Z, b.rot.Z); * minR[j].Z = Math.Min(minR[j].Z, b.rot.Z); * * Bone f1 = new Bone(tempvbn); * f1.pos = b.pos; * f1.rot = b.rot; * f1.sca = b.sca; * //Frames[i].Add(f1); * } * a.NextFrame(vbn); * }*/ // NODE INFO int t2Size = 0; for (int i = 0; i < a.bones.Count; i++) { int flag = 0; conRot[i] = false; conScale[i] = false; conTrans[i] = false; // check for constant if (maxT[i].Equals(minT[i])) { conTrans[i] = true; } if (maxR[i].Equals(minR[i])) { conRot[i] = true; } if (maxS[i].Equals(minS[i])) { conScale[i] = true; } if (hasTrans[i]) { flag |= 0x01000000; } if (hasRot[i]) { flag |= 0x02000000; } if (hasScale[i]) { flag |= 0x04000000; } if (conTrans[i] && hasTrans[i]) { flag |= 0x00200000; } else { flag |= 0x00080000; } if (conRot[i] && hasRot[i]) { flag |= 0x00007000; } else { flag |= 0x00005000; } if (conScale[i] && hasScale[i]) { flag |= 0x00000200; } else { flag |= 0x00000080; } flag |= 0x00000001; //uint id = 999; Bone b = vbn.getBone(a.bones[i].Text); int hash = -1; if (MainForm.hashes.names.ContainsKey(a.bones[i].Text)) { hash = (int)MainForm.hashes.names[a.bones[i].Text]; } else { if (b != null) { hash = (int)b.boneId; } else { continue; } } //if(hash == -1) //hash = (int)FileData.crc32(getNodeId(nodeid.get(i)).name); o.WriteInt(flag); // flags... o.WriteInt(hash); //hash o.WriteInt(t1.Size()); // Offset in 1 table o.WriteInt(t2Size); // Offset in 2 table // calculate size needed if (hasTrans[i]) { t1.WriteFloat(minT[i].X); t1.WriteFloat(minT[i].Y); t1.WriteFloat(minT[i].Z); if (!conTrans[i]) { maxT[i].X -= minT[i].X; maxT[i].Y -= minT[i].Y; maxT[i].Z -= minT[i].Z; t1.WriteFloat(maxT[i].X); t1.WriteFloat(maxT[i].Y); t1.WriteFloat(maxT[i].Z); t2Size += 6; } } if (hasRot[i]) { t1.WriteFloat(minR[i].X); t1.WriteFloat(minR[i].Y); t1.WriteFloat(minR[i].Z); if (!conRot[i]) { maxR[i].X -= minR[i].X; maxR[i].Y -= minR[i].Y; maxR[i].Z -= minR[i].Z; t1.WriteFloat(maxR[i].X); t1.WriteFloat(maxR[i].Y); t1.WriteFloat(maxR[i].Z); t2Size += 6; } } if (hasScale[i]) { t1.WriteFloat(minS[i].X); t1.WriteFloat(minS[i].Y); t1.WriteFloat(minS[i].Z); if (!conScale[i]) { maxS[i].X -= minS[i].X; maxS[i].Y -= minS[i].Y; maxS[i].Z -= minS[i].Z; t1.WriteFloat(maxS[i].X); t1.WriteFloat(maxS[i].Y); t1.WriteFloat(maxS[i].Z); t2Size += 6; } } } o.WriteIntAt(o.Size(), 0x18); o.WriteOutput(t1); o.WriteIntAt(o.Size(), 0x1C); // INTERPOLATION a.SetFrame(0); bool go = true; for (int i = 0; i < a.frameCount; i++) { //a.NextFrame(vbn); for (int j = 0; j < a.bones.Count; j++) { Bone node = vbn.getBone(a.bones[j].Text); Animation.KeyNode anode = a.bones[j]; //if (node == null) continue; if (hasTrans[j] && !conTrans[j]) { t2.WriteShort((int)(((anode.xpos.GetValue(i) - minT[j].X) / maxT[j].X) * 0xFFFF)); t2.WriteShort((int)(((anode.ypos.GetValue(i) - minT[j].Y) / maxT[j].Y) * 0xFFFF)); t2.WriteShort((int)(((anode.zpos.GetValue(i) - minT[j].Z) / maxT[j].Z) * 0xFFFF)); } if (hasRot[j] && !conRot[j]) { Quaternion r = new Quaternion(); if (anode.rotType == Animation.RotationType.Quaternion) { Animation.KeyFrame[] x = anode.xrot.GetFrame(i); Animation.KeyFrame[] y = anode.yrot.GetFrame(i); Animation.KeyFrame[] z = anode.zrot.GetFrame(i); Animation.KeyFrame[] w = anode.wrot.GetFrame(i); Quaternion q1 = new Quaternion(x[0].Value, y[0].Value, z[0].Value, w[0].Value); Quaternion q2 = new Quaternion(x[1].Value, y[1].Value, z[1].Value, w[1].Value); if (x[0].Frame == i) { r = q1; } else if (x[1].Frame == i) { r = q2; } else { r = Quaternion.Slerp(q1, q2, (i - x[0].Frame) / (x[1].Frame - x[0].Frame)); } } else if (anode.rotType == Animation.RotationType.Euler) { float x = anode.xrot.HasAnimation() ? anode.xrot.GetValue(i) : node.rotation[0]; float y = anode.yrot.HasAnimation() ? anode.yrot.GetValue(i) : node.rotation[1]; float z = anode.zrot.HasAnimation() ? anode.zrot.GetValue(i) : node.rotation[2]; r = Animation.EulerToQuat(z, y, x); } r.Normalize(); t2.WriteShort((int)(((r.X - minR[j].X) / maxR[j].X) * 0xFFFF)); t2.WriteShort((int)(((r.Y - minR[j].Y) / maxR[j].Y) * 0xFFFF)); t2.WriteShort((int)(((r.Z - minR[j].Z) / maxR[j].Z) * 0xFFFF)); } if (hasScale[j] && !conScale[j]) { t2.WriteShort((int)(((anode.xsca.GetValue(i) - minS[j].X) / maxS[j].X) * 0xFFFF)); t2.WriteShort((int)(((anode.ysca.GetValue(i) - minS[j].Y) / maxS[j].Y) * 0xFFFF)); t2.WriteShort((int)(((anode.zsca.GetValue(i) - minS[j].Z) / maxS[j].Z) * 0xFFFF)); } } if (go) { o.WriteShortAt(t2.Size(), 0x12); go = false; } } o.WriteOutput(t2); return(o.GetBytes()); } }