-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGltfModelFactory.cpp
More file actions
642 lines (537 loc) · 25.3 KB
/
GltfModelFactory.cpp
File metadata and controls
642 lines (537 loc) · 25.3 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
#include"GltfModelFactory.h"
size_t GltfModelFactory::Load(const std::string& filePath)
{
size_t hash = hashFilePath(filePath);
if(modelStorage.find(hash) != modelStorage.end())
{
return hash;
}
tinygltf::Model gltfModel;
tinygltf::TinyGLTF gltfContext;
std::string error;
std::string warning;
bool binary = false;
binary = gltfContext.LoadBinaryFromFile(&gltfModel, &error, &warning, filePath);
if (!binary)
{
throw std::runtime_error("faile load file");
}
const tinygltf::Scene& scene = gltfModel.scenes[gltfModel.defaultScene > -1 ? gltfModel.defaultScene : 0];
minPos = glm::vec3(FLT_MAX, FLT_MAX, FLT_MAX);
maxPos = glm::vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
vertexNum = 0;
indexNum = 0;
std::shared_ptr<GltfModel> model =
std::make_shared<GltfModel>(bufferFactory, layoutFactory, descriptorSetFactory);
//GltfNodeを確保する
model->nodes.resize(gltfModel.nodes.size());
//gltfモデルのシーンは一つのみを前提とする
loadNode(model, scene.nodes[0], gltfModel);
//AABB用の座標を設定する
model->initPoseMax = maxPos;
model->initPoseMin = minPos;
loadMaterial(model, gltfModel);//マテリアルデータの読み取り
if (gltfModel.animations.size() > 0)
{
loadAnimations(model, gltfModel);//アニメーションの取得
}
loadSkin(model, gltfModel);//スキンメッシュアニメーション用のスキンを読み取り
setSkin(model);//スキンの設定
//gpu上の頂点バッファなどを作成
model->setPointBufferNum();
modelStorage[hash] = model;//ストレージにモデルを加える
return hash;
}
//gltfモデルのノードを再帰的に読み込む
void GltfModelFactory::loadNode(std::shared_ptr<GltfModel> model, const int nodeIndex, const tinygltf::Model& gltfModel)
{
const tinygltf::Node& gltfNode = gltfModel.nodes[nodeIndex];
model->nodes[0].offset = 0;
//parentIndexは親ノードの配列上の番号
model->nodes[0].parentIndex = -1;
//indexはgltf上のノードの番号
model->nodes[0].index = nodeIndex;
model->nodes[0].name = gltfNode.name;
model->nodes[0].skinIndex = gltfNode.skin;
model->nodes[0].matrix = glm::mat4(1.0f);
if (gltfNode.matrix.size() == 16) {
model->nodes[0].matrix = glm::make_mat4x4(gltfNode.matrix.data());
}
if (gltfNode.mesh > -1)
{
//メッシュの読み取り
loadMesh(gltfNode, gltfModel, model->nodes[0].mesh, model, gltfNode.mesh);
}
if (gltfNode.children.size() > 0)
{
size_t offset = 1;
//現在のオフセットを親ノードのインデックスとして設定
int parentIndex = 0;
for (size_t i = 0; i < gltfNode.children.size(); i++)
{
loadNode(offset, parentIndex, model, gltfNode.children[i], gltfModel);
}
}
}
//gltfモデルのノードを再帰的に読み込む
void GltfModelFactory::loadNode(size_t& offset, int parentIndex, std::shared_ptr<GltfModel> model, const int nodeIndex
, const tinygltf::Model& gltfModel)
{
const tinygltf::Node& gltfNode = gltfModel.nodes[nodeIndex];
model->nodes[offset].offset = offset;
model->nodes[offset].parentIndex = parentIndex;
model->nodes[offset].index = nodeIndex;
model->nodes[offset].name = gltfNode.name;
model->nodes[offset].skinIndex = gltfNode.skin;
model->nodes[offset].matrix = glm::mat4(1.0f);
if (gltfNode.matrix.size() == 16) {
model->nodes[offset].matrix = glm::make_mat4x4(gltfNode.matrix.data());
}
if (gltfNode.mesh > -1)
{
//メッシュの読み取り
loadMesh(gltfNode, gltfModel, model->nodes[offset].mesh, model, gltfNode.mesh);
}
//ノードを配置する位置を一つ後ろにする
offset++;
if (gltfNode.children.size() > 0)
{
//親ノードのインデックスを設定
parentIndex = static_cast<int>(offset) - 1;
for (size_t i = 0; i < gltfNode.children.size(); i++)
{
loadNode(offset, parentIndex, model, gltfNode.children[i], gltfModel);
}
}
}
//gltfモデルのメッシュを読み込む
void GltfModelFactory::loadMesh(const tinygltf::Node& gltfNode, const tinygltf::Model& gltfModel
, Mesh& mesh, std::shared_ptr<GltfModel> model, int meshIndex)
{
const tinygltf::Mesh gltfMesh = gltfModel.meshes[meshIndex];
int indexStart = 0;
int vertexNum = 0;;
mesh.meshIndex = model->meshCount;
model->meshCount++;
for (size_t i = 0; i < gltfMesh.primitives.size(); i++)
{
const tinygltf::Primitive glPrimitive = gltfMesh.primitives[i];
loadPrimitive(mesh, indexStart, glPrimitive, gltfModel, model);//プリミティブの読み取り
}
model->primitiveCount += static_cast<uint32_t>(gltfMesh.primitives.size());
}
//プリミティブの読み取り
void GltfModelFactory::loadPrimitive(Mesh& mesh, int& indexStart
, tinygltf::Primitive glPrimitive, tinygltf::Model glModel, std::shared_ptr<GltfModel> model)
{
const float* bufferPos = nullptr;
int vertexCount;
const float* bufferNormals = nullptr;
const float* bufferTexCoordSet0 = nullptr;
const float* bufferTexCoordSet1 = nullptr;
const float* bufferColorSet0 = nullptr;
const void* bufferJoints = nullptr;
const float* bufferWeights = nullptr;
glm::vec3 posMin{};
glm::vec3 posMax{};
int posByteStride;
int normByteStride;
int uv0ByteStride;
int uv1ByteStride;
int color0ByteStride;
int jointByteStride;
int weightByteStride;
int jointComponentType;
const tinygltf::Accessor& posAccessor = glModel.accessors[glPrimitive.attributes.find("POSITION")->second];//プリミティブから位置を表すバッファにアクセスするための変数
const tinygltf::BufferView& posView = glModel.bufferViews[posAccessor.bufferView];//アクセッサーを介してバッファーから値を読み取る
bufferPos = reinterpret_cast<const float*>(&(glModel.buffers[posView.buffer].data[posAccessor.byteOffset + posView.byteOffset]));//それぞれのオフセットを足して参照する位置を調整する
posMin = glm::vec3(posAccessor.minValues[0], posAccessor.minValues[1], posAccessor.minValues[2]);
posMax = glm::vec3(posAccessor.maxValues[0], posAccessor.maxValues[1], posAccessor.maxValues[2]);
vertexCount = static_cast<uint32_t>(posAccessor.count);//このプリミティブの持つ頂点の数
posByteStride = posAccessor.ByteStride(posView) ? (posAccessor.ByteStride(posView) / sizeof(float)) : tinygltf::GetNumComponentsInType(TINYGLTF_TYPE_VEC3);//頂点を読み取る際の頂点のデータの幅を取得
//AABBのために頂点の最小値と最大値を求める
for (int i = 0; i < 3; i++)
{
minPos[i] = std::min(minPos[i], posMin[i]);
maxPos[i] = std::max(maxPos[i], posMax[i]);
}
//以下頂点座標と同じように、取得していく
if (glPrimitive.attributes.find("NORMAL") != glPrimitive.attributes.end()) {
const tinygltf::Accessor& normAccessor = glModel.accessors[glPrimitive.attributes.find("NORMAL")->second];
const tinygltf::BufferView& normView = glModel.bufferViews[normAccessor.bufferView];
bufferNormals = reinterpret_cast<const float*>(&(glModel.buffers[normView.buffer].data[normAccessor.byteOffset + normView.byteOffset]));
normByteStride = normAccessor.ByteStride(normView) ? (normAccessor.ByteStride(normView) / sizeof(float)) : tinygltf::GetNumComponentsInType(TINYGLTF_TYPE_VEC3);
}
//uvの読み取り
if (glPrimitive.attributes.find("TEXCOORD_0") != glPrimitive.attributes.end()) {
const tinygltf::Accessor& uvAccessor = glModel.accessors[glPrimitive.attributes.find("TEXCOORD_0")->second];
const tinygltf::BufferView& uvView = glModel.bufferViews[uvAccessor.bufferView];
bufferTexCoordSet0 = reinterpret_cast<const float*>(&(glModel.buffers[uvView.buffer].data[uvAccessor.byteOffset + uvView.byteOffset]));
uv0ByteStride = uvAccessor.ByteStride(uvView) ? (uvAccessor.ByteStride(uvView) / sizeof(float)) : tinygltf::GetNumComponentsInType(TINYGLTF_TYPE_VEC2);
}
if (glPrimitive.attributes.find("TEXCOORD_1") != glPrimitive.attributes.end()) {
const tinygltf::Accessor& uvAccessor = glModel.accessors[glPrimitive.attributes.find("TEXCOORD_1")->second];
const tinygltf::BufferView& uvView = glModel.bufferViews[uvAccessor.bufferView];
bufferTexCoordSet1 = reinterpret_cast<const float*>(&(glModel.buffers[uvView.buffer].data[uvAccessor.byteOffset + uvView.byteOffset]));
uv1ByteStride = uvAccessor.ByteStride(uvView) ? (uvAccessor.ByteStride(uvView) / sizeof(float)) : tinygltf::GetNumComponentsInType(TINYGLTF_TYPE_VEC2);
}
//頂点カラーの読み取り
if (glPrimitive.attributes.find("COLOR_0") != glPrimitive.attributes.end()) {
const tinygltf::Accessor& accessor = glModel.accessors[glPrimitive.attributes.find("COLOR_0")->second];
const tinygltf::BufferView& view = glModel.bufferViews[accessor.bufferView];
bufferColorSet0 = reinterpret_cast<const float*>(&(glModel.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
color0ByteStride = accessor.ByteStride(view) ? (accessor.ByteStride(view) / sizeof(float)) : tinygltf::GetNumComponentsInType(TINYGLTF_TYPE_VEC3);
}
//スケルトンの番号の読み取り
if (glPrimitive.attributes.find("JOINTS_0") != glPrimitive.attributes.end()) {
const tinygltf::Accessor& jointAccessor = glModel.accessors[glPrimitive.attributes.find("JOINTS_0")->second];
const tinygltf::BufferView& jointView = glModel.bufferViews[jointAccessor.bufferView];
bufferJoints = &(glModel.buffers[jointView.buffer].data[jointAccessor.byteOffset + jointView.byteOffset]);
jointComponentType = jointAccessor.componentType;
jointByteStride = jointAccessor.ByteStride(jointView) ? (jointAccessor.ByteStride(jointView) / tinygltf::GetComponentSizeInBytes(jointComponentType)) : tinygltf::GetNumComponentsInType(TINYGLTF_TYPE_VEC4);
}
//スケルトンのウェイトの読み取り
if (glPrimitive.attributes.find("WEIGHTS_0") != glPrimitive.attributes.end()) {
const tinygltf::Accessor& weightAccessor = glModel.accessors[glPrimitive.attributes.find("WEIGHTS_0")->second];
const tinygltf::BufferView& weightView = glModel.bufferViews[weightAccessor.bufferView];
bufferWeights = reinterpret_cast<const float*>(&(glModel.buffers[weightView.buffer].data[weightAccessor.byteOffset + weightView.byteOffset]));
weightByteStride = weightAccessor.ByteStride(weightView) ? (weightAccessor.ByteStride(weightView) / sizeof(float)) : tinygltf::GetNumComponentsInType(TINYGLTF_TYPE_VEC4);
}
bool hasSkin = false;
if (bufferJoints && bufferWeights)
{
hasSkin = true;
}
//頂点に各要素を設定していく
for (size_t v = 0; v < posAccessor.count; v++) {
Vertex vert;
vert.pos = glm::vec3(glm::make_vec3(&bufferPos[v * posByteStride]));
vert.normal = glm::normalize(glm::vec3(bufferNormals ? glm::make_vec3(&bufferNormals[v * normByteStride]) : glm::vec3(0.0f)));
vert.texCoord0 = bufferTexCoordSet0 ? glm::make_vec2(&bufferTexCoordSet0[v * uv0ByteStride]) : glm::vec2(0.0f);
vert.texCoord1 = bufferTexCoordSet1 ? glm::make_vec2(&bufferTexCoordSet1[v * uv1ByteStride]) : glm::vec2(0.0f);
vert.color = bufferColorSet0 ? glm::make_vec4(&bufferColorSet0[v * color0ByteStride]) : glm::vec4(1.0f);
if (hasSkin)
{
switch (jointComponentType) {
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT: {
const uint16_t* buf = static_cast<const uint16_t*>(bufferJoints);
vert.boneID1 = glm::ivec4(glm::make_vec4(&buf[v * jointByteStride]));
break;
}
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE: {
const uint8_t* buf = static_cast<const uint8_t*>(bufferJoints);
vert.boneID1 = glm::ivec4(glm::make_vec4(&buf[v * jointByteStride]));
break;
}
}
}
else {
vert.boneID1 = glm::ivec4(0);
}
vert.weight1 = hasSkin ? glm::make_vec4(&bufferWeights[v * weightByteStride]) : glm::vec4(0.0f);
if (glm::length(vert.weight1) == 0.0f) {
vert.weight1 = glm::vec4(1.0f, 0.0f, 0.0f, 0.0f);
}
mesh.vertices.push_back(vert);
}
int indexCount;
if (glPrimitive.indices > -1)
{
const tinygltf::Accessor& accessor = glModel.accessors[glPrimitive.indices > -1 ? glPrimitive.indices : 0];
const tinygltf::BufferView& bufferView = glModel.bufferViews[accessor.bufferView];
const tinygltf::Buffer& buffer = glModel.buffers[bufferView.buffer];
indexCount = static_cast<int>(accessor.count);
const void* dataPtr = &(buffer.data[accessor.byteOffset + bufferView.byteOffset]);
switch (accessor.componentType) {
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_INT: {
const uint32_t* buf = static_cast<const uint32_t*>(dataPtr);
for (size_t index = 0; index < accessor.count; index++) {
mesh.indices.push_back(buf[index] + indexStart);
}
break;
}
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_SHORT: {
const uint16_t* buf = static_cast<const uint16_t*>(dataPtr);
for (size_t index = 0; index < accessor.count; index++) {
mesh.indices.push_back(buf[index] + indexStart);
}
break;
}
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_BYTE: {
const uint8_t* buf = static_cast<const uint8_t*>(dataPtr);
for (size_t index = 0; index < accessor.count; index++) {
mesh.indices.push_back(buf[index] + indexStart);
}
break;
}
}
}
Primitive primitive = { model->primitiveCount,indexStart,indexCount,vertexCount,glPrimitive.material };
primitive.setBoundingBox(posMin, posMax);
mesh.primitives.push_back(primitive);
indexStart += indexCount;
vertexNum += vertexCount;
indexNum += indexStart;
}
//アニメーションを読み込む
void GltfModelFactory::loadAnimations(std::shared_ptr<GltfModel> model, const tinygltf::Model& gltfModel)
{
for (tinygltf::Animation anim : gltfModel.animations)
{
Animation animation{};
animation.name = anim.name;
if (anim.name.empty()) {
animation.name = std::to_string(model->animations.size());
}
//アニメーションのサンプリングの設定
for (auto& samp : anim.samplers) {
AnimationSampler sampler{};
if (samp.interpolation == "LINEAR") {
sampler.interpolation = AnimationSampler::InterpolationType::LINEAR;
}
if (samp.interpolation == "STEP") {
sampler.interpolation = AnimationSampler::InterpolationType::STEP;
}
if (samp.interpolation == "CUBICSPLINE") {
sampler.interpolation = AnimationSampler::InterpolationType::CUBICSPLINE;
}
//アニメーションのキーの時間の取得
{
const tinygltf::Accessor& accessor = gltfModel.accessors[samp.input];
const tinygltf::BufferView& bufferView = gltfModel.bufferViews[accessor.bufferView];
const tinygltf::Buffer& buffer = gltfModel.buffers[bufferView.buffer];
assert(accessor.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT);
const void* dataPtr = &buffer.data[accessor.byteOffset + bufferView.byteOffset];
const float* buf = static_cast<const float*>(dataPtr);
for (size_t index = 0; index < accessor.count; index++) {
sampler.inputs.push_back(buf[index]);
}
for (auto input : sampler.inputs) {
if (input < animation.start) {
animation.start = input;
};
if (input > animation.end) {
animation.end = input;
}
}
}
//アニメーションのキーの移動/回転/拡大の設定
{
const tinygltf::Accessor& accessor = gltfModel.accessors[samp.output];
const tinygltf::BufferView& bufferView = gltfModel.bufferViews[accessor.bufferView];
const tinygltf::Buffer& buffer = gltfModel.buffers[bufferView.buffer];
assert(accessor.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT);
const void* dataPtr = &buffer.data[accessor.byteOffset + bufferView.byteOffset];
switch (accessor.type) {
case TINYGLTF_TYPE_VEC3: {
const glm::vec3* buf = static_cast<const glm::vec3*>(dataPtr);
for (size_t index = 0; index < accessor.count; index++) {
sampler.outputsVec4.push_back(glm::vec4(buf[index], 0.0f));
sampler.outputs.push_back(buf[index][0]);
sampler.outputs.push_back(buf[index][1]);
sampler.outputs.push_back(buf[index][2]);
}
break;
}
case TINYGLTF_TYPE_VEC4: {
const glm::vec4* buf = static_cast<const glm::vec4*>(dataPtr);
for (size_t index = 0; index < accessor.count; index++) {
sampler.outputsVec4.push_back(buf[index]);
sampler.outputs.push_back(buf[index][0]);
sampler.outputs.push_back(buf[index][1]);
sampler.outputs.push_back(buf[index][2]);
sampler.outputs.push_back(buf[index][3]);
}
break;
}
default: {
std::cout << "unknown type" << std::endl;
break;
}
}
}
animation.samplers.push_back(sampler);
}
//移動/回転/拡大の設定
for (auto& source : anim.channels) {
AnimationChannel channel{};
if (source.target_path == "rotation") {
channel.path = AnimationChannel::PathType::ROTATION;
}
if (source.target_path == "translation") {
channel.path = AnimationChannel::PathType::TRANSLATION;
}
if (source.target_path == "scale") {
channel.path = AnimationChannel::PathType::SCALE;
}
if (source.target_path == "weights") {
std::cout << "weights not yet supported, skipping channel" << std::endl;
continue;
}
channel.samplerIndex = source.sampler;
channel.nodeOffset = model->nodeFromIndex(source.target_node);
if (channel.nodeOffset > -1) {
continue;
}
animation.channels.push_back(channel);
}
model->animations[animation.name] = animation;
}
}
//スケルトンを読み込む
void GltfModelFactory::loadSkin(std::shared_ptr<GltfModel> model, tinygltf::Model gltfModel)
{
for (tinygltf::Skin& source : gltfModel.skins) {
Skin newSkin = Skin();
newSkin.name = source.name;
//ルートノードの設定
if (source.skeleton > -1) {
newSkin.skinRootNodeOffset = model->nodeFromIndex(source.skeleton);
}
else
{
newSkin.skinRootNodeOffset = 0;
}
//スケルトンが番号で指定したノードを見つけ、Modelクラスのスケルトンにノードを設定していく
int globalHasSkinNodeIndex = 1;
newSkin.jointNodeOffset.resize(source.joints.size());
for (int i = 0;i < source.joints.size();i++)
{
int nodeOffset = model->nodeFromIndex(source.joints[i]);
if (nodeOffset > -1)
{
model->nodes[nodeOffset].globalHasSkinNodeIndex = globalHasSkinNodeIndex;
newSkin.jointNodeOffset[i] = nodeOffset;
globalHasSkinNodeIndex++;
}
}
model->jointNum = globalHasSkinNodeIndex;
//ボーン空間に戻す行列を設定していく
if (source.inverseBindMatrices > -1)
{
const tinygltf::Accessor& accessor = gltfModel.accessors[source.inverseBindMatrices];
const tinygltf::BufferView& bufferView = gltfModel.bufferViews[accessor.bufferView];
const tinygltf::Buffer& buffer = gltfModel.buffers[bufferView.buffer];
newSkin.inverseBindMatrices.resize(accessor.count);
memcpy(newSkin.inverseBindMatrices.data(), &buffer.data[accessor.byteOffset + bufferView.byteOffset], accessor.count * sizeof(glm::mat4));
}
model->skins.push_back(newSkin);
}
setSkin(model);//スキンを設定する
}
//スキンを設定する
void GltfModelFactory::setSkin(std::shared_ptr<GltfModel> model)
{
for (int i = 0; i < model->nodes.size(); i++)
{
if (model->nodes[i].skinIndex > -1)
{
model->nodes[i].skin = model->skins[model->nodes[i].skinIndex];
}
}
}
//マテリアルを読み込む
void GltfModelFactory::loadMaterial(std::shared_ptr<GltfModel> model, tinygltf::Model& gltfModel)
{
for (tinygltf::Material& mat : gltfModel.materials)
{
materialBuilder->initProperty();
if (mat.values.find("baseColorTexture") != mat.values.end())
{
tinygltf::Parameter param = mat.values["baseColorTexture"];
const tinygltf::Image image =
gltfModel.images[gltfModel.textures[param.TextureIndex()].source];
std::shared_ptr<Texture> texture
= textureFactory->Create(
image.component,
image.image.data(),
image.width,
image.height,
TexturePattern::NORMAL
);
materialBuilder->withBaseColorTexture(param.TextureTexCoord(), texture);
}
if (mat.values.find("metallicRoughnessTexture") != mat.values.end())
{
tinygltf::Parameter param = mat.values["metallicRoughnessTexture"];
const tinygltf::Image image =
gltfModel.images[gltfModel.textures[param.TextureIndex()].source];
std::shared_ptr<Texture> texture
= textureFactory->Create(
image.component,
image.image.data(),
image.width,
image.height,
TexturePattern::NORMAL
);
materialBuilder->withMetallicRoughnessTexture(param.TextureTexCoord(), texture);
}
if (mat.values.find("roughnessFactor") != mat.values.end())
{
materialBuilder->withRoughnessFactor(static_cast<float>(mat.values["roughnessFactor"].Factor()));
}
if (mat.values.find("metallicFactor") != mat.values.end())
{
materialBuilder->withRoughnessFactor(static_cast<float>(mat.values["metallicFactor"].Factor()));
}
if (mat.values.find("baseColorFactor") != mat.values.end())
{
materialBuilder->withBaseColorFactor(glm::make_vec4(mat.values["baseColorFactor"].ColorFactor().data()));
}
if (mat.additionalValues.find("normalTexture") != mat.additionalValues.end())
{
tinygltf::Parameter param = mat.additionalValues["normalTexture"];
const tinygltf::Image image =
gltfModel.images[gltfModel.textures[param.TextureIndex()].source];
std::shared_ptr<Texture> texture
= textureFactory->Create(
image.component,
image.image.data(),
image.width,
image.height,
TexturePattern::NORMAL
);
materialBuilder->withNormalTexture(param.TextureTexCoord(), texture);
}
if (mat.additionalValues.find("emissiveTexture") != mat.additionalValues.end())
{
tinygltf::Parameter param = mat.additionalValues["emissiveTexture"];
const tinygltf::Image image =
gltfModel.images[gltfModel.textures[param.TextureIndex()].source];
std::shared_ptr<Texture> texture
= textureFactory->Create(
image.component,
image.image.data(),
image.width,
image.height,
TexturePattern::NORMAL
);
materialBuilder->withEmissiveTexture(param.TextureTexCoord(), texture);
}
if (mat.additionalValues.find("occlusionTexture") != mat.additionalValues.end())
{
tinygltf::Parameter param = mat.additionalValues["occlusionTexture"];
const tinygltf::Image image =
gltfModel.images[gltfModel.textures[param.TextureIndex()].source];
std::shared_ptr<Texture> texture
= textureFactory->Create(
image.component,
image.image.data(),
image.width,
image.height,
TexturePattern::NORMAL
);
materialBuilder->withOcclusionTexture(param.TextureTexCoord(), texture);
}
if (mat.additionalValues.find("alphaCutoff") != mat.additionalValues.end())
{
materialBuilder->withAlphaMaskCutOff(static_cast<float>(mat.additionalValues["alphaCutoff"].Factor()));
}
if (mat.additionalValues.find("emissiveFactor") != mat.additionalValues.end())
{
materialBuilder->withEmissiveFactor(glm::vec4(glm::make_vec3(mat.additionalValues["emissiveFactor"].ColorFactor().data()), 1.0));
}
std::shared_ptr<Material> material = materialBuilder->Create(materialBuilder->Build());
model->materials.push_back(material);
}
}