1 // Copyright (c) 2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <XCAFDoc_VisMaterial.hxx>
16 #include <Graphic3d_Aspects.hxx>
17 #include <Graphic3d_MaterialAspect.hxx>
18 #include <Standard_GUID.hxx>
19 #include <TDF_Label.hxx>
20 #include <XCAFPrs_Texture.hxx>
22 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_VisMaterial, TDF_Attribute)
24 //=======================================================================
27 //=======================================================================
28 const Standard_GUID& XCAFDoc_VisMaterial::GetID()
30 static Standard_GUID THE_VIS_MAT_ID ("EBB00255-03A0-4845-BD3B-A70EEDEEFA78");
31 return THE_VIS_MAT_ID;
34 //=======================================================================
35 //function : Constructor
37 //=======================================================================
38 XCAFDoc_VisMaterial::XCAFDoc_VisMaterial()
39 : myAlphaMode (Graphic3d_AlphaMode_BlendAuto),
41 myFaceCulling (Graphic3d_TypeOfBackfacingModel_Auto)
46 //=======================================================================
47 //function : SetMetalRougnessMaterial
49 //=======================================================================
50 void XCAFDoc_VisMaterial::SetPbrMaterial (const XCAFDoc_VisMaterialPBR& theMaterial)
53 myPbrMat = theMaterial;
56 //=======================================================================
57 //function : SetCommonMaterial
59 //=======================================================================
60 void XCAFDoc_VisMaterial::SetCommonMaterial (const XCAFDoc_VisMaterialCommon& theMaterial)
63 myCommonMat = theMaterial;
66 //=======================================================================
67 //function : SetAlphaMode
69 //=======================================================================
70 void XCAFDoc_VisMaterial::SetAlphaMode (Graphic3d_AlphaMode theMode,
71 Standard_ShortReal theCutOff)
74 myAlphaMode = theMode;
75 myAlphaCutOff = theCutOff;
78 //=======================================================================
79 //function : SetFaceCulling
81 //=======================================================================
82 void XCAFDoc_VisMaterial::SetFaceCulling (Graphic3d_TypeOfBackfacingModel theFaceCulling)
85 myFaceCulling = theFaceCulling;
88 //=======================================================================
91 //=======================================================================
92 void XCAFDoc_VisMaterial::Restore (const Handle(TDF_Attribute)& theWith)
94 XCAFDoc_VisMaterial* anOther = dynamic_cast<XCAFDoc_VisMaterial* >(theWith.get());
95 myPbrMat = anOther->myPbrMat;
96 myCommonMat = anOther->myCommonMat;
97 myAlphaMode = anOther->myAlphaMode;
98 myAlphaCutOff = anOther->myAlphaCutOff;
99 myFaceCulling = anOther->myFaceCulling;
102 //=======================================================================
103 //function : NewEmpty
105 //=======================================================================
106 Handle(TDF_Attribute) XCAFDoc_VisMaterial::NewEmpty() const
108 return new XCAFDoc_VisMaterial();
111 //=======================================================================
114 //=======================================================================
115 void XCAFDoc_VisMaterial::Paste (const Handle(TDF_Attribute)& theInto,
116 const Handle(TDF_RelocationTable)& ) const
118 XCAFDoc_VisMaterial* anOther = dynamic_cast<XCAFDoc_VisMaterial* >(theInto.get());
120 anOther->myPbrMat = myPbrMat;
121 anOther->myCommonMat = myCommonMat;
122 anOther->myAlphaMode = myAlphaMode;
123 anOther->myAlphaCutOff = myAlphaCutOff;
124 anOther->myFaceCulling = myFaceCulling;
127 // =======================================================================
128 // function : BaseColor
130 // =======================================================================
131 Quantity_ColorRGBA XCAFDoc_VisMaterial::BaseColor() const
133 if (myPbrMat.IsDefined)
135 return myPbrMat.BaseColor;
137 else if (myCommonMat.IsDefined)
139 return Quantity_ColorRGBA (myCommonMat.DiffuseColor, 1.0f - myCommonMat.Transparency);
141 return Quantity_ColorRGBA (Quantity_NOC_WHITE);
144 //=======================================================================
145 //function : ConvertToCommonMaterial
147 //=======================================================================
148 XCAFDoc_VisMaterialCommon XCAFDoc_VisMaterial::ConvertToCommonMaterial()
150 if (myCommonMat.IsDefined)
154 else if (!myPbrMat.IsDefined)
156 return XCAFDoc_VisMaterialCommon();
159 // convert metal-roughness into common
160 XCAFDoc_VisMaterialCommon aComMat;
161 aComMat.IsDefined = true;
162 aComMat.DiffuseTexture = myPbrMat.BaseColorTexture;
163 aComMat.DiffuseColor = myPbrMat.BaseColor.GetRGB();
164 aComMat.SpecularColor = Quantity_Color (Graphic3d_Vec3 (myPbrMat.Metallic));
165 aComMat.Transparency = 1.0f - myPbrMat.BaseColor.Alpha();
166 aComMat.Shininess = 1.0f - myPbrMat.Roughness;
170 //=======================================================================
171 //function : ConvertToPbrMaterial
173 //=======================================================================
174 XCAFDoc_VisMaterialPBR XCAFDoc_VisMaterial::ConvertToPbrMaterial()
176 if (myPbrMat.IsDefined)
180 else if (!myCommonMat.IsDefined)
182 return XCAFDoc_VisMaterialPBR();
185 XCAFDoc_VisMaterialPBR aPbrMat;
186 aPbrMat.IsDefined = true;
187 aPbrMat.BaseColorTexture = myCommonMat.DiffuseTexture;
188 aPbrMat.BaseColor.SetRGB (myCommonMat.DiffuseColor);
189 aPbrMat.BaseColor.SetAlpha (1.0f - myCommonMat.Transparency);
190 aPbrMat.Metallic = Graphic3d_PBRMaterial::MetallicFromSpecular (myCommonMat.SpecularColor);
191 aPbrMat.Roughness = Graphic3d_PBRMaterial::RoughnessFromSpecular (myCommonMat.SpecularColor, myCommonMat.Shininess);
195 //=======================================================================
196 //function : FillMaterialAspect
198 //=======================================================================
199 void XCAFDoc_VisMaterial::FillMaterialAspect (Graphic3d_MaterialAspect& theAspect) const
201 if (myCommonMat.IsDefined)
203 theAspect = Graphic3d_MaterialAspect (Graphic3d_NameOfMaterial_UserDefined);
204 theAspect.SetAmbientColor (myCommonMat.AmbientColor);
205 theAspect.SetDiffuseColor (myCommonMat.DiffuseColor);
206 theAspect.SetSpecularColor(myCommonMat.SpecularColor);
207 theAspect.SetEmissiveColor(myCommonMat.EmissiveColor);
208 theAspect.SetTransparency (myCommonMat.Transparency);
209 theAspect.SetShininess (myCommonMat.Shininess);
211 // convert common into metal-roughness
212 if (!myPbrMat.IsDefined)
214 Graphic3d_PBRMaterial aPbr;
215 aPbr.SetColor (myCommonMat.DiffuseColor);
216 aPbr.SetMetallic (Graphic3d_PBRMaterial::MetallicFromSpecular (myCommonMat.SpecularColor));
217 aPbr.SetRoughness (Graphic3d_PBRMaterial::RoughnessFromSpecular (myCommonMat.SpecularColor, myCommonMat.Shininess));
218 theAspect.SetPBRMaterial (aPbr);
219 theAspect.SetBSDF (Graphic3d_BSDF::CreateMetallicRoughness (aPbr));
223 if (myPbrMat.IsDefined)
225 if (!myCommonMat.IsDefined)
227 // convert metal-roughness into common
228 theAspect = Graphic3d_MaterialAspect (Graphic3d_NameOfMaterial_UserDefined);
229 theAspect.SetDiffuseColor (myPbrMat.BaseColor.GetRGB());
230 theAspect.SetAlpha (myPbrMat.BaseColor.Alpha());
231 theAspect.SetSpecularColor(Quantity_Color (Graphic3d_Vec3 (myPbrMat.Metallic)));
232 theAspect.SetShininess (1.0f - myPbrMat.Roughness);
235 Graphic3d_PBRMaterial aPbr;
236 aPbr.SetColor (myPbrMat.BaseColor);
237 aPbr.SetMetallic (myPbrMat.Metallic);
238 aPbr.SetRoughness(myPbrMat.Roughness);
239 aPbr.SetEmission (myPbrMat.EmissiveFactor);
240 aPbr.SetIOR (myPbrMat.RefractionIndex);
241 theAspect.SetRefractionIndex (myPbrMat.RefractionIndex);
242 theAspect.SetPBRMaterial (aPbr);
243 theAspect.SetBSDF (Graphic3d_BSDF::CreateMetallicRoughness (aPbr));
247 //=======================================================================
248 //function : FillAspect
250 //=======================================================================
251 void XCAFDoc_VisMaterial::FillAspect (const Handle(Graphic3d_Aspects)& theAspect) const
258 Graphic3d_MaterialAspect aMaterial;
259 FillMaterialAspect (aMaterial);
260 theAspect->SetFrontMaterial (aMaterial);
261 theAspect->SetAlphaMode (myAlphaMode , myAlphaCutOff);
262 theAspect->SetFaceCulling (myFaceCulling);
264 const Handle(Image_Texture)& aColorTexture = !myPbrMat.BaseColorTexture.IsNull() ? myPbrMat.BaseColorTexture : myCommonMat.DiffuseTexture;
265 Standard_Integer aNbTexUnits = 0;
266 if (!aColorTexture.IsNull()) { ++aNbTexUnits; }
267 if (!myPbrMat.EmissiveTexture.IsNull()) { ++aNbTexUnits; }
268 if (!myPbrMat.NormalTexture.IsNull()) { ++aNbTexUnits; }
269 if (!myPbrMat.OcclusionTexture.IsNull()) { ++aNbTexUnits; }
270 if (!myPbrMat.MetallicRoughnessTexture.IsNull()) { ++aNbTexUnits; }
271 if (aNbTexUnits == 0)
276 Standard_Integer aTexIter = 0;
277 Handle(Graphic3d_TextureSet) aTextureSet = new Graphic3d_TextureSet (aNbTexUnits);
278 if (!aColorTexture.IsNull())
280 aTextureSet->SetValue (aTexIter++, new XCAFPrs_Texture (*aColorTexture, Graphic3d_TextureUnit_BaseColor));
282 if (!myPbrMat.EmissiveTexture.IsNull())
284 aTextureSet->SetValue (aTexIter++, new XCAFPrs_Texture (*myPbrMat.EmissiveTexture, Graphic3d_TextureUnit_Emissive));
286 if (!myPbrMat.OcclusionTexture.IsNull())
288 aTextureSet->SetValue (aTexIter++, new XCAFPrs_Texture (*myPbrMat.OcclusionTexture, Graphic3d_TextureUnit_Occlusion));
290 if (!myPbrMat.NormalTexture.IsNull())
292 aTextureSet->SetValue (aTexIter++, new XCAFPrs_Texture (*myPbrMat.NormalTexture, Graphic3d_TextureUnit_Normal));
294 if (!myPbrMat.MetallicRoughnessTexture.IsNull())
296 aTextureSet->SetValue (aTexIter++, new XCAFPrs_Texture (*myPbrMat.MetallicRoughnessTexture, Graphic3d_TextureUnit_MetallicRoughness));
299 theAspect->SetTextureSet (aTextureSet);
300 theAspect->SetTextureMapOn (true);
303 //=======================================================================
304 //function : DumpJson
306 //=======================================================================
307 void XCAFDoc_VisMaterial::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
309 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
311 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
313 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myRawName.get())
315 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myPbrMat)
316 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myCommonMat)
318 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAlphaMode)
319 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAlphaCutOff)
320 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myFaceCulling)