Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-13 08:26:17

0001 // Author: Ilya Khramov
0002 // Copyright (c) 2019 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _Graphic3d_PBRMaterial_HeaderFile
0016 #define _Graphic3d_PBRMaterial_HeaderFile
0017 
0018 #include <Image_PixMap.hxx>
0019 #include <Graphic3d_BSDF.hxx>
0020 #include <Graphic3d_Vec2.hxx>
0021 #include <Graphic3d_Vec3.hxx>
0022 #include <Quantity_Color.hxx>
0023 
0024 //! Class implementing Metallic-Roughness physically based material definition
0025 class Graphic3d_PBRMaterial
0026 {
0027 public:
0028   //! Creates new physically based material in Metallic-Roughness system.
0029   //! 'metallic' parameter is 0 by default.
0030   //! 'roughness' parameter is 1 by default.
0031   //! 'color' parameter is (0, 0, 0) by default.
0032   //! 'alpha' parameter is 1 by default.
0033   //! 'IOR' parameter is 1.5 by default.
0034   //! 'emission' parameter is (0, 0, 0) by default.
0035   Standard_EXPORT Graphic3d_PBRMaterial();
0036 
0037   //! Creates new physically based material in Metallic-Roughness system from Graphic3d_BSDF.
0038   Standard_EXPORT Graphic3d_PBRMaterial(const Graphic3d_BSDF& theBSDF);
0039 
0040   //! Returns material's metallic coefficient in [0, 1] range.
0041   //! 1 for metals and 0 for dielectrics.
0042   //! It is preferable to be exactly 0 or 1. Average values are needed for textures mixing in
0043   //! shader.
0044   Standard_ShortReal Metallic() const { return myMetallic; }
0045 
0046   //! Modifies metallic coefficient of material in [0, 1] range.
0047   Standard_EXPORT void SetMetallic(Standard_ShortReal theMetallic);
0048 
0049   //! Maps roughness from [0, 1] to [MinRoughness, 1] for calculations.
0050   Standard_EXPORT static Standard_ShortReal Roughness(Standard_ShortReal theNormalizedRoughness);
0051 
0052   //! Returns real value of roughness in [MinRoughness, 1] range for calculations.
0053   Standard_ShortReal Roughness() const { return Roughness(myRoughness); }
0054 
0055   //! Returns roughness mapping parameter in [0, 1] range.
0056   //! Roughness is defined in [0, 1] for handful material settings
0057   //! and is mapped to [MinRoughness, 1] for calculations.
0058   Standard_ShortReal NormalizedRoughness() const { return myRoughness; }
0059 
0060   //! Modifies roughness coefficient of material in [0, 1] range.
0061   Standard_EXPORT void SetRoughness(Standard_ShortReal theRoughness);
0062 
0063   //! Returns index of refraction in [1, 3] range.
0064   Standard_ShortReal IOR() const { return myIOR; }
0065 
0066   //! Modifies index of refraction in [1, 3] range.
0067   //! In practice affects only on non-metal materials reflection possibilities.
0068   Standard_EXPORT void SetIOR(Standard_ShortReal theIOR);
0069 
0070   //! Returns albedo color with alpha component of material.
0071   const Quantity_ColorRGBA& Color() const { return myColor; }
0072 
0073   //! Modifies albedo color with alpha component.
0074   Standard_EXPORT void SetColor(const Quantity_ColorRGBA& theColor);
0075 
0076   //! Modifies only albedo color.
0077   Standard_EXPORT void SetColor(const Quantity_Color& theColor);
0078 
0079   //! Returns alpha component in range [0, 1].
0080   Standard_ShortReal Alpha() const { return myColor.Alpha(); };
0081 
0082   //! Modifies alpha component.
0083   Standard_EXPORT void SetAlpha(Standard_ShortReal theAlpha);
0084 
0085   //! Returns light intensity emitted by material.
0086   //! Values are greater or equal 0.
0087   Graphic3d_Vec3 Emission() const { return myEmission; }
0088 
0089   //! Modifies light intensity emitted by material.
0090   Standard_EXPORT void SetEmission(const Graphic3d_Vec3& theEmission);
0091 
0092   //! Generates material in Metallic-Roughness system from Graphic3d_BSDF.
0093   Standard_EXPORT void SetBSDF(const Graphic3d_BSDF& theBSDF);
0094 
0095 public:
0096   //! PBR materials comparison operator.
0097   Standard_Boolean operator==(const Graphic3d_PBRMaterial& theOther) const
0098   {
0099     return (myMetallic == theOther.myMetallic) && (myRoughness == theOther.myRoughness)
0100            && (myIOR == theOther.myIOR) && (myColor == theOther.myColor)
0101            && (myEmission == theOther.myEmission);
0102   }
0103 
0104 public:
0105   //! Generates 2D look up table of scale and bias for fresnell zero coefficient.
0106   //! It is needed for calculation reflectance part of environment lighting.
0107   //! @param[out]  theLUT table storage (must be Image_Format_RGF).
0108   //! @param[in]  theNbIntegralSamples number of importance samples in hemisphere integral
0109   //! calculation for every table item.
0110   Standard_EXPORT static void GenerateEnvLUT(const Handle(Image_PixMap)& theLUT,
0111                                              unsigned int theNbIntegralSamples = 1024);
0112 
0113   //! Compute material roughness from common material (specular color + shininess).
0114   //! @param[in] theSpecular  specular color
0115   //! @param[in] theShiness   normalized shininess coefficient within [0..1] range
0116   //! @return roughness within [0..1] range
0117   Standard_EXPORT static Standard_ShortReal RoughnessFromSpecular(const Quantity_Color& theSpecular,
0118                                                                   const Standard_Real   theShiness);
0119 
0120   //! Compute material metallicity from common material (specular color).
0121   //! @param[in] theSpecular  specular color
0122   //! @return metallicity within [0..1] range
0123   static Standard_ShortReal MetallicFromSpecular(const Quantity_Color& theSpecular)
0124   {
0125     return ((Graphic3d_Vec3)theSpecular).maxComp();
0126   }
0127 
0128 public:
0129   //! Roughness cannot be 0 in real calculations, so it returns minimal achievable level of
0130   //! roughness in practice
0131   static Standard_ShortReal MinRoughness() { return 0.01f; }
0132 
0133 public:
0134   //! Shows how much times less samples can be used in certain roughness value specular IBL map
0135   //! generation in compare with samples number for map with roughness of 1. Specular IBL maps with
0136   //! less roughness values have higher resolution but require less samples for the same quality of
0137   //! baking. So that reducing samples number is good strategy to improve performance of baking. The
0138   //! samples number for specular IBL map with roughness of 1 (the maximum possible samples number)
0139   //! is expected to be defined as baking parameter. Samples number for other roughness values can
0140   //! be calculated by multiplication origin samples number by this factor.
0141   //! @param theProbability value from 0 to 1 controlling strength of samples reducing.
0142   //! Bigger values result in slower reduction to provide better quality but worse performance.
0143   //! Value of 1 doesn't affect at all so that 1 will be returned (it can be used to disable
0144   //! reduction strategy).
0145   //! @param theRoughness roughness value of current generated specular IBL map (from 0 to 1).
0146   //! @return factor to calculate number of samples for current specular IBL map baking.
0147   //! Be aware! It has no obligation to return 1 in case of roughness of 1.
0148   //! Be aware! It produces poor quality with small number of origin samples. In that case it is
0149   //! recommended to be disabled.
0150   Standard_EXPORT static Standard_ShortReal SpecIBLMapSamplesFactor(
0151     Standard_ShortReal theProbability,
0152     Standard_ShortReal theRoughness);
0153 
0154   //! Dumps the content of me into the stream
0155   Standard_EXPORT void DumpJson(Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0156 
0157 private:
0158   //! Calculates geometry factor of Cook-Torrance BRDF using Smith formula.
0159   static Standard_ShortReal lutGenGeometryFactor(Standard_ShortReal theCosL,
0160                                                  Standard_ShortReal theCosV,
0161                                                  Standard_ShortReal theRoughness);
0162 
0163   //! Generates quasi-random point from Hammersley set.
0164   //! @param theNumber number of point
0165   //! @param theCount size of Hammersley set
0166   static Graphic3d_Vec2 lutGenHammersley(unsigned int theNumber, unsigned int theCount);
0167 
0168   //! Generates only cosine theta of direction in spherical coordinates system
0169   //! according to micro facet distribution function from Cook-Torrance BRDF.
0170   static Standard_ShortReal lutGenImportanceSampleCosTheta(
0171     Standard_ShortReal theHammerslayPointComponent,
0172     Standard_ShortReal theRoughness);
0173 
0174   //! Generates direction using point from Hammersley set
0175   //! according to micro facet distribution function from Cook-Torrance BRDF.
0176   static Graphic3d_Vec3 lutGenImportanceSample(const Graphic3d_Vec2& theHammerslayPoint,
0177                                                Standard_ShortReal    theRoughness);
0178 
0179   //! Generates vector using cosine of angle between up vector (normal in hemisphere)
0180   //! and desired vector.
0181   //! x component for resulting vector will be zero.
0182   static Graphic3d_Vec3 lutGenView(Standard_ShortReal theCosV);
0183 
0184   //! Returns reflected vector according axis.
0185   //! @param theVector vector is needed to be reflected.
0186   //! @param theAxis axis of reflection.
0187   static Graphic3d_Vec3 lutGenReflect(const Graphic3d_Vec3& theVector,
0188                                       const Graphic3d_Vec3& theAxis);
0189 
0190 private:
0191   Quantity_ColorRGBA myColor;     //!< albedo color with alpha component [0, 1]
0192   Standard_ShortReal myMetallic;  //!< metallic coefficient of material [0, 1]
0193   Standard_ShortReal myRoughness; //!< roughness coefficient of material [0, 1]
0194   Graphic3d_Vec3     myEmission;  //!< light intensity emitted by material [>= 0]
0195   Standard_ShortReal myIOR;       //!< index of refraction [1, 3]
0196 };
0197 
0198 #endif // _Graphic3d_PBRMaterial_HeaderFile