Selaa lähdekoodia

Update clear coat to latest spec + fix sheen

Popov72 5 vuotta sitten
vanhempi
commit
6936db9425

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

@@ -177,11 +177,13 @@ export class PBRMaterialPropertyGridComponent extends React.Component<IPBRMateri
                             <SliderLineComponent label="Roughness" target={material.clearCoat} propertyName="roughness" minimum={0} maximum={1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             <SliderLineComponent label="Roughness" target={material.clearCoat} propertyName="roughness" minimum={0} maximum={1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             <SliderLineComponent label="IOR" target={material.clearCoat} propertyName="indexOfRefraction" minimum={1.0} maximum={3} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             <SliderLineComponent label="IOR" target={material.clearCoat} propertyName="indexOfRefraction" minimum={1.0} maximum={3} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             <TextureLinkLineComponent label="Clear coat" texture={material.clearCoat.texture} onTextureCreated={(texture) => material.clearCoat.texture = texture} onTextureRemoved={() => material.clearCoat.texture = null} material={material} onSelectionChangedObservable={this.props.onSelectionChangedObservable} onDebugSelectionChangeObservable={this._onDebugSelectionChangeObservable} />
                             <TextureLinkLineComponent label="Clear coat" texture={material.clearCoat.texture} onTextureCreated={(texture) => material.clearCoat.texture = texture} onTextureRemoved={() => material.clearCoat.texture = null} material={material} onSelectionChangedObservable={this.props.onSelectionChangedObservable} onDebugSelectionChangeObservable={this._onDebugSelectionChangeObservable} />
+                            <TextureLinkLineComponent label="Roughness" texture={material.clearCoat.textureRoughness} onTextureCreated={(texture) => material.clearCoat.textureRoughness = texture} onTextureRemoved={() => material.clearCoat.textureRoughness = null} material={material} onSelectionChangedObservable={this.props.onSelectionChangedObservable} onDebugSelectionChangeObservable={this._onDebugSelectionChangeObservable} />
                             <TextureLinkLineComponent label="Bump" texture={material.clearCoat.bumpTexture} onTextureCreated={(texture) => material.clearCoat.bumpTexture = texture} onTextureRemoved={() => material.clearCoat.bumpTexture = null} material={material} onSelectionChangedObservable={this.props.onSelectionChangedObservable} onDebugSelectionChangeObservable={this._onDebugSelectionChangeObservable} />
                             <TextureLinkLineComponent label="Bump" texture={material.clearCoat.bumpTexture} onTextureCreated={(texture) => material.clearCoat.bumpTexture = texture} onTextureRemoved={() => material.clearCoat.bumpTexture = null} material={material} onSelectionChangedObservable={this.props.onSelectionChangedObservable} onDebugSelectionChangeObservable={this._onDebugSelectionChangeObservable} />
                             {
                             {
                                 material.clearCoat.bumpTexture &&
                                 material.clearCoat.bumpTexture &&
                                 <SliderLineComponent label="Bump strength" target={material.clearCoat.bumpTexture} propertyName="level" minimum={0} maximum={2} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                                 <SliderLineComponent label="Bump strength" target={material.clearCoat.bumpTexture} propertyName="level" minimum={0} maximum={2} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             }
                             }
+                            <CheckBoxLineComponent label="Use roughness from main texture" target={material.clearCoat} propertyName="useRoughnessFromMainTexture" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             <CheckBoxLineComponent label="Tint" target={material.clearCoat} propertyName="isTintEnabled" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             <CheckBoxLineComponent label="Tint" target={material.clearCoat} propertyName="isTintEnabled" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                             {
                             {
                                 material.clearCoat.isEnabled && material.clearCoat.isTintEnabled &&
                                 material.clearCoat.isEnabled && material.clearCoat.isTintEnabled &&

+ 2 - 1
loaders/src/glTF/2.0/Extensions/KHR_materials_clearcoat.ts

@@ -61,6 +61,7 @@ export class KHR_materials_clearcoat implements IGLTFLoaderExtension {
         const promises = new Array<Promise<any>>();
         const promises = new Array<Promise<any>>();
 
 
         babylonMaterial.clearCoat.isEnabled = true;
         babylonMaterial.clearCoat.isEnabled = true;
+        babylonMaterial.clearCoat.useRoughnessFromMainTexture = false;
 
 
         if (properties.clearcoatFactor != undefined) {
         if (properties.clearcoatFactor != undefined) {
             babylonMaterial.clearCoat.intensity = properties.clearcoatFactor;
             babylonMaterial.clearCoat.intensity = properties.clearcoatFactor;
@@ -86,7 +87,7 @@ export class KHR_materials_clearcoat implements IGLTFLoaderExtension {
         if (properties.clearcoatRoughnessTexture) {
         if (properties.clearcoatRoughnessTexture) {
             promises.push(this._loader.loadTextureInfoAsync(`${context}/clearcoatRoughnessTexture`, properties.clearcoatRoughnessTexture, (texture) => {
             promises.push(this._loader.loadTextureInfoAsync(`${context}/clearcoatRoughnessTexture`, properties.clearcoatRoughnessTexture, (texture) => {
                 texture.name = `${babylonMaterial.name} (ClearCoat Roughness)`;
                 texture.name = `${babylonMaterial.name} (ClearCoat Roughness)`;
-                babylonMaterial.clearCoat.texture = texture;
+                babylonMaterial.clearCoat.textureRoughness = texture;
             }));
             }));
         }
         }
 
 

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

@@ -221,7 +221,10 @@ export class PBRMaterialDefines extends MaterialDefines
     public CLEARCOAT = false;
     public CLEARCOAT = false;
     public CLEARCOAT_DEFAULTIOR = false;
     public CLEARCOAT_DEFAULTIOR = false;
     public CLEARCOAT_TEXTURE = false;
     public CLEARCOAT_TEXTURE = false;
+    public CLEARCOAT_TEXTURE_ROUGHNESS = false;
     public CLEARCOAT_TEXTUREDIRECTUV = 0;
     public CLEARCOAT_TEXTUREDIRECTUV = 0;
+    public CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE = false;
+    public CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL = false;
     public CLEARCOAT_BUMP = false;
     public CLEARCOAT_BUMP = false;
     public CLEARCOAT_BUMPDIRECTUV = 0;
     public CLEARCOAT_BUMPDIRECTUV = 0;
     public CLEARCOAT_TINT = false;
     public CLEARCOAT_TINT = false;

+ 70 - 17
src/Materials/PBR/pbrClearCoatConfiguration.ts

@@ -18,9 +18,12 @@ export interface IMaterialClearCoatDefines {
     CLEARCOAT: boolean;
     CLEARCOAT: boolean;
     CLEARCOAT_DEFAULTIOR: boolean;
     CLEARCOAT_DEFAULTIOR: boolean;
     CLEARCOAT_TEXTURE: boolean;
     CLEARCOAT_TEXTURE: boolean;
+    CLEARCOAT_TEXTURE_ROUGHNESS: boolean;
     CLEARCOAT_TEXTUREDIRECTUV: number;
     CLEARCOAT_TEXTUREDIRECTUV: number;
     CLEARCOAT_BUMP: boolean;
     CLEARCOAT_BUMP: boolean;
     CLEARCOAT_BUMPDIRECTUV: number;
     CLEARCOAT_BUMPDIRECTUV: number;
+    CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE: boolean;
+    CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL: boolean;
 
 
     CLEARCOAT_TINT: boolean;
     CLEARCOAT_TINT: boolean;
     CLEARCOAT_TINT_TEXTURE: boolean;
     CLEARCOAT_TINT_TEXTURE: boolean;
@@ -73,12 +76,32 @@ export class PBRClearCoatConfiguration {
 
 
     private _texture: Nullable<BaseTexture> = null;
     private _texture: Nullable<BaseTexture> = null;
     /**
     /**
-     * Stores the clear coat values in a texture.
+     * Stores the clear coat values in a texture (red channel is intensity and green channel is roughness)
+     * If useRoughnessFromMainTexture is false, the green channel of texture is not used and the green channel of textureRoughness is used instead
+     * if textureRoughness is not empty, else no texture roughness is used
      */
      */
     @serializeAsTexture()
     @serializeAsTexture()
     @expandToProperty("_markAllSubMeshesAsTexturesDirty")
     @expandToProperty("_markAllSubMeshesAsTexturesDirty")
     public texture: Nullable<BaseTexture> = null;
     public texture: Nullable<BaseTexture> = null;
 
 
+    private _useRoughnessFromMainTexture = true;
+    /**
+     * Indicates that the green channel of the texture property will be used for roughness (default: true)
+     * If false, the green channel from textureRoughness is used for roughness
+     */
+    @serialize()
+    @expandToProperty("_markAllSubMeshesAsTexturesDirty")
+    public useRoughnessFromMainTexture = true;
+
+    private _textureRoughness: Nullable<BaseTexture> = null;
+    /**
+     * Stores the clear coat roughness in a texture (green channel)
+     * Not used if useRoughnessFromMainTexture is true
+     */
+    @serializeAsTexture()
+    @expandToProperty("_markAllSubMeshesAsTexturesDirty")
+    public textureRoughness: Nullable<BaseTexture> = null;
+
     private _bumpTexture: Nullable<BaseTexture> = null;
     private _bumpTexture: Nullable<BaseTexture> = null;
     /**
     /**
      * Define the clear coat specific bump texture.
      * Define the clear coat specific bump texture.
@@ -160,6 +183,12 @@ export class PBRClearCoatConfiguration {
                     }
                     }
                 }
                 }
 
 
+                if (this._textureRoughness && MaterialFlags.ClearCoatTextureEnabled) {
+                    if (!this._textureRoughness.isReadyOrNotBlocking()) {
+                        return false;
+                    }
+                }
+
                 if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.ClearCoatBumpTextureEnabled && !disableBumpMap) {
                 if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.ClearCoatBumpTextureEnabled && !disableBumpMap) {
                     // Bump texture cannot be not blocking.
                     // Bump texture cannot be not blocking.
                     if (!this._bumpTexture.isReady()) {
                     if (!this._bumpTexture.isReady()) {
@@ -186,6 +215,8 @@ export class PBRClearCoatConfiguration {
     public prepareDefines(defines: IMaterialClearCoatDefines, scene: Scene): void {
     public prepareDefines(defines: IMaterialClearCoatDefines, scene: Scene): void {
         if (this._isEnabled) {
         if (this._isEnabled) {
             defines.CLEARCOAT = true;
             defines.CLEARCOAT = true;
+            defines.CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE = this._useRoughnessFromMainTexture;
+            defines.CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL = this._texture !== null && this._texture._texture === this._textureRoughness?._texture;
 
 
             if (defines._areTexturesDirty) {
             if (defines._areTexturesDirty) {
                 if (scene.texturesEnabled) {
                 if (scene.texturesEnabled) {
@@ -195,6 +226,12 @@ export class PBRClearCoatConfiguration {
                         defines.CLEARCOAT_TEXTURE = false;
                         defines.CLEARCOAT_TEXTURE = false;
                     }
                     }
 
 
+                    if (this._textureRoughness && MaterialFlags.ClearCoatTextureEnabled) {
+                        MaterialHelper.PrepareDefinesForMergedUV(this._textureRoughness, defines, "CLEARCOAT_TEXTURE_ROUGHNESS");
+                    } else {
+                        defines.CLEARCOAT_TEXTURE_ROUGHNESS = false;
+                    }
+
                     if (this._bumpTexture && MaterialFlags.ClearCoatBumpTextureEnabled) {
                     if (this._bumpTexture && MaterialFlags.ClearCoatBumpTextureEnabled) {
                         MaterialHelper.PrepareDefinesForMergedUV(this._bumpTexture, defines, "CLEARCOAT_BUMP");
                         MaterialHelper.PrepareDefinesForMergedUV(this._bumpTexture, defines, "CLEARCOAT_BUMP");
                     } else {
                     } else {
@@ -222,9 +259,12 @@ export class PBRClearCoatConfiguration {
         else {
         else {
             defines.CLEARCOAT = false;
             defines.CLEARCOAT = false;
             defines.CLEARCOAT_TEXTURE = false;
             defines.CLEARCOAT_TEXTURE = false;
+            defines.CLEARCOAT_TEXTURE_ROUGHNESS = false;
             defines.CLEARCOAT_BUMP = false;
             defines.CLEARCOAT_BUMP = false;
             defines.CLEARCOAT_TINT = false;
             defines.CLEARCOAT_TINT = false;
             defines.CLEARCOAT_TINT_TEXTURE = false;
             defines.CLEARCOAT_TINT_TEXTURE = false;
+            defines.CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE = false;
+            defines.CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL = false;
         }
         }
     }
     }
 
 
@@ -240,9 +280,13 @@ export class PBRClearCoatConfiguration {
      */
      */
     public bindForSubMesh(uniformBuffer: UniformBuffer, scene: Scene, engine: Engine, disableBumpMap: boolean, isFrozen: boolean, invertNormalMapX: boolean, invertNormalMapY: boolean): void {
     public bindForSubMesh(uniformBuffer: UniformBuffer, scene: Scene, engine: Engine, disableBumpMap: boolean, isFrozen: boolean, invertNormalMapX: boolean, invertNormalMapY: boolean): void {
         if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
         if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
-            if (this._texture && MaterialFlags.ClearCoatTextureEnabled) {
-                uniformBuffer.updateFloat2("vClearCoatInfos", this._texture.coordinatesIndex, this._texture.level);
-                MaterialHelper.BindTextureMatrix(this._texture, uniformBuffer, "clearCoat");
+            if ((this._texture || this._textureRoughness) && MaterialFlags.ClearCoatTextureEnabled) {
+                uniformBuffer.updateFloat4("vClearCoatInfos", this._texture?.coordinatesIndex ?? this._textureRoughness?.coordinatesIndex ?? 0, this._texture?.level ?? 0, 0 /* not used */, this._textureRoughness?.level ?? 0);
+                if (this._texture) {
+                    MaterialHelper.BindTextureMatrix(this._texture, uniformBuffer, "clearCoat");
+                } else if (this._textureRoughness) {
+                    MaterialHelper.BindTextureMatrix(this._textureRoughness, uniformBuffer, "clearCoat");
+                }
             }
             }
 
 
             if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.ClearCoatTextureEnabled && !disableBumpMap) {
             if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.ClearCoatTextureEnabled && !disableBumpMap) {
@@ -287,6 +331,10 @@ export class PBRClearCoatConfiguration {
                 uniformBuffer.setTexture("clearCoatSampler", this._texture);
                 uniformBuffer.setTexture("clearCoatSampler", this._texture);
             }
             }
 
 
+            if (this._textureRoughness && (this._texture === null || this._texture._texture !== this._textureRoughness._texture) && MaterialFlags.ClearCoatTextureEnabled) {
+                uniformBuffer.setTexture("clearCoatRoughnessSampler", this._textureRoughness);
+            }
+
             if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.ClearCoatBumpTextureEnabled && !disableBumpMap) {
             if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.ClearCoatBumpTextureEnabled && !disableBumpMap) {
                 uniformBuffer.setTexture("clearCoatBumpSampler", this._bumpTexture);
                 uniformBuffer.setTexture("clearCoatBumpSampler", this._bumpTexture);
             }
             }
@@ -307,6 +355,10 @@ export class PBRClearCoatConfiguration {
             return true;
             return true;
         }
         }
 
 
+        if (this._textureRoughness === texture) {
+            return true;
+        }
+
         if (this._bumpTexture === texture) {
         if (this._bumpTexture === texture) {
             return true;
             return true;
         }
         }
@@ -327,6 +379,10 @@ export class PBRClearCoatConfiguration {
             activeTextures.push(this._texture);
             activeTextures.push(this._texture);
         }
         }
 
 
+        if (this._textureRoughness) {
+            activeTextures.push(this._textureRoughness);
+        }
+
         if (this._bumpTexture) {
         if (this._bumpTexture) {
             activeTextures.push(this._bumpTexture);
             activeTextures.push(this._bumpTexture);
         }
         }
@@ -345,6 +401,10 @@ export class PBRClearCoatConfiguration {
             animatables.push(this._texture);
             animatables.push(this._texture);
         }
         }
 
 
+        if (this._textureRoughness && this._textureRoughness.animations && this._textureRoughness.animations.length > 0) {
+            animatables.push(this._textureRoughness);
+        }
+
         if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
         if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
             animatables.push(this._bumpTexture);
             animatables.push(this._bumpTexture);
         }
         }
@@ -360,17 +420,10 @@ export class PBRClearCoatConfiguration {
      */
      */
     public dispose(forceDisposeTextures?: boolean): void {
     public dispose(forceDisposeTextures?: boolean): void {
         if (forceDisposeTextures) {
         if (forceDisposeTextures) {
-            if (this._texture) {
-                this._texture.dispose();
-            }
-
-            if (this._bumpTexture) {
-                this._bumpTexture.dispose();
-            }
-
-            if (this._tintTexture) {
-                this._tintTexture.dispose();
-            }
+            this._texture?.dispose();
+            this._textureRoughness?.dispose();
+            this._bumpTexture?.dispose();
+            this._tintTexture?.dispose();
         }
         }
     }
     }
 
 
@@ -418,7 +471,7 @@ export class PBRClearCoatConfiguration {
      * @param samplers defines the current sampler list.
      * @param samplers defines the current sampler list.
      */
      */
     public static AddSamplers(samplers: string[]): void {
     public static AddSamplers(samplers: string[]): void {
-        samplers.push("clearCoatSampler", "clearCoatBumpSampler", "clearCoatTintSampler");
+        samplers.push("clearCoatSampler", "clearCoatRoughnessSampler", "clearCoatBumpSampler", "clearCoatTintSampler");
     }
     }
 
 
     /**
     /**
@@ -428,7 +481,7 @@ export class PBRClearCoatConfiguration {
     public static PrepareUniformBuffer(uniformBuffer: UniformBuffer): void {
     public static PrepareUniformBuffer(uniformBuffer: UniformBuffer): void {
         uniformBuffer.addUniform("vClearCoatParams", 2);
         uniformBuffer.addUniform("vClearCoatParams", 2);
         uniformBuffer.addUniform("vClearCoatRefractionParams", 4);
         uniformBuffer.addUniform("vClearCoatRefractionParams", 4);
-        uniformBuffer.addUniform("vClearCoatInfos", 2);
+        uniformBuffer.addUniform("vClearCoatInfos", 4);
         uniformBuffer.addUniform("clearCoatMatrix", 16);
         uniformBuffer.addUniform("clearCoatMatrix", 16);
         uniformBuffer.addUniform("vClearCoatBumpInfos", 2);
         uniformBuffer.addUniform("vClearCoatBumpInfos", 2);
         uniformBuffer.addUniform("vClearCoatTangentSpaceParams", 2);
         uniformBuffer.addUniform("vClearCoatTangentSpaceParams", 2);

+ 4 - 2
src/Materials/PBR/pbrSheenConfiguration.ts

@@ -163,7 +163,7 @@ export class PBRSheenConfiguration {
             defines.SHEEN_ROUGHNESS = this._roughness !== null;
             defines.SHEEN_ROUGHNESS = this._roughness !== null;
             defines.SHEEN_ALBEDOSCALING = this._albedoScaling;
             defines.SHEEN_ALBEDOSCALING = this._albedoScaling;
             defines.SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE = this._useRoughnessFromMainTexture;
             defines.SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE = this._useRoughnessFromMainTexture;
-            defines.SHEEN_TEXTURE_ROUGHNESS_IDENTICAL = this.texture !== null && this.texture._texture === this.textureRoughness?._texture;
+            defines.SHEEN_TEXTURE_ROUGHNESS_IDENTICAL = this._texture !== null && this._texture._texture === this._textureRoughness?._texture;
 
 
             if (defines._areTexturesDirty) {
             if (defines._areTexturesDirty) {
                 if (scene.texturesEnabled) {
                 if (scene.texturesEnabled) {
@@ -184,6 +184,7 @@ export class PBRSheenConfiguration {
         else {
         else {
             defines.SHEEN = false;
             defines.SHEEN = false;
             defines.SHEEN_TEXTURE = false;
             defines.SHEEN_TEXTURE = false;
+            defines.SHEEN_TEXTURE_ROUGHNESS = false;
             defines.SHEEN_LINKWITHALBEDO = false;
             defines.SHEEN_LINKWITHALBEDO = false;
             defines.SHEEN_ROUGHNESS = false;
             defines.SHEEN_ROUGHNESS = false;
             defines.SHEEN_ALBEDOSCALING = false;
             defines.SHEEN_ALBEDOSCALING = false;
@@ -226,7 +227,8 @@ export class PBRSheenConfiguration {
             if (this._texture && MaterialFlags.SheenTextureEnabled) {
             if (this._texture && MaterialFlags.SheenTextureEnabled) {
                 uniformBuffer.setTexture("sheenSampler", this._texture);
                 uniformBuffer.setTexture("sheenSampler", this._texture);
             }
             }
-            if (this.textureRoughness && (this.texture === null || this.texture._texture !== this.textureRoughness._texture) && MaterialFlags.SheenTextureEnabled) {
+
+            if (this._textureRoughness && (this._texture === null || this._texture._texture !== this._textureRoughness._texture) && MaterialFlags.SheenTextureEnabled) {
                 uniformBuffer.setTexture("sheenRoughnessSampler", this._textureRoughness);
                 uniformBuffer.setTexture("sheenRoughnessSampler", this._textureRoughness);
             }
             }
         }
         }

+ 15 - 1
src/Shaders/ShadersInclude/pbrBlockClearcoat.fx

@@ -36,6 +36,9 @@ struct clearcoatOutParams
         const in vec3 geometricNormalW,
         const in vec3 geometricNormalW,
         const in vec3 viewDirectionW,
         const in vec3 viewDirectionW,
         const in vec2 vClearCoatParams,
         const in vec2 vClearCoatParams,
+    #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL)
+        const in vec4 clearCoatMapRoughnessData,
+    #endif
         const in vec3 specularEnvironmentR0,
         const in vec3 specularEnvironmentR0,
     #ifdef CLEARCOAT_TEXTURE
     #ifdef CLEARCOAT_TEXTURE
         const in vec2 clearCoatMapData,
         const in vec2 clearCoatMapData,
@@ -98,12 +101,23 @@ struct clearcoatOutParams
 
 
         #ifdef CLEARCOAT_TEXTURE
         #ifdef CLEARCOAT_TEXTURE
             clearCoatIntensity *= clearCoatMapData.x;
             clearCoatIntensity *= clearCoatMapData.x;
-            clearCoatRoughness *= clearCoatMapData.y;
+            #ifdef CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE
+                clearCoatRoughness *= clearCoatMapData.y;
+            #endif
             #if DEBUGMODE > 0
             #if DEBUGMODE > 0
                 outParams.clearCoatMapData = clearCoatMapData;
                 outParams.clearCoatMapData = clearCoatMapData;
             #endif
             #endif
         #endif
         #endif
 
 
+
+        #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE)
+            #ifdef CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL
+                clearCoatRoughness *= clearCoatMapData.y;
+            #else
+                clearCoatRoughness *= clearCoatMapRoughnessData.y;
+            #endif
+        #endif
+
         outParams.clearCoatIntensity = clearCoatIntensity;
         outParams.clearCoatIntensity = clearCoatIntensity;
         outParams.clearCoatRoughness = clearCoatRoughness;
         outParams.clearCoatRoughness = clearCoatRoughness;
 
 

+ 1 - 1
src/Shaders/ShadersInclude/pbrFragmentDeclaration.fx

@@ -70,7 +70,7 @@ uniform mat4 view;
     uniform vec4 vClearCoatRefractionParams;
     uniform vec4 vClearCoatRefractionParams;
 
 
     #ifdef CLEARCOAT_TEXTURE
     #ifdef CLEARCOAT_TEXTURE
-        uniform vec2 vClearCoatInfos;
+        uniform vec4 vClearCoatInfos;
         uniform mat4 clearCoatMatrix;
         uniform mat4 clearCoatMatrix;
     #endif
     #endif
 
 

+ 16 - 5
src/Shaders/ShadersInclude/pbrFragmentSamplersDeclaration.fx

@@ -87,7 +87,7 @@
 #endif
 #endif
 
 
 #ifdef CLEARCOAT
 #ifdef CLEARCOAT
-    #ifdef CLEARCOAT_TEXTURE
+    #if defined(CLEARCOAT_TEXTURE) || defined(CLEARCOAT_TEXTURE_ROUGHNESS)
         #if CLEARCOAT_TEXTUREDIRECTUV == 1
         #if CLEARCOAT_TEXTUREDIRECTUV == 1
             #define vClearCoatUV vMainUV1
             #define vClearCoatUV vMainUV1
         #elif CLEARCOAT_TEXTUREDIRECTUV == 2
         #elif CLEARCOAT_TEXTUREDIRECTUV == 2
@@ -95,9 +95,16 @@
         #else
         #else
             varying vec2 vClearCoatUV;
             varying vec2 vClearCoatUV;
         #endif
         #endif
+    #endif
+
+    #ifdef CLEARCOAT_TEXTURE
         uniform sampler2D clearCoatSampler;
         uniform sampler2D clearCoatSampler;
     #endif
     #endif
 
 
+    #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL)
+        uniform sampler2D clearCoatRoughnessSampler;
+    #endif
+
     #ifdef CLEARCOAT_BUMP
     #ifdef CLEARCOAT_BUMP
         #if CLEARCOAT_BUMPDIRECTUV == 1
         #if CLEARCOAT_BUMPDIRECTUV == 1
             #define vClearCoatBumpUV vMainUV1
             #define vClearCoatBumpUV vMainUV1
@@ -122,7 +129,7 @@
 #endif
 #endif
 
 
 #ifdef SHEEN
 #ifdef SHEEN
-    #ifdef SHEEN_TEXTURE
+    #if defined(SHEEN_TEXTURE) || defined(SHEEN_TEXTURE_ROUGHNESS)
         #if SHEEN_TEXTUREDIRECTUV == 1
         #if SHEEN_TEXTUREDIRECTUV == 1
             #define vSheenUV vMainUV1
             #define vSheenUV vMainUV1
         #elif SHEEN_TEXTUREDIRECTUV == 2
         #elif SHEEN_TEXTUREDIRECTUV == 2
@@ -130,10 +137,14 @@
         #else
         #else
             varying vec2 vSheenUV;
             varying vec2 vSheenUV;
         #endif
         #endif
+    #endif
+
+    #ifdef SHEEN_TEXTURE
         uniform sampler2D sheenSampler;
         uniform sampler2D sheenSampler;
-        #if defined(SHEEN_ROUGHNESS) && defined(SHEEN_TEXTURE_ROUGHNESS) && !defined(SHEEN_TEXTURE_ROUGHNESS_IDENTICAL)
-            uniform sampler2D sheenRoughnessSampler;
-        #endif
+    #endif
+
+    #if defined(SHEEN_ROUGHNESS) && defined(SHEEN_TEXTURE_ROUGHNESS) && !defined(SHEEN_TEXTURE_ROUGHNESS_IDENTICAL)
+        uniform sampler2D sheenRoughnessSampler;
     #endif
     #endif
 #endif
 #endif
 
 

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

@@ -40,7 +40,7 @@ uniform Material
 
 
     uniform vec2 vClearCoatParams;
     uniform vec2 vClearCoatParams;
     uniform vec4 vClearCoatRefractionParams;
     uniform vec4 vClearCoatRefractionParams;
-    uniform vec2 vClearCoatInfos;
+    uniform vec4 vClearCoatInfos;
     uniform mat4 clearCoatMatrix;
     uniform mat4 clearCoatMatrix;
     uniform vec2 vClearCoatBumpInfos;
     uniform vec2 vClearCoatBumpInfos;
     uniform vec2 vClearCoatTangentSpaceParams;
     uniform vec2 vClearCoatTangentSpaceParams;

+ 1 - 1
src/Shaders/ShadersInclude/pbrVertexDeclaration.fx

@@ -59,7 +59,7 @@ uniform float pointSize;
 // Clear Coat
 // Clear Coat
 #ifdef CLEARCOAT
 #ifdef CLEARCOAT
     #ifdef CLEARCOAT_TEXTURE
     #ifdef CLEARCOAT_TEXTURE
-        uniform vec2 vClearCoatInfos;
+        uniform vec4 vClearCoatInfos;
         uniform mat4 clearCoatMatrix;
         uniform mat4 clearCoatMatrix;
     #endif
     #endif
 
 

+ 7 - 0
src/Shaders/pbr.fragment.fx

@@ -347,6 +347,10 @@ void main(void) {
             vec2 clearCoatMapData = texture2D(clearCoatSampler, vClearCoatUV + uvOffset).rg * vClearCoatInfos.y;
             vec2 clearCoatMapData = texture2D(clearCoatSampler, vClearCoatUV + uvOffset).rg * vClearCoatInfos.y;
         #endif
         #endif
 
 
+        #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL)
+            vec4 clearCoatMapRoughnessData = texture2D(clearCoatRoughnessSampler, vClearCoatUV + uvOffset) * vClearCoatInfos.w;
+        #endif
+
         #if defined(CLEARCOAT_TINT) && defined(CLEARCOAT_TINT_TEXTURE)
         #if defined(CLEARCOAT_TINT) && defined(CLEARCOAT_TINT_TEXTURE)
             vec4 clearCoatTintMapData = toLinearSpace(texture2D(clearCoatTintSampler, vClearCoatTintUV + uvOffset));
             vec4 clearCoatTintMapData = toLinearSpace(texture2D(clearCoatTintSampler, vClearCoatTintUV + uvOffset));
         #endif
         #endif
@@ -360,6 +364,9 @@ void main(void) {
             geometricNormalW,
             geometricNormalW,
             viewDirectionW,
             viewDirectionW,
             vClearCoatParams,
             vClearCoatParams,
+            #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL)
+                clearCoatMapRoughnessData,
+            #endif
             specularEnvironmentR0,
             specularEnvironmentR0,
         #ifdef CLEARCOAT_TEXTURE
         #ifdef CLEARCOAT_TEXTURE
             clearCoatMapData,
             clearCoatMapData,

+ 4 - 4
src/Shaders/pbr.vertex.fx

@@ -79,7 +79,7 @@ varying vec2 vBumpUV;
 #endif
 #endif
 
 
 #ifdef CLEARCOAT
 #ifdef CLEARCOAT
-    #if defined(CLEARCOAT_TEXTURE) && CLEARCOAT_TEXTUREDIRECTUV == 0 
+    #if (defined(CLEARCOAT_TEXTURE) || defined(CLEARCOAT_TEXTURE_ROUGHNESS) && CLEARCOAT_TEXTUREDIRECTUV == 0 
         varying vec2 vClearCoatUV;
         varying vec2 vClearCoatUV;
     #endif
     #endif
 
 
@@ -93,7 +93,7 @@ varying vec2 vBumpUV;
 #endif
 #endif
 
 
 #ifdef SHEEN
 #ifdef SHEEN
-    #if defined(SHEEN_TEXTURE) && SHEEN_TEXTUREDIRECTUV == 0 
+    #if (defined(SHEEN_TEXTURE) || defined(SHEEN_TEXTURE_ROUGHNESS)) && SHEEN_TEXTUREDIRECTUV == 0 
         varying vec2 vSheenUV;
         varying vec2 vSheenUV;
     #endif
     #endif
 #endif
 #endif
@@ -351,7 +351,7 @@ void main(void) {
 #endif
 #endif
 
 
 #ifdef CLEARCOAT
 #ifdef CLEARCOAT
-    #if defined(CLEARCOAT_TEXTURE) && CLEARCOAT_TEXTUREDIRECTUV == 0 
+    #if (defined(CLEARCOAT_TEXTURE) || defined(CLEARCOAT_TEXTURE_ROUGHNESS)) && CLEARCOAT_TEXTUREDIRECTUV == 0 
         if (vClearCoatInfos.x == 0.)
         if (vClearCoatInfos.x == 0.)
         {
         {
             vClearCoatUV = vec2(clearCoatMatrix * vec4(uvUpdated, 1.0, 0.0));
             vClearCoatUV = vec2(clearCoatMatrix * vec4(uvUpdated, 1.0, 0.0));
@@ -386,7 +386,7 @@ void main(void) {
 #endif
 #endif
 
 
 #ifdef SHEEN
 #ifdef SHEEN
-    #if defined(SHEEN_TEXTURE) && SHEEN_TEXTUREDIRECTUV == 0 
+    #if (defined(SHEEN_TEXTURE) || defined(SHEEN_TEXTURE_ROUGHNESS)) && SHEEN_TEXTUREDIRECTUV == 0 
         if (vSheenInfos.x == 0.)
         if (vSheenInfos.x == 0.)
         {
         {
             vSheenUV = vec2(sheenMatrix * vec4(uvUpdated, 1.0, 0.0));
             vSheenUV = vec2(sheenMatrix * vec4(uvUpdated, 1.0, 0.0));