• R/O
  • SSH
  • HTTPS

mmdx: Commit


Commit MetaInfo

Revision664 (tree)
Zeit2011-01-11 09:55:29
Autorwilfrem

Log Message

アニメーションプレイヤーのModelへの組み込み

Ändern Zusammenfassung

Diff

--- branches/XNA4/MikuMikuDanceXNA/Model/MMDBoneManager.cs (revision 663)
+++ branches/XNA4/MikuMikuDanceXNA/Model/MMDBoneManager.cs (revision 664)
@@ -9,12 +9,31 @@
99 public class MMDBoneManager
1010 {
1111 ReadOnlyCollection<MMDBone> bones;
12- ReadOnlyCollection<MMDIK> iks;
13-
12+ public ReadOnlyCollection<MMDIK> IKs { get; private set; }
13+ Dictionary<string, int> boneDic;
14+
1415 public MMDBoneManager(List<MMDBone> bones, List<MMDIK> iks)
1516 {
1617 this.bones = new ReadOnlyCollection<MMDBone>(bones);
17- this.iks = new ReadOnlyCollection<MMDIK>(iks);
18+ this.IKs = new ReadOnlyCollection<MMDIK>(iks);
19+ boneDic = new Dictionary<string, int>();
20+ for (int i = 0; i < bones.Count; i++)
21+ {
22+ boneDic.Add(bones[i].Name, i);
23+ }
1824 }
25+
26+ public MMDBone this[int index] { get { return bones[index]; } }
27+ public MMDBone this[string key] { get { return bones[boneDic[key]]; } }
28+ public int IndexOf(string key)
29+ {
30+ int result;
31+ if (!boneDic.TryGetValue(key, out result))
32+ return -1;
33+ return result;
34+ }
35+ public int Count { get { return bones.Count; } }
36+
37+
1938 }
2039 }
--- branches/XNA4/MikuMikuDanceXNA/Model/MMDModel.cs (revision 663)
+++ branches/XNA4/MikuMikuDanceXNA/Model/MMDModel.cs (revision 664)
@@ -15,6 +15,9 @@
1515 //private readonly SkinningData skinningData;
1616 readonly MMDBoneManager boneManager;
1717 readonly Dictionary<string, MMDMotionData> attachedMotion;
18+ private AnimationPlayer animationPlayer;
19+ public MMDBoneManager BoneManager { get { return boneManager; } }
20+ public AnimationPlayer AnimationPlayer { get { return animationPlayer; } }
1821
1922 /// <summary>
2023 /// このモデルのメッシュパーツ
@@ -28,7 +31,7 @@
2831 this.attachedMotion = attachedMotion;
2932
3033 Parts = new ReadOnlyCollection<MMDModelPart>(modelParts);
31-
34+ animationPlayer = new AnimationPlayer(boneManager);
3235 }
3336
3437 /// <summary>
--- branches/XNA4/MikuMikuDanceXNA/Motion/AnimationPlayer.cs (revision 663)
+++ branches/XNA4/MikuMikuDanceXNA/Motion/AnimationPlayer.cs (revision 664)
@@ -2,6 +2,9 @@
22 using System.Collections.Generic;
33 using System.Linq;
44 using System.Text;
5+using MikuMikuDance.XNA.Model;
6+using MikuMikuDance.XNA.Misc;
7+using System.Collections;
58
69 namespace MikuMikuDance.XNA.Motion
710 {
@@ -8,9 +11,22 @@
811 /// <summary>
912 /// モーショントラックの管理、ポーズのブレンディング等を行うクラス
1013 /// </summary>
11- public class AnimationPlayer : IEnumerable<MMDMotionTrack>
14+ public class AnimationPlayer
1215 {
1316 Dictionary<string, MMDMotionTrack> motionTracks = new Dictionary<string, MMDMotionTrack>();
17+ MMDBoneManager boneManager;
18+ Dictionary<string, QuatTransform> BindPoses;
19+ Dictionary<string, QuatTransform> Poses;
20+ internal AnimationPlayer(MMDBoneManager bones)
21+ {
22+ boneManager = bones;
23+ BindPoses = new Dictionary<string, QuatTransform>();
24+ for (int i = 0; i < boneManager.Count; ++i)
25+ {
26+ BindPoses.Add(boneManager[i].Name, boneManager[i].BindPose);
27+ }
28+ Poses = new Dictionary<string, QuatTransform>(BindPoses.Count);
29+ }
1430
1531 /// <summary>
1632 /// モーショントラックの取得
@@ -67,26 +83,33 @@
6783 }
6884 }
6985
70- //foreachで列挙可能に……
71- #region IEnumerable<MotionTrack> メンバー
72- /// <summary>
73- /// Enumerator列挙子取得
74- /// </summary>
75- /// <returns>IEnumerator</returns>
76- public IEnumerator<MMDMotionTrack> GetEnumerator()
86+ internal void Update()
7787 {
78- return ((IEnumerable<MMDMotionTrack>)motionTracks).GetEnumerator();
88+ //差分ポーズを元にした加算ブレンディング
89+ Poses.Clear();
90+ foreach (var track in motionTracks)
91+ {
92+ track.Value.Update();
93+ foreach (var subpose in track.Value.SubPoses)
94+ {
95+ QuatTransform pose1, pose2, sub = subpose.Value, result;
96+ if (!Poses.TryGetValue(subpose.Key, out pose1))
97+ {
98+ if (!BindPoses.TryGetValue(subpose.Key, out pose1))
99+ continue;//無いモーションは無視
100+ }
101+ QuatTransform.Multiply(ref sub, ref pose1, out pose2);
102+ QuatTransform.Lerp(ref pose1, ref pose2, track.Value.BoneBlendingFactor, out result);
103+ Poses[subpose.Key] = result;
104+ }
105+ }
106+ //ボーンマネージャへの書き戻し
107+ foreach (var pose in Poses)
108+ {
109+ boneManager[pose.Key].LocalTransform = pose.Value;
110+ }
79111 }
80112
81- #endregion
82-
83- #region IEnumerable メンバー
84-
85- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
86- {
87- return motionTracks.GetEnumerator();
88- }
89-
90- #endregion
113+
91114 }
92115 }
--- branches/XNA4/MikuMikuDanceXNA/Motion/MMDMotionTrack.cs (revision 663)
+++ branches/XNA4/MikuMikuDanceXNA/Motion/MMDMotionTrack.cs (revision 664)
@@ -25,14 +25,20 @@
2525 //モーションデータの読み出し位置
2626 Dictionary<string, int> bonePos = new Dictionary<string, int>();
2727 Dictionary<string, int> facePos = new Dictionary<string, int>();
28+ //ブレンディングファクター
29+ float m_blendingFactor = 1;
2830
2931 //トラックから抽出されたボーンの差分一覧
3032 internal Dictionary<string, QuatTransform> SubPoses { get; private set; }
3133
3234 /// <summary>
33- /// モーション加算合成時のブレンディングファクター
35+ /// ブレンディングファクター
3436 /// </summary>
35- public float BoneBlendingFactor { get; set; }
37+ /// <remarks>このトラックのモーションをどの程度モデルに適応するか。0~1の範囲の値</remarks>
38+ public float BoneBlendingFactor {
39+ get { return m_blendingFactor; }
40+ set { m_blendingFactor = MathHelper.Clamp(value, 0, 1); }
41+ }
3642 /// <summary>
3743 /// モーション再生用FPMs
3844 /// </summary>
@@ -43,8 +49,7 @@
4349 boneFrames = motionData.BoneFrames;
4450 //表情の配列抜き出し
4551 faceFrames = motionData.FaceFrames;
46- //初期ブレンディングファクターは1
47- BoneBlendingFactor = 1;
52+ //FPS=60
4853 FramePerMillisecond = 60m / 1000m;
4954 //差分一覧を作成
5055 SubPoses = new Dictionary<string, QuatTransform>(motionData.BoneFrames.Count);
--- branches/XNA4/MikuMikuDanceXNA/Misc/QuatTransform.cs (revision 663)
+++ branches/XNA4/MikuMikuDanceXNA/Misc/QuatTransform.cs (revision 664)
@@ -197,5 +197,19 @@
197197 result = new QuatTransform();
198198 Quaternion.CreateFromYawPitchRoll(0, 0, rot, out result.Rotation);
199199 }
200+
201+ /// <summary>
202+ /// 姿勢の線形補間
203+ /// </summary>
204+ /// <param name="pose1">姿勢1</param>
205+ /// <param name="pose2">姿勢2</param>
206+ /// <param name="amount">補完係数(0-1)</param>
207+ /// <param name="result">線形補間された姿勢</param>
208+ /// <remarks>Quaternionは球状線形補間を使用</remarks>
209+ internal static void Lerp(ref QuatTransform pose1, ref QuatTransform pose2, float amount, out QuatTransform result)
210+ {
211+ Vector3.Lerp(ref pose1.Translation, ref pose2.Translation, amount, out result.Translation);
212+ Quaternion.Slerp(ref pose1.Rotation, ref pose2.Rotation, amount, out result.Rotation);
213+ }
200214 }
201215 }
Show on old repository browser