void CreateBoneOverview(GameSkeleton skeleton)
        {
            var boneIndexsUsed = _meshNode.Geometry.GetUniqeBlendIndices();

            Bones.Clear();

            if (skeleton == null)
            {
                return;
            }

            for (int i = 0; i < skeleton.BoneCount; i++)
            {
                var parentBoneId = skeleton.GetParentBone(i);
                if (parentBoneId == -1)
                {
                    Bones.Add(CreateNode(i, parentBoneId, skeleton.BoneNames[i], boneIndexsUsed));
                }
                else
                {
                    var treeParent = GetParent(Bones, parentBoneId);

                    if (treeParent != null)
                    {
                        treeParent.Children.Add(CreateNode(i, parentBoneId, skeleton.BoneNames[i], boneIndexsUsed));
                    }
                }
            }

            Bones = FilterHelper.FilterBoneList("", true, Bones);
        }
        void CreateBoneOverview(GameSkeleton skeleton)
        {
            SelectedBone = null;
            Bones.Clear();
            BoneCount = 0;

            if (skeleton == null)
            {
                return;
            }

            BoneCount = skeleton.BoneCount;
            for (int i = 0; i < skeleton.BoneCount; i++)
            {
                var parentBoneId = skeleton.GetParentBone(i);
                if (parentBoneId == -1)
                {
                    Bones.Add(CreateNode(i, parentBoneId, skeleton.BoneNames[i]));
                }
                else
                {
                    var treeParent = GetParent(Bones, parentBoneId);

                    if (treeParent != null)
                    {
                        treeParent.Children.Add(CreateNode(i, parentBoneId, skeleton.BoneNames[i]));
                    }
                }
            }
        }
Esempio n. 3
0
        //Matrix GetBoneMatrix()
        //{
        //    var offset_m4 = (Matrix.Translation(bone.location) * Quaternion(bone.rotation_quaternion).to_matrix().to_4x4())
        //    return bone.pose_matrix * offset_m4.inverted()
        //}

        public override void ReProcessFucker()
        {
            var mapping = MeshBones.Bones.First().BuildRemappingList();

            for (int i = 0; i < _humanoid01Skeleton.BoneCount; i++)
            {
                var mappedIndex = mapping.FirstOrDefault(x => x.OriginalValue == i);
                if (mappedIndex != null)
                {
                    var scale = 1.0f;
                    _animationClip.DynamicFrames[0].Scale[i] = new Vector3(scale);
                    //scale = 1.4f;
                    //if (i == 0)
                    //    _animationClip.DynamicFrames[0].Scale[i] = new Vector3(scale);

                    var parentBoneId      = _humanoid01Skeleton.GetParentBone(i);
                    var dwarfParentBoneId = _dwarfSkeleton.GetParentBone(mappedIndex.NewValue);

                    if (parentBoneId != -1 && dwarfParentBoneId != -1)
                    {
                        var boneLength0 = Vector3.Distance(_humanoid01Skeleton.GetWorldTransform(i).Translation, _humanoid01Skeleton.GetWorldTransform(parentBoneId).Translation);
                        var boneLength1 = Vector3.Distance(_dwarfSkeleton.GetWorldTransform(mappedIndex.NewValue).Translation, _dwarfSkeleton.GetWorldTransform(dwarfParentBoneId).Translation);
                        scale = boneLength1 / boneLength0;
                        if (scale == 0 || float.IsNaN(scale))
                        {
                            scale = 1;
                        }

                        //scale = 0.4f;
                        _animationClip.DynamicFrames[0].Scale[i] = new Vector3(scale);
                    }

                    //_animationClip.DynamicFrames[0].Position[i] = Vector3.Transform( _dwarfSkeleton.Translation[mappedIndex.NewValue], Matrix.Invert(Matrix.CreateScale(scale)));
                    //_animationClip.DynamicFrames[0].Position[i] = _dwarfSkeleton.Translation[mappedIndex.NewValue];
                    // _animationClip.DynamicFrames[0].Rotation[i] = _dwarfSkeleton.Rotation[mappedIndex.NewValue];
                }
            }

            //foreach (var mesh in _meshNodes)
            //    mesh.Scale = new Vector3(0.5f);
        }
        void FitMeshToSkeleton()
        {
            AnimationClip animationClip = new AnimationClip();

            animationClip.DynamicFrames.Add(new AnimationClip.KeyFrame());

            for (int i = 0; i < _currentSkeleton.BoneCount; i++)
            {
                //animationClip.DynamicFrames[0].Rotation.Add(Quaternion.Identity);
                //animationClip.DynamicFrames[0].Position.Add(Vector3.Zero);

                animationClip.DynamicFrames[0].Rotation.Add(_currentSkeleton.Rotation[i]);
                animationClip.DynamicFrames[0].Position.Add(_currentSkeleton.Translation[i]);

                animationClip.RotationMappings.Add(new Filetypes.RigidModel.AnimationFile.AnimationBoneMapping(i));
                animationClip.TranslationMappings.Add(new Filetypes.RigidModel.AnimationFile.AnimationBoneMapping(i));
            }

            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[i] = _targetSkeleton.Translation[mappedIndex.NewValue];
                    animationClip.DynamicFrames[0].Rotation[i] = _targetSkeleton.Rotation[mappedIndex.NewValue];
                }
            }

            _currentSkeleton.RebuildSkeletonMatrix();

            MeshAnimationHelper meshAnimationHelper = new MeshAnimationHelper(_meshOwner, Matrix.Identity);
            var animationFrame = AnimationSampler.Sample(0, 0, _currentSkeleton, animationClip);

            _currentSkeleton.SetAnimationFrame(animationFrame);

            int vertexCount = _meshOwner.Geometry.VertexCount();

            for (int i = 0; i < vertexCount; i++)
            {
                var vertTransform = meshAnimationHelper.GetVertexTransform(animationFrame, i);
                _meshOwner.Geometry.TransformVertex(i, (vertTransform));
            }

            _meshOwner.Geometry.RebuildVertexBuffer();
        }
Esempio n. 5
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);
        }