public static void Save(STSkeletonAnimation anim, String Fname) { System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); customCulture.NumberFormat.NumberDecimalSeparator = "."; 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(); } }
public void FillSkeleton(SuperBMDLib.BMD.INF1 INF1, STSkeleton skeleton, List <SuperBMDLib.Rigging.Bone> flatSkeleton) { for (int i = 1; i < INF1.FlatNodes.Count; i++) { SuperBMDLib.Scenegraph.SceneNode curNode = INF1.FlatNodes[i]; if (curNode.Type == SuperBMDLib.Scenegraph.Enums.NodeType.Joint) { var Bone = flatSkeleton[curNode.Index]; var stBone = new STBone(skeleton); stBone.Text = Bone.Name; stBone.FromTransform(Bone.TransformationMatrix); skeleton.bones.Add(stBone); } } int boneIndex = 0; for (int i = 1; i < INF1.FlatNodes.Count; i++) { SuperBMDLib.Scenegraph.SceneNode curNode = INF1.FlatNodes[i]; if (curNode.Type == SuperBMDLib.Scenegraph.Enums.NodeType.Joint) { var Bone = flatSkeleton[curNode.Index]; var stBone = skeleton.bones[boneIndex]; if (curNode.Parent != null && curNode.Parent.Type == SuperBMDLib.Scenegraph.Enums.NodeType.Joint) { var parent = flatSkeleton[curNode.Parent.Index]; var boneParent = skeleton.GetBone(parent.Name); if (boneParent != null) { stBone.parentIndex = skeleton.bones.IndexOf(boneParent); } } else { stBone.parentIndex = -1; } boneIndex++; } } }
public static void SaveAnimation(string FileName, Animation anim, STSkeleton skeleton) { anim.SetFrame(anim.FrameCount - 1); //from last frame for (int f = 0; f < anim.FrameCount; ++f) //go through each frame with nextFrame { anim.NextFrame(skeleton); } anim.NextFrame(skeleton); //go on first frame SEAnim seAnim = new SEAnim(); seAnim.Looping = anim.CanLoop; seAnim.AnimType = AnimationType.Absolute; //Reset active animation to 0 anim.SetFrame(0); for (int frame = 0; frame < anim.FrameCount; frame++) { anim.NextFrame(skeleton, false, true); foreach (Animation.KeyNode boneAnim in anim.Bones) { if (boneAnim.HasKeyedFrames(frame)) { STBone bone = skeleton.GetBone(boneAnim.Text); if (bone == null) { continue; } Vector3 position = bone.GetPosition(); Quaternion rotation = bone.GetRotation(); Vector3 scale = bone.GetScale(); seAnim.AddTranslationKey(boneAnim.Text, frame, position.X, position.Y, position.Z); seAnim.AddRotationKey(boneAnim.Text, frame, rotation.X, rotation.Y, rotation.Z, rotation.W); seAnim.AddScaleKey(boneAnim.Text, frame, scale.X, scale.Y, scale.Z); } } } seAnim.Write(FileName); }
public static void Save(Animation anim, STSkeleton Skeleton, String Fname) { System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; 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.NextFrame(Skeleton, false, true); file.WriteLine($"time {i}"); foreach (Animation.KeyNode sb in anim.Bones) { STBone b = Skeleton.GetBone(sb.Text); 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(); } }
public static void SaveAnimation(string FileName, Animation anim, STSkeleton skeleton) { SEAnim seAnim = new SEAnim(); //Reset active animation to 0 anim.SetFrame(0); for (int frame = 0; frame < anim.FrameCount; frame++) { anim.NextFrame(skeleton); //Reset it when it reaches the total frame count if (anim.Frame >= anim.FrameCount) { anim.Frame = 0; } foreach (Animation.KeyNode boneAnim in anim.Bones) { STBone bone = skeleton.GetBone(boneAnim.Text); if (bone == null) { continue; } Vector3 position = bone.GetPosition(); Quaternion rotation = bone.GetRotation(); Vector3 scale = bone.GetScale(); seAnim.AddTranslationKey(boneAnim.Text, frame, position.X, position.Y, position.Z); seAnim.AddRotationKey(boneAnim.Text, frame, rotation.X, rotation.Y, rotation.Z, rotation.W); seAnim.AddScaleKey(boneAnim.Text, frame, scale.X, scale.Y, scale.Z); } //Add frames to the playing animation anim.Frame += 1f; } seAnim.Write(FileName); }
public static void Save(STSkeletonAnimation anim, string FileName) { STSkeleton skeleton = anim.GetActiveSkeleton(); SEAnim seAnim = new SEAnim(); seAnim.Looping = anim.Loop; seAnim.AnimType = AnimationType.Absolute; anim.SetFrame(0); for (int frame = 0; frame < anim.FrameCount; frame++) { anim.SetFrame(frame); anim.NextFrame(); foreach (STAnimGroup boneAnim in anim.AnimGroups) { if (boneAnim.GetTracks().Any(x => x.HasKeys)) { STBone bone = skeleton.GetBone(boneAnim.Name); if (bone == null) { continue; } Vector3 position = bone.GetPosition(); Quaternion rotation = bone.GetRotation(); Vector3 scale = bone.GetScale(); seAnim.AddTranslationKey(boneAnim.Name, frame, position.X, position.Y, position.Z); seAnim.AddRotationKey(boneAnim.Name, frame, rotation.X, rotation.Y, rotation.Z, rotation.W); seAnim.AddScaleKey(boneAnim.Name, frame, scale.X, scale.Y, scale.Z); } } } seAnim.Write(FileName); }
public static Animation Read(string FileName, STSkeleton skeleton) { Animation anim = new Animation(); var seanim = SEAnim.Read(FileName); anim.FrameCount = seanim.FrameCount; anim.CanLoop = seanim.Looping; foreach (var bone in seanim.Bones) { STBone genericBone = skeleton.GetBone(bone); if (genericBone != null) { var boneAnim = new Animation.KeyNode(bone); boneAnim.RotType = Animation.RotationType.EULER; boneAnim.UseSegmentScaleCompensate = false; anim.Bones.Add(boneAnim); float PositionX = 0; float PositionY = 0; float PositionZ = 0; float RotationX = 0; float RotationY = 0; float RotationZ = 0; float ScaleX = 0; float ScaleY = 0; float ScaleZ = 0; if (seanim.AnimType == AnimationType.Relative) { PositionX = genericBone.position[0]; PositionY = genericBone.position[1]; PositionZ = genericBone.position[2]; RotationX = genericBone.rotation[0]; RotationY = genericBone.rotation[1]; RotationZ = genericBone.rotation[2]; ScaleX = genericBone.scale[0]; ScaleY = genericBone.scale[1]; ScaleZ = genericBone.scale[2]; } System.Console.WriteLine(bone); if (seanim.AnimationPositionKeys.ContainsKey(bone)) { var translationKeys = seanim.AnimationPositionKeys[bone]; foreach (SEAnimFrame animFrame in translationKeys) { System.Console.WriteLine(animFrame.Frame + " T " + ((SELib.Utilities.Vector3)animFrame.Data).X); System.Console.WriteLine(animFrame.Frame + " T " + ((SELib.Utilities.Vector3)animFrame.Data).Y); System.Console.WriteLine(animFrame.Frame + " T " + ((SELib.Utilities.Vector3)animFrame.Data).Z); boneAnim.XPOS.Keys.Add(new Animation.KeyFrame() { Value = (float)((SELib.Utilities.Vector3)animFrame.Data).X + PositionX, Frame = animFrame.Frame, }); boneAnim.YPOS.Keys.Add(new Animation.KeyFrame() { Value = (float)((SELib.Utilities.Vector3)animFrame.Data).Y + PositionY, Frame = animFrame.Frame, }); boneAnim.ZPOS.Keys.Add(new Animation.KeyFrame() { Value = (float)((SELib.Utilities.Vector3)animFrame.Data).Z + PositionZ, Frame = animFrame.Frame, }); } } if (seanim.AnimationRotationKeys.ContainsKey(bone)) { var rotationnKeys = seanim.AnimationRotationKeys[bone]; foreach (SEAnimFrame animFrame in rotationnKeys) { var quat = ((SELib.Utilities.Quaternion)animFrame.Data); var euler = STMath.ToEulerAngles(quat.X, quat.Y, quat.Z, quat.W); System.Console.WriteLine(animFrame.Frame + " R " + euler.X); System.Console.WriteLine(animFrame.Frame + " R " + euler.Y); System.Console.WriteLine(animFrame.Frame + " R " + euler.Z); boneAnim.XROT.Keys.Add(new Animation.KeyFrame() { Value = euler.X + RotationX, Frame = animFrame.Frame, }); boneAnim.YROT.Keys.Add(new Animation.KeyFrame() { Value = euler.Y + RotationY, Frame = animFrame.Frame, }); boneAnim.ZROT.Keys.Add(new Animation.KeyFrame() { Value = euler.Z + RotationZ, Frame = animFrame.Frame, }); } } if (seanim.AnimationScaleKeys.ContainsKey(bone)) { var scaleKeys = seanim.AnimationScaleKeys[bone]; foreach (SEAnimFrame animFrame in scaleKeys) { System.Console.WriteLine(animFrame.Frame + " S " + ((SELib.Utilities.Vector3)animFrame.Data).X); System.Console.WriteLine(animFrame.Frame + " S " + ((SELib.Utilities.Vector3)animFrame.Data).Y); System.Console.WriteLine(animFrame.Frame + " S " + ((SELib.Utilities.Vector3)animFrame.Data).Z); boneAnim.XSCA.Keys.Add(new Animation.KeyFrame() { Value = (float)((SELib.Utilities.Vector3)animFrame.Data).X + ScaleX, Frame = animFrame.Frame, }); boneAnim.YSCA.Keys.Add(new Animation.KeyFrame() { Value = (float)((SELib.Utilities.Vector3)animFrame.Data).Y + ScaleY, Frame = animFrame.Frame, }); boneAnim.ZSCA.Keys.Add(new Animation.KeyFrame() { Value = (float)((SELib.Utilities.Vector3)animFrame.Data).Z + ScaleZ, Frame = animFrame.Frame, }); } } else { boneAnim.XSCA.Keys.Add(new Animation.KeyFrame() { Value = 1, Frame = 0, }); boneAnim.YSCA.Keys.Add(new Animation.KeyFrame() { Value = 1, Frame = 0, }); boneAnim.ZSCA.Keys.Add(new Animation.KeyFrame() { Value = 1, Frame = 0, }); } } } return(anim); }
public static void SaveAnimation(string FileName, Animation anim, STSkeleton skeleton) { anim.SetFrame(anim.FrameCount - 1); //from last frame for (int f = 0; f < anim.FrameCount; ++f) //go through each frame with nextFrame { anim.NextFrame(skeleton); } anim.NextFrame(skeleton); //go on first frame foreach (STBone b in skeleton.getBoneTreeOrder()) { if (anim.HasBone(b.Text)) { Animation.KeyNode n = anim.GetBone(b.Text); if (n.XPOS.HasAnimation()) { WriteKey(n.XPOS); } if (n.YPOS.HasAnimation()) { WriteKey(n.YPOS); } if (n.ZPOS.HasAnimation()) { WriteKey(n.ZPOS); } if (n.XROT.HasAnimation()) { WriteKey(n.XROT); } if (n.YROT.HasAnimation()) { WriteKey(n.YROT); } if (n.ZROT.HasAnimation()) { WriteKey(n.ZROT); } if (n.XSCA.HasAnimation()) { WriteKey(n.XSCA); } if (n.YSCA.HasAnimation()) { WriteKey(n.YSCA); } if (n.ZSCA.HasAnimation()) { WriteKey(n.ZSCA); } } } SEAnim seAnim = new SEAnim(); seAnim.Looping = anim.CanLoop; seAnim.AnimType = AnimationType.Absolute; //Reset active animation to 0 anim.SetFrame(0); for (int frame = 0; frame < anim.FrameCount; frame++) { anim.NextFrame(skeleton, false, true); foreach (Animation.KeyNode boneAnim in anim.Bones) { if (boneAnim.HasKeyedFrames(frame)) { STBone bone = skeleton.GetBone(boneAnim.Text); if (bone == null) { continue; } Vector3 position = bone.GetPosition(); Quaternion rotation = bone.GetRotation(); Vector3 scale = bone.GetScale(); //Exporting at specific keys doesn't work atm //Todo bool IsTranlationKeyed = (boneAnim.XPOS.GetKeyFrame(frame).IsKeyed || boneAnim.YPOS.GetKeyFrame(frame).IsKeyed || boneAnim.ZPOS.GetKeyFrame(frame).IsKeyed); bool IsRotationKeyed = (boneAnim.XROT.GetKeyFrame(frame).IsKeyed || boneAnim.YROT.GetKeyFrame(frame).IsKeyed || boneAnim.ZROT.GetKeyFrame(frame).IsKeyed); bool IsScaleKeyed = (boneAnim.XSCA.GetKeyFrame(frame).IsKeyed || boneAnim.YSCA.GetKeyFrame(frame).IsKeyed || boneAnim.ZSCA.GetKeyFrame(frame).IsKeyed); seAnim.AddTranslationKey(boneAnim.Text, frame, position.X, position.Y, position.Z); seAnim.AddRotationKey(boneAnim.Text, frame, rotation.X, rotation.Y, rotation.Z, rotation.W); seAnim.AddScaleKey(boneAnim.Text, frame, scale.X, scale.Y, scale.Z); } } } seAnim.Write(FileName); }
public void NextFrame(STSkeleton skeleton, bool isChild = false, bool AdancedNextFrame = false, string bn = "") { 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, AdancedNextFrame); } } bool Updated = false; // no need to update skeleton of animations that didn't change foreach (KeyNode node in Bones) { // Get Skeleton Node STBone b = null; b = skeleton.GetBone(node.Text); if (b == null) { continue; } if (bn != "" && bn != node.Text) { continue; //If a bone name is provided, skip any bones that aren't that bone. } b.UseSegmentScaleCompensate = node.UseSegmentScaleCompensate; Updated = true; if (node.XPOS.HasAnimation()) { b.pos.X = node.XPOS.GetValue(Frame); } if (node.YPOS.HasAnimation()) { b.pos.Y = node.YPOS.GetValue(Frame); } if (node.ZPOS.HasAnimation()) { 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.EulerRotation.X; float y = node.YROT.HasAnimation() ? node.YROT.GetValue(Frame) : b.EulerRotation.Y; float z = node.ZROT.HasAnimation() ? node.ZROT.GetValue(Frame) : b.EulerRotation.Z; b.rot = EulerToQuat(z, y, x); } } } if (AdancedNextFrame) { Frame++; if (Frame > FrameCount) { Frame = 0; } } if (!isChild && Updated) { skeleton.update(); } }