-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKF_UtilityFBX.cpp
More file actions
836 lines (740 loc) · 27 KB
/
KF_UtilityFBX.cpp
File metadata and controls
836 lines (740 loc) · 27 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
//--------------------------------------------------------------------------------
//
// gameObject3D.h
// Author : Xu Wenjie
// Date : 2017-04-27
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// インクルードファイル
//--------------------------------------------------------------------------------
#include "main.h"
#include "KF_UtilityFBX.h"
#include "KF_Utility.h"
//--------------------------------------------------------------------------------
//
// Public
//
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// Load
//--------------------------------------------------------------------------------
MyModel CKFUtilityFBX::Load(const string& strFilePath)
{
MyModel myModel;
//FBX読込実験コード
auto lSdkManager = FbxManager::Create();
// Create the IO settings object.
auto ios = FbxIOSettings::Create(lSdkManager, IOSROOT);
lSdkManager->SetIOSettings(ios);
// Create an importer using the SDK manager.
auto lImporter = FbxImporter::Create(lSdkManager, "");
// Use the first argument as the filename for the importer.
if (!lImporter->Initialize(strFilePath.c_str(), -1, lSdkManager->GetIOSettings()))
{
char buf[MAX_PATH];
wsprintf(buf, "Call to FbxImporter::Initialize() failed.\nError returned: %s\n\n", lImporter->GetStatus().GetErrorString());
MessageBox(NULL, buf, "error", MB_OK);
lImporter->Destroy();
lSdkManager->Destroy();
return myModel;
}
// Create a new scene so that it can be populated by the imported file.
auto lScene = FbxScene::Create(lSdkManager, "myScene");
// Import the contents of the file into the scene.
lImporter->Import(lScene);
// 三角分割
FbxGeometryConverter lConverter(lSdkManager);
lConverter.Triangulate(lScene, true);
// 縮退ポリゴンの削除
lConverter.RemoveBadPolygonsFromMeshes(lScene);
// マテリアルごとにメッシュ分離
lConverter.SplitMeshesPerMaterial(lScene, true);
// Node
myModel.pNode = recursiveNode(lSdkManager, lScene->GetRootNode(), nullptr);
// Material
analyzeMaterial(lScene, myModel.mapMaterial);
// Animator
myModel.pAnimator = analyzeAnimator(lImporter, lScene);
if (myModel.pAnimator)
{
analyzeAnimation(lImporter, lScene, myModel.pAnimator);
matchClusterWithSkeleton(myModel.pAnimator->Clusters, myModel.pNode);
}
lImporter->Destroy();
lScene->Destroy();
lSdkManager->Destroy();
return myModel;
}
//--------------------------------------------------------------------------------
// Load
//--------------------------------------------------------------------------------
MyModel CKFUtilityFBX::LoadFromTxt(const string& strFilePath)
{
MyModel myModel;
FILE* filePointer = nullptr;
fopen_s(&filePointer, strFilePath.c_str(), "r");
if (!filePointer) return myModel;
return myModel;
////パーツ数の読み込み
//string buffer;
//string compare = "NUM_MODEL = ";
//int partsNumber = 0;
//if (CKFUtility::GetStringUntilString(filePointer, compare, buffer) > 0)
//{
// char numberBuffer[16];
// for (int count = compare.length(), countNum = 0; buffer[count] != '\n'; ++count, ++countNum)
// {
// numberBuffer[countNum] = buffer[count];
// }
// partsNumber = atoi(numberBuffer);
//}
////Meshの読み込み
//vector<string> modelPathes;
//modelPathes.resize(partsNumber);
//for (int countPart = 0; countPart < partsNumber; countPart++)
//{
// compare = "MODEL_FILENAME = ";
// if (CKFUtility::GetStringUntilString(filePointer, compare, buffer) > 0)
// {
// for (int count = compare.length(), countPath = 0; buffer[count] != '\t'; ++count, countPath++)
// {
// modelPathes[countPart].push_back(buffer[count]);
// }
// }
//}
////parts infoの読み込み
//vector<CMyNode*> nodes;
//nodes.resize(partsNumber);
//for (int countPart = 0; countPart < partsNumber; ++countPart)
//{
// if (CKFUtility::GetStringUntilString(filePointer, "\tPARTSSET", buffer) > 0)
// {
// //Index
// fgets(&buffer[0], buffer.length(), filePointer);
// //Parent
// fgets(&buffer[0], buffer.length(), filePointer);
// char parentIDBuffer[4] = {};
// for (unsigned int count = strlen("\t\tPARENT = "), countParent = 0; buffer[count] != '\t'; ++count, ++countParent)
// {
// parentIDBuffer[countParent] = buffer[count];
// }
// int parentID = atoi(parentIDBuffer);
// //Pos
// fgets(&buffer[0], buffer.length(), filePointer);
// char positionBuffer[3][16] = {};
// int countAxis = 0;
// int countPos = 0;
// for (int count = strlen("\t\tPOS = "); buffer[count] != '\n'; ++count)
// {
// if (buffer[count] == ' ')
// {
// countAxis++;
// countPos = 0;
// continue;
// }
// positionBuffer[countAxis][countPos] = buffer[count];
// countPos++;
// }
// auto offsetPosition = CKFVec3((float)atof(positionBuffer[0]), (float)atof(positionBuffer[1]), (float)atof(positionBuffer[2]));
// //Rot
// fgets(&buffer[0], buffer.length(), filePointer);
// char rotationBuffer[3][16] = {};
// countAxis = 0;
// int countRot = 0;
// for (unsigned int count = strlen("\t\tROT = "); buffer[count] != '\n'; ++count)
// {
// if (buffer[count] == ' ')
// {
// countAxis++;
// countRot = 0;
// continue;
// }
// rotationBuffer[countAxis][countRot] = buffer[count];
// countRot++;
// }
// auto offsetRotation = CKFVec3((float)atof(rotationBuffer[0]), (float)atof(rotationBuffer[1]), (float)atof(rotationBuffer[2]));
// nodes[countPart] = new CMyNode;
// nodes[countPart]->Translation = offsetPosition;
// nodes[countPart]->Rotation = offsetRotation;
// nodes[countPart]->Scale = CKFVec3(1.0f);
// if (parentID >= 0)
// {
// nodes[parentID]->Children.push_back(nodes[countPart]);
// }
// // mesh
// string Name, strType;
// CKFUtility::AnalyzeFilePath(modelPathes[countPart], Name, strType);
// if (strType._Equal("x"))
// {
// }
// }
//}
//myModel.pNode = nodes[0];
////motionの読み込み
//int countMotion = 0;
//while (CKFUtility::GetStringUntilString(filePointer, "MOTIONSET", buffer) > 0)
//{
// MotionInfo motionInfo;
// //LOOP
// if (CKFUtility::GetStringUntilString(filePointer, "LOOP = ", buffer) > 0)
// {
// char loopBuffer[2] = {};
// for (int count = strlen("\tLOOP = "), countLoop = 0; buffer[count] != '\t'; ++count, ++countLoop)
// {
// loopBuffer[countLoop] = buffer[count];
// }
// motionInfo.IsLoop = atoi(loopBuffer) != 0 ? true : false;
// }
// //キーフレーム数取得
// int keyNumber = 0;
// if (CKFUtility::GetStringUntilString(filePointer, "NUM_KEY = ", buffer) > 0)
// {
// char keyBuffer[2] = {};
// for (int count = strlen("\tNUM_KEY = "), countKey = 0; buffer[count] != '\t'; ++count, countKey++)
// {
// keyBuffer[countKey] = buffer[count];
// }
// keyNumber = atoi(keyBuffer);
// }
// vector<MotionFrame> keyFrames;
// vector<int> keyFrameNumbers;
// keyFrames.resize(keyNumber);
// keyFrameNumbers.resize(keyNumber);
// //キーフレームの読み込み
// for (int countKey = 0; countKey < keyNumber; ++countKey)
// {
// if (CKFUtility::GetStringUntilString(filePointer, "KEYSET", buffer) > 0)
// {
// //Frame
// if (CKFUtility::GetStringUntilString(filePointer, "FRAME = ", buffer) > 0)
// {
// char frameBuffer[4] = {};
// for (unsigned int count = strlen("\t\tFRAME = "), countFrame = 0; buffer[count] != '\n'; ++count, ++countFrame)
// {
// frameBuffer[countFrame] = buffer[count];
// }
// keyFrameNumbers[countKey] = atoi(frameBuffer);
// }
// //Set Key per Parts
// for (int countPart = 0; countPart < partsNumber; ++countPart)
// {
// CKFVec3 position;
// CKFQuaternion rotation;
// //Pos
// if (CKFUtility::GetStringUntilString(filePointer, "POS = ", buffer) > 0)
// {
// char positionBuffer[3][16] = {};
// int countAxis = 0;
// int countPos = 0;
// for (unsigned int count = strlen("\t\t\tPOS = "); buffer[count] != '\n'; ++count)
// {
// if (buffer[count] == ' ')
// {
// countAxis++;
// countPos = 0;
// continue;
// }
// positionBuffer[countAxis][countPos] = buffer[count];
// countPos++;
// }
// position = CKFVec3((float)atof(positionBuffer[0]), (float)atof(positionBuffer[1]), (float)atof(positionBuffer[2]));
// }
// //Rot
// if (CKFUtility::GetStringUntilString(filePointer, "ROT = ", buffer) > 0)
// {
// char rotationBuffer[3][16] = {};
// int countAxis = 0;
// int countRot = 0;
// for (unsigned int count = strlen("\t\t\tROT = "); buffer[count] != '\n'; ++count)
// {
// if (buffer[count] == ' ')
// {
// countAxis++;
// countRot = 0;
// continue;
// }
// rotationBuffer[countAxis][countRot] = buffer[count];
// countRot++;
// }
// rotation = CKFVec3((float)atof(rotationBuffer[0]), (float)atof(rotationBuffer[1]), (float)atof(rotationBuffer[2])).ToCKFQuaternion();
// }
// keyFrames[countKey].BoneFrames.push_back(BoneFrame(position, rotation));
// }
// }
// }
// //フレームに転換
// int countFrame = 0;
// for (int countKey = 0; countKey < keyNumber; ++countKey)
// {
// motionInfo.MotionFrames.resize(motionInfo.MotionFrames.size() + keyFrameNumbers[countKey]);
// for (int countKeyFrame = 0; countKeyFrame < keyFrameNumbers[countKey]; ++countKeyFrame)
// {
// float rate = (float)countKeyFrame / keyFrameNumbers[countKey];
// auto currentIterator = keyFrames[countKey].BoneFrames.begin();
// if (!motionInfo.IsLoop && countKey == keyNumber - 1)
// {// 最後のキーは補間なし
// for (int countBone = 0; countBone < partsNumber; ++countBone)
// {
// motionInfo.MotionFrames[countFrame].BoneFrames.push_back(BoneFrame(currentIterator->Position, currentIterator->Rotation));
// ++currentIterator;
// }
// }
// else
// {// 補間
// auto nextIterator = keyFrames[(countKey + 1) % keyNumber].BoneFrames.begin();
// for (int countBone = 0; countBone < partsNumber; ++countBone)
// {
// auto& position = Math::Lerp(currentIterator->Position, nextIterator->Position, rate);
// auto& rotation = Math::Slerp(currentIterator->Rotation, nextIterator->Rotation, rate);
// motionInfo.MotionFrames[countFrame].BoneFrames.push_back(BoneFrame(position, rotation));
// ++currentIterator;
// ++nextIterator;
// }
// }
// ++countFrame;
// }
// }
//}
}
//--------------------------------------------------------------------------------
// Load
//--------------------------------------------------------------------------------
void CKFUtilityFBX::LoadAnimation(const string& strFilePath, CAnimator* animator)
{
//FBX読込実験コード
auto lSdkManager = FbxManager::Create();
// Create the IO settings object.
auto ios = FbxIOSettings::Create(lSdkManager, IOSROOT);
lSdkManager->SetIOSettings(ios);
// Create an importer using the SDK manager.
auto lImporter = FbxImporter::Create(lSdkManager, "");
// Use the first argument as the filename for the importer.
if (!lImporter->Initialize(strFilePath.c_str(), -1, lSdkManager->GetIOSettings()))
{
char buf[MAX_PATH];
wsprintf(buf, "Call to FbxImporter::Initialize() failed.\nError returned: %s\n\n", lImporter->GetStatus().GetErrorString());
MessageBox(NULL, buf, "error", MB_OK);
lImporter->Destroy();
lSdkManager->Destroy();
return;
}
// Create a new scene so that it can be populated by the imported file.
auto lScene = FbxScene::Create(lSdkManager, "myScene");
// Import the contents of the file into the scene.
lImporter->Import(lScene);
analyzeAnimation(lImporter, lScene, animator);
lImporter->Destroy();
lScene->Destroy();
lSdkManager->Destroy();
}
//--------------------------------------------------------------------------------
// Save
//--------------------------------------------------------------------------------
bool CKFUtilityFBX::Save(const MyModel& model, const string& fileName, const OutType& type)
{
if (type == Json)
{
//Model
auto& filePath = "data/model/" + fileName + ".json";
//上書き確認
ifstream checkFile(filePath);
if (checkFile.is_open())
{
checkFile.close();
auto id = MessageBox(NULL, "上書きしますか?", "確認", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2);
if (id == IDNO) return false;
}
//Save
ofstream file(filePath);
if (!file.is_open()) return false;
JSONOutputArchive archive(file);
model.pNode->RecursiveSave(archive, fileName, model.pAnimator ? true : false);
file.close();
//Animator
if (model.pAnimator)model.pAnimator->SaveAsJson(fileName);
//Material
Material::SaveAsJson(model.mapMaterial);
}
else if (type == Binary)
{
//Model
auto& filePath = "data/model/" + fileName + ".model";
//上書き確認
ifstream checkFile(filePath);
if (checkFile.is_open())
{
checkFile.close();
auto id = MessageBox(NULL, "上書きしますか?", "確認", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2);
if (id == IDNO) return false;
}
//Save
ofstream file(filePath, ios::binary);
if (!file.is_open()) return false;
BinaryOutputArchive archive(file);
model.pNode->RecursiveSave(archive, fileName, model.pAnimator ? true : false);
file.close();
//Animator
if (model.pAnimator)model.pAnimator->SaveAsBinary(fileName);
//Material
Material::SaveAsBinary(model.mapMaterial);
}
return true;
}
#ifdef USING_DIRECTX
//--------------------------------------------------------------------------------
// FindRepetition
// 重ねたら番号を返す、そうじゃないなら-1を返す
//--------------------------------------------------------------------------------
int CKFUtilityFBX::FindRepetition(const list<VertexDX>& listVtx, const VertexDX& vtx)
{
int nCnt = 0;
for (auto itr = listVtx.begin(); itr != listVtx.end(); ++itr, ++nCnt)
{
if (vtx == *itr) { return nCnt; }
}
return -1;
}
#endif
//--------------------------------------------------------------------------------
//
// Private
//
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// recursiveNode
//--------------------------------------------------------------------------------
CMyNode* CKFUtilityFBX::recursiveNode(FbxManager* pManager, FbxNode* pNode, CMyNode* parent)
{
if (!pNode) { return NULL; }
auto pMyNode = new CMyNode;
pMyNode->Parent = parent;
pMyNode->Name = pNode->GetName();
auto& local = CKFMtx44::FbxToMtx(pNode->EvaluateLocalTransform());
CKFMath::MtxToTransRotScale(local, pMyNode->Translation, pMyNode->Rotation, pMyNode->Scale);
for (int nCnt = 0; nCnt < pNode->GetNodeAttributeCount(); nCnt++)
{
auto type = pNode->GetNodeAttributeByIndex(nCnt)->GetAttributeType();
pMyNode->AttributeNames.push_back(getAttributeTypeName(type));
if (type == FbxNodeAttribute::eMesh)
{ //Mesh情報
pMyNode->Meshes.push_back(Mesh());
FbxMesh* pMesh = FbxCast<FbxMesh>(pNode->GetNodeAttributeByIndex(nCnt));
pMesh->GenerateTangentsDataForAllUVSets();
pMyNode->analyzePoint(pMesh);
pMyNode->analyzeNormal(pMesh);
pMyNode->analyzeUV(pMesh);
pMyNode->analyzeMaterial(pMesh);
pMyNode->analyzeCluster(pMesh);
}
}
for (int nCnt = 0; nCnt < pNode->GetChildCount(); ++nCnt)
{
auto child = recursiveNode(pManager, pNode->GetChild(nCnt), pMyNode);
pMyNode->Children.push_back(child);
}
return pMyNode;
}
//--------------------------------------------------------------------------------
// analyzeMaterial
//--------------------------------------------------------------------------------
void CKFUtilityFBX::analyzeMaterial(FbxScene* lScene, unordered_map<string, Material>& mapMaterial)
{
// シーンのマテリアルの数を取得する。
int materialCount = lScene->GetMaterialCount();
for (int count = 0; count < materialCount; ++count)
{
Material material;
string materialName;
// マテリアルを取得する。
auto pMaterial = lScene->GetMaterial(count);
// マテリアルの名前を取得する。
// マテリアルの名前は、メッシュノードとの関連付けに使用する。
materialName = pMaterial->GetName();
// クラスIDを取得し、マテリアルを、ランバートマテリアル、又はフォンマテリアルにキャストする。
auto& classId = pMaterial->GetClassId();
if (classId.Is(FbxSurfaceLambert::ClassId))
{
FbxSurfaceLambert* pLambert = (FbxSurfaceLambert*)pMaterial;
material.Ambient.m_fR = (float)pLambert->Ambient.Get()[0];
material.Ambient.m_fG = (float)pLambert->Ambient.Get()[1];
material.Ambient.m_fB = (float)pLambert->Ambient.Get()[2];
material.Diffuse.m_fR = (float)pLambert->Diffuse.Get()[0];
material.Diffuse.m_fG = (float)pLambert->Diffuse.Get()[1];
material.Diffuse.m_fB = (float)pLambert->Diffuse.Get()[2];
material.Emissive.m_fR = (float)pLambert->Emissive.Get()[0];
material.Emissive.m_fG = (float)pLambert->Emissive.Get()[1];
material.Emissive.m_fB = (float)pLambert->Emissive.Get()[2];
}
else if (classId.Is(FbxSurfacePhong::ClassId))
{
FbxSurfacePhong* pPhong = (FbxSurfacePhong*)pMaterial;
material.Ambient.m_fR = (float)pPhong->Ambient.Get()[0];
material.Ambient.m_fG = (float)pPhong->Ambient.Get()[1];
material.Ambient.m_fB = (float)pPhong->Ambient.Get()[2];
material.Diffuse.m_fR = (float)pPhong->Diffuse.Get()[0];
material.Diffuse.m_fG = (float)pPhong->Diffuse.Get()[1];
material.Diffuse.m_fB = (float)pPhong->Diffuse.Get()[2];
material.Emissive.m_fR = (float)pPhong->Emissive.Get()[0];
material.Emissive.m_fG = (float)pPhong->Emissive.Get()[1];
material.Emissive.m_fB = (float)pPhong->Emissive.Get()[2];
material.Specular.m_fR = (float)pPhong->Specular.Get()[0];
material.Specular.m_fG = (float)pPhong->Specular.Get()[1];
material.Specular.m_fB = (float)pPhong->Specular.Get()[2];
material.Power = (float)pPhong->Shininess.Get();
}
// マテリアルの、ディフューズのプロパティを取得する。
auto& property = pMaterial->FindProperty(FbxSurfaceMaterial::sDiffuse);
// テクスチャファイル数を取得する。
int fileTextureCount = property.GetSrcObjectCount<FbxFileTexture>();
if (fileTextureCount > 0)
{
for (int count = 0; count < fileTextureCount; ++count)
{
// テクスチャファイルを取得する。
auto pFileTexture = lScene->GetSrcObject<FbxFileTexture>(count);
auto fbxType = pFileTexture->GetTextureType();
auto fbxUse = pFileTexture->GetTextureUse();
// ファイルテクスチャ名を取得する。
string path = pFileTexture->GetFileName();
string name;
string type;
CKFUtility::AnalyzeFilePath(path, name, type);
CKFUtility::CorrectTexType(type);
name += '.' + type;
material.ColorTexture = name;
break;
}
}
else
{
// マテリアルのインプリメンテーションを取得する。
auto pImplementation = GetImplementation(pMaterial, FBXSDK_IMPLEMENTATION_CGFX);
if (!pImplementation) return;
// インプリメンテーションのルートテーブルを取得する。
auto pRootTable = pImplementation->GetRootTable();
if (!pRootTable) return;
// ルートテーブルのエントリー数を取得する
size_t entryCount = pRootTable->GetEntryCount();
for (size_t count = 0; count < entryCount; ++count)
{
// ルートテーブルのエントリーを取得する。
auto& entry = pRootTable->GetEntry(count);
// エントリー名を取得する。
string entryName = entry.GetSource();
// エントリー名から、プロパティを取得する。
property = pMaterial->RootProperty.FindHierarchical(entryName.c_str());
// ファイルテクスチャ数を取得する。
fileTextureCount = property.GetSrcObjectCount<FbxFileTexture>();
for (int countTexture = 0; countTexture < fileTextureCount; ++countTexture)
{
// ファイルテクスチャを取得する。
auto pFileTexture = property.GetSrcObject<FbxFileTexture>(countTexture);
auto fbxType = pFileTexture->GetTextureType();
auto fbxUse = pFileTexture->GetTextureUse();
// ファイルテクスチャ名を取得する。
string path = pFileTexture->GetFileName();
string name;
string type;
CKFUtility::AnalyzeFilePath(path, name, type);
CKFUtility::CorrectTexType(type);
name += '.' + type;
if (entryName == "Maya|DiffuseTexture")
{
material.ColorTexture = name;
}
//break;
}
}
}
mapMaterial.emplace(materialName, material);
}
}
//--------------------------------------------------------------------------------
// analyzeAnimator
//--------------------------------------------------------------------------------
CAnimator* CKFUtilityFBX::analyzeAnimator(FbxImporter* lImporter, FbxScene* lScene)
{
// Cluster
int clusterNumber = lScene->GetMemberCount<FbxCluster>();
map<string, FbxCluster*> clusters;
for (int countBoneFrame = 0; countBoneFrame < clusterNumber; ++countBoneFrame)
{
auto pCluster = lScene->GetSrcObject<FbxCluster>(countBoneFrame);
string name = pCluster->GetLink()->GetName();
if (clusters.find(name) == clusters.end())
{
clusters.emplace(name, pCluster);
}
}
if (clusters.empty())
{
MessageBox(NULL, "clusterが見つからない!!", "analyzeAnimation", MB_OK | MB_ICONWARNING);
return nullptr;
}
auto pAnimator = new CAnimator;
// Cluster Matrix
pAnimator->Clusters.resize(clusters.size());
pAnimator->ClusterNames.resize(clusters.size());
int countCluster = 0;
for (auto pair : clusters)
{
pAnimator->Clusters[countCluster].Name = pair.first;
pAnimator->ClusterNames[countCluster] = pair.first;
FbxAMatrix meshGlobalInitPosition;
pair.second->GetTransformMatrix(meshGlobalInitPosition);
FbxAMatrix boneGlobalInitPosition;
pair.second->GetTransformLinkMatrix(boneGlobalInitPosition);
pAnimator->Clusters[countCluster].BindPoseInverse = CKFMtx44::FbxToMtx(boneGlobalInitPosition.Inverse() * meshGlobalInitPosition);
++countCluster;
}
clusters.clear();
return pAnimator;
}
//--------------------------------------------------------------------------------
// analyzeAnimation
//--------------------------------------------------------------------------------
void CKFUtilityFBX::analyzeAnimation(FbxImporter* lImporter, FbxScene* lScene, CAnimator* animator)
{
// Anim数
auto animationNumber = lImporter->GetAnimStackCount();
if (!animationNumber) return;
// Skeleton
list<FbxNode*> skeletons;
findSkeletons(lScene->GetRootNode(), skeletons);
if (skeletons.size() < animator->Clusters.size())
{
MessageBox(NULL, "skeletonの数が合わない!!", "analyzeAnimation", MB_OK | MB_ICONWARNING);
return;
}
animator->Motions.reserve(animator->Motions.size() + animationNumber);
FbxArray<FbxString*> animationNames;
lScene->FillAnimStackNameArray(animationNames);
//one frame time
FbxTime oneFrameTime;
oneFrameTime.SetTime(0, 0, 0, 1, 0, 0, FbxTime::eFrames60);
for (int nCnt = 0; nCnt < animationNumber; ++nCnt)
{
Motion motion;
motion.Name = animationNames[nCnt]->Buffer();
//Anime情報
auto pTakeInfo = lScene->GetTakeInfo(animationNames[nCnt]->Buffer());
//アニメーション開始終了時間
auto startTime = pTakeInfo->mLocalTimeSpan.GetStart();
auto endTime = pTakeInfo->mLocalTimeSpan.GetStop();
int frameNumber = (int)((endTime - startTime) / (oneFrameTime)).Get();
motion.Frames.reserve(frameNumber);
for (auto currentTime = startTime; currentTime < endTime; currentTime += oneFrameTime)
{
Frame frame;
frame.BoneFrames.resize(animator->Clusters.size());
for (auto skeleton : skeletons)
{
string name = skeleton->GetName();
int clusterNo = -1;
for (int countCluster = 0; countCluster < (int)animator->Clusters.size(); ++countCluster)
{
if (name == animator->Clusters[countCluster].Name)
{
clusterNo = countCluster;
break;
}
}
if (clusterNo == -1)
{
continue;
}
frame.BoneFrames[clusterNo].Name = name;
auto& matrix = CKFMtx44::FbxToMtx(skeleton->EvaluateLocalTransform(currentTime));
CKFMath::MtxToTransRotScale(matrix,
frame.BoneFrames[clusterNo].Translation,
frame.BoneFrames[clusterNo].Rotation,
frame.BoneFrames[clusterNo].Scale);
}
motion.Frames.push_back(frame);
}
motion.Frames.shrink_to_fit();
motion.ChangeWhenOverExitFrame =
motion.EndFrame = motion.Frames.size() - 1;
animator->Motions.push_back(motion);
}
animator->Motions.shrink_to_fit();
}
//--------------------------------------------------------------------------------
// findSkeletons
//--------------------------------------------------------------------------------
void CKFUtilityFBX::findSkeletons(FbxNode* pNode, list<FbxNode*>& listSkeleton)
{
if (!pNode) return;
for (int nCnt = 0; nCnt < pNode->GetNodeAttributeCount(); nCnt++)
{
auto type = pNode->GetNodeAttributeByIndex(nCnt)->GetAttributeType();
if (type == FbxNodeAttribute::eSkeleton)
{
listSkeleton.push_back(pNode);
}
}
for (int nCnt = 0; nCnt < pNode->GetChildCount(); ++nCnt)
{
findSkeletons(pNode->GetChild(nCnt), listSkeleton);
}
}
//--------------------------------------------------------------------------------
// matchClusterWithSkeleton
//--------------------------------------------------------------------------------
void CKFUtilityFBX::matchClusterWithSkeleton(vector<Cluster>& clusters, CMyNode* node)
{
if (!node) return;
for (auto& cluster : clusters)
{
if (!cluster.Node && cluster.Name == node->Name)
{
cluster.Node = node;
break;
}
}
for (auto child : node->Children)
{
matchClusterWithSkeleton(clusters, child);
}
}
//--------------------------------------------------------------------------------
// getGeometry
// Get the geometry offset to a node. It is never inherited by the children
//--------------------------------------------------------------------------------
FbxAMatrix CKFUtilityFBX::getGeometry(FbxNode* pNode)
{
const FbxVector4 lT = pNode->GetGeometricTranslation(FbxNode::eSourcePivot);
const FbxVector4 lR = pNode->GetGeometricRotation(FbxNode::eSourcePivot);
const FbxVector4 lS = pNode->GetGeometricScaling(FbxNode::eSourcePivot);
return FbxAMatrix(lT, lR, lS);
}
//--------------------------------------------------------------------------------
// GetAttributeTypeName
//--------------------------------------------------------------------------------
string CKFUtilityFBX::getAttributeTypeName(FbxNodeAttribute::EType type)
{
switch (type)
{
case FbxNodeAttribute::eUnknown: return "unidentified";
case FbxNodeAttribute::eNull: return "null";
case FbxNodeAttribute::eMarker: return "marker";
case FbxNodeAttribute::eSkeleton: return "skeleton";
case FbxNodeAttribute::eMesh: return "mesh";
case FbxNodeAttribute::eNurbs: return "nurbs";
case FbxNodeAttribute::ePatch: return "patch";
case FbxNodeAttribute::eCamera: return "camera";
case FbxNodeAttribute::eCameraStereo: return "stereo";
case FbxNodeAttribute::eCameraSwitcher: return "camera switcher";
case FbxNodeAttribute::eLight: return "light";
case FbxNodeAttribute::eOpticalReference: return "optical reference";
case FbxNodeAttribute::eOpticalMarker: return "marker";
case FbxNodeAttribute::eNurbsCurve: return "nurbs curve";
case FbxNodeAttribute::eTrimNurbsSurface: return "trim nurbs surface";
case FbxNodeAttribute::eBoundary: return "boundary";
case FbxNodeAttribute::eNurbsSurface: return "nurbs surface";
case FbxNodeAttribute::eShape: return "shape";
case FbxNodeAttribute::eLODGroup: return "lodgroup";
case FbxNodeAttribute::eSubDiv: return "subdiv";
default: return "unknown";
}
}