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