1 // Author: Ilya Khramov
2 // Copyright (c) 2019 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _Graphic3d_PBRMaterial_HeaderFile
16 #define _Graphic3d_PBRMaterial_HeaderFile
18 #include <Image_PixMap.hxx>
19 #include <Graphic3d_BSDF.hxx>
20 #include <Graphic3d_Vec2.hxx>
21 #include <Graphic3d_Vec3.hxx>
22 #include <Graphic3d_Vec4.hxx>
23 #include <Quantity_Color.hxx>
25 //! Class implementing Metallic-Roughness physically based material definition
26 class Graphic3d_PBRMaterial
30 //! Creates new physically based material in Metallic-Roughness system.
31 //! 'metallic' parameter is 0 by default.
32 //! 'roughness' parameter is 1 by default.
33 //! 'color' parameter is (0, 0, 0) by default.
34 //! 'alpha' parameter is 1 by default.
35 //! 'IOR' parameter is 1.5 by default.
36 //! 'emission' parameter is (0, 0, 0) by default.
37 Standard_EXPORT Graphic3d_PBRMaterial();
39 //! Creates new physically based material in Metallic-Roughness system from Graphic3d_BSDF.
40 Standard_EXPORT Graphic3d_PBRMaterial (const Graphic3d_BSDF& theBSDF);
42 //! Returns material's metallic coefficient in [0, 1] range.
43 //! 1 for metals and 0 for dielectrics.
44 //! It is preferable to be exactly 0 or 1. Average values are needed for textures mixing in shader.
45 Standard_ShortReal Metallic() const { return myMetallic; }
47 //! Modifies metallic coefficient of material in [0, 1] range.
48 Standard_EXPORT void SetMetallic (Standard_ShortReal theMetallic);
50 //! Maps roughness from [0, 1] to [MinRoughness, 1] for calculations.
51 Standard_EXPORT static Standard_ShortReal Roughness(Standard_ShortReal theNormalizedRoughness);
53 //! Returns real value of roughness in [MinRoughness, 1] range for calculations.
54 Standard_ShortReal Roughness() const { return Roughness(myRoughness); }
56 //! Returns roughness mapping parameter in [0, 1] range.
57 //! Roughness is defined in [0, 1] for handful material settings
58 //! and is mapped to [MinRoughness, 1] for calculations.
59 Standard_ShortReal NormalizedRoughness() const { return myRoughness; }
61 //! Modifies roughness coefficient of material in [0, 1] range.
62 Standard_EXPORT void SetRoughness (Standard_ShortReal theRoughness);
64 //! Returns index of refraction in [1, 3] range.
65 Standard_ShortReal IOR() const { return myIOR; }
67 //! Modifies index of refraction in [1, 3] range.
68 //! In practice affects only on non-metal materials reflection possibilities.
69 Standard_EXPORT void SetIOR (Standard_ShortReal theIOR);
71 //! Returns albedo color with alpha component of material.
72 const Quantity_ColorRGBA& Color() const { return myColor; }
74 //! Modifies albedo color with alpha component.
75 Standard_EXPORT void SetColor (const Quantity_ColorRGBA& theColor);
77 //! Modifies only albedo color.
78 Standard_EXPORT void SetColor (const Quantity_Color& theColor);
80 //! Returns alpha component in range [0, 1].
81 Standard_ShortReal Alpha() const { return myColor.Alpha(); };
83 //! Modifies alpha component.
84 Standard_EXPORT void SetAlpha (Standard_ShortReal theAlpha);
86 //! Returns light intensity emitted by material.
87 //! Values are greater or equal 0.
88 Graphic3d_Vec3 Emission() const { return myEmission; }
90 //! Modifies light intensity emitted by material.
91 Standard_EXPORT void SetEmission (const Graphic3d_Vec3& theEmission);
93 //! Generates material in Metallic-Roughness system from Graphic3d_BSDF.
94 Standard_EXPORT void SetBSDF (const Graphic3d_BSDF& theBSDF);
98 //! PBR materials comparison operator.
99 Standard_Boolean operator== (const Graphic3d_PBRMaterial& theOther) const
101 return (myMetallic == theOther.myMetallic)
102 && (myRoughness == theOther.myRoughness)
103 && (myIOR == theOther.myIOR)
104 && (myColor == theOther.myColor)
105 && (myEmission == theOther.myEmission);
110 //! Generates 2D look up table of scale and bias for fresnell zero coefficient.
111 //! It is needed for calculation reflectance part of environment lighting.
112 //! @param [out] theLUT table storage (must be Image_Format_RGF).
113 //! @param [in] theIntegralSamplesCount number of importance samples in hemisphere integral calculation for every table item.
114 Standard_EXPORT static void GenerateEnvLUT (const Handle(Image_PixMap)& theLUT,
115 unsigned int theNbIntegralSamples = 1024);
117 //! Compute material roughness from common material (specular color + shininess).
118 //! @param theSpecular [in] specular color
119 //! @param theShiness [in] normalized shininess coefficient within [0..1] range
120 //! @return roughness within [0..1] range
121 Standard_EXPORT static Standard_ShortReal RoughnessFromSpecular (const Quantity_Color& theSpecular,
122 const Standard_Real theShiness);
124 //! Compute material metallicity from common material (specular color).
125 //! @param theSpecular [in] specular color
126 //! @return metallicity within [0..1] range
127 static Standard_ShortReal MetallicFromSpecular (const Quantity_Color& theSpecular)
129 return ((Graphic3d_Vec3 )theSpecular).maxComp();
134 //! Roughness cannot be 0 in real calculations, so it returns minimal achievable level of roughness in practice
135 static Standard_ShortReal MinRoughness() { return 0.01f; }
139 //! Shows how much times less samples can be used in certain roughness value specular IBL map generation
140 //! in compare with samples number for map with roughness of 1.
141 //! Specular IBL maps with less roughness values have higher resolution but require less samples for the same quality of baking.
142 //! So that reducing samples number is good strategy to improve performance of baking.
143 //! The samples number for specular IBL map with roughness of 1 (the maximum possible samples number) is expected to be defined as baking parameter.
144 //! Samples number for other roughness values can be calculated by multiplication origin samples number by this factor.
145 //! @param theProbability value from 0 to 1 controlling strength of samples reducing.
146 //! Bigger values result in slower reduction to provide better quality but worse performance.
147 //! Value of 1 doesn't affect at all so that 1 will be returned (it can be used to disable reduction strategy).
148 //! @param theRoughness roughness value of current generated specular IBL map (from 0 to 1).
149 //! @return factor to calculate number of samples for current specular IBL map baking.
150 //! Be aware! It has no obligation to return 1 in case of roughness of 1.
151 //! Be aware! It produces poor quality with small number of origin samples. In that case it is recommended to be disabled.
152 Standard_EXPORT static Standard_ShortReal SpecIBLMapSamplesFactor (Standard_ShortReal theProbability,
153 Standard_ShortReal theRoughness);
157 //! Calculates geometry factor of Cook-Torrance BRDF using Smith formula.
158 static Standard_ShortReal lutGenGeometryFactor (Standard_ShortReal theCosL,
159 Standard_ShortReal theCosV,
160 Standard_ShortReal theRoughness);
162 //! Generates quasi-random point from Hammersley set.
163 //! @param theNumber number of point
164 //! @param theCount size of Hammersley set
165 static Graphic3d_Vec2 lutGenHammersley (unsigned int theNumber, unsigned int theCount);
167 //! Generates only cosine theta of direction in spherical coordinates system
168 //! according to micro facet distribution function from Cook-Torrance BRDF.
169 static Standard_ShortReal lutGenImportanceSampleCosTheta (Standard_ShortReal theHammerslayPointComponent,
170 Standard_ShortReal theRoughness);
172 //! Generates direction using point from Hammersley set
173 //! according to micro facet distribution function from Cook-Torrance BRDF.
174 static Graphic3d_Vec3 lutGenImportanceSample (const Graphic3d_Vec2 &theHammerslayPoint,
175 Standard_ShortReal theRoughness);
177 //! Generates vector using cosine of angle between up vector (normal in hemisphere)
178 //! and desired vector.
179 //! x component for resulting vector will be zero.
180 static Graphic3d_Vec3 lutGenView (Standard_ShortReal theCosV);
182 //! Returns reflected vector according axis.
183 //! @param theVector vector is needed to be reflected.
184 //! @param theAxis axis of reflection.
185 static Graphic3d_Vec3 lutGenReflect (const Graphic3d_Vec3 &theVector,
186 const Graphic3d_Vec3 &theAxis);
190 Quantity_ColorRGBA myColor; //!< albedo color with alpha component [0, 1]
191 Standard_ShortReal myMetallic; //!< metallic coefficient of material [0, 1]
192 Standard_ShortReal myRoughness; //!< roughness coefficient of material [0, 1]
193 Graphic3d_Vec3 myEmission; //!< light intensity emitted by material [>= 0]
194 Standard_ShortReal myIOR; //!< index of refraction [1, 3]
198 #endif // _Graphic3d_PBRMaterial_HeaderFile