116 lines
3.8 KiB
C#
116 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Aiwaz.Contracts;
|
|
|
|
namespace Aiwaz.Resources
|
|
{
|
|
public class Bone : IBone
|
|
{
|
|
#region IBone Members
|
|
|
|
private SlimDX.Matrix transformation;
|
|
private SlimDX.Matrix invPoseTransformation;
|
|
private float lastAnimationTime = float.MinValue;
|
|
private IBone parent;
|
|
private SlimDX.Matrix[] matrixArray = new SlimDX.Matrix[0];
|
|
private ITransformationAnimation transformationAnimation;
|
|
private Dictionary<int, IBone> boneIndexList = new Dictionary<int, IBone>();
|
|
|
|
public Bone(string name, int index, SlimDX.Vector3 poseTranslation, SlimDX.Vector3 poseScale, SlimDX.Quaternion poseRotation)
|
|
{
|
|
PoseTransformation = SlimDX.Matrix.Transformation(SlimDX.Vector3.Zero, SlimDX.Quaternion.Identity, poseScale, SlimDX.Vector3.Zero, poseRotation, poseTranslation);
|
|
invPoseTransformation = this.PoseTransformation;
|
|
invPoseTransformation.Invert();
|
|
this.Index = index;
|
|
this.ChildBones = new List<IBone>();
|
|
}
|
|
|
|
public SlimDX.Matrix GetTransformationAtTime(float time, SlimDX.Matrix rootTransformationMatrix)
|
|
{
|
|
if (this.TransformationAnimation != null)
|
|
{
|
|
if (time != lastAnimationTime)
|
|
{
|
|
if (this.Parent != null)
|
|
{
|
|
transformation = (this.TransformationAnimation.GetTransformationAtTime(time) * invPoseTransformation) * this.Parent.GetTransformationAtTime(time, rootTransformationMatrix);
|
|
transformation = invPoseTransformation * this.TransformationAnimation.GetTransformationAtTime(time);
|
|
//m_Transformation = m_Transformation * ( * m_InvPoseTransformation);
|
|
}
|
|
else
|
|
transformation = this.TransformationAnimation.GetTransformationAtTime(time) * invPoseTransformation;
|
|
}
|
|
}
|
|
return transformation;
|
|
}
|
|
|
|
public Dictionary<int, IBone> BoneIndexList
|
|
{
|
|
get
|
|
{
|
|
if (parent != null)
|
|
return parent.BoneIndexList;
|
|
|
|
if (boneIndexList.Count == 0)
|
|
this.FillBoneIndexList(this);
|
|
|
|
return boneIndexList;
|
|
}
|
|
}
|
|
|
|
public List<IBone> ChildBones { get; protected set; }
|
|
|
|
public int Index { get; protected set; }
|
|
|
|
public SlimDX.Matrix[] MatrixArray
|
|
{
|
|
get
|
|
{
|
|
if (matrixArray == null)
|
|
matrixArray = new SlimDX.Matrix[this.BoneIndexList.Count];
|
|
return matrixArray;
|
|
}
|
|
}
|
|
|
|
public string Name { get; protected set; }
|
|
|
|
public SlimDX.Matrix PoseTransformation { get; protected set; }
|
|
|
|
public IBone Parent
|
|
{
|
|
get { return parent; }
|
|
set
|
|
{
|
|
if (parent != null)
|
|
parent.ChildBones.Remove(this);
|
|
parent = value;
|
|
if (parent != null)
|
|
parent.ChildBones.Add(this);
|
|
}
|
|
}
|
|
|
|
public ITransformationAnimation TransformationAnimation
|
|
{
|
|
get { return transformationAnimation; }
|
|
set
|
|
{
|
|
transformationAnimation = value;
|
|
lastAnimationTime = float.MinValue;
|
|
transformation = new SlimDX.Matrix();
|
|
}
|
|
}
|
|
|
|
internal void FillBoneIndexList(IBone currentBone)
|
|
{
|
|
boneIndexList[currentBone.Index] = currentBone;
|
|
foreach (var childBone in currentBone.ChildBones)
|
|
this.FillBoneIndexList(childBone);
|
|
}
|
|
|
|
|
|
#endregion
|
|
}
|
|
}
|