GetBone() public method

Gets a bone by its name.
public GetBone ( string name ) : Bone
name string Name of the bone to retrieve.
return Bone
Beispiel #1
0
 public void Apply(Skeleton skeleton, float time, float weight, bool accumulate, float scale)
 {
     // loop through tracks and update them all with current time
     foreach (KeyValuePair <ushort, NodeAnimationTrack> pair in nodeTrackList)
     {
         NodeAnimationTrack track = pair.Value;
         Bone bone = skeleton.GetBone(pair.Key);
         track.ApplyToNode(bone, time, weight, accumulate, scale);
     }
 }
Beispiel #2
0
 public void Apply(Skeleton skeleton, float time, float weight, bool accumulate, float scale)
 {
     // loop through tracks and update them all with current time
     foreach (var pair in this.nodeTrackList)
     {
         var track = pair.Value;
         var bone  = skeleton.GetBone(pair.Key);
         track.ApplyToNode(bone, time, weight, accumulate, scale);
     }
 }
 protected void TransformSkeleton(Matrix4 unscaledTransform, float scale)
 {
     Matrix4 invExportTransform = unscaledTransform.Inverse();
     Dictionary<string, Matrix4> fullInverseBoneTransforms = new Dictionary<string, Matrix4>();
     Skeleton newSkeleton = new Skeleton(skeleton.Name);
     // Construct new versions of the bones, and build
     // the inverse bind matrix that will be needed.
     for (ushort i = 0; i < skeleton.BoneCount; ++i) {
         Bone bone = skeleton.GetBone(i);
         Bone newBone = newSkeleton.CreateBone(bone.Name, bone.Handle);
         fullInverseBoneTransforms[bone.Name] =
             bone.BindDerivedInverseTransform * invExportTransform;
     }
     //  Build the parenting relationship for the new skeleton
     for (ushort i = 0; i < skeleton.BoneCount; ++i) {
         Bone bone = skeleton.GetBone(i);
         Bone newBone = newSkeleton.GetBone(i);
         Bone parentBone = (Bone)bone.Parent;
         if (parentBone != null) {
             Bone newParentBone = newSkeleton.GetBone(parentBone.Handle);
             newParentBone.AddChild(newBone);
         }
     }
     // Set the orientation and position for the various bones
     for (ushort i = 0; i < newSkeleton.BoneCount; ++i) {
         Bone bone = skeleton.GetBone(i);
         string boneName = bone.Name;
         string parentName = (bone.Parent == null) ? null : bone.Parent.Name;
         Matrix4 transform = GetLocalBindMatrix(fullInverseBoneTransforms, boneName, parentName, true);
         Quaternion orientation = GetRotation(transform);
         Bone newBone = newSkeleton.GetBone(i);
         newBone.Orientation = orientation;
         // newBone.Scale = transform.Scale;
         newBone.Position = scale * transform.Translation;
     }
     newSkeleton.SetBindingPose();
     for (int i = 0; i < skeleton.AnimationCount; ++i) {
         Animation anim = skeleton.GetAnimation(i);
         Animation newAnim = newSkeleton.CreateAnimation(anim.Name, anim.Length);
         TransformAnimation(unscaledTransform, scale, newAnim, anim, newSkeleton);
     }
     skeleton = newSkeleton;
 }
        protected void TransformAnimation(Matrix4 unscaledTransform, float scale,
										  Animation newAnim, Animation anim,
										  Skeleton newSkeleton)
        {
            // With the new idea I had for transforming these, I need the tracks
            // set up for the parent bones before I can handle the child bones.
            for (int i = 0; i < anim.Tracks.Count; ++i) {
                AnimationTrack track = anim.Tracks[i];
                AnimationTrack newTrack = newAnim.CreateTrack(track.Handle);
                Bone targetBone = (Bone)track.TargetNode;
                newTrack.TargetNode = newSkeleton.GetBone(targetBone.Handle);
            }
            // This gets the ordered bone list, and transforms the tracks in
            // that order instead.
            List<Bone> orderedBoneList = GetOrderedBoneList(null, newSkeleton);
            foreach (Bone bone in orderedBoneList)
                TransformTrack(unscaledTransform, scale, newAnim, anim, bone);
        }
 public List<Bone> GetOrderedBoneList(Bone parentBone, Skeleton skel)
 {
     List<Bone> rv = new List<Bone>();
     // Get all the bones that are my children
     List<Bone> childBones = new List<Bone>();
     for (ushort i = 0; i < skel.BoneCount; ++i) {
         Bone bone = skel.GetBone(i);
         if (bone.Parent == parentBone)
             childBones.Add(bone);
     }
     rv.AddRange(childBones);
     // For each of those bones, get their descendents
     foreach (Bone childBone in childBones) {
         List<Bone> bones = GetOrderedBoneList(childBone, skel);
         rv.AddRange(bones);
     }
     return rv;
 }
        protected void TransformAnimation(Matrix4 exportTransform, 
										  Animation newAnim, Animation anim,
										  Skeleton newSkeleton)
        {
            foreach (NodeAnimationTrack track in anim.NodeTracks.Values) {
                NodeAnimationTrack newTrack = newAnim.CreateNodeTrack(track.Handle);
                Bone targetBone = (Bone)track.TargetNode;
                newTrack.TargetNode = newSkeleton.GetBone(targetBone.Handle);
                TransformTrack(exportTransform, newTrack, track, targetBone);
            }
        }
Beispiel #7
0
		public void Apply( Skeleton skeleton, float time, float weight, bool accumulate, float scale )
		{
			// loop through tracks and update them all with current time
			foreach ( KeyValuePair<ushort, NodeAnimationTrack> pair in nodeTrackList )
			{
				NodeAnimationTrack track = pair.Value;
				Bone bone = skeleton.GetBone( pair.Key );
				track.ApplyToNode( bone, time, weight, accumulate, scale );
			}
		}
 /// <summary>
 ///   Utility method to add attachment points to an existing skeleton
 /// </summary>
 /// <param name="dstDir">the directory to which the modified skeleton will be saved</param>
 /// <param name="skelFile">the name of the file to which the modified skeleton will be written</param>
 /// <param name="skeleton">the original skeleton</param>
 /// <param name="attachPoints">the list of attachment points</param>
 private static void AddAttachments( string dstDir,
                                    string skelFile,
                                    Skeleton skeleton,
                                    List<AttachmentPointNode> attachPoints )
 {
     if( skeleton.AttachmentPoints.Count > 0 &&
         attachPoints.Count != skeleton.AttachmentPoints.Count )
         log.WarnFormat( "Skeleton attachment points count ({0}) does not match new count ({1})",
                         skeleton.AttachmentPoints.Count, attachPoints.Count );
     foreach( AttachmentPointNode attachPoint in attachPoints )
     {
         Quaternion rotate;
         Vector3 translate, scale;
         Matrix4 transform = attachPoint.Transform;
         Matrix4.DecomposeMatrix( ref transform, out translate, out rotate, out scale );
         Bone parentBone = skeleton.GetBone( attachPoint.ParentBone );
         bool isDup = false;
         foreach( AttachmentPoint tmp in skeleton.AttachmentPoints )
             if( tmp.Name == attachPoint.Name )
                 isDup = true;
         if( isDup )
             continue;
         skeleton.CreateAttachmentPoint( attachPoint.Name, parentBone.Handle, rotate, translate );
     }
     OgreSkeletonSerializer skelWriter = new OgreSkeletonSerializer();
     skelWriter.ExportSkeleton( skeleton, dstDir + skelFile );
 }
Beispiel #9
0
		public void Apply( Skeleton skeleton, float time, float weight, bool accumulate, float scale )
		{
			// loop through tracks and update them all with current time
			foreach ( var pair in this.nodeTrackList )
			{
				var track = pair.Value;
				var bone = skeleton.GetBone( pair.Key );
				track.ApplyToNode( bone, time, weight, accumulate, scale );
			}
		}
        public static void CleanupAnimation(Skeleton skel, Animation anim)
        {
            Animation newAnim = skel.CreateAnimation("_replacement", anim.Length);
            Animation tmpAnim = skel.CreateAnimation("_temporary", anim.Length);
            foreach (NodeAnimationTrack track in anim.NodeTracks.Values) {
                Bone bone = skel.GetBone((ushort)track.Handle);
                NodeAnimationTrack newTrack = newAnim.CreateNodeTrack(track.Handle, bone);
                NodeAnimationTrack tmpTrack = tmpAnim.CreateNodeTrack(track.Handle, bone);
                int maxFrame = track.KeyFrames.Count;
                int lastKeyFrame = -1;
                for (int keyFrameIndex = 0; keyFrameIndex < track.KeyFrames.Count; ++keyFrameIndex) {
                    if (anim.InterpolationMode == InterpolationMode.Linear) {
                        // Linear is based on one point before and one after.
                        TransformKeyFrame cur = track.GetTransformKeyFrame(keyFrameIndex);
                        if (keyFrameIndex == 0 ||
                            keyFrameIndex == (track.KeyFrames.Count - 1)) {
                            // Add the key frame if it is the first or last keyframe.
                            lastKeyFrame = keyFrameIndex;
                            DuplicateKeyFrame(newTrack, cur);
                        } else {
                            // Make sure tmpTrack is clean.. we just use it for interpolation
                            tmpTrack.RemoveAllKeyFrames();
                            TransformKeyFrame prior = track.GetTransformKeyFrame(lastKeyFrame);
                            TransformKeyFrame next = track.GetTransformKeyFrame(keyFrameIndex + 1);
                            DuplicateKeyFrame(tmpTrack, prior);
                            DuplicateKeyFrame(tmpTrack, next);
                            // Check to see if removing this last keyframe will throw off
                            // any of the other keyframes that were considered redundant.
                            bool needKeyFrame = false;
                            for (int i = lastKeyFrame + 1; i <= keyFrameIndex; ++i) {
                                TransformKeyFrame orig = track.GetTransformKeyFrame(i);
                                TransformKeyFrame interp = new TransformKeyFrame(tmpTrack, orig.Time);
                                tmpTrack.GetInterpolatedKeyFrame(orig.Time, interp);
                                // Is this interpolated frame useful or redundant?
                                if (!CompareKeyFrames(interp, cur)) {
                                    needKeyFrame = true;
                                    break;
                                }
                            }
                            if (needKeyFrame) {
                                lastKeyFrame = keyFrameIndex;
                                DuplicateKeyFrame(newTrack, cur);
                            }
                        }
                    } else if (anim.InterpolationMode == InterpolationMode.Spline) {
                        // Spline is based on two points before and two after.
                        TransformKeyFrame cur = track.GetTransformKeyFrame(keyFrameIndex);
            #if DISABLED_CODE
                        if (keyFrameIndex == 0 ||
                            keyFrameIndex == 1 ||
                            keyFrameIndex == (track.KeyFrames.Count - 1) ||
                            keyFrameIndex == (track.KeyFrames.Count - 2)) {
                            // Add the key frame if it is the first, second, last or second to last keyframe.
                            DuplicateKeyFrame(newTrack, cur);
                        } else {
                            // Make sure tmpTrack is clean.. we just use it for interpolation
                            tmpTrack.RemoveAllKeyFrames();
                            TransformKeyFrame prior1 = track.GetTransformKeyFrame(keyFrameIndex - 2);
                            TransformKeyFrame prior2 = track.GetTransformKeyFrame(keyFrameIndex - 1);
                            TransformKeyFrame next1 = track.GetTransformKeyFrame(keyFrameIndex + 1);
                            TransformKeyFrame next2 = track.GetTransformKeyFrame(keyFrameIndex + 2);
                            DuplicateKeyFrame(tmpTrack, prior1);
                            DuplicateKeyFrame(tmpTrack, prior2);
                            DuplicateKeyFrame(tmpTrack, next1);
                            DuplicateKeyFrame(tmpTrack, next2);
                            TransformKeyFrame interp = new TransformKeyFrame(tmpTrack, cur.Time);
                            tmpTrack.GetInterpolatedKeyFrame(cur.Time, interp);
                            // Is this interpolated frame useful or redundant?
                            if (!CompareKeyFrames(interp, cur))
                                DuplicateKeyFrame(newTrack, cur);
                        }
            #else
                        DuplicateKeyFrame(newTrack, cur);
            #endif
                    } else {
                        System.Diagnostics.Debug.Assert(false, "Invalid InterpolationMode: " + anim.InterpolationMode);
                    }
                }
            }
            skel.RemoveAnimation(tmpAnim.Name);
            skel.RemoveAnimation(newAnim.Name);
            skel.RemoveAnimation(anim.Name);

            // Recreate the animation with the proper name (awkward)
            anim = skel.CreateAnimation(anim.Name, anim.Length);
            foreach (NodeAnimationTrack track in newAnim.NodeTracks.Values) {
                Bone bone = skel.GetBone((ushort)track.Handle);
                NodeAnimationTrack newTrack = anim.CreateNodeTrack(track.Handle, bone);
                foreach (KeyFrame keyFrame in track.KeyFrames)
                    DuplicateKeyFrame(newTrack, (TransformKeyFrame)keyFrame);
            }
        }
        /// <summary>
        ///   Set up the attachment point in the skeleton or mesh based on the 
        ///   information in the attachPoint.
        /// </summary>
        /// <param name="transform">the world transform to apply to this object</param>
        /// <param name="attachPoint">the AttachmentPointNode with information about parent bone, and the relative transform</param>
        /// <param name="skeleton">the skeleton for whcih we may create the attachment (if there is a parent bone)</param>
        /// <param name="mesh">the mesh for which we may create the attachment point (if there is no parent bone)</param>
        protected void ProcessAttachmentPointNode( Matrix4 transform,
                                                  AttachmentPointNode attachPoint,
                                                  Skeleton skeleton, Mesh mesh )
        {
            string parentName = attachPoint.ParentBone;
            Bone parentBone = null;
            if( parentName != null )
                parentBone = skeleton.GetBone( parentName );

            Matrix4 attachPointTransform = attachPoint.Transform;
            if( parentBone == null )
            {
                // The parent bone will already have the transform in the hierarchy
                attachPointTransform = transform * attachPointTransform;
            }
            Quaternion orientation = MathHelpers.GetRotation( attachPointTransform );
            Vector3 position = m_UnitConversion * attachPointTransform.Translation;
            if( parentBone != null )
                skeleton.CreateAttachmentPoint( attachPoint.Name, parentBone.Handle, orientation, position );
            else
                mesh.CreateAttachmentPoint( attachPoint.Name, orientation, position );
            m_Log.InfoFormat( "Created attachment point: {0}", attachPoint.Name );
            m_Log.InfoFormat( "Parent Bone: {0}", parentName );
            m_Log.InfoFormat( "Orientation: {0}", orientation );
            m_Log.InfoFormat( "Position: {0}", position );
        }