Jelajahi Sumber

Cleaning of the code

Popov72 5 tahun lalu
induk
melakukan
c01d9d0a61

+ 12 - 2
inspector/src/components/actionTabs/tabs/propertyGrids/materials/pbrMaterialPropertyGridComponent.tsx

@@ -128,6 +128,14 @@ export class PBRMaterialPropertyGridComponent extends React.Component<IPBRMateri
             { label: "Alpha", value: 87 },
         ];
 
+        (material.sheen as any)._useRoughness = (material.sheen as any)._useRoughness ?? material.sheen.roughness !== null;
+        material.sheen.roughness = material.sheen.roughness ?? (material.sheen as any)._saveRoughness ?? 0;
+
+        if (!(material.sheen as any)._useRoughness) {
+            (material.sheen as any)._saveRoughness = material.sheen.roughness;
+            material.sheen.roughness = null;
+        }
+
         return (
             <div className="pane">
                 <CommonMaterialPropertyGridComponent globalState={this.props.globalState} lockObject={this.props.lockObject} material={material} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
@@ -208,8 +216,10 @@ 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} />
-                            <CheckBoxLineComponent label="Softer look" target={material.sheen} propertyName="softer" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
+                            <CheckBoxLineComponent label="Use roughness" target={material.sheen} propertyName="_useRoughness" />
+                            { (material.sheen as any)._useRoughness &&
+                                <SliderLineComponent label="Roughness" target={material.sheen} propertyName="roughness" minimum={0} maximum={1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
+                            }
                             <CheckBoxLineComponent label="Albedo scaling" target={material.sheen} propertyName="albedoScaling" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                         </div>
                     }

+ 2 - 0
loaders/src/glTF/2.0/Extensions/KHR_materials_sheen.ts

@@ -91,6 +91,8 @@ export class KHR_materials_sheen implements IGLTFLoaderExtension {
             babylonMaterial.sheen.roughness = properties.roughnessFactor;
         }
 
+        babylonMaterial.sheen.albedoScaling = true;
+
         return Promise.all(promises).then(() => { });
     }
 }

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

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

+ 10 - 13
src/Materials/PBR/pbrSheenConfiguration.ts

@@ -18,7 +18,6 @@ export interface IMaterialSheenDefines {
     SHEEN_TEXTUREDIRECTUV: number;
     SHEEN_LINKWITHALBEDO: boolean;
     SHEEN_ROUGHNESS: boolean;
-    SHEEN_SOFTER: boolean;
     SHEEN_ALBEDOSCALING: boolean;
 
     /** @hidden */
@@ -68,21 +67,21 @@ export class PBRSheenConfiguration {
     @expandToProperty("_markAllSubMeshesAsTexturesDirty")
     public texture: Nullable<BaseTexture> = null;
 
-    private _roughness = 0;
+    private _roughness: Nullable<number> = null;
     /**
      * 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
+     * To stay backward compatible, material roughness is used instead if sheen roughness = null
      */
     @serialize()
     @expandToProperty("_markAllSubMeshesAsTexturesDirty")
-    public roughness = 0.0;
-
-    private _softer = false;
-    @serialize()
-    @expandToProperty("_markAllSubMeshesAsTexturesDirty")
-    public softer = false;
+    public roughness: Nullable<number> = null;
 
+    /**
+     * If true, the sheen effect is layered above the base BRDF with the albedo-scaling technique.
+     * It allows the strength of the sheen effect to not depend on the base color of the material,
+     * making it easier to setup and tweak the effect
+     */
     private _albedoScaling = false;
     @serialize()
     @expandToProperty("_markAllSubMeshesAsTexturesDirty")
@@ -133,8 +132,7 @@ export class PBRSheenConfiguration {
         if (this._isEnabled) {
             defines.SHEEN = this._isEnabled;
             defines.SHEEN_LINKWITHALBEDO = this._linkSheenWithAlbedo;
-            defines.SHEEN_ROUGHNESS = this._roughness !== 0;
-            defines.SHEEN_SOFTER = this._softer;
+            defines.SHEEN_ROUGHNESS = this._roughness !== null;
             defines.SHEEN_ALBEDOSCALING = this._albedoScaling;
 
             if (defines._areTexturesDirty) {
@@ -152,7 +150,6 @@ export class PBRSheenConfiguration {
             defines.SHEEN_TEXTURE = false;
             defines.SHEEN_LINKWITHALBEDO = false;
             defines.SHEEN_ROUGHNESS = false;
-            defines.SHEEN_SOFTER = false;
             defines.SHEEN_ALBEDOSCALING = false;
         }
     }
@@ -177,7 +174,7 @@ export class PBRSheenConfiguration {
                 this.color.b,
                 this.intensity);
 
-            if (this._roughness !== 0) {
+            if (this._roughness !== null) {
                 uniformBuffer.updateFloat("vSheenRoughness", this._roughness);
             }
         }

+ 4 - 0
src/Shaders/ShadersInclude/pbrBRDFFunctions.fx

@@ -39,6 +39,7 @@
     }
 #endif
 
+/* NOT USED
 #if defined(SHEEN) && defined(SHEEN_SOFTER)
 // Approximation of (integral on hemisphere)[f_sheen*cos(theta)*dtheta*dphi]
 float getBRDFLookupCharlieSheen(float NdotV, float perceptualRoughness)
@@ -48,6 +49,7 @@ float getBRDFLookupCharlieSheen(float NdotV, float perceptualRoughness)
     return 0.65584461 * c3 + 1.0 / (4.16526551 + exp(-7.97291361*perceptualRoughness+6.33516894));
 }
 #endif
+*/
 
 #if !defined(ENVIRONMENTBRDF) || defined(REFLECTIONMAP_SKYBOX) || defined(ALPHAFRESNEL)
     vec3 getReflectanceFromAnalyticalBRDFLookup_Jones(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
@@ -257,6 +259,7 @@ float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
         return 1. / (4. * (NdotL + NdotV - NdotL * NdotV));
     }
 
+    /* NOT USED
     #ifdef SHEEN_SOFTER
         // http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
         float l(float x, float alphaG)
@@ -281,6 +284,7 @@ float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
             return G / (4.0 * NdotV * NdotL);
         }
     #endif
+    */
 #endif
 
 // ______________________________________________________________________

+ 3 - 3
src/Shaders/ShadersInclude/pbrDirectLightingFunctions.fx

@@ -143,11 +143,11 @@ vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler, m
         // vec3 fresnel = fresnelSchlickGGX(info.VdotH, reflectance0, reflectance90);
         float fresnel = 1.;
         float distribution = normalDistributionFunction_CharlieSheen(NdotH, alphaG);
-        #ifdef SHEEN_SOFTER
+        /*#ifdef SHEEN_SOFTER
             float visibility = visibility_CharlieSheen(info.NdotL, info.NdotV, alphaG);
-        #else
+        #else */
             float visibility = visibility_Ashikhmin(info.NdotL, info.NdotV);
-        #endif
+        /* #endif */
 
         float sheenTerm = fresnel * distribution * visibility;
         return sheenTerm * info.attenuation * info.NdotL * lightColor;

+ 54 - 50
src/Shaders/pbr.fragment.fx

@@ -894,16 +894,16 @@ void main(void) {
     #endif
 
     // _____________________________ Sheen Environment Oclusion __________________________
-    #if defined(SHEEN) && defined(REFLECTION)
-        #ifdef SHEEN_SOFTER
+    #if defined(SHEEN) && defined(ENVIRONMENTBRDF)
+        /*#ifdef SHEEN_SOFTER
             vec3 environmentSheenBrdf = vec3(0., 0., getBRDFLookupCharlieSheen(NdotV, sheenRoughness));
-        #else
+        #else*/
             #ifdef SHEEN_ROUGHNESS
                 vec3 environmentSheenBrdf = getBRDFLookup(NdotV, sheenRoughness);
             #else
                 vec3 environmentSheenBrdf = environmentBrdf;
             #endif
-        #endif
+        /*#endif*/
         vec3 sheenEnvironmentReflectance = getSheenReflectanceFromBRDFLookup(sheenColor, environmentSheenBrdf);
 
         #ifdef RADIANCEOCCLUSION
@@ -922,15 +922,7 @@ void main(void) {
             // Sheen Lobe Layering.
             // environmentSheenBrdf.b is (integral on hemisphere)[f_sheen*cos(theta)*dtheta*dphi], which happens to also be the directional albedo needed for albedo scaling.
             // See section 6.2.3 in https://dassaultsystemes-technology.github.io/EnterprisePBRShadingModel/spec-2021x.md.html#components/sheen
-            float albedoScaling = 1.0 - sheenIntensity * max(max(sheenColor.r, sheenColor.g), sheenColor.b) * environmentSheenBrdf.b;
-            #ifdef REFLECTION
-                environmentIrradiance *= albedoScaling;
-                specularEnvironmentReflectance *= albedoScaling;
-            #endif
-            #ifdef SPECULARTERM
-                specularBase *= albedoScaling;
-            #endif
-            diffuseBase *= albedoScaling;
+            float sheenAlbedoScaling = 1.0 - sheenIntensity * max(max(sheenColor.r, sheenColor.g), sheenColor.b) * environmentSheenBrdf.b;
         #endif
     #endif
 
@@ -967,10 +959,10 @@ void main(void) {
 
             #ifdef REFLECTION
                 environmentIrradiance *= absorption;
+            #endif
 
-                #ifdef SHEEN
-                    sheenEnvironmentReflectance *= absorption;
-                #endif
+            #if defined(SHEEN) && defined(ENVIRONMENTBRDF)
+                sheenEnvironmentReflectance *= absorption;
             #endif
 
             specularEnvironmentReflectance *= absorption;
@@ -984,10 +976,10 @@ void main(void) {
 
         #ifdef REFLECTION
             environmentIrradiance *= conservationFactor;
+        #endif
 
-            #ifdef SHEEN
-                sheenEnvironmentReflectance *= conservationFactor;
-            #endif
+        #if defined(SHEEN) && defined(ENVIRONMENTBRDF)
+            sheenEnvironmentReflectance *= conservationFactor;
         #endif
 
         specularEnvironmentReflectance *= conservationFactor;
@@ -1072,6 +1064,10 @@ void main(void) {
         #endif
     #endif
 
+    #if defined(SHEEN) && defined(SHEEN_ALBEDOSCALING) && defined(ENVIRONMENTBRDF)
+        surfaceAlbedo.rgb = sheenAlbedoScaling * surfaceAlbedo.rgb;
+    #endif
+
     // _____________________________ Irradiance ______________________________________
     #ifdef REFLECTION
         vec3 finalIrradiance = environmentIrradiance;
@@ -1091,6 +1087,10 @@ void main(void) {
         #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
             finalSpecularScaled *= energyConservationFactor;
         #endif
+
+        #if defined(SHEEN) && defined(ENVIRONMENTBRDF) && defined(SHEEN_ALBEDOSCALING)
+            finalSpecularScaled *= sheenAlbedoScaling;
+        #endif
     #endif
 
     // _____________________________ Radiance ________________________________________
@@ -1103,6 +1103,10 @@ void main(void) {
         #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
             finalRadianceScaled *= energyConservationFactor;
         #endif
+
+        #if defined(SHEEN) && defined(ENVIRONMENTBRDF) && defined(SHEEN_ALBEDOSCALING)
+            finalRadianceScaled *= sheenAlbedoScaling;
+        #endif
     #endif
 
     // _____________________________ Refraction ______________________________________
@@ -1111,6 +1115,27 @@ void main(void) {
         finalRefraction *= refractionTransmittance;
     #endif
 
+    // ________________________________ Sheen ________________________________________
+    #ifdef SHEEN
+        vec3 finalSheen = sheenBase * sheenColor;
+        finalSheen = max(finalSheen, 0.0);
+
+        vec3 finalSheenScaled = finalSheen * vLightingIntensity.x * vLightingIntensity.w;
+        // #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
+            // The sheen does not use the same BRDF so not energy conservation is possible
+            // Should be less a problem as it is usually not metallic
+            // finalSheenScaled *= energyConservationFactor;
+        // #endif
+        
+        #if defined(REFLECTION) && defined(ENVIRONMENTBRDF)
+            vec3 finalSheenRadiance = environmentSheenRadiance.rgb;
+            finalSheenRadiance *= sheenEnvironmentReflectance;
+
+            // Full value needed for alpha. 
+            vec3 finalSheenRadianceScaled = finalSheenRadiance * vLightingIntensity.z;
+        #endif
+    #endif
+
     // _____________________________ Clear Coat _______________________________________
     #ifdef CLEARCOAT
         vec3 finalClearCoat = clearCoatBase;
@@ -1139,27 +1164,6 @@ void main(void) {
         #endif
     #endif
 
-    // ________________________________ Sheen ________________________________________
-    #ifdef SHEEN
-        vec3 finalSheen = sheenBase * sheenColor;
-        finalSheen = max(finalSheen, 0.0);
-
-        vec3 finalSheenScaled = finalSheen * vLightingIntensity.x * vLightingIntensity.w;
-        // #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
-            // The sheen does not use the same BRDF so not energy conservation is possible
-            // Should be less a problem as it is usually not metallic
-            // finalSheenScaled *= energyConservationFactor;
-        // #endif
-        
-        #ifdef REFLECTION
-            vec3 finalSheenRadiance = environmentSheenRadiance.rgb;
-            finalSheenRadiance *= sheenEnvironmentReflectance;
-
-            // Full value needed for alpha. 
-            vec3 finalSheenRadianceScaled = finalSheenRadiance * vLightingIntensity.z;
-        #endif
-    #endif
-
     // _____________________________ Highlights on Alpha _____________________________
     #ifdef ALPHABLEND
         float luminanceOverAlpha = 0.0;
@@ -1224,30 +1228,30 @@ void main(void) {
     //	finalSpecular			* vLightingIntensity.x * vLightingIntensity.w +
         finalSpecularScaled +
     #endif
-    #ifdef CLEARCOAT
-    // Computed in the previous step to help with alpha luminance.
-    //	finalClearCoat			* vLightingIntensity.x * vLightingIntensity.w +
-        finalClearCoatScaled +
-    #endif
     #ifdef SHEEN
     // Computed in the previous step to help with alpha luminance.
     //	finalSheen  			* vLightingIntensity.x * vLightingIntensity.w +
         finalSheenScaled +
     #endif
+    #ifdef CLEARCOAT
+    // Computed in the previous step to help with alpha luminance.
+    //	finalClearCoat			* vLightingIntensity.x * vLightingIntensity.w +
+        finalClearCoatScaled +
+    #endif
     #ifdef REFLECTION
     // Comupted in the previous step to help with alpha luminance.
     //	finalRadiance			* vLightingIntensity.z +
         finalRadianceScaled +
+        #if defined(SHEEN) && defined(ENVIRONMENTBRDF)
+        //  Comupted in the previous step to help with alpha luminance.
+        //  finalSheenRadiance * vLightingIntensity.z 
+            finalSheenRadianceScaled +
+        #endif
         #ifdef CLEARCOAT
         //  Comupted in the previous step to help with alpha luminance.
         //  finalClearCoatRadiance * vLightingIntensity.z 
             finalClearCoatRadianceScaled +
         #endif
-        #ifdef SHEEN
-        //  Comupted in the previous step to help with alpha luminance.
-        //  finalSheenRadiance * vLightingIntensity.z 
-            finalSheenRadianceScaled +
-        #endif
     #endif
     #ifdef SS_REFRACTION
         finalRefraction			* vLightingIntensity.z +