/// <summary> /// Constructs a new animation player. /// </summary> public AnimationPlayer(SkinningData skinningData) { if (skinningData == null) throw new ArgumentNullException("skinningData"); skinningDataValue = skinningData; bone_matrices = new Matrix[skinningData.BindPose.Count]; world_matrices = new Matrix[skinningData.BindPose.Count]; skin_matrices = new Matrix[skinningData.BindPose.Count]; keyframe_matrices = new Matrix[skinningData.BindPose.Count]; bone_transforms = new Transform[skinningData.BindPose.Count]; skinningDataValue.BindPose.CopyTo(bone_matrices, 0); for (int i = 0; i < bone_matrices.Length; i++) bone_transforms[i] = new Transform(bone_matrices[i]); }
public static void Multiply(ref Transform transform1, ref Transform transform2, out Transform result) { Quaternion q; Vector3 t, s; s = new Vector3(); s.X = transform2.scale.X * transform1.scale.X; s.Y = transform2.scale.Y * transform1.scale.Y; s.Z = transform2.scale.Z * transform1.scale.Z; if (transform2.rotation.W == 1 && (transform2.rotation.X == 0 && transform2.rotation.Y == 0 && transform2.rotation.Z == 0)) { q.X = transform1.rotation.X; q.Y = transform1.rotation.Y; q.Z = transform1.rotation.Z; q.W = transform1.rotation.W; t.X = transform1.translation.X; t.Y = transform1.translation.Y; t.Z = transform1.translation.Z; } else { float num12 = transform2.rotation.X + transform2.rotation.X; float num2 = transform2.rotation.Y + transform2.rotation.Y; float num = transform2.rotation.Z + transform2.rotation.Z; float num11 = transform2.rotation.W * num12; float num10 = transform2.rotation.W * num2; float num9 = transform2.rotation.W * num; float num8 = transform2.rotation.X * num12; float num7 = transform2.rotation.X * num2; float num6 = transform2.rotation.X * num; float num5 = transform2.rotation.Y * num2; float num4 = transform2.rotation.Y * num; float num3 = transform2.rotation.Z * num; t.X = ((transform1.translation.X * ((1f - num5) - num3)) + (transform1.translation.Y * (num7 - num9))) + (transform1.translation.Z * (num6 + num10)); t.Y = ((transform1.translation.X * (num7 + num9)) + (transform1.translation.Y * ((1f - num8) - num3))) + (transform1.translation.Z * (num4 - num11)); t.Z = ((transform1.translation.X * (num6 - num10)) + (transform1.translation.Y * (num4 + num11))) + (transform1.translation.Z * ((1f - num8) - num5)); num12 = (transform2.rotation.Y * transform1.rotation.Z) - (transform2.rotation.Z * transform1.rotation.Y); num11 = (transform2.rotation.Z * transform1.rotation.X) - (transform2.rotation.X * transform1.rotation.Z); num10 = (transform2.rotation.X * transform1.rotation.Y) - (transform2.rotation.Y * transform1.rotation.X); num9 = ((transform2.rotation.X * transform1.rotation.X) + (transform2.rotation.Y * transform1.rotation.Y)) + (transform2.rotation.Z * transform1.rotation.Z); q.X = ((transform2.rotation.X * transform1.rotation.W) + (transform1.rotation.X * transform2.rotation.W)) + num12; q.Y = ((transform2.rotation.Y * transform1.rotation.W) + (transform1.rotation.Y * transform2.rotation.W)) + num11; q.Z = ((transform2.rotation.Z * transform1.rotation.W) + (transform1.rotation.Z * transform2.rotation.W)) + num10; q.W = (transform2.rotation.W * transform1.rotation.W) - num9; } t.X = t.X * transform2.scale.X + transform2.translation.X; t.Y = t.Y * transform2.scale.Y + transform2.translation.Y; t.Z = t.Z * transform2.scale.Z + transform2.translation.Z; result = new Transform(); result.rotation.X = q.X; result.rotation.Y = q.Y; result.rotation.Z = q.Z; result.rotation.W = q.W; result.translation.X = t.X; result.translation.Y = t.Y; result.translation.Z = t.Z; result.scale = s; }
public static void Lerp(ref Transform from, ref Transform to, float amount, out Transform result) { Quaternion.Lerp(ref from.rotation, ref to.rotation, amount, out result.rotation); Vector3.Lerp(ref from.translation, ref to.translation, amount, out result.translation); Vector3.Lerp(ref from.scale, ref to.scale, amount, out result.scale); }
public Transform Lerp(ref Transform to, float amount) { Transform result; Lerp(ref this, ref to, amount, out result); return result; }
/// <summary> /// Helper used by the Update method to refresh the BoneTransforms data. /// </summary> public void UpdateBoneTransforms(TimeSpan time, bool relativeToCurrentTime) { if (currentClipValue == null) throw new InvalidOperationException( "AnimationPlayer.Update was called before StartClip"); // Update the animation position. if (relativeToCurrentTime) { time += currentTimeValue; // If we reached the end, loop back to the start. while (time >= currentClipValue.Duration) time -= currentClipValue.Duration; } if ((time < TimeSpan.Zero)) throw new ArgumentOutOfRangeException("time"); // If the position moved backwards, reset the keyframe index. if (time < currentTimeValue) { currentKeyframe = 0; skinningDataValue.BindPose.CopyTo(bone_matrices, 0); } currentTimeValue = time; // Read keyframe matrices. IList<Keyframe> keyframes = currentClipValue.Keyframes; while (currentKeyframe < keyframes.Count) { Keyframe keyframe = keyframes[currentKeyframe]; // Stop when we've read up to the current time position. if (keyframe.Time > currentTimeValue) break; // float keyframe_time = (float)(keyframe.Time.TotalSeconds); // if(keyframe_time > curr_keyframe_time) // { // prev_keyframe_time = curr_keyframe_time; // curr_keyframe_time = keyframe_time; // } keyframe_matrices[keyframe.Bone] = keyframe.Transform; currentKeyframe++; } //float curr_time = (float)currentTimeValue.TotalSeconds; //float amount = 0; //if(curr_keyframe_time != prev_keyframe_time) // amount = ((curr_time - curr_keyframe_time) / (curr_keyframe_time - prev_keyframe_time)); float amount = 0.1f; for (int i = 0; i < bone_matrices.Length; i++) { Transform transform = new Transform(ref keyframe_matrices[i]); Transform.Lerp(ref bone_transforms[i], ref transform, amount, out bone_transforms[i]); bone_transforms[i].GetMatrix(out bone_matrices[i]); } }