//public List<Constraint> Constrains = new List<Constraint>();
 //public List<Joint> Joints = new List<Joint>();
 //public Dictionary<Bone, IBroadphaseEntity> Parts = new Dictionary<Bone, IBroadphaseEntity>();
 //public void AddToWorld(World world)
 //{
 //    foreach (IBroadphaseEntity body in Parts.Values)
 //    {
 //        if (body.GetType() == typeof(RigidBody))
 //        {
 //            world.AddBody(body as RigidBody);
 //        }
 //        else if (body.GetType() == typeof(SoftBody))
 //        {
 //            world.AddBody(body as SoftBody);
 //        }
 //    }
 //    foreach (Constraint c in Constrains)
 //    {
 //        world.AddConstraint(c);
 //    }
 //}
 public Matrix GetTransforms(Bone b)
 {
     //if (Parts.ContainsKey(b))
         //return b.InverseBindPose * JConvert.ToDXMatrix((Parts[b] as RigidBody).Orientation) * Matrix.Translation(JConvert.ToDXVector((Parts[b] as RigidBody).Position));
     //else
         return Matrix.Identity;
 }
 public Keyframe(Bone bone, float time, Matrix output, InterpolationType interpolation)
 {
     Bone = bone;
     Time = time;
     Transform = output;
     Interpolation = interpolation;
     Transform.Decompose(out Scale, out Rotation, out Translation);
 }
        /// <summary>
        /// Constructs a new skinned mesh primitive, with given vertices and indices
        /// </summary>
        /// <param name="device">Device to create primitive with.</param>
        /// <param name="VerticesArray">An array of vertices</param>
        /// <param name="IndicesArray">An array of indices</param>
        public SkinnedMeshPrimitive(Renderer Renderer, Vertex[] VerticesArray, int[] IndicesArray, Bone[] Bones)
        {
            this.Renderer = Renderer;
            this.CollisionShape = new EmptyShape();
            this.AnimationClips = new List<AnimationClip>();
            this.Bones = new List<Bone>();
            this.BonesSID = new Dictionary<string, Bone>();

            List<Vector3> vertices = new List<Vector3>();
            List<int> triIndex = new List<int>();
            foreach (Vertex v in VerticesArray)
            {
                GeometryData.AddVertex(v);
            }
            for (int i = 0; i < IndicesArray.Length; i++)
            {
                GeometryData.AddIndex(IndicesArray[i]);
            }
            foreach (Bone b in Bones)
            {
                this.Bones.Add(b);
                BonesSID.Add(b.Name, b);
            }

            BufferDescription BBDesc = new BufferDescription()
            {
                Usage = ResourceUsage.Dynamic,
                BindFlags = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = MaxBones * (sizeof(float) * 4 * 4),
                StructureByteStride = 0,
            };
            BoneBuffer = new Buffer(Renderer.Device, BBDesc);

            InitializePrimitive();
        }
 public Matrix GetTransformsByTime(Bone bone, float time)
 {
     if (!KeyframesT.ContainsKey(bone))
         return Matrix.Identity;
     float closestPrev = 0;
     float closestNext = float.MaxValue;
     Keyframe kPrev = null;
     Keyframe kNext = null;
     foreach (Keyframe k in KeyframesT[bone])
     {
         if (k.Time < time)
         {
             if (k.Time >= closestPrev)
             {
                 closestPrev = k.Time;
                 kPrev = k;
             }
         }
         if (k.Time > time)
         {
             if (k.Time < closestNext)
             {
                 closestNext = k.Time;
                 kNext = k;
             }
         }
     }
     float weight = (float)Interpolation.Linear((double)closestPrev, (double)closestNext, (double)time);
     if (kPrev == null)
     {
         return Matrix.Identity;
     }
     else if (kNext == null)
     {
         return Matrix.Identity;
     }
     else
     {
         Quaternion Rotation = Quaternion.Lerp(kPrev.Rotation, kNext.Rotation, weight);
         Vector3 Translation = Vector3.Lerp(kPrev.Translation, kNext.Translation, weight);
         bone.Position = Translation;
         return Matrix.RotationQuaternion(Rotation) * Matrix.Translation(Translation);
     }
 }