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 <BinMXCAFDoc_VisMaterialDriver.hxx>
16 #include <BinObjMgt_Persistent.hxx>
17 #include <Message_Messenger.hxx>
18 #include <XCAFDoc_VisMaterial.hxx>
20 IMPLEMENT_STANDARD_RTTIEXT(BinMXCAFDoc_VisMaterialDriver, BinMDF_ADriver)
22 //! Encode alpha mode into character.
23 static Standard_Byte alphaModeToChar (Graphic3d_AlphaMode theMode)
27 case Graphic3d_AlphaMode_Opaque: return 'O';
28 case Graphic3d_AlphaMode_Mask: return 'M';
29 case Graphic3d_AlphaMode_Blend: return 'B';
30 case Graphic3d_AlphaMode_BlendAuto: return 'A';
35 //! Decode alpha mode from character.
36 static Graphic3d_AlphaMode alphaModeFromChar (Standard_Byte theMode)
40 case 'O': return Graphic3d_AlphaMode_Opaque;
41 case 'M': return Graphic3d_AlphaMode_Mask;
42 case 'B': return Graphic3d_AlphaMode_Blend;
43 case 'A': return Graphic3d_AlphaMode_BlendAuto;
45 return Graphic3d_AlphaMode_BlendAuto;
49 static void writeVec3 (BinObjMgt_Persistent& theTarget,
50 const Graphic3d_Vec3& theVec3)
52 theTarget.PutShortReal (theVec3[0]);
53 theTarget.PutShortReal (theVec3[1]);
54 theTarget.PutShortReal (theVec3[2]);
58 static void writeVec4 (BinObjMgt_Persistent& theTarget,
59 const Graphic3d_Vec4& theVec4)
61 theTarget.PutShortReal (theVec4[0]);
62 theTarget.PutShortReal (theVec4[1]);
63 theTarget.PutShortReal (theVec4[2]);
64 theTarget.PutShortReal (theVec4[3]);
68 static void readVec3 (const BinObjMgt_Persistent& theSource,
69 Graphic3d_Vec3& theVec3)
71 theSource.GetShortReal (theVec3[0]);
72 theSource.GetShortReal (theVec3[1]);
73 theSource.GetShortReal (theVec3[2]);
77 static void readColor (const BinObjMgt_Persistent& theSource,
78 Quantity_Color& theColor)
81 readVec3 (theSource, aVec3);
82 theColor = Quantity_Color (aVec3);
86 static void readColor (const BinObjMgt_Persistent& theSource,
87 Quantity_ColorRGBA& theColor)
90 theSource.GetShortReal (aVec4[0]);
91 theSource.GetShortReal (aVec4[1]);
92 theSource.GetShortReal (aVec4[2]);
93 theSource.GetShortReal (aVec4[3]);
94 theColor = Quantity_ColorRGBA (aVec4);
97 //! Encode texture path.
98 static void writeTexture (BinObjMgt_Persistent& theTarget,
99 const Handle(Image_Texture)& theImage)
101 theTarget.PutAsciiString (!theImage.IsNull()
102 && !theImage->FilePath().IsEmpty()
103 && theImage->FileOffset() == -1
104 ? theImage->FilePath()
108 //! Decode texture path.
109 static void readTexture (const BinObjMgt_Persistent& theSource,
110 Handle(Image_Texture)& theTexture)
112 TCollection_AsciiString aPath;
113 theSource.GetAsciiString (aPath);
114 if (!aPath.IsEmpty())
116 theTexture = new Image_Texture (aPath);
120 //=======================================================================
121 //function : Constructor
123 //=======================================================================
124 BinMXCAFDoc_VisMaterialDriver::BinMXCAFDoc_VisMaterialDriver (const Handle(Message_Messenger)& theMsgDriver)
125 : BinMDF_ADriver(theMsgDriver, STANDARD_TYPE(XCAFDoc_VisMaterial)->Name())
129 //=======================================================================
130 //function : NewEmpty
132 //=======================================================================
133 Handle(TDF_Attribute) BinMXCAFDoc_VisMaterialDriver::NewEmpty() const
135 return new XCAFDoc_VisMaterial();
138 //=======================================================================
141 //=======================================================================
142 Standard_Boolean BinMXCAFDoc_VisMaterialDriver::Paste (const BinObjMgt_Persistent& theSource,
143 const Handle(TDF_Attribute)& theTarget,
144 BinObjMgt_RRelocationTable& /*theRelocTable*/) const
146 Handle(XCAFDoc_VisMaterial) aMat = Handle(XCAFDoc_VisMaterial)::DownCast(theTarget);
147 Standard_Byte aVerMaj = 0, aVerMin = 0;
148 theSource.GetByte (aVerMaj);
149 theSource.GetByte (aVerMin);
150 if (aVerMaj < 1 || aVerMaj > MaterialVersionMajor)
152 myMessageDriver->Send (TCollection_AsciiString ("Skipping XCAFDoc_VisMaterial of unknown version ")
153 + Standard_Integer(aVerMaj) + "." + Standard_Integer(aVerMin)
154 + " (supported version: " + Standard_Integer(MaterialVersionMajor) + "." + Standard_Integer(MaterialVersionMinor) + ")");
158 Standard_Byte isDoubleSided = 0, anAlphaMode = 0;
159 Standard_ShortReal anAlphaCutOff = 0.5f;
160 theSource.GetByte (isDoubleSided);
161 theSource.GetByte (anAlphaMode);
162 theSource.GetShortReal (anAlphaCutOff);
163 aMat->SetDoubleSided (isDoubleSided == '1');
164 aMat->SetAlphaMode (alphaModeFromChar (anAlphaMode), anAlphaCutOff);
166 bool hasPbrMat = false;
167 theSource.GetBoolean (hasPbrMat);
170 XCAFDoc_VisMaterialPBR aPbrMat;
171 aPbrMat.IsDefined = true;
172 readColor (theSource, aPbrMat.BaseColor);
173 readVec3 (theSource, aPbrMat.EmissiveFactor);
174 theSource.GetShortReal (aPbrMat.Metallic);
175 theSource.GetShortReal (aPbrMat.Roughness);
176 readTexture (theSource, aPbrMat.BaseColorTexture);
177 readTexture (theSource, aPbrMat.MetallicRoughnessTexture);
178 readTexture (theSource, aPbrMat.EmissiveTexture);
179 readTexture (theSource, aPbrMat.OcclusionTexture);
180 readTexture (theSource, aPbrMat.NormalTexture);
181 aMat->SetPbrMaterial (aPbrMat);
184 bool hasComMat = false;
185 theSource.GetBoolean (hasComMat);
188 XCAFDoc_VisMaterialCommon aComMat;
189 aComMat.IsDefined = true;
190 readColor (theSource, aComMat.AmbientColor);
191 readColor (theSource, aComMat.DiffuseColor);
192 readColor (theSource, aComMat.SpecularColor);
193 readColor (theSource, aComMat.EmissiveColor);
194 theSource.GetShortReal (aComMat.Shininess);
195 theSource.GetShortReal (aComMat.Transparency);
196 readTexture (theSource, aComMat.DiffuseTexture);
197 aMat->SetCommonMaterial (aComMat);
199 return Standard_True;
202 //=======================================================================
205 //=======================================================================
206 void BinMXCAFDoc_VisMaterialDriver::Paste (const Handle(TDF_Attribute)& theSource,
207 BinObjMgt_Persistent& theTarget,
208 BinObjMgt_SRelocationTable& ) const
210 Handle(XCAFDoc_VisMaterial) aMat = Handle(XCAFDoc_VisMaterial)::DownCast(theSource);
211 theTarget.PutByte (MaterialVersionMajor);
212 theTarget.PutByte (MaterialVersionMinor);
214 theTarget.PutByte (aMat->IsDoubleSided() ? '1' : '0');
215 theTarget.PutByte (alphaModeToChar (aMat->AlphaMode()));
216 theTarget.PutShortReal (aMat->AlphaCutOff());
218 theTarget.PutBoolean (aMat->HasPbrMaterial());
219 if (aMat->HasPbrMaterial())
221 const XCAFDoc_VisMaterialPBR& aPbrMat = aMat->PbrMaterial();
222 writeVec4 (theTarget, aPbrMat.BaseColor);
223 writeVec3 (theTarget, aPbrMat.EmissiveFactor);
224 theTarget.PutShortReal (aPbrMat.Metallic);
225 theTarget.PutShortReal (aPbrMat.Roughness);
226 writeTexture (theTarget, aPbrMat.BaseColorTexture);
227 writeTexture (theTarget, aPbrMat.MetallicRoughnessTexture);
228 writeTexture (theTarget, aPbrMat.EmissiveTexture);
229 writeTexture (theTarget, aPbrMat.OcclusionTexture);
230 writeTexture (theTarget, aPbrMat.NormalTexture);
233 theTarget.PutBoolean (aMat->HasCommonMaterial());
234 if (aMat->HasCommonMaterial())
236 const XCAFDoc_VisMaterialCommon& aComMat = aMat->CommonMaterial();
237 writeVec3 (theTarget, aComMat.AmbientColor);
238 writeVec3 (theTarget, aComMat.DiffuseColor);
239 writeVec3 (theTarget, aComMat.SpecularColor);
240 writeVec3 (theTarget, aComMat.EmissiveColor);
241 theTarget.PutShortReal (aComMat.Shininess);
242 theTarget.PutShortReal (aComMat.Transparency);
243 writeTexture (theTarget, aComMat.DiffuseTexture);