1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/*
* PROJECT: NyMmd
* --------------------------------------------------------------------------------
* The MMD for Java is Java version MMD Motion player class library.
* NyMmd is modules which removed the ARToolKit origin codes from ARTK_MMD,
* and was ported to Java.
*
* This is based on the ARTK_MMD v0.1 by PY.
* http://ppyy.if.land.to/artk_mmd.html
* py1024<at>gmail.com
* http://www.nicovideo.jp/watch/sm7398691
*
*
* The MIT License
* Copyright (C)2008-2012 nyatla
* nyatla39<at>gmail.com
* http://nyatla.jp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package jp.nyatla.nymmd;
import java.util.*;
import java.util.Arrays;
import java.io.*;
import jp.nyatla.nymmd.struct.DataReader;
import jp.nyatla.nymmd.struct.vmd.VMD_Face;
import jp.nyatla.nymmd.struct.vmd.VMD_Header;
import jp.nyatla.nymmd.struct.vmd.VMD_Motion;
import jp.nyatla.nymmd.types.*;
//------------------------------
//ボーンキーフレームソート用比較関数
//------------------------------
class BoneCompare implements java.util.Comparator<BoneKeyFrame>
{
public int compare(BoneKeyFrame o1, BoneKeyFrame o2)
{
return (int)(o1.fFrameNo - o2.fFrameNo);
}
}
//------------------------------
//表情キーフレームソート用比較関数
//------------------------------
class FaceCompare implements java.util.Comparator<FaceKeyFrame>
{
public int compare(FaceKeyFrame o1, FaceKeyFrame o2)
{
return (int)(o1.fFrameNo - o2.fFrameNo);
}
}
public class MmdVmdMotion_BasicClass
{
private MotionData[] _motion_data_array; // ボーンごとのキーフレームデータのリスト
private FaceData[] _face_data_array; // 表情ごとのキーフレームデータのリスト
private float _fMaxFrame; // 最後のフレーム番号
public MmdVmdMotion_BasicClass(InputStream i_stream) throws MmdException
{
initialize(i_stream);
return;
}
public MotionData[] refMotionDataArray()
{
return this._motion_data_array;
}
public FaceData[] refFaceDataArray()
{
return this._face_data_array;
}
public float getMaxFrame()
{
return this._fMaxFrame;
}
private boolean initialize(InputStream i_st) throws MmdException
{
DataReader reader=new DataReader(i_st);
// ヘッダのチェック
VMD_Header tmp_vmd_header=new VMD_Header();
tmp_vmd_header.read(reader);
if(!tmp_vmd_header.szHeader.equalsIgnoreCase("Vocaloid Motion Data 0002"))
{
throw new MmdException();
}
//ボーンと最大フレームを取得
float[] max_frame=new float[1];
this._motion_data_array=createMotionDataList(reader,max_frame);
this._fMaxFrame=max_frame[0];
//表情と最大フレームを再取得
this._face_data_array=createFaceDataList(reader,max_frame);
this._fMaxFrame=this._fMaxFrame>max_frame[0]?this._fMaxFrame:max_frame[0];
return true;
}
private static FaceData[] createFaceDataList(DataReader i_reader,float[] o_max_frame) throws MmdException
{
//-----------------------------------------------------
// 表情のキーフレーム数を取得
Vector<FaceData> result=new Vector<FaceData>();
int ulNumFaceKeyFrames=i_reader.readInt();
//規定フレーム数分表情を読み込み
VMD_Face[] tmp_vmd_face=new VMD_Face[ulNumFaceKeyFrames];
for(int i=0;i<ulNumFaceKeyFrames;i++){
tmp_vmd_face[i]= new VMD_Face();
tmp_vmd_face[i].read(i_reader);
}
float max_frame=0.0f;
for(int i = 0 ; i < ulNumFaceKeyFrames ; i++)
{
if(max_frame < (float)tmp_vmd_face[i].ulFrameNo ){
max_frame = (float)tmp_vmd_face[i].ulFrameNo; // 最大フレーム更新
}
boolean is_found=false;
for(int i2=0;i2<result.size();i2++)
{
final FaceData pFaceTemp = result.get(i2);
if(pFaceTemp.szFaceName.equals(tmp_vmd_face[i].szFaceName))
{
// リストに追加済み
pFaceTemp.ulNumKeyFrames++;
is_found=true;
break;
}
}
if(!is_found)
{
// リストにない場合は新規ノードを追加
FaceData pNew = new FaceData();
pNew.szFaceName=tmp_vmd_face[i].szFaceName;
pNew.ulNumKeyFrames = 1;
result.add(pNew);
}
}
// キーフレーム配列を確保
for(int i=0;i<result.size();i++)
{
FaceData pFaceTemp=result.get(i);
pFaceTemp.pKeyFrames = FaceKeyFrame.createArray(pFaceTemp.ulNumKeyFrames);
pFaceTemp.ulNumKeyFrames = 0; // 配列インデックス用にいったん0にする
}
// 表情ごとにキーフレームを格納
for(int i = 0 ; i < ulNumFaceKeyFrames ; i++)
{
for(int i2=0;i2<result.size();i2++)
{
FaceData pFaceTemp = result.get(i2);
if(pFaceTemp.szFaceName.equals(tmp_vmd_face[i].szFaceName))
{
FaceKeyFrame pKeyFrame = pFaceTemp.pKeyFrames[pFaceTemp.ulNumKeyFrames];
pKeyFrame.fFrameNo = (float)tmp_vmd_face[i].ulFrameNo;
pKeyFrame.fRate = tmp_vmd_face[i].fFactor;
pFaceTemp.ulNumKeyFrames++;
break;
}
}
}
// キーフレーム配列を昇順にソート
for(int i=0;i<result.size();i++)
{
FaceData pFaceTemp = result.get(i);
Arrays.sort(pFaceTemp.pKeyFrames, new FaceCompare());
}
o_max_frame[0]=max_frame;
return result.toArray(new FaceData[result.size()]);
}
private static MotionData[] createMotionDataList(DataReader i_reader,float[] o_max_frame) throws MmdException
{
Vector<MotionData> result=new Vector<MotionData>();
// まずはモーションデータ中のボーンごとのキーフレーム数をカウント
final int ulNumBoneKeyFrames=i_reader.readInt();
//ボーンを指定数読み込み
VMD_Motion[] tmp_vmd_motion=new VMD_Motion[ulNumBoneKeyFrames];
for(int i=0;i<ulNumBoneKeyFrames;i++){
tmp_vmd_motion[i]= new VMD_Motion();
tmp_vmd_motion[i].read(i_reader);
}
float max_frame=0.0f;
for(int i = 0 ; i < ulNumBoneKeyFrames ; i++)
{
if( max_frame < tmp_vmd_motion[i].ulFrameNo){
max_frame = tmp_vmd_motion[i].ulFrameNo; // 最大フレーム更新
}
boolean is_found=false;
for(int i2=0;i2<result.size();i2++)
{
final MotionData pMotTemp = result.get(i2);
if(pMotTemp.szBoneName.equals(tmp_vmd_motion[i].szBoneName))
{
// リストに追加済みのボーン
pMotTemp.ulNumKeyFrames++;
is_found=true;
break;
}
}
if(!is_found)
{
// リストにない場合は新規ノードを追加
MotionData pNew = new MotionData();
pNew.szBoneName=tmp_vmd_motion[i].szBoneName;
pNew.ulNumKeyFrames = 1;
result.add(pNew);
}
}
// キーフレーム配列を確保
for(int i=0;i<result.size();i++)
{
final MotionData pMotTemp = result.get(i);
pMotTemp.pKeyFrames = BoneKeyFrame.createArray(pMotTemp.ulNumKeyFrames);
pMotTemp.ulNumKeyFrames = 0; // 配列インデックス用にいったん0にする
}
// ボーンごとにキーフレームを格納
for(int i = 0 ; i < ulNumBoneKeyFrames ; i++)
{
for(int i2=0;i2<result.size();i2++)
{
final MotionData pMotTemp = result.get(i2);
if(pMotTemp.szBoneName.equals(tmp_vmd_motion[i].szBoneName))
{
final BoneKeyFrame pKeyFrame = pMotTemp.pKeyFrames[pMotTemp.ulNumKeyFrames];
pKeyFrame.fFrameNo = (float)tmp_vmd_motion[i].ulFrameNo;
pKeyFrame.vec3Position.setValue(tmp_vmd_motion[i].vec3Position);
pKeyFrame.vec4Rotate.QuaternionNormalize(tmp_vmd_motion[i].vec4Rotate);
pMotTemp.ulNumKeyFrames++;
break;
}
}
}
// キーフレーム配列を昇順にソート
for(int i=0;i<result.size();i++)
{
final MotionData pMotTemp = result.get(i);
Arrays.sort(pMotTemp.pKeyFrames, new BoneCompare());
}
o_max_frame[0]=max_frame;
return result.toArray(new MotionData[result.size()]);
}
}
|