public static Mesh GetOutfitMesh(Skeleton Skel, ulong id) { if (OutfitMeshes.ContainsKey(id)) { return OutfitMeshes[id]; } var mesh = new Mesh(); mesh.Read(ContentManager.GetResourceFromLongID(id)); mesh.ProcessMesh(Skel); OutfitMeshes.Add(id, mesh); return mesh; }
public Skeleton Clone() { var result = new Skeleton(); result.Name = this.Name; result.Bones = new Bone[Bones.Length]; for (int i = 0; i < Bones.Length; i++) { result.Bones[i] = Bones[i].Clone(); } /** Construct tree **/ foreach (var bone in result.Bones) { bone.Children = result.Bones.Where(x => x.ParentName == bone.Name).ToArray(); } result.RootBone = result.Bones.FirstOrDefault(x => x.ParentName == "NULL"); result.ComputeBonePositions(result.RootBone, Matrix.Identity); return result; }
/// <summary> /// Advances the frame of an animation for a skeleton used on this mesh. /// </summary> /// <param name="Skel">A skeleton used to render this mesh.</param> /// <param name="Animation">The animation to advance.</param> /// <param name="AnimationTime">The playback time for an animation (how long has it been playing for?)</param> /// <param name="TimeDelta">The timedelta of the rendering loop.</param> public void AdvanceFrame(Skeleton Skel, Anim Animation, ref float AnimationTime, float TimeDelta) { float Duration = (float)Animation.Motions[0].NumFrames / 30; AnimationTime += TimeDelta; AnimationTime = AnimationTime % Duration; //Loop the animation for (int i = 0; i < Animation.Motions.Count; i++) { int BoneIndex = Skel.FindBone(Animation.Motions[i].BoneName); if (BoneIndex == -1) continue; Bone Bne = Skel.Bones[BoneIndex]; int Frame = (int)(AnimationTime * 30); float FractionShown = AnimationTime * 30 - Frame; int NextFrame = (Frame + 1 != Animation.Motions[0].NumFrames) ? Frame + 1 : 0; if (Animation.Motions[i].HasTranslation == 1) { Vector3 Translation = new Vector3(Animation.Motions[i].Translations[Frame, 0], Animation.Motions[i].Translations[Frame, 1], Animation.Motions[i].Translations[Frame, 2]); Vector3 NextTranslation = new Vector3(Animation.Motions[i].Translations[NextFrame, 0], Animation.Motions[i].Translations[NextFrame, 1], Animation.Motions[i].Translations[NextFrame, 2]); Vector3 UpdatedTranslation = new Vector3(); UpdatedTranslation.X = (1 - FractionShown) * Translation.X + FractionShown * NextTranslation.X; UpdatedTranslation.Y = (1 - FractionShown) * Translation.Y + FractionShown * NextTranslation.Y; UpdatedTranslation.Z = (1 - FractionShown) * Translation.Z + FractionShown * NextTranslation.Z; Bne.GlobalTranslation = UpdatedTranslation; } if (Animation.Motions[i].HasRotation == 1) { Quaternion Rotation = new Quaternion(Animation.Motions[i].Rotations[Frame, 0], Animation.Motions[i].Rotations[Frame, 1], Animation.Motions[i].Rotations[Frame, 2], Animation.Motions[i].Rotations[Frame, 3]); Quaternion NextRotation = new Quaternion(Animation.Motions[i].Rotations[NextFrame, 0], Animation.Motions[i].Rotations[NextFrame, 1], Animation.Motions[i].Rotations[NextFrame, 2], Animation.Motions[i].Rotations[NextFrame, 3]); //Use Slerp to interpolate float W1, W2 = 1.0f; float CosTheta = DotProduct(Rotation, NextRotation); if (CosTheta < 0) { CosTheta *= -1; W2 *= -1; } float Theta = (float)Math.Acos(CosTheta); float SinTheta = (float)Math.Sin(Theta); if (SinTheta > 0.001f) { W1 = (float)Math.Sin((1.0f - FractionShown) * Theta) / SinTheta; W2 *= (float)Math.Sin(FractionShown * Theta) / SinTheta; } else { W1 = 1.0f - FractionShown; W2 = FractionShown; } Quaternion UpdatedRotation = new Quaternion(); UpdatedRotation.X = W1 * Rotation.X + W2 * NextRotation.X; UpdatedRotation.Y = W1 * Rotation.Y + W2 * NextRotation.Y; UpdatedRotation.Z = W1 * Rotation.Z + W2 * NextRotation.Z; UpdatedRotation.W = W1 * Rotation.W + W2 * NextRotation.W; } } }
/// <summary> /// Assigns children to all the bones in a skeleton. /// </summary> /// <param name="Skel">The skeleton to whose bones to assign children to.</param> public void AssignChildren(ref Skeleton Skel) { for(int i = 0; i < Skel.Bones.Length; i++) { for (int j = 0; j < m_Bones.Length; j++) { if (m_Bones[j].ParentName == Skel.Bones[i].BoneName) { //Skel.Bones[i] is the parent of m_Bones[j] Skel.Bones[i].Children[Skel.Bones[i].NumChildren] = m_Bones[j]; Skel.Bones[i].NumChildren++; } } } }
/// <summary> /// Processes the loaded mesh's data and populates an array of /// VertexPositionNormalTexture elements that can be looped to /// render the mesh. Assumes that TransformVertices2() and /// BlendVertices2() has been called for bodymeshes! /// </summary> public void ProcessMesh(Skeleton Skel) { VertexPositionNormalTexture[] NormVerticies = new VertexPositionNormalTexture[TotalVertexCount]; for (int i = 0; i < TotalVertexCount; i++) { NormVerticies[i] = new VertexPositionNormalTexture(); NormVerticies[i].Position.X = TransformedVertices[i].Vertex.Coord.X; NormVerticies[i].Position.Y = TransformedVertices[i].Vertex.Coord.Y; NormVerticies[i].Position.Z = TransformedVertices[i].Vertex.Coord.Z; NormVerticies[i].Normal.X = TransformedVertices[i].Vertex.NormalCoord.X; NormVerticies[i].Normal.Y = TransformedVertices[i].Vertex.NormalCoord.Y; NormVerticies[i].Normal.Z = TransformedVertices[i].Vertex.NormalCoord.Z; } for (int i = 0; i < RealVertexCount; i++) { NormVerticies[i].TextureCoordinate.X = TransformedVertices[i].Vertex.TextureCoord.X; NormVerticies[i].TextureCoordinate.Y = TransformedVertices[i].Vertex.TextureCoord.Y; } m_VertexNTexPositions = NormVerticies; }