diff --git a/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.cxx b/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.cxx index 2807225b72..a6c035582f 100644 --- a/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.cxx +++ b/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.cxx @@ -163,12 +163,10 @@ Standard_Boolean BinMXCAFDoc_VisMaterialDriver::Paste (const BinObjMgt_Persisten aMat->SetDoubleSided (isDoubleSided == '1'); aMat->SetAlphaMode (alphaModeFromChar (anAlphaMode), anAlphaCutOff); - bool hasPbrMat = false; - theSource.GetBoolean (hasPbrMat); - if (hasPbrMat) + XCAFDoc_VisMaterialPBR aPbrMat; + theSource.GetBoolean (aPbrMat.IsDefined); + if (aPbrMat.IsDefined) { - XCAFDoc_VisMaterialPBR aPbrMat; - aPbrMat.IsDefined = true; readColor (theSource, aPbrMat.BaseColor); readVec3 (theSource, aPbrMat.EmissiveFactor); theSource.GetShortReal (aPbrMat.Metallic); @@ -196,6 +194,22 @@ Standard_Boolean BinMXCAFDoc_VisMaterialDriver::Paste (const BinObjMgt_Persisten readTexture (theSource, aComMat.DiffuseTexture); aMat->SetCommonMaterial (aComMat); } + + if (aVerMaj > MaterialVersionMajor_1 + || (aVerMaj == MaterialVersionMajor_1 + && aVerMin >= MaterialVersionMinor_1)) + { + if (aPbrMat.IsDefined) + { + theSource.GetShortReal (aPbrMat.RefractionIndex); + } + } + + if (aPbrMat.IsDefined) + { + aMat->SetPbrMaterial (aPbrMat); + } + return Standard_True; } @@ -242,4 +256,9 @@ void BinMXCAFDoc_VisMaterialDriver::Paste (const Handle(TDF_Attribute)& theSourc theTarget.PutShortReal (aComMat.Transparency); writeTexture (theTarget, aComMat.DiffuseTexture); } + + if (aMat->HasPbrMaterial()) + { + theTarget.PutShortReal (aMat->PbrMaterial().RefractionIndex); + } } diff --git a/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.hxx b/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.hxx index db98c1bcd9..7931933451 100644 --- a/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.hxx +++ b/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.hxx @@ -27,7 +27,15 @@ class BinMXCAFDoc_VisMaterialDriver : public BinMDF_ADriver DEFINE_STANDARD_RTTIEXT(BinMXCAFDoc_VisMaterialDriver, BinMDF_ADriver) //! Persistence version (major for breaking changes, minor for adding new fields at end). - enum { MaterialVersionMajor = 1, MaterialVersionMinor = 0 }; + enum + { + MaterialVersionMajor_1 = 1, + MaterialVersionMinor_0 = 0, + MaterialVersionMinor_1 = 1, //!< added IOR + + MaterialVersionMajor = MaterialVersionMajor_1, + MaterialVersionMinor = MaterialVersionMinor_1 + }; public: //! Main constructor. diff --git a/src/Graphic3d/Graphic3d_BSDF.cxx b/src/Graphic3d/Graphic3d_BSDF.cxx index 81d53fb9c1..39821b7b19 100644 --- a/src/Graphic3d/Graphic3d_BSDF.cxx +++ b/src/Graphic3d/Graphic3d_BSDF.cxx @@ -204,10 +204,25 @@ Graphic3d_BSDF Graphic3d_BSDF::CreateMetallicRoughness (const Graphic3d_PBRMater const Standard_ShortReal aRougness2 = thePbr.NormalizedRoughness() * thePbr.NormalizedRoughness(); Graphic3d_BSDF aBsdf; - aBsdf.FresnelBase = Graphic3d_Fresnel::CreateSchlick (aDiff * thePbr.Metallic()); - aBsdf.Ks.SetValues (Graphic3d_Vec3 (thePbr.Alpha()), aRougness2); - aBsdf.Kt = Graphic3d_Vec3 (1.0f - thePbr.Alpha()); - aBsdf.Kd = aDiff * (1.0f - thePbr.Metallic()); aBsdf.Le = thePbr.Emission(); + if (thePbr.IOR() > 1.0f + && thePbr.Alpha() < 1.0f + && thePbr.Metallic() <= ShortRealEpsilon()) + { + aBsdf.FresnelCoat = Graphic3d_Fresnel::CreateDielectric (thePbr.IOR()); + aBsdf.Kt = Graphic3d_Vec3(1.0f); + aBsdf.Kc.r() = aBsdf.Kt.r(); + aBsdf.Kc.g() = aBsdf.Kt.g(); + aBsdf.Kc.b() = aBsdf.Kt.b(); + aBsdf.Absorption.SetValues (thePbr.Color().GetRGB(), thePbr.Alpha() * 0.25f); + } + else + { + aBsdf.FresnelBase = Graphic3d_Fresnel::CreateSchlick (aDiff * thePbr.Metallic()); + aBsdf.Ks.SetValues (Graphic3d_Vec3 (thePbr.Alpha()), aRougness2); + aBsdf.Kt = Graphic3d_Vec3 (1.0f - thePbr.Alpha()); + aBsdf.Kd = aDiff * (1.0f - thePbr.Metallic()); + } + return aBsdf; } diff --git a/src/RWGltf/RWGltf_GltfJsonParser.cxx b/src/RWGltf/RWGltf_GltfJsonParser.cxx index a47d72f04c..b43a3ebdc9 100644 --- a/src/RWGltf/RWGltf_GltfJsonParser.cxx +++ b/src/RWGltf/RWGltf_GltfJsonParser.cxx @@ -542,6 +542,7 @@ bool RWGltf_GltfJsonParser::gltfParsePbrMaterial (Handle(RWGltf_MaterialMetallic const RWGltf_JsonValue* aDoubleSidedVal = findObjectMember (theMatNode, "doubleSided"); const RWGltf_JsonValue* anAlphaModeVal = findObjectMember (theMatNode, "alphaMode"); const RWGltf_JsonValue* anAlphaCutoffVal = findObjectMember (theMatNode, "alphaCutoff"); + // TODO ADOBE_materials_thin_transparency extension can be used to read IOR (Index of Refraction for transparent materials) if (aMetalRoughVal == NULL) { return false; diff --git a/src/XCAFDoc/XCAFDoc_VisMaterial.cxx b/src/XCAFDoc/XCAFDoc_VisMaterial.cxx index c865308634..3872c5875d 100644 --- a/src/XCAFDoc/XCAFDoc_VisMaterial.cxx +++ b/src/XCAFDoc/XCAFDoc_VisMaterial.cxx @@ -237,6 +237,8 @@ void XCAFDoc_VisMaterial::FillMaterialAspect (Graphic3d_MaterialAspect& theAspec aPbr.SetMetallic (myPbrMat.Metallic); aPbr.SetRoughness(myPbrMat.Roughness); aPbr.SetEmission (myPbrMat.EmissiveFactor); + aPbr.SetIOR (myPbrMat.RefractionIndex); + theAspect.SetRefractionIndex (myPbrMat.RefractionIndex); theAspect.SetPBRMaterial (aPbr); theAspect.SetBSDF (Graphic3d_BSDF::CreateMetallicRoughness (aPbr)); } diff --git a/src/XCAFDoc/XCAFDoc_VisMaterialPBR.hxx b/src/XCAFDoc/XCAFDoc_VisMaterialPBR.hxx index bbe3d3c7cb..802768c2b9 100644 --- a/src/XCAFDoc/XCAFDoc_VisMaterialPBR.hxx +++ b/src/XCAFDoc/XCAFDoc_VisMaterialPBR.hxx @@ -31,6 +31,7 @@ struct XCAFDoc_VisMaterialPBR Graphic3d_Vec3 EmissiveFactor; //!< emissive color; [0.0, 0.0, 0.0] by default Standard_ShortReal Metallic; //!< metalness (or scale factor to the texture) within range [0.0, 1.0]; 1.0 by default Standard_ShortReal Roughness; //!< roughness (or scale factor to the texture) within range [0.0, 1.0]; 1.0 by default + Standard_ShortReal RefractionIndex; //!< IOR (index of refraction) within range [1.0, 3.0]; 1.5 by default Standard_Boolean IsDefined; //!< defined flag; FALSE by default //! Empty constructor. @@ -39,6 +40,7 @@ struct XCAFDoc_VisMaterialPBR EmissiveFactor (0.0f, 0.0f, 0.0f), Metallic (1.0f), Roughness (1.0f), + RefractionIndex (1.5f), IsDefined (Standard_False) {} //! Compare two materials. @@ -65,7 +67,8 @@ struct XCAFDoc_VisMaterialPBR && theOther.BaseColor == BaseColor && theOther.EmissiveFactor == EmissiveFactor && theOther.Metallic == Metallic - && theOther.Roughness == Roughness; + && theOther.Roughness == Roughness + && theOther.RefractionIndex == RefractionIndex; } }; diff --git a/src/XDEDRAW/XDEDRAW_Colors.cxx b/src/XDEDRAW/XDEDRAW_Colors.cxx index 59b88e168e..b44169c728 100644 --- a/src/XDEDRAW/XDEDRAW_Colors.cxx +++ b/src/XDEDRAW/XDEDRAW_Colors.cxx @@ -878,6 +878,7 @@ static Standard_Integer XGetVisMaterial (Draw_Interpretor& theDI, Standard_Integ const XCAFDoc_VisMaterialPBR& aMatPbr = aMat->PbrMaterial(); theDI << "PBR.BaseColor: " << (Graphic3d_Vec3 )aMatPbr.BaseColor.GetRGB() << "\n"; theDI << "PBR.Transparency: " << (1.0 - aMatPbr.BaseColor.Alpha()) << "\n"; + theDI << "PBR.RefractionIndex: " << aMatPbr.RefractionIndex << "\n"; if (!aMatPbr.BaseColorTexture.IsNull()) { theDI << "PBR.BaseColorTexture: " << aMatPbr.BaseColorTexture->TextureId() << "\n"; @@ -952,6 +953,19 @@ static Standard_Integer XAddVisMaterial (Draw_Interpretor& , Standard_Integer th } aMatPbr.BaseColor.SetAlpha (1.0f - aMatCom.Transparency); } + else if (anArgIter + 1 < theNbArgs + && (anArg == "-refractionindex" || anArg == "-ior")) + { + aMatPbr.RefractionIndex = (Standard_ShortReal )Draw::Atof (theArgVec[anArgIter + 1]); + if (aMatPbr.RefractionIndex < 1.0f || aMatPbr.RefractionIndex > 3.0f) + { + std::cout << "Syntax error at '" << anArg << "'\n"; + return 1; + } + + ++anArgIter; + aMatPbr.IsDefined = true; + } else if (anArg == "-alphaMode" && anArgIter + 2 < theNbArgs && parseNormalizedReal (theArgVec[anArgIter + 2], aRealValue)) @@ -1301,7 +1315,7 @@ void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di) __FILE__, XGetVisMaterial, g); di.Add ("XAddVisMaterial", "Doc Material" - "\n\t\t: [-transparency 0..1] [-alphaMode {Opaque|Mask|Blend|BlendAuto} CutOffValue]" + "\n\t\t: [-transparency 0..1] [-alphaMode {Opaque|Mask|Blend|BlendAuto} CutOffValue] [-refractionIndex 1..3]" "\n\t\t: [-diffuse RGB] [-diffuseTexture ImagePath]" "\n\t\t: [-specular RGB] [-ambient RGB] [-emissive RGB] [-shininess 0..1]" "\n\t\t: [-baseColor RGB] [-baseColorTexture ImagePath]" diff --git a/src/XmlMXCAFDoc/XmlMXCAFDoc_VisMaterialDriver.cxx b/src/XmlMXCAFDoc/XmlMXCAFDoc_VisMaterialDriver.cxx index 687d1c5b2e..1656fe7389 100644 --- a/src/XmlMXCAFDoc/XmlMXCAFDoc_VisMaterialDriver.cxx +++ b/src/XmlMXCAFDoc/XmlMXCAFDoc_VisMaterialDriver.cxx @@ -25,6 +25,7 @@ IMPLEMENT_DOMSTRING(AlphaMode, "alpha_mode") IMPLEMENT_DOMSTRING(AlphaCutOff, "alpha_cutoff") // IMPLEMENT_DOMSTRING(BaseColor, "base_color") +IMPLEMENT_DOMSTRING(RefractionIndex, "ior") IMPLEMENT_DOMSTRING(EmissiveFactor, "emissive_factor") IMPLEMENT_DOMSTRING(Metallic, "metallic") IMPLEMENT_DOMSTRING(Roughness, "roughness") @@ -253,6 +254,7 @@ Standard_Boolean XmlMXCAFDoc_VisMaterialDriver::Paste (const XmlObjMgt_Persisten readVec3 (theSource, ::EmissiveFactor(), aPbrMat.EmissiveFactor); readReal (theSource, ::Metallic(), aPbrMat.Metallic); readReal (theSource, ::Roughness(), aPbrMat.Roughness); + readReal (theSource, ::RefractionIndex(), aPbrMat.RefractionIndex); readTexture (theSource, ::BaseColorTexture(), aPbrMat.BaseColorTexture); readTexture (theSource, ::MetallicRoughnessTexture(), aPbrMat.MetallicRoughnessTexture); readTexture (theSource, ::EmissiveTexture(), aPbrMat.EmissiveTexture); @@ -298,6 +300,7 @@ void XmlMXCAFDoc_VisMaterialDriver::Paste (const Handle(TDF_Attribute)& theSourc writeVec3 (theTarget, ::EmissiveFactor(), aPbrMat.EmissiveFactor); writeReal (theTarget, ::Metallic(), aPbrMat.Metallic); writeReal (theTarget, ::Roughness(), aPbrMat.Roughness); + writeReal (theTarget, ::RefractionIndex(), aPbrMat.RefractionIndex); writeTexture (theTarget, ::BaseColorTexture(), aPbrMat.BaseColorTexture); writeTexture (theTarget, ::MetallicRoughnessTexture(), aPbrMat.MetallicRoughnessTexture); writeTexture (theTarget, ::EmissiveTexture(), aPbrMat.EmissiveTexture); diff --git a/tests/v3d/materials/ior b/tests/v3d/materials/ior new file mode 100644 index 0000000000..a3200c2b3e --- /dev/null +++ b/tests/v3d/materials/ior @@ -0,0 +1,47 @@ +puts "========" +puts "0031284: Visualization - XCAFDoc_VisMaterialPBR lacks Index of Refraction" +puts "========" + +pload MODELING XDE OCAF VISUALIZATION +box b 0.5 1.0 -0.5 3 1 1 +psphere s1 0.5 +psphere s2 0.5 +psphere s3 0.5 +ttranslate s1 1 0 0 +ttranslate s2 2 0 0 +ttranslate s3 3 0 0 +catch { Close D } +XNewDoc D +set l0 [XAddShape D b 0] +set l1 [XAddShape D s1 0] +set l2 [XAddShape D s2 0] +set l3 [XAddShape D s3 0] +XAddVisMaterial D m1 -baseColor GREEN -refractionIndex 1.0 -transparency 0.5 -metallic 0 -roughness 0 +XAddVisMaterial D m2 -baseColor GREEN -refractionIndex 1.3 -transparency 0.5 -metallic 0 -roughness 0 +XAddVisMaterial D m3 -baseColor GREEN -refractionIndex 2.5 -transparency 0.5 -metallic 0 -roughness 0 +XSetVisMaterial D $l1 m1 +XSetVisMaterial D $l2 m2 +XSetVisMaterial D $l3 m3 + +vclear +vinit View1 +vcamera -persp +vfront +XDisplay -dispMode 1 D +vfit +vviewparams -scale 245 -proj -0 -0.75 -0.66 -up 0 -0.66 0.75 -at 2.0 0.84 0.04 +vlight -change 0 -intensity 2.5 +vlight -change 1 -intensity 1.0 +set aCubeMap [locate_data_file Circus_CubeMap_V.png] +vbackground -cubemap $aCubeMap + +vrenderparams -shadingModel PBR -raster +vdump $imagedir/${casename}_pbr.png + +vrenderparams -raytrace -gi 0 -reflections 1 -rayDepth 10 +vfps 200 +vdump $imagedir/${casename}_rt.png + +vrenderparams -raytrace -gi 1 +vfps 200 +vdump $imagedir/${casename}_pt.png