示例#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);
        }
 public Matrix GetWorldTransform()
 {
     return(_gameSkeleton.GetAnimatedWorldTranform(_boneIndex));
 }