Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Animation

Discussion in 'Scripting' started by puyaceo, Mar 4, 2012.

  1. puyaceo

    puyaceo

    Joined:
    Jan 25, 2012
    Posts:
    76
    I'm trying to write a program for skeletal animation in DirectX 9, I have used LoadMeshFromHierarchy function to load an animated mesh...now I would like to bypass the animController so that I can dictate the animation by reading keyframes from the animated mesh file(ex. tiny.x) and looping through those keys at will.

    Here is what I have so far...at this point I have already parsed the .x file successfully and stored each animation, and animationkey for the sole animation set within a class (Anim). When I run this update function the animated mesh is disfigured, i can't figure out why...I assume it is the process by which I update the transformation matrix for each frame...here is my code:

    Code (csharp):
    1. void cAnimationCollection::Update(DWORD AnimSetIndex, DWORD time)
    2. {
    3.     D3DXFRAME_EXTENDED *currentFrame = (D3DXFRAME_EXTENDED*)m_entity->m_frameRoot;
    4.    
    5.     cAnimationSet *AnimSet = m_AnimationSets;
    6.    
    7.     assert(AnimSetIndex <= index);
    8.     while(AnimSet != NULL)
    9.     {
    10.         if(AnimSet->m_index == AnimSetIndex)
    11.         {
    12.             cAnimation *Anim = AnimSet->m_Animations;
    13.             while(Anim != NULL)
    14.             {  
    15.                 D3DXMatrixIdentity(&Anim->m_Frame->TransformationMatrix);
    16.                 if(Anim->m_NumScaleKeys  Anim->m_ScaleKeys)
    17.                 {
    18.                     DWORD ScaleKey=0, ScaleKey2=0;
    19.                     for(DWORD i = 0; i < Anim->m_NumScaleKeys; i++)
    20.                     {
    21.                         if(time >= Anim->m_ScaleKeys[i].m_Time)
    22.                             ScaleKey = i;
    23.                     }
    24.                     ScaleKey2 = (ScaleKey>=(Anim->m_NumScaleKeys-1))?ScaleKey:ScaleKey+1;
    25.                     float TimeDiff = Anim->m_ScaleKeys[ScaleKey2].m_Time - Anim->m_ScaleKeys[ScaleKey].m_Time;
    26.                     if(!TimeDiff)
    27.                         TimeDiff = 1;
    28.                     float Scalar = ((float)time - Anim->m_ScaleKeys[ScaleKey].m_Time) / (float)TimeDiff;
    29.                     D3DXVECTOR3 vecScale = Anim->m_ScaleKeys[ScaleKey2].m_VecKey - Anim->m_ScaleKeys[ScaleKey].m_VecKey;
    30.                     vecScale *= Scalar;
    31.                     vecScale += Anim->m_ScaleKeys[ScaleKey].m_VecKey;
    32.  
    33.                     D3DXMATRIX matScale;
    34.                     D3DXMatrixScaling(&matScale, vecScale.x, vecScale.y, vecScale.z);
    35.                     Anim->m_Frame->TransformationMatrix *= matScale;
    36.                 }
    37.                 if(Anim->m_NumRotationKeys  Anim->m_RotationKeys)
    38.                 {
    39.                     DWORD RotKey=0, RotKey2=0;
    40.                     for(DWORD i = 0; i < Anim->m_NumRotationKeys; i++)
    41.                     {
    42.                         if(time >= Anim->m_RotationKeys[i].m_Time)
    43.                             RotKey = i;
    44.                     }
    45.                     RotKey2 = (RotKey>=(Anim->m_NumRotationKeys-1))?RotKey:RotKey+1;
    46.                     float TimeDiff = Anim->m_RotationKeys[RotKey2].m_Time - Anim->m_RotationKeys[RotKey].m_Time;
    47.                     if(!TimeDiff)
    48.                         TimeDiff = 1;
    49.                     float Scalar = ((float)time - Anim->m_RotationKeys[RotKey].m_Time) / (float)TimeDiff;
    50.                     D3DXQUATERNION quatRotation;
    51.                     D3DXQuaternionSlerp(&quatRotation,
    52.                         &Anim->m_RotationKeys[RotKey].m_QuatKey,
    53.                         &Anim->m_RotationKeys[RotKey2].m_QuatKey,
    54.                         Scalar);
    55.  
    56.                     D3DXMATRIX matRotation;
    57.                     D3DXMatrixRotationQuaternion(&matRotation, &quatRotation);
    58.                     Anim->m_Frame->TransformationMatrix *= matRotation;
    59.                 }
    60.                 if(Anim->m_NumTranslationKeys  Anim->m_TranslationKeys)
    61.                 {
    62.                     DWORD PosKey=0, PosKey2=0;
    63.                     for(DWORD i = 0; i < Anim->m_NumTranslationKeys; i++)
    64.                     {
    65.                         if(time >= Anim->m_TranslationKeys[i].m_Time)
    66.                             PosKey = i;
    67.                     }
    68.                     PosKey2 = (PosKey>=(Anim->m_NumTranslationKeys-1))?PosKey:PosKey+1;
    69.                     float TimeDiff = Anim->m_TranslationKeys[PosKey2].m_Time - Anim->m_TranslationKeys[PosKey].m_Time;
    70.                     if(!TimeDiff)
    71.                         TimeDiff = 1;
    72.                     float Scalar = ((float)time - Anim->m_TranslationKeys[PosKey].m_Time) / (float)TimeDiff;
    73.                     D3DXVECTOR3 vecPos = Anim->m_TranslationKeys[PosKey2].m_VecKey - Anim->m_TranslationKeys[PosKey].m_VecKey;
    74.                     vecPos *= Scalar;
    75.                     vecPos += Anim->m_TranslationKeys[PosKey].m_VecKey;;
    76.                     D3DXMATRIX matTranslation;
    77.                     D3DXMatrixTranslation(&matTranslation, vecPos.x, vecPos.y, vecPos.z);
    78.                     Anim->m_Frame->TransformationMatrix *= matTranslation;
    79.                 }
    80.                 if(Anim->m_NumMatrixKeys  Anim->m_MatrixKeys)
    81.                 {
    82.                     DWORD Key1 = 0, Key2 = 0;
    83.                     for(DWORD i=0;i<Anim->m_NumMatrixKeys;i++)
    84.                     {
    85.                         if(time >= Anim->m_MatrixKeys[i].m_Time)
    86.                             Key1 = i;
    87.                     }
    88.                     Key2 = (Key1>=(Anim->m_NumMatrixKeys-1))?Key1:Key1+1;
    89.                     float TimeDiff = Anim->m_MatrixKeys[Key2].m_Time - Anim->m_MatrixKeys[Key1].m_Time;
    90.                     if(!TimeDiff)          
    91.                         TimeDiff = 1;        
    92.                     float Scalar = ((float)time - Anim->m_MatrixKeys[Key1].m_Time) / (float)TimeDiff;
    93.                     D3DXMATRIX matDiff = Anim->m_MatrixKeys[Key2].m_MatKey - Anim->m_MatrixKeys[Key1].m_MatKey;
    94.                     matDiff *= Scalar;        
    95.                     matDiff += Anim->m_MatrixKeys[Key1].m_MatKey;
    96.                     Anim->m_Frame->TransformationMatrix *= matDiff;    
    97.                 }
    98.                
    99.                 Anim = Anim->m_Next;
    100.             }
    101.         }
    102.         AnimSet = AnimSet->m_Next;
    103.     }
    104.    
    105.     m_entity->UpdateFrameMatrices(m_entity->m_frameRoot, 0);
    106.     m_entity->UpdateSkinnedMesh(m_entity->m_frameRoot);
    107.     if(AnimSet == NULL)
    108.         return;
    109. }

    Is my method correct? The first thing I do for each frame is reset the transformation matrix to identity, then I calculate an interpolated value for each key(translation, scale, rotation, matrix) and apply it to the transformation matrix...then I update the frame matrices, and then the skinned mesh.

    If it is design in C Lang you would that make Animation more Simple? I simply would like to override the Animation bones and make the upper area of the Character face towards the crosshairs though also play some of the animation so if it is idle animation the animation plays so it is moving though also facing the crosshairs. This isgreat as I can make the character fire weapons and walk with this script and not need different animations.

    Any ideas?
     
    Last edited: Mar 4, 2012
  2. Thomas-Pasieka

    Thomas-Pasieka

    Moderator

    Joined:
    Sep 19, 2005
    Posts:
    2,123
unityunity