void Instalize(AnimationFrame start) { foreach (var pair in boneReference) { Vector4 rot = start.BonePoritions[pair.Value].Rotation; Vector3 pos = start.BonePoritions[pair.Value].Position; Matrix4 localInv = Matrix4.Identity; Quaternion rotation = Quaternion.Identity; if (_animation.GetRotationType == Animation.RotationType.Quaternion) { rotation = new Quaternion(rot.Xyz, rot.W); } else { rotation = Quaternion.FromAxisAngle(Vector3.UnitZ, rot.Z) * Quaternion.FromAxisAngle(Vector3.UnitY, rot.Y) * Quaternion.FromAxisAngle(Vector3.UnitX, rot.X); } if (_animation.TransType == Animation.TransformType.LocalRelative) { BoneController.GetBone(pair.Key).SetTransform(rotation, pos); } else if (_animation.TransType == Animation.TransformType.LocalAbsolute) { BoneController.GetBone(pair.Key).InitialLocalTransform = Matrix4.CreateFromQuaternion(rotation) * Matrix4.CreateTranslation(pos); } } }
private void PerformMorph(float degree) { if (boneController == null) { return; } foreach (var bone in bones) { var pos = bone.Item2 * degree; var rot = Quaternion.Slerp(Quaternion.Identity, bone.Item3, degree); boneController.GetBone(bone.Item1).SetTransform(rot, pos); } }
public void SyncBone2Body(OpenTK.Mathematics.Matrix4 world) { var mat = (BoneController.GetBone(BoneID).TransformMatrix *world).Convert(); Body.MotionState.WorldTransform = startTransform * mat; }
internal void Update(float delta) { animController?.Update(); if (!_isPlaing) { return; } _isEnded = false; _time += delta; _time = (_time >= _length + _frameLength) ? _time % _length : _time; int prevFrame = (int)(_time / _frameLength); int curFrame = (prevFrame == _animation.frames.Length - 1) ? 0 : prevFrame + 1; float frameDelta = _time / _frameLength; frameDelta = frameDelta - (int)frameDelta; AnimationFrame frame1 = _animation.frames[prevFrame], frame2 = _animation.frames[curFrame]; foreach (var pair in boneReference) { Quaternion rotation = Quaternion.Identity; Vector3 pos = frame1.BonePoritions[pair.Value].Position + (frame2.BonePoritions[pair.Value].Position - frame1.BonePoritions[pair.Value].Position) * frameDelta; if (_animation.GetRotationType == Animation.RotationType.Quaternion) { Quaternion prevQuat = new Quaternion(frame1.BonePoritions[pair.Value].Rotation.Xyz, frame1.BonePoritions[pair.Value].Rotation.W); Quaternion nextQuat = new Quaternion(frame2.BonePoritions[pair.Value].Rotation.Xyz, frame2.BonePoritions[pair.Value].Rotation.W); rotation = Quaternion.Slerp(prevQuat, nextQuat, frameDelta); } else { var deltaRot = frame2.BonePoritions[pair.Value].Rotation - frame1.BonePoritions[pair.Value].Rotation; CycleDeltaCheck(ref deltaRot.X); CycleDeltaCheck(ref deltaRot.Y); CycleDeltaCheck(ref deltaRot.Z); Vector4 rot = frame1.BonePoritions[pair.Value].Rotation + deltaRot * frameDelta; rotation = Quaternion.FromAxisAngle(Vector3.UnitZ, rot.Z) * Quaternion.FromAxisAngle(Vector3.UnitY, rot.Y) * Quaternion.FromAxisAngle(Vector3.UnitX, rot.X); } //skip masked out bone if (!BoneController.GetBone(pair.Key).FollowAnimation) { continue; } if (_animation.TransType == Animation.TransformType.LocalRelative) { BoneController.GetBone(pair.Key).SetTransform(rotation, pos); } else if (_animation.TransType == Animation.TransformType.LocalAbsolute) { BoneController.GetBone(pair.Key).InitialLocalTransform = Matrix4.CreateFromQuaternion(rotation) * Matrix4.CreateTranslation(pos); } } if (curFrame == _animation.frames.Length - 1) { if (!IsRepeat) { //Pause(); _isPlaing = false; _isEnded = true; } } }