public void Unfuck(NewBlendableTransform targetTPose, NewBlendableTransform importTPose, float posScale, string boneName) { var newFrames = new List <NewBlendableTransform>(); for (int i = 0; i < Frames.Count; i++) { var newPos = (Frames[i].Translation); if (newPos.LengthSquared() > 0) { newPos = Vector3.Normalize(newPos.ToXna()).ToCS() * targetTPose.Translation.Length(); } if (boneName == "MASTER") { //throw new Exception("breh"); } newFrames.Add(new NewBlendableTransform() { Translation = newPos, Scale = (Frames[i].Scale * (importTPose.Scale / targetTPose.Scale)), Rotation = (Frames[i].Rotation.ToXna() * (importTPose.Rotation.ToXna() * Quaternion.Inverse(targetTPose.Rotation.ToXna()))).ToCS(), }); } Frames = newFrames; }
//public HKBoneTrackThing(List<NewBlendableTransform> frames) //{ // Frames = frames; // CalculateRelativeFrames(); //} public void UnfuckFirstRelFrame(NewBlendableTransform targetTPose, NewBlendableTransform importTPose, float posScale, string boneName) { //RelativeFrames[0] = new NewBlendableTransform() //{ // Translation = RelativeFrames[0].Translation,// + (targetTPose.Translation - importTPose.Translation), // Scale = RelativeFrames[0].Scale,// * (targetTPose.Scale / importTPose.Scale), // Rotation = RelativeFrames[0].Rotation * (importTPose.Rotation / targetTPose.Rotation), //}; }
public override NewBlendableTransform GetBlendableTransformOnFrame(int hkxBoneIndex, float frame) { var frameRatio = frame % 1; frame = frame % FrameCount; if (frameRatio != 0) { var blendFrom = Frames[(int)Math.Floor(frame)].BoneTransforms[hkxBoneIndex]; var blendTo = Frames[(int)Math.Ceiling(frame)].BoneTransforms[hkxBoneIndex]; return(NewBlendableTransform.Lerp(blendFrom, blendTo, frameRatio)); } else { return(Frames[(int)frame].BoneTransforms[hkxBoneIndex]); } }
public override NewBlendableTransform GetTransformOnFrame(int transformTrackIndex, float frame, bool enableLooping) { var frameRatio = frame % 1; if (enableLooping) { frame = frame % FrameCount; } else { frame = Math.Min(frame, FrameCount); } if (frameRatio != 0) { var blendFrom = Frames[(int)Math.Floor(frame)].BoneTransforms[transformTrackIndex]; var blendTo = Frames[(int)Math.Ceiling(frame)].BoneTransforms[transformTrackIndex]; return(NewBlendableTransform.Lerp(blendFrom, blendTo, frameRatio)); } else { return(Frames[(int)frame].BoneTransforms[transformTrackIndex]); } }
private Matrix4x4 GetMatrix(int hkxBoneIndex, float frameIndex) { HavokAnimationData anim = Souls.dsAnimation; NewBlendableTransform newBlendableTransform = anim.GetBlendableTransformOnFrame(hkxBoneIndex, frameIndex); var currentTransformMatrix = newBlendableTransform.GetMatrix(); int parentIndex = Souls.dsAnimation.hkaSkeleton.ParentIndices[hkxBoneIndex].data; if (parentIndex >= 0) { currentTransformMatrix *= GetMatrix(parentIndex, frameIndex); } else { //var rootMotion = rootMotionCache.GetRootMotionOnFrame((int)frameIndex); //currentTransformMatrix *= Microsoft.Xna.Framework.Matrix.CreateRotationY(rootMotion.W); //currentTransformMatrix *= Microsoft.Xna.Framework.Matrix.CreateTranslation(rootMotion.X, rootMotion.Y, rootMotion.Z); } return(currentTransformMatrix); }
public DsSkeleton ParseSkeleton() { List <DsBoneData> boneDatas = bones.Select( bone => { FLVER.Bone flverBone = Flver.Bones.Single(flverBone => flverBone.Name == bone.Name); return(new DsBoneData(bone, flverBone, Scene)); } ).ToList(); //exportData.FbxNode.LclTranslation.Set(flverBone.Translation.ToFbxDouble3()); //exportData.FbxNode.LclRotation.Set(flverBone.Rotation.ToFbxDouble3()); //exportData.FbxNode.LclScaling.Set(flverBone.Scale.ToFbxDouble3()); for (int boneIndex = 0; boneIndex < bones.Count; ++boneIndex) { DsBoneData boneData = boneDatas[boneIndex]; DsBoneData parentBoneData = boneDatas.Find(parentBoneData => parentBoneData.exportData.SoulsData.Name == boneData.exportData.SoulsData.ParentName); boneData.SetParent(parentBoneData); } Func <DsBoneData, Matrix4x4> calculateTransform = (boneData) => { Matrix4x4 rawGlobalTransform = CalculateGlobalTransform(boneData, Flver, hkaSkeleton); var preFixupMatrix = Matrix4x4.CreateRotationZ((float)(-Math.PI / 2)); // Matrix4x4.CreateScale(new Vector3(1, 1, 1)); // * Matrix4x4.CreateRotationX((float)(-Math.PI)) * Matrix4x4.CreateRotationY((float)(-Math.PI / 2)) * Matrix4x4.CreateRotationZ((float)(Math.PI / 2)); var postFixupMatrix = Matrix4x4.CreateScale(1, 1, 1); // Matrix4x4.CreateRotationZ((float)(Math.PI / 2)); // Matrix4x4.CreateScale(new Vector3(1, 1, 1));// * Matrix4x4.CreateRotationX((float)(-Math.PI / 2)) * Matrix4x4.CreateRotationZ((float)(Math.PI / 2)); if (boneData.parent == null) { Matrix4x4 preFixupParent = Matrix4x4.Identity; Matrix4x4 postFixupParent = Matrix4x4.Identity; // * Matrix4x4.CreateRotationX((float)(-Math.PI / 2)) * Matrix4x4.CreateRotationZ((float)(Math.PI / 2)); ; //* Matrix4x4.CreateScale(1,1,-1); preFixupMatrix *= preFixupParent; postFixupMatrix *= postFixupParent; } else { } //var t = rawGlobalTransform.Translation; //t.Z = -t.Z; //rawGlobalTransform.Translation = t; var fixedTransform = preFixupMatrix * rawGlobalTransform * postFixupMatrix; var btr = new NewBlendableTransform(fixedTransform); btr.Translation.Z = -btr.Translation.Z; btr.Rotation.X = -btr.Rotation.X; btr.Rotation.Y = -btr.Rotation.Y; return(btr.GetMatrix()); }; //Func<DsBoneData, Matrix4x4> calculateParentTransform = (boneData) => //{ // Matrix4x4 rawGlobalTransform = CalculateGlobalTransform(boneData, Flver, hkaSkeleton); // var preFixupMatrix = Matrix4x4.CreateScale(new Vector3(1, 1, 1)); // * Matrix4x4.CreateRotationX((float)(-Math.PI)) * Matrix4x4.CreateRotationY((float)(-Math.PI / 2)) * Matrix4x4.CreateRotationZ((float)(Math.PI / 2)); // var postFixupMatrix = Matrix4x4.CreateScale(new Vector3(1, 1, 1)); // * Matrix4x4.CreateRotationX((float)(-Math.PI / 2)) * Matrix4x4.CreateRotationZ((float)(Math.PI / 2)); // if (boneData.parent == null) // { // postFixupMatrix *= Matrix4x4.CreateRotationX((float)(-Math.PI / 2)) * Matrix4x4.CreateRotationZ((float)(Math.PI / 2)); // } // else // { // //postFixupMatrix *= Matrix4x4.CreateScale(1, 1, -1); // } // return preFixupMatrix * rawGlobalTransform * postFixupMatrix; //}; foreach (var boneData in boneDatas) { var globalTransform = calculateTransform(boneData); if (boneData.parent != null) { var globalParentTransform = calculateTransform(boneData.parent); Matrix4x4 invertedGlobalParentTransform; if (Matrix4x4.Invert(globalParentTransform, out invertedGlobalParentTransform)) { globalTransform *= invertedGlobalParentTransform; } else { throw new Exception(); } } else { } Vector3 scale; Quaternion rotation; Vector3 translation; if (Matrix4x4.Decompose(globalTransform, out scale, out rotation, out translation)) { boneData.exportData.FbxNode.LclTranslation.Set(translation.ToFbxDouble3()); Vector3 euler = rotation.QuaternionToEuler(); boneData.exportData.FbxNode.LclRotation.Set(euler.ToFbxDouble3()); boneData.exportData.FbxNode.LclScaling.Set(scale.ToFbxDouble3()); } else { throw new Exception(); } } //FbxSkeleton skeletonRoot = FbxSkeleton.Create(Scene, "ActualRoot"); //FbxNode skeletonRootNode = skeletonRoot.CreateNode(); foreach (var root in boneDatas.Where(bone => bone.parent == null)) { Scene.GetRootNode().AddChild(root.exportData.FbxNode); } return(new DsSkeleton(null, boneDatas)); }
private IDictionary <int, FrameData> ExtractAnimationData(HavokAnimationData anim) { IDictionary <int, FrameData> frameDatas = new Dictionary <int, FrameData>(); try { for (int frameIndex = 0; frameIndex < Souls.dsAnimation.FrameCount; ++frameIndex) { var frameBoneDatas = new List <FrameData.FrameBoneData>(); FrameData data = new FrameData() { boneDatas = frameBoneDatas, frameIndex = frameIndex }; Func <DsBoneData, Matrix4x4> calculateTransform = bone => { if (bone.parent == null) { return(Matrix4x4.Identity); } var calculatedMatrix = GetMatrix(bone.exportData.SoulsData.HkxBoneIndex, frameIndex); var hackPreMatrix = Matrix4x4.CreateRotationZ((float)(-Math.PI / 2)); // * Matrix4x4.CreateRotationY((float)(-Math.PI / 2)); ; // Microsoft.Xna.Framework.Matrix.CreateScale(-1, 1, 1); var hackPostMatrix = Matrix4x4.Identity; // Matrix4x4.CreateScale(-1, 1, 1); // Matrix4x4.CreateScale(-1,1,1); // * Matrix4x4.CreateRotationY((float)(Math.PI)); // Matrix4x4.CreateScale(1, 1, -1); var transformedMatrix = hackPreMatrix * calculatedMatrix * hackPostMatrix; var btr = new NewBlendableTransform(transformedMatrix); btr.Translation.Z = -btr.Translation.Z; btr.Rotation.X = -btr.Rotation.X; btr.Rotation.Y = -btr.Rotation.Y; return(btr.GetMatrix()); }; foreach (var bone in Souls.skeleton.boneDatas) { var boneData111 = new FrameData.FrameBoneData() { hkxBoneIndex = bone.exportData.SoulsData.HkxBoneIndex }; var calculatedMatrix = calculateTransform(bone); var hackNewBlendableMatrix = calculatedMatrix; if (bone.parent != null) { Matrix4x4 parentMatrix = calculateTransform(bone.parent); if (Matrix4x4.Invert(parentMatrix, out var inverted)) { hackNewBlendableMatrix *= inverted; } else { throw new Exception(); } } else { Func <Matrix4x4> getRefMatrix = () => { if (Souls.animRefFrame == null) { return(Matrix4x4.Identity); } return(Matrix4x4.CreateWorld(Vector3.Zero, Souls.animRefFrame.Forward.ToVector3(), Souls.animRefFrame.Up.ToVector3())); }; var refMatrix = Matrix4x4.Identity; // getRefMatrix(); Matrix4x4 additionalPostTransformation = refMatrix; // this is a root bone if (anim.RootMotion != null) { var rootMotion = anim.RootMotion.ExtractRootMotion(0, anim.Duration * (((float)frameIndex / anim.FrameCount))); rootMotion.positionChange.Z = -rootMotion.positionChange.Z; Matrix4x4 rootMotionTransformation = Matrix4x4.CreateRotationY(rootMotion.directionChange) * Matrix4x4.CreateTranslation(rootMotion.positionChange); additionalPostTransformation *= rootMotionTransformation; } hackNewBlendableMatrix *= additionalPostTransformation; } var newBlendableTransform = new NewBlendableTransform(hackNewBlendableMatrix); int hkxBoneIndex = bone.exportData.SoulsData.HkxBoneIndex; boneData111.hkxBoneIndex = hkxBoneIndex; boneData111.transform = newBlendableTransform; frameBoneDatas.Add(boneData111); } frameDatas.Add(frameIndex, data); } return(frameDatas); } catch (Exception ex) { throw new AnimationExportException("An exception occured while exporting animations!", ex); } }