/src/assimp/code/AssetLib/IFC/IFCMaterial.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2025, assimp team |
6 | | |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use of this software in source and binary forms, |
10 | | with or without modification, are permitted provided that the |
11 | | following conditions are met: |
12 | | |
13 | | * Redistributions of source code must retain the above |
14 | | copyright notice, this list of conditions and the |
15 | | following disclaimer. |
16 | | |
17 | | * Redistributions in binary form must reproduce the above |
18 | | copyright notice, this list of conditions and the |
19 | | following disclaimer in the documentation and/or other |
20 | | materials provided with the distribution. |
21 | | |
22 | | * Neither the name of the assimp team, nor the names of its |
23 | | contributors may be used to endorse or promote products |
24 | | derived from this software without specific prior |
25 | | written permission of the assimp team. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | |
39 | | ---------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | /// @file IFCMaterial.cpp |
43 | | /// @brief Implementation of conversion routines to convert IFC materials to aiMaterial |
44 | | |
45 | | #ifndef ASSIMP_BUILD_NO_IFC_IMPORTER |
46 | | |
47 | | #include "IFCUtil.h" |
48 | | #include <limits> |
49 | | #include <assimp/material.h> |
50 | | |
51 | | namespace Assimp { |
52 | | namespace IFC { |
53 | | |
54 | | // ------------------------------------------------------------------------------------------------ |
55 | 0 | static int ConvertShadingMode(const std::string& name) { |
56 | 0 | if (name == "BLINN") { |
57 | 0 | return aiShadingMode_Blinn; |
58 | 0 | } |
59 | 0 | else if (name == "FLAT" || name == "NOTDEFINED") { |
60 | 0 | return aiShadingMode_NoShading; |
61 | 0 | } |
62 | 0 | else if (name == "PHONG") { |
63 | 0 | return aiShadingMode_Phong; |
64 | 0 | } |
65 | 0 | IFCImporter::LogWarn("shading mode ", name, " not recognized by Assimp, using Phong instead"); |
66 | 0 | return aiShadingMode_Phong; |
67 | 0 | } |
68 | | |
69 | | // ------------------------------------------------------------------------------------------------ |
70 | 0 | static void FillMaterial(aiMaterial* mat,const IFC::Schema_2x3::IfcSurfaceStyle* surf,ConversionData& conv) { |
71 | 0 | aiString name; |
72 | 0 | name.Set((surf->Name? surf->Name.Get() : "IfcSurfaceStyle_Unnamed")); |
73 | 0 | mat->AddProperty(&name,AI_MATKEY_NAME); |
74 | | |
75 | | // now see which kinds of surface information are present |
76 | 0 | for (const std::shared_ptr<const IFC::Schema_2x3::IfcSurfaceStyleElementSelect> &sel2 : surf->Styles) { |
77 | 0 | if (const IFC::Schema_2x3::IfcSurfaceStyleShading* shade = sel2->ResolveSelectPtr<IFC::Schema_2x3::IfcSurfaceStyleShading>(conv.db)) { |
78 | 0 | aiColor4D col_base,col; |
79 | |
|
80 | 0 | ConvertColor(col_base, shade->SurfaceColour); |
81 | 0 | mat->AddProperty(&col_base,1, AI_MATKEY_COLOR_DIFFUSE); |
82 | |
|
83 | 0 | if (const IFC::Schema_2x3::IfcSurfaceStyleRendering* ren = shade->ToPtr<IFC::Schema_2x3::IfcSurfaceStyleRendering>()) { |
84 | |
|
85 | 0 | if (ren->Transparency) { |
86 | 0 | const float t = 1.f-static_cast<float>(ren->Transparency.Get()); |
87 | 0 | mat->AddProperty(&t,1, AI_MATKEY_OPACITY); |
88 | 0 | } |
89 | |
|
90 | 0 | if (ren->DiffuseColour) { |
91 | 0 | ConvertColor(col, *ren->DiffuseColour.Get(),conv,&col_base); |
92 | 0 | mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE); |
93 | 0 | } |
94 | |
|
95 | 0 | if (ren->SpecularColour) { |
96 | 0 | ConvertColor(col, *ren->SpecularColour.Get(),conv,&col_base); |
97 | 0 | mat->AddProperty(&col,1, AI_MATKEY_COLOR_SPECULAR); |
98 | 0 | } |
99 | |
|
100 | 0 | if (ren->TransmissionColour) { |
101 | 0 | ConvertColor(col, *ren->TransmissionColour.Get(),conv,&col_base); |
102 | 0 | mat->AddProperty(&col,1, AI_MATKEY_COLOR_TRANSPARENT); |
103 | 0 | } |
104 | |
|
105 | 0 | if (ren->ReflectionColour) { |
106 | 0 | ConvertColor(col, *ren->ReflectionColour.Get(),conv,&col_base); |
107 | 0 | mat->AddProperty(&col,1, AI_MATKEY_COLOR_REFLECTIVE); |
108 | 0 | } |
109 | |
|
110 | 0 | const int shading = (ren->SpecularHighlight && ren->SpecularColour)?ConvertShadingMode(ren->ReflectanceMethod):static_cast<int>(aiShadingMode_Gouraud); |
111 | 0 | mat->AddProperty(&shading,1, AI_MATKEY_SHADING_MODEL); |
112 | |
|
113 | 0 | if (ren->SpecularHighlight) { |
114 | 0 | if(const ::Assimp::STEP::EXPRESS::REAL* rt = ren->SpecularHighlight.Get()->ToPtr<::Assimp::STEP::EXPRESS::REAL>()) { |
115 | | // at this point we don't distinguish between the two distinct ways of |
116 | | // specifying highlight intensities. leave this to the user. |
117 | 0 | const float e = static_cast<float>(*rt); |
118 | 0 | mat->AddProperty(&e,1,AI_MATKEY_SHININESS); |
119 | 0 | } |
120 | 0 | else { |
121 | 0 | IFCImporter::LogWarn("unexpected type error, SpecularHighlight should be a REAL"); |
122 | 0 | } |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | | // ------------------------------------------------------------------------------------------------ |
130 | 0 | unsigned int ProcessMaterials(uint64_t id, unsigned int prevMatId, ConversionData& conv, bool forceDefaultMat) { |
131 | 0 | STEP::DB::RefMapRange range = conv.db.GetRefs().equal_range(id); |
132 | 0 | for(;range.first != range.second; ++range.first) { |
133 | 0 | if(const IFC::Schema_2x3::IfcStyledItem* const styled = conv.db.GetObject((*range.first).second)->ToPtr<IFC::Schema_2x3::IfcStyledItem>()) { |
134 | 0 | for(const IFC::Schema_2x3::IfcPresentationStyleAssignment& as : styled->Styles) { |
135 | 0 | for (const std::shared_ptr<const IFC::Schema_2x3::IfcPresentationStyleSelect> &sel : as.Styles) { |
136 | |
|
137 | 0 | if( const IFC::Schema_2x3::IfcSurfaceStyle* const surf = sel->ResolveSelectPtr<IFC::Schema_2x3::IfcSurfaceStyle>(conv.db) ) { |
138 | | // try to satisfy from cache |
139 | 0 | ConversionData::MaterialCache::iterator mit = conv.cached_materials.find(surf); |
140 | 0 | if( mit != conv.cached_materials.end() ) |
141 | 0 | return mit->second; |
142 | | |
143 | | // not found, create new material |
144 | 0 | const std::string side = static_cast<std::string>(surf->Side); |
145 | 0 | if( side != "BOTH" ) { |
146 | 0 | IFCImporter::LogWarn("ignoring surface side marker on IFC::IfcSurfaceStyle: ", side); |
147 | 0 | } |
148 | |
|
149 | 0 | std::unique_ptr<aiMaterial> mat(new aiMaterial()); |
150 | |
|
151 | 0 | FillMaterial(mat.get(), surf, conv); |
152 | |
|
153 | 0 | conv.materials.push_back(mat.release()); |
154 | 0 | unsigned int matindex = static_cast<unsigned int>(conv.materials.size() - 1); |
155 | 0 | conv.cached_materials[surf] = matindex; |
156 | 0 | return matindex; |
157 | 0 | } |
158 | 0 | } |
159 | 0 | } |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | | // no local material defined. If there's global one, use that instead |
164 | 0 | if ( prevMatId != std::numeric_limits<uint32_t>::max() ) { |
165 | 0 | return prevMatId; |
166 | 0 | } |
167 | | |
168 | | // we're still here - create an default material if required, or simply fail otherwise |
169 | 0 | if ( !forceDefaultMat ) { |
170 | 0 | return std::numeric_limits<uint32_t>::max(); |
171 | 0 | } |
172 | | |
173 | 0 | aiString name; |
174 | 0 | name.Set("<IFCDefault>"); |
175 | | |
176 | | // look if there's already a default material with this base color |
177 | 0 | for( size_t a = 0; a < conv.materials.size(); ++a ) { |
178 | 0 | aiString mname; |
179 | 0 | conv.materials[a]->Get(AI_MATKEY_NAME, mname); |
180 | 0 | if ( name == mname ) { |
181 | 0 | return ( unsigned int )a; |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | // we're here, yet - no default material with suitable color available. Generate one |
186 | 0 | std::unique_ptr<aiMaterial> mat(new aiMaterial()); |
187 | 0 | mat->AddProperty(&name,AI_MATKEY_NAME); |
188 | |
|
189 | 0 | const aiColor4D col = aiColor4D( 0.6f, 0.6f, 0.6f, 1.0f); // aiColor4D( color.r, color.g, color.b, 1.0f); |
190 | 0 | mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE); |
191 | |
|
192 | 0 | conv.materials.push_back(mat.release()); |
193 | 0 | return (unsigned int) conv.materials.size() - 1; |
194 | 0 | } |
195 | | |
196 | | } // ! IFC |
197 | | } // ! Assimp |
198 | | |
199 | | #endif // ASSIMP_BUILD_NO_IFC_IMPORTER |