4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MathematicaでMikuMikuDance 2

Last updated at Posted at 2016-04-10

表情モーフィングの計算

1.モーフィング計算

まずモーフィングの計算を行う関数を定義します。

Vlerp[v1_, v2_, t_] := Module[
   {vr, ret},
   vr = (1 - t) v1 + t v2;
   Return[vr];
   ];
SkinMorph[Vertex_, Skin_, Motion_] :=
  Module[{i, j, d, e, f, base, ret, Name, t, emotion},
   ret = Vertex;
   If[Length[Motion] == 0, Return[Vertex]];
   base = Skin[[1]];
   For[i = 1, i <= Length[Motion], i++,
    Name = Motion[[i, 1, 2]];
    t = Motion[[i, 3, 2]];
    j = Flatten[Position[Skin[[All, 1]], Name]];
    
    If[Length[j] == 0, Continue[]];
    j = j[[1]];
    d = Skin[[j]];
    For[j = 1, j <= d[[2]], j++,
     e = Vlerp[
       base[[4, d[[4, j, 1]] + 1, 2]],
       base[[4, d[[4, j, 1]] + 1, 2]] + d[[4, j, 2]],
       t];
     base = ReplacePart[base, {4, d[[4, j, 1]] + 1, 2} -> e];
     ret = ReplacePart[ret, {base[[4, d[[4, j, 1]] + 1, 1]] + 1} -> e];
     ];(* For j *)
    ];
   Return[ret];
   ];

2. 表情の指定

あにまさ式ミクさんのPMDファイルの表情の種類を確認するには下記コマンドを入力します。

In[246]:= f["Skin_data"][[All, 1]]
Out[246]= {"base", "真面目", "困る", "にこり", "怒り", "上", "下", \
"まばたき", "笑い", "ウィンク", "ウィンク2", "ウィンク右", "ウィンク2右", "はぅ", \
"なごみ", "びっくり", "じと目", "なぬ!", "瞳小", "あ", "い", "う", "お", \
"\[FilledUpTriangle]", "\[And]", "\[Omega]", \
"\[Omega]\[EmptySquare]", "はんっ!", "ぺろっ", "えー", "にやり"}

例えばミクさんに、にこりとウィンクしてぺろっとして頂くには下記の様なリストを作成します。

g1 = {
   {{"SkinName", "ウィンク"}, {"FlameNo", 1}, {"Weight", 1.`}},
   {{"SkinName", "にこり"}, {"FlameNo", 1}, {"Weight", 1.`}},
   {{"SkinName", "ぺろっ"}, {"FlameNo", 1}, {"Weight", 1.`}}
   };

ShiftJISの漢字コードが上手くノートブックに入力できない場合は下記入力をしてみてください。

In[23]:= g1 = {
  {{"SkinName", f["Skin_data"][[All, 1]][[10]]}, {"FlameNo", 1},
    {"Weight", 1.`}},
  {{"SkinName", f["Skin_data"][[All, 1]][[4]]}, {"FlameNo", 1},
    {"Weight", 1.`}},
  {{"SkinName", f["Skin_data"][[All, 1]][[29]]}, {"FlameNo", 1},
    {"Weight", 1.`}}
  }
Out[23]= {
{{"SkinName", "ウィンク"}, {"FlameNo", 1}, {"Weight", 1.}},
{{"SkinName", "にこり"}, {"FlameNo", 1}, {"Weight", 1.}},
{{"SkinName", "ぺろっ"}, {"FlameNo", 1}, {"Weight", 1.}}
}

3. 表情モーフィングの実行

表情モーフィングをした頂点座標を計算します。

vertex = SkinMorph[f["VertexVectors"], f["Skin_data"], g1];

4. 結果の表示

表示してみます。

Graphics3D[
 Prepend[Flatten[#, 1] & /@ 
   Partition[
    Riffle[f["TextureList"][[# + 1]] & /@ f["MaterialIndices"], 
     Polygon[#[[1]], #[[2]], #[[
         3]]] & /@ ({vertex[[# + 1]], 
          VertexNormals -> f["NormalVectors"][[# + 1]], 
          VertexTextureCoordinates -> f["UVVectors"][[# + 1]]} & /@ 
        f["FaceIndices"])], 2], EdgeForm[]], 
 ViewPoint -> {0.295918150638636`, 0.295873255916524`, 3.357810516475874`}, 
 ViewCenter -> {{0.5`, 0.5`, 0.5`},
                      {0.47532409931375563`,-1.0411988335153852`}}, 
 ViewAngle -> 0.07048280598964335`, 
 ViewVertical -> 
      {0.05306019054920806`, 0.8551795243995924`, 1.5428727481287545`}
]

名称未定義-3.png
ウィンクして、にこりと笑って、ペロっとしていると思います。

つづく

5 関連リンク

MathematicaでMikuMikuDance 3 ステージの読み込み

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?