| 513 | } |
| 514 | |
| 515 | bool ImportMaterials(ModelData& result, AssimpImporterData& data, String& errorMsg) |
| 516 | { |
| 517 | const uint32 materialsCount = data.Scene->mNumMaterials; |
| 518 | result.Materials.Resize(materialsCount, false); |
| 519 | for (uint32 i = 0; i < materialsCount; i++) |
| 520 | { |
| 521 | auto& materialSlot = result.Materials[i]; |
| 522 | const aiMaterial* aMaterial = data.Scene->mMaterials[i]; |
| 523 | |
| 524 | aiString aName; |
| 525 | if (aMaterial->Get(AI_MATKEY_NAME, aName) == AI_SUCCESS) |
| 526 | materialSlot.Name = String(aName.C_Str()).TrimTrailing(); |
| 527 | materialSlot.AssetID = Guid::Empty; |
| 528 | |
| 529 | if (EnumHasAnyFlags(data.Options.ImportTypes, ImportDataTypes::Materials)) |
| 530 | { |
| 531 | aiColor3D aColor; |
| 532 | if (aMaterial->Get(AI_MATKEY_COLOR_DIFFUSE, aColor) == AI_SUCCESS) |
| 533 | materialSlot.Diffuse.Color = ToColor(aColor); |
| 534 | if (aMaterial->Get(AI_MATKEY_COLOR_EMISSIVE, aColor) == AI_SUCCESS) |
| 535 | materialSlot.Emissive.Color = ToColor(aColor); |
| 536 | if (aMaterial->Get(AI_MATKEY_COLOR_EMISSIVE, aColor) == AI_SUCCESS) |
| 537 | materialSlot.Emissive.Color = ToColor(aColor); |
| 538 | bool aBoolean; |
| 539 | if (aMaterial->Get(AI_MATKEY_TWOSIDED, aBoolean) == AI_SUCCESS) |
| 540 | materialSlot.TwoSided = aBoolean; |
| 541 | if (aMaterial->Get(AI_MATKEY_ENABLE_WIREFRAME, aBoolean) == AI_SUCCESS) |
| 542 | materialSlot.Wireframe = aBoolean; |
| 543 | float aFloat; |
| 544 | if (aMaterial->Get(AI_MATKEY_OPACITY, aFloat) == AI_SUCCESS) |
| 545 | materialSlot.Opacity.Value = aFloat; |
| 546 | if (aMaterial->Get(AI_MATKEY_GLOSSINESS_FACTOR, aFloat) == AI_SUCCESS) |
| 547 | materialSlot.Roughness.Value = 1.0f - aFloat; |
| 548 | else if (aMaterial->Get(AI_MATKEY_SHININESS, aFloat) == AI_SUCCESS) |
| 549 | materialSlot.Roughness.Value = MaterialSlotEntry::ShininessToRoughness(aFloat); |
| 550 | if (aMaterial->Get(AI_MATKEY_EMISSIVE_INTENSITY, aFloat) == AI_SUCCESS) |
| 551 | materialSlot.Emissive.Color *= aFloat; |
| 552 | |
| 553 | if (EnumHasAnyFlags(data.Options.ImportTypes, ImportDataTypes::Textures)) |
| 554 | { |
| 555 | ImportMaterialTexture(result, data, aMaterial, aiTextureType_DIFFUSE, materialSlot.Diffuse.TextureIndex, TextureEntry::TypeHint::ColorRGB, true); |
| 556 | ImportMaterialTexture(result, data, aMaterial, aiTextureType_EMISSIVE, materialSlot.Emissive.TextureIndex, TextureEntry::TypeHint::ColorRGB); |
| 557 | ImportMaterialTexture(result, data, aMaterial, aiTextureType_NORMALS, materialSlot.Normals.TextureIndex, TextureEntry::TypeHint::Normals); |
| 558 | ImportMaterialTexture(result, data, aMaterial, aiTextureType_OPACITY, materialSlot.Opacity.TextureIndex, TextureEntry::TypeHint::ColorRGBA); |
| 559 | ImportMaterialTexture(result, data, aMaterial, aiTextureType_METALNESS, materialSlot.Metalness.TextureIndex, TextureEntry::TypeHint::ColorRGB); |
| 560 | ImportMaterialTexture(result, data, aMaterial, aiTextureType_DIFFUSE_ROUGHNESS, materialSlot.Roughness.TextureIndex, TextureEntry::TypeHint::ColorRGB, true); |
| 561 | |
| 562 | if (materialSlot.Roughness.TextureIndex != -1 && (data.Path.EndsWith(TEXT(".gltf")) || data.Path.EndsWith(TEXT(".glb")))) |
| 563 | { |
| 564 | // glTF specification with a single metallicRoughnessTexture (G = roughness, B = metalness) |
| 565 | materialSlot.Roughness.Channel = 1; |
| 566 | materialSlot.Metalness.Channel = 2; |
| 567 | } |
| 568 | |
| 569 | if (materialSlot.Diffuse.TextureIndex != -1) |
| 570 | { |
| 571 | // Detect using alpha mask in diffuse texture |
| 572 | materialSlot.Diffuse.HasAlphaMask = TextureTool::HasAlpha(result.Textures[materialSlot.Diffuse.TextureIndex].FilePath); |
no test coverage detected