Пример #1
0
        public override void OnMappingCreated(int humanoid01BoneIndex, int dwarfBoneIndex)
        {
            if (_dwarfSkeleton == null)
            {
                return;
            }

            var baseScale = 0.5f;

            _animationClip.DynamicFrames[0].Scale[0] = new Vector3(baseScale);
            //var basechildBones = _humanoid01Skeleton.GetChildBones(0);
            //foreach (var childBoneIndex in basechildBones)
            //{
            //    float invScale = 1 / baseScale;
            //    _animationClip.DynamicFrames[0].Scale[childBoneIndex] = new Vector3(invScale);
            //}


            var desiredParentWorldTransform = _dwarfSkeleton.GetWorldTransform(dwarfBoneIndex);
            var dwarfParentBone             = _dwarfSkeleton.GetParentBone(dwarfBoneIndex);
            var dwarfParentWorldPos         = _dwarfSkeleton.GetWorldTransform(dwarfParentBone);
            var dwarfBoneLength             = Vector3.Distance(desiredParentWorldTransform.Translation, dwarfParentWorldPos.Translation);



            var parentBoneIndex = _humanoid01Skeleton.GetParentBone(humanoid01BoneIndex);
            var parentWorld     = _humanoid01Skeleton.GetAnimatedWorldTranform(parentBoneIndex);
            var matrix          = desiredParentWorldTransform * Matrix.Invert(parentWorld);

            matrix.Decompose(out var _, out var newRotation, out var newPosition);


            var humanodB0         = _humanoid01Skeleton.GetWorldTransform(humanoid01BoneIndex);
            var humanodB1         = _humanoid01Skeleton.GetWorldTransform(parentBoneIndex);
            var humanodBoneLength = Vector3.Distance(humanodB0.Translation, humanodB1.Translation);

            float scale = dwarfBoneLength / (humanodBoneLength * baseScale);

            scale = 1;
            _animationClip.DynamicFrames[0].Rotation[humanoid01BoneIndex] = newRotation;
            _animationClip.DynamicFrames[0].Position[humanoid01BoneIndex] = newPosition;//;Quaternion.Multiply(_currentSkeleton.Rotation[newBoneIndex], );
            _animationClip.DynamicFrames[0].Scale[humanoid01BoneIndex]   *= new Vector3(scale);



            var childBones = _humanoid01Skeleton.GetChildBones(humanoid01BoneIndex);

            foreach (var childBoneIndex in childBones)
            {
                float invScale = 1 / scale;
                _animationClip.DynamicFrames[0].Scale[childBoneIndex] *= new Vector3(invScale);
            }

            //ReProcessFucker();
            return;

            // Reset animation
            //  _animationClip.DynamicFrames[0].Position[dwarfBoneIndex] = _humanoid01Skeleton.Translation[humanoid01BoneIndex];

            //// Get the position the the bone we want to move
            // var currentWorldTransform = _humanoid01Skeleton.GetAnimatedWorldTranform(humanoid01BoneIndex);
            //var desiredParentWorldTransform= _dwarfSkeleton.GetWorldTransform(dwarfBoneIndex);
            //
            //currentWorldTransform.Decompose(out var _, out var currentRot, out var currentTrans);
            //desiredParentWorldTransform.Decompose(out var _, out var desiredRot, out var desiredTrans);
            //
            // //var relativeRotation = desiredRot * Quaternion.Inverse(currentRot);
            // var relativeRotation = desiredRot* Quaternion.Inverse(currentRot);
            // var relativePosition = currentTrans - desiredTrans;
            //
            // var parentBoneIndex = _dwarfSkeleton.GetParentBone(dwarfBoneIndex);
            // var parentWorld = _dwarfSkeleton.GetWorldTransform(parentBoneIndex);
            //
            // var w = Matrix.CreateWorld(
            //     Vector3.Zero,
            //     parentWorld.Forward,
            //     parentWorld.Up);
            //
            // var invW = Matrix.Invert(w);
            //
            // //relativePosition = Vector3.Transform(relativePosition, invW);
            // relativeRotation = Quaternion.Multiply(relativeRotation, Quaternion.CreateFromRotationMatrix(invW));
            //
            // _animationClip.DynamicFrames[0].Position[dwarfBoneIndex] = currentTrans;// _humanoid01Skeleton.Translation[humanoid01BoneIndex] + relativePosition;
            // //_animationClip.DynamicFrames[0].Rotation[currentSkeletonBoneIndex] =  Quaternion.Multiply(relativeRotation, _currentSkeleton.Rotation[targetSkeletonBoneIndex]);
            // //_animationClip.DynamicFrames[0].Rotation[newBoneIndex] = relativeRotation;//;Quaternion.Multiply(_currentSkeleton.Rotation[newBoneIndex], );

            return;

            //---- Attemp 1
            // Get origianl parent bone
            //var desiredWorldPos = _currentSkeleton.GetWorldTransform(newBoneIndex);
            //
            //var parentBone = _targetSkeleton.GetParentBone(originalBoneIndex);
            //var parentBoneWorldTrans = _targetSkeleton.GetWorldTransform(parentBone);
            //
            //var transDiff = parentBoneWorldTrans * Matrix.Invert(desiredWorldPos);
            ////var transDiff = desiredWorldPos * Matrix.Invert(parentBoneWorldTrans);
            //
            //var result = transDiff.Decompose(out var _, out var rotation, out var trans);
            //
            //var orgTrans = _animationClip.DynamicFrames[0].Position[originalBoneIndex];
            //var orgRot = _animationClip.DynamicFrames[0].Rotation[originalBoneIndex];
            //
            //_animationClip.DynamicFrames[0].Position[newBoneIndex] = trans;
            //_animationClip.DynamicFrames[0].Rotation[newBoneIndex] = rotation;



            //return
            // Convert currentWorld into origina parentbone space



            //var parent

            //_targetSkeleton.SetBoneFromWorld(55, curretWorld);
            //_targetSkeleton.RebuildSkeletonMatrix();



            // var diff = curretWorld - targetWorld;

            //

            ;
            //for (int i = 0; i < _currentSkeleton.BoneCount; i++)
            //{
            //    var mappedIndex = _mapping.FirstOrDefault(x => x.OriginalValue == i);
            //    if (mappedIndex != null)
            //    {
            //        var parentBoneId = _currentSkeleton.GetParentBone(i);
            //        var parentBoneMapping = _mapping.FirstOrDefault(x => x.OriginalValue == parentBoneId);
            //_animationClip.DynamicFrames[0].Position[originalBoneIndex] = _currentSkeleton.Translation[newBoneIndex];
            //_animationClip.DynamicFrames[0].Rotation[originalBoneIndex] = _currentSkeleton.Rotation[newBoneIndex];


            // _animationPlayer.GetCurrentFrame().BoneTransforms[0].Translation


            //    }
            //}

            //_currentSkeleton.RebuildSkeletonMatrix();


            //base.OnMappingCreated(originalBoneIndex, newBoneIndex);
        }
Пример #2
0
        public AnimationClip GenerateMountAnimation(AnimationClip mountAnimation, AnimationClip riderAnimation)
        {
            Vector3 translationOffset    = new Vector3((float)_animationSettings.Translation.X.Value, (float)_animationSettings.Translation.Y.Value, (float)_animationSettings.Translation.Z.Value);
            Vector3 rotationOffset       = new Vector3((float)_animationSettings.Rotation.X.Value, (float)_animationSettings.Rotation.Y.Value, (float)_animationSettings.Rotation.Z.Value);
            var     rotationOffsetMatrix = Quaternion.CreateFromYawPitchRoll(MathHelper.ToRadians(rotationOffset.X), MathHelper.ToRadians(rotationOffset.Y), MathHelper.ToRadians(rotationOffset.Z));

            var newRiderAnim = riderAnimation.Clone();

            newRiderAnim.MergeStaticAndDynamicFrames();

            View3D.Animation.AnimationEditor.LoopAnimation(newRiderAnim, (int)_animationSettings.LoopCounter.Value);

            // Resample
            if (_animationSettings.FitAnimation)
            {
                newRiderAnim = View3D.Animation.AnimationEditor.ReSample(_riderSkeleton, newRiderAnim, mountAnimation.DynamicFrames.Count, mountAnimation.PlayTimeInSec);
            }

            newRiderAnim.StaticFrame = null;

            var maxFrameCount = Math.Min(mountAnimation.DynamicFrames.Count, newRiderAnim.DynamicFrames.Count);

            for (int i = 0; i < maxFrameCount; i++)
            {
                var mountFrame = AnimationSampler.Sample(i, 0, _mountSkeleton, new List <AnimationClip> {
                    mountAnimation
                });
                var mountBoneWorldMatrix = _mountVertexPositionResolver.GetVertexTransformWorld(mountFrame, _mountVertexId);
                mountBoneWorldMatrix.Decompose(out var _, out var mountVertexRot, out var mountVertexPos);

                // Make sure the rider moves along in the world with the same speed as the mount
                var mountMovement = mountFrame.BoneTransforms[0].Translation;
                newRiderAnim.DynamicFrames[i].Position[0] = mountMovement;// mountAnimation.DynamicFrames[i].Position[0];
                newRiderAnim.DynamicFrames[i].Rotation[0] = Quaternion.Identity;

                var origianlRotation = Quaternion.Identity;
                if (_animationSettings.KeepRiderRotation)
                {
                    var riderFrame = AnimationSampler.Sample(i, 0, _riderSkeleton, new List <AnimationClip> {
                        newRiderAnim
                    });
                    var riderBoneWorldmatrix = riderFrame.GetSkeletonAnimatedWorld(_riderSkeleton, _riderBoneIndex);
                    riderBoneWorldmatrix.Decompose(out var _, out origianlRotation, out var _);
                }

                var originalPosition = newRiderAnim.DynamicFrames[i].Position[_riderBoneIndex];
                var originalRotation = newRiderAnim.DynamicFrames[i].Rotation[_riderBoneIndex];

                var newRiderPosition = mountVertexPos + translationOffset - mountFrame.BoneTransforms[0].Translation;
                var newRiderRotation = Quaternion.Multiply(Quaternion.Multiply(mountVertexRot, origianlRotation), rotationOffsetMatrix);

                var riderPositionDiff = newRiderPosition - originalPosition;
                var riderRotationDiff = newRiderRotation * Quaternion.Inverse(originalRotation);

                newRiderAnim.DynamicFrames[i].Position[_riderBoneIndex] = newRiderPosition;
                newRiderAnim.DynamicFrames[i].Rotation[_riderBoneIndex] = newRiderRotation;

                // Find all the bones at the same level (normally attachmentpoints) and move them as well
                var parentBoneIndex = _riderSkeleton.GetParentBone(_riderBoneIndex);
                if (parentBoneIndex != -1)
                {
                    var childNodes = _riderSkeleton.GetChildBones(parentBoneIndex);

                    for (int boneId = 0; boneId < childNodes.Count; boneId++)
                    {
                        var id = childNodes[boneId];
                        if (id == _riderBoneIndex)
                        {
                            continue;
                        }
                        newRiderAnim.DynamicFrames[i].Position[id] += riderPositionDiff;
                        newRiderAnim.DynamicFrames[i].Rotation[id]  = riderRotationDiff * newRiderAnim.DynamicFrames[i].Rotation[id];
                    }
                }
            }

            return(newRiderAnim);
        }