ここでは「立ち」状態と「歩く」状態の間のクロスディゾルブのアニメーションブレンドの方法を紹介します。
○blendFactor という名の新しい float型のメンバ変数を追加して下さい。0~1.0fの間でスケーリングし、現在のアニメーションと目標のアニメーションの間でどのくらい混合しているのかを表現します。
  1. // Add this as a member variable
  2. float blendFactor = 0;
  3.  
○入力に応じて、状態を変化をさせ、適切な blendFactor を設定します。「立ち」状態での変化。
  1. // Add this to the beginning of the if (state=="idle") block in the UpdateState method
  2. // Remove the old if (keyState.IsKeyDown(Keys.W)) block
  3. if (keyState.IsKeyDown(Keys.W))
  4. {
  5. blendFactor = 0;
  6. state = "idleToWalk";
  7. }
  8.  
○「歩く」状態での変化。
  1. // Add this to the beginning of the if (state=="walk") block in the UpdateState method
  2. // Remove the old if (keyState.IsKeyUp(Keys.W)) block
  3. if (keyState.IsKeyUp(Keys.W))
  4. {
  5. blendFactor = 0;
  6. state = "walkToIdle";
  7. }
  8.  
○ドワーフは「立ち」状態から「歩く」状態に移行いるとき、blendFactor を増加させ、アニメーションを設定し、適切な値でアニメーションブレンドを行います。次のソースコードを UpdateState メソッドに追加して下さい。
  1. // Add this to the UpdateState method
  2. else if (state == "idleToWalk")
  3. {
  4. blendFactor += .1f;
  5. currentSpeed = blendFactor * WALK_SPEED;
  6. if (blendFactor >= 1)
  7. {
  8. blendFactor = 1;
  9. state = "walk";
  10. }
  11. foreach (BonePose p in poses)
  12. {
  13. p.CurrentController = idle;
  14. p.CurrentBlendController = walk;
  15. p.BlendFactor = blendFactor;
  16. }
  17. }
  18.  
○ドワーフが「歩く」状態から「立ち」状態に移行しているとき、同じようにしたいと思います。しかし、blendFactor が増加するのに従ってドワーフが減速するので、速度をスケーリング(1 - blendFactor)します。
  1. // Add this to the UpdateState method
  2. else if (state == "walkToIdle")
  3. {
  4. blendFactor += .1f;
  5. currentSpeed = (1f-blendFactor) * WALK_SPEED;
  6. if (blendFactor >= 1)
  7. {
  8. blendFactor = 1;
  9. state = "idle";
  10. }
  11. foreach (BonePose p in poses)
  12. {
  13. p.CurrentController = walk;
  14. p.CurrentBlendController = idle;
  15. p.BlendFactor = blendFactor;
  16. }
  17. }
  18.  

最終更新:2009年06月21日 12:49