-
Notifications
You must be signed in to change notification settings - Fork 6
/
BVHConverter.cs
314 lines (297 loc) · 12.1 KB
/
BVHConverter.cs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media.Media3D;
namespace WpfApplication1
{
public class BVHConverter
{
private readonly BVH m_src;
public BVHConverter(BVH src)
{
m_src = src;
}
public BVH Convert()
{
BVH dest = SLTemplate();
dest.Frames.Value = m_src.Frames.Value + 1;
dest.FrameTime.Value = m_src.FrameTime.Value;
dest.FrameList.Clear();
FrameElement firstFrame = new FrameElement(dest);
firstFrame.GetJointFrame("hip").SetValue("Yposition", 43.5285);
dest.FrameList.Add(firstFrame);
foreach (FrameElement frame in m_src.FrameList)
{
dest.FrameList.Add(ConvertFrame(dest, frame));
}
return dest;
}
private FrameElement ConvertFrame(BVH dest, FrameElement srcFrame)
{
FrameElement destFrame = new FrameElement(dest);
foreach (CompositeElement joint in dest.JointList)
{
JointFrame jfDest = destFrame.GetJointFrame(joint.Name);
JointFrame jfSrc = srcFrame.GetJointFrame(GetSrcJointName(joint.Name));
if (jfSrc == null)
continue;
foreach (string channel in new string[]{"Zrotation", "Xrotation", "Yrotation"})
{
jfDest.SetValue(channel, jfSrc.GetValue(channel));
}
ConvertJointFrame(jfDest, joint.Name, srcFrame);
}
return destFrame;
}
private static void ConvertJointFrame(JointFrame jfDest, string jointName, FrameElement srcFrame)
{
Matrix3D matrix;
switch (jointName)
{
case "hip":
matrix = jfDest.Matrix;
JointFrame jfRoot = srcFrame.GetJointFrame("root");
Matrix3D rootMatrix = jfRoot.Matrix;
rootMatrix.OffsetX *= 4;
rootMatrix.OffsetY *= 4;
rootMatrix.OffsetZ *= 4;
rootMatrix.OffsetY += 12;
matrix.Append(rootMatrix);
jfDest.Matrix = matrix;
break;
case "abdomen":
matrix = jfDest.Matrix;
JointFrame jfLower = srcFrame.GetJointFrame("lower_body");
Matrix3D lowerMatrix = jfLower.Matrix;
lowerMatrix.Invert();
matrix.Append(lowerMatrix);
jfDest.Matrix = matrix;
break;
// cast "chest" はなにもしない。MMDBVHで指定されている数値をそのままSLBVHとして利用
case "lShldr":
matrix = jfDest.Matrix;
matrix.RotatePrepend(new Quaternion(new Vector3D(0, 0, 1), -35));
jfDest.Matrix = matrix;
break;
case "lForeArm":
case "lHand":
matrix = jfDest.Matrix;
matrix.RotatePrepend(new Quaternion(new Vector3D(0, 0, 1), -35));
matrix.Rotate(new Quaternion(new Vector3D(0, 0, 1), 35));
jfDest.Matrix = matrix;
break;
case "rShldr":
// jfDest.AddValue("Zrotation", 35);
matrix = jfDest.Matrix;
matrix.RotatePrepend(new Quaternion(new Vector3D(0, 0, 1), 35));
jfDest.Matrix = matrix;
break;
case "rForeArm":
case "rHand":
matrix = jfDest.Matrix;
matrix.RotatePrepend(new Quaternion(new Vector3D(0, 0, 1), 35));
matrix.Rotate(new Quaternion(new Vector3D(0, 0, 1), -35));
jfDest.Matrix = matrix;
break;
}
}
private string GetSrcJointName(string destName)
{
switch (destName)
{
case "hip":
return "lower_body";
case "abdomen":
return "upper_body";
//added------------------
//mio側の上半身2-upper_body2のカスタマイズを不要にするためBVH.csで名前変換
//mioカスタマイズ済みで upper_body2 があった場合でも対応
case "chest":
return "upper_body2";
//-----------------------
case "lCollar":
return "shoulder.L";
case "lShldr":
return "arm.L";
case "lForeArm":
return "elbow.L";
case "lHand":
return "wrist.L";
case "lThigh":
return "leg.L";
case "lShin":
return "knee.L";
case "lFoot":
return "ankle.L";
case "rCollar":
return "shoulder.R";
case "rShldr":
return "arm.R";
case "rForeArm":
return "elbow.R";
case "rHand":
return "wrist.R";
case "rThigh":
return "leg.R";
case "rShin":
return "knee.R";
case "rFoot":
return "ankle.R";
}
return destName;
}
private static BVH SLTemplate()
{
BVH result = new BVH();
string template = @"
HIERARCHY
ROOT hip
{
OFFSET 0.000000 0.000000 0.000000
CHANNELS 6 Xposition Yposition Zposition Xrotation Zrotation Yrotation
JOINT abdomen
{
OFFSET 0.000000 3.422050 0.000000
CHANNELS 3 Xrotation Zrotation Yrotation
JOINT chest
{
OFFSET 0.000000 8.486693 -0.684411
CHANNELS 3 Xrotation Zrotation Yrotation
JOINT neck
{
OFFSET 0.000000 10.266162 -0.273764
CHANNELS 3 Xrotation Zrotation Yrotation
JOINT head
{
OFFSET 0.000000 3.148285 0.000000
CHANNELS 3 Xrotation Zrotation Yrotation
End Site
{
OFFSET 0.000000 3.148289 0.000000
}
}
}
JOINT lCollar
{
OFFSET 3.422053 6.707223 -0.821293
CHANNELS 3 Yrotation Zrotation Xrotation
JOINT lShldr
{
OFFSET 3.285171 0.000000 0.000000
CHANNELS 3 Zrotation Yrotation Xrotation
JOINT lForeArm
{
OFFSET 10.129278 0.000000 0.000000
CHANNELS 3 Yrotation Zrotation Xrotation
JOINT lHand
{
OFFSET 8.486692 0.000000 0.000000
CHANNELS 3 Zrotation Yrotation Xrotation
End Site
{
OFFSET 4.106464 0.000000 0.000000
}
}
}
}
}
JOINT rCollar
{
OFFSET -3.558935 6.707223 -0.821293
CHANNELS 3 Yrotation Zrotation Xrotation
JOINT rShldr
{
OFFSET -3.148289 0.000000 0.000000
CHANNELS 3 Zrotation Yrotation Xrotation
JOINT rForeArm
{
OFFSET -10.266159 0.000000 0.000000
CHANNELS 3 Yrotation Zrotation Xrotation
JOINT rHand
{
OFFSET -8.349810 0.000000 0.000000
CHANNELS 3 Zrotation Yrotation Xrotation
End Site
{
OFFSET -4.106464 0.000000 0.000000
}
}
}
}
}
}
}
JOINT lThigh
{
OFFSET 5.338403 -1.642589 1.368821
CHANNELS 3 Xrotation Zrotation Yrotation
JOINT lShin
{
OFFSET -2.053232 -20.121670 0.000000
CHANNELS 3 Xrotation Zrotation Yrotation
JOINT lFoot
{
OFFSET 0.000000 -19.300380 -1.231939
CHANNELS 3 Xrotation Yrotation Zrotation
End Site
{
OFFSET 0.000000 -2.463878 4.653993
}
}
}
}
JOINT rThigh
{
OFFSET -5.338403 -1.642589 1.368821
CHANNELS 3 Xrotation Zrotation Yrotation
JOINT rShin
{
OFFSET 2.053232 -20.121670 0.000000
CHANNELS 3 Xrotation Zrotation Yrotation
JOINT rFoot
{
OFFSET 0.000000 -19.300380 -1.231939
CHANNELS 3 Xrotation Yrotation Zrotation
End Site
{
OFFSET 0.000000 -2.463878 4.653993
}
}
}
}
}
MOTION
Frames: 1
Frame Time: 0.033333
0.000000 43.528519 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
";
byte[] bytes = Encoding.Default.GetBytes(template);
using (MemoryStream stream = new MemoryStream(bytes))
{
result.Load(stream);
}
foreach (CompositeElement joint in result.JointList)
{
switch (joint.Channels.ChannelList.Length)
{
case 3:
joint.Channels.ChannelList[0] = "Zrotation";
joint.Channels.ChannelList[1] = "Xrotation";
joint.Channels.ChannelList[2] = "Yrotation";
break;
case 6:
joint.Channels.ChannelList[0] = "Xposition";
joint.Channels.ChannelList[1] = "Yposition";
joint.Channels.ChannelList[2] = "Zposition";
joint.Channels.ChannelList[3] = "Zrotation";
joint.Channels.ChannelList[4] = "Xrotation";
joint.Channels.ChannelList[5] = "Yrotation";
break;
}
}
return result;
}
}
}