| 65 | } |
| 66 | |
| 67 | std::vector<pxr::UsdShadeMaterial> USDSerializer::createMaterials(const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& styles) |
| 68 | { |
| 69 | if(styles.empty()) |
| 70 | throw std::runtime_error("No styles to create materials from"); |
| 71 | |
| 72 | std::vector<pxr::UsdShadeMaterial> materials {}; |
| 73 | |
| 74 | for(auto styleptr : styles) { |
| 75 | auto& style = *styleptr; |
| 76 | std::string material_path(style.name); |
| 77 | usd_utils::toPath(material_path); |
| 78 | |
| 79 | if(materials_.find(material_path) != materials_.end()) { |
| 80 | materials.push_back(materials_[material_path]); |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | const std::string path("/Looks/" + material_path); |
| 85 | auto material = pxr::UsdShadeMaterial::Define(stage_, pxr::SdfPath(path)); |
| 86 | auto shader = pxr::UsdShadeShader::Define(stage_, pxr::SdfPath(path + "/Shader")); |
| 87 | shader.CreateIdAttr().Set(pxr::TfToken("UsdPreviewSurface")); |
| 88 | |
| 89 | float rgba[4] { 0.18f, 0.18f, 0.18f, 1.0f }; |
| 90 | if (style.diffuse) |
| 91 | for (int i = 0; i < 3; ++i) |
| 92 | rgba[i] = static_cast<float>(style.diffuse.ccomponents()(i)); |
| 93 | shader.CreateInput(pxr::TfToken("diffuseColor"), pxr::SdfValueTypeNames->Color3f).Set(pxr::GfVec3f(rgba[0], rgba[1], rgba[2])); |
| 94 | |
| 95 | if (style.has_transparency()) |
| 96 | rgba[3] -= style.transparency; |
| 97 | shader.CreateInput(pxr::TfToken("opacity"), pxr::SdfValueTypeNames->Float).Set(rgba[3]); |
| 98 | |
| 99 | if(style.specular) { |
| 100 | for (int i = 0; i < 3; ++i) |
| 101 | rgba[i] = static_cast<float>(style.specular.ccomponents()(i)); |
| 102 | shader.CreateInput(pxr::TfToken("useSpecularWorkflow"), pxr::SdfValueTypeNames->Int).Set(1); |
| 103 | } else { |
| 104 | shader.CreateInput(pxr::TfToken("useSpecularWorkflow"), pxr::SdfValueTypeNames->Int).Set(0); |
| 105 | } |
| 106 | shader.CreateInput(pxr::TfToken("specularColor"), pxr::SdfValueTypeNames->Color3f).Set(pxr::GfVec3f(rgba[0], rgba[1], rgba[2])); |
| 107 | |
| 108 | material.CreateSurfaceOutput().ConnectToSource(shader.ConnectableAPI(), pxr::TfToken("surface")); |
| 109 | materials.push_back(material); |
| 110 | materials_[material_path] = material; |
| 111 | } |
| 112 | |
| 113 | return materials; |
| 114 | } |
| 115 | |
| 116 | void USDSerializer::writeHeader() { |
| 117 | stage_->GetRootLayer()->SetComment("File generated by IfcOpenShell " + std::string(IFCOPENSHELL_VERSION)); |