浏览代码

Add roughness to PBR sheen

Popov72 5 年之前
父节点
当前提交
108d6cfbc5

+ 1 - 0
inspector/src/components/actionTabs/tabs/propertyGrids/materials/pbrMaterialPropertyGridComponent.tsx

@@ -208,6 +208,7 @@ export class PBRMaterialPropertyGridComponent extends React.Component<IPBRMateri
                             <SliderLineComponent label="Intensity" target={material.sheen} propertyName="intensity" minimum={0} maximum={1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             <Color3LineComponent label="Color" target={material.sheen} propertyName="color" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             <TextureLinkLineComponent label="Texture" texture={material.sheen.texture} material={material} onSelectionChangedObservable={this.props.onSelectionChangedObservable} onDebugSelectionChangeObservable={this._onDebugSelectionChangeObservable} />
+                            <SliderLineComponent label="Roughness" target={material.sheen} propertyName="roughness" minimum={0} maximum={1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                         </div>
                     }
                 </LineContainerComponent>

+ 1 - 0
src/Materials/PBR/pbrBaseMaterial.ts

@@ -214,6 +214,7 @@ export class PBRMaterialDefines extends MaterialDefines
     public SHEEN_TEXTURE = false;
     public SHEEN_TEXTUREDIRECTUV = 0;
     public SHEEN_LINKWITHALBEDO = false;
+    public SHEEN_ROUGHNESS = false;
 
     public SUBSURFACE = false;
 

+ 19 - 1
src/Materials/PBR/pbrSheenConfiguration.ts

@@ -17,6 +17,7 @@ export interface IMaterialSheenDefines {
     SHEEN_TEXTURE: boolean;
     SHEEN_TEXTUREDIRECTUV: number;
     SHEEN_LINKWITHALBEDO: boolean;
+    SHEEN_ROUGHNESS: boolean;
 
     /** @hidden */
     _areTexturesDirty: boolean;
@@ -65,6 +66,16 @@ export class PBRSheenConfiguration {
     @expandToProperty("_markAllSubMeshesAsTexturesDirty")
     public texture: Nullable<BaseTexture> = null;
 
+    private _roughness = 0;
+    /**
+     * Defines the sheen roughness.
+     * It is not taken into account if linkSheenWithAlbedo is true.
+     * To stay backward compatible, material roughness is used instead if sheen roughness = 0
+     */
+    @serialize()
+    @expandToProperty("_markAllSubMeshesAsTexturesDirty")
+    public roughness = 0.0;
+
     /** @hidden */
     private _internalMarkAllSubMeshesAsTexturesDirty: () => void;
 
@@ -110,6 +121,7 @@ export class PBRSheenConfiguration {
         if (this._isEnabled) {
             defines.SHEEN = this._isEnabled;
             defines.SHEEN_LINKWITHALBEDO = this._linkSheenWithAlbedo;
+            defines.SHEEN_ROUGHNESS = this._roughness !== 0;
 
             if (defines._areTexturesDirty) {
                 if (scene.texturesEnabled) {
@@ -125,6 +137,7 @@ export class PBRSheenConfiguration {
             defines.SHEEN = false;
             defines.SHEEN_TEXTURE = false;
             defines.SHEEN_LINKWITHALBEDO = false;
+            defines.SHEEN_ROUGHNESS = false;
         }
     }
 
@@ -147,6 +160,10 @@ export class PBRSheenConfiguration {
                 this.color.g,
                 this.color.b,
                 this.intensity);
+
+            if (this._roughness !== 0) {
+                uniformBuffer.updateFloat("vSheenRoughness", this._roughness);
+            }
         }
 
         // Textures
@@ -229,7 +246,7 @@ export class PBRSheenConfiguration {
      * @param uniforms defines the current uniform list.
      */
     public static AddUniforms(uniforms: string[]): void {
-        uniforms.push("vSheenColor", "vSheenInfos", "sheenMatrix");
+        uniforms.push("vSheenColor", "vSheenRoughness", "vSheenInfos", "sheenMatrix");
     }
 
     /**
@@ -238,6 +255,7 @@ export class PBRSheenConfiguration {
      */
     public static PrepareUniformBuffer(uniformBuffer: UniformBuffer): void {
         uniformBuffer.addUniform("vSheenColor", 4);
+        uniformBuffer.addUniform("vSheenRoughness", 1);
         uniformBuffer.addUniform("vSheenInfos", 2);
         uniformBuffer.addUniform("sheenMatrix", 16);
     }

+ 6 - 0
src/Shaders/ShadersInclude/lightFragment.fx

@@ -76,6 +76,12 @@
                 #ifdef SHEEN_LINKWITHALBEDO
                     // BE Carefull: Sheen intensity is replacing the roughness value.
                     preInfo.roughness = sheenIntensity;
+                #else
+                    #ifdef HEMILIGHT{X}
+                        preInfo.roughness = sheenRoughness;
+                    #else
+                        preInfo.roughness = adjustRoughnessFromLightProperties(sheenRoughness, light{X}.vLightSpecular.a, preInfo.lightDistance);
+                    #endif
                 #endif
                 info.sheen = computeSheenLighting(preInfo, normalW, sheenColor, specularEnvironmentR90, AARoughnessFactors.x, light{X}.vLightDiffuse.rgb);
             #endif

+ 3 - 0
src/Shaders/ShadersInclude/pbrFragmentDeclaration.fx

@@ -100,6 +100,9 @@ uniform mat4 view;
 // Sheen
 #ifdef SHEEN
     uniform vec4 vSheenColor;
+    #ifdef SHEEN_ROUGHNESS
+        uniform float vSheenRoughness;
+    #endif
 
     #ifdef SHEEN_TEXTURE
         uniform vec2 vSheenInfos;

+ 1 - 0
src/Shaders/ShadersInclude/pbrUboDeclaration.fx

@@ -50,6 +50,7 @@ uniform Material
     uniform mat4 anisotropyMatrix;
 
     uniform vec4 vSheenColor;
+    uniform float vSheenRoughness;
     uniform vec2 vSheenInfos;
     uniform mat4 sheenMatrix;
 

+ 13 - 2
src/Shaders/pbr.fragment.fx

@@ -205,6 +205,7 @@ void main(void) {
         vec3 baseColor = surfaceAlbedo;
 
         #ifdef REFLECTANCE
+            // *** NOT USED ANYMORE ***
             // Following Frostbite Remapping,
             // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf page 115
             // vec3 f0 = 0.16 * reflectance * reflectance * (1.0 - metallic) + baseColor * metallic;
@@ -551,7 +552,12 @@ void main(void) {
             #ifdef SHEEN_TEXTURE
                 sheenColor.rgb *= toLinearSpace(sheenMapData.rgb);
             #endif
-            float sheenRoughness = roughness;
+            
+            #ifdef SHEEN_ROUGHNESS
+                float sheenRoughness = vSheenRoughness;
+            #else
+                float sheenRoughness = roughness;
+            #endif
 
             // Sheen Lobe Layering.
             sheenIntensity *= (1. - reflectance);
@@ -887,7 +893,12 @@ void main(void) {
 
     // _____________________________ Sheen Environment Oclusion __________________________
     #if defined(SHEEN) && defined(REFLECTION)
-        vec3 sheenEnvironmentReflectance = getSheenReflectanceFromBRDFLookup(sheenColor, environmentBrdf);
+        #ifdef SHEEN_ROUGHNESS
+            vec3 environmentSheenBrdf = getBRDFLookup(NdotV, sheenRoughness);
+        #else
+            vec3 environmentSheenBrdf = environmentBrdf;
+        #endif
+        vec3 sheenEnvironmentReflectance = getSheenReflectanceFromBRDFLookup(sheenColor, environmentSheenBrdf);
 
         #ifdef RADIANCEOCCLUSION
             sheenEnvironmentReflectance *= seo;