Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShadersChunk: Only use clearcoat chunks when clearcoat > 0 #22405

Merged
merged 5 commits into from Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/materials/MeshPhysicalMaterial.js
Expand Up @@ -34,6 +34,7 @@ import * as MathUtils from '../math/MathUtils.js';

class MeshPhysicalMaterial extends MeshStandardMaterial {

#clearcoat = 0;
#transmission = 0;

constructor( parameters ) {
Expand All @@ -49,7 +50,6 @@ class MeshPhysicalMaterial extends MeshStandardMaterial {

this.type = 'MeshPhysicalMaterial';

this.clearcoat = 0.0;
this.clearcoatMap = null;
this.clearcoatRoughness = 0.0;
this.clearcoatRoughnessMap = null;
Expand Down Expand Up @@ -90,6 +90,24 @@ class MeshPhysicalMaterial extends MeshStandardMaterial {

}

get clearcoat() {

return this.#clearcoat;

}

set clearcoat( value ) {

if ( this.#clearcoat > 0 !== value > 0 ) {

this.version ++;

}

this.#clearcoat = value;

}

get transmission() {

return this.#transmission;
Expand Down
@@ -1,5 +1,5 @@
export default /* glsl */`
#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT

vec3 clearcoatNormal = geometryNormal;

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shaders/ShaderChunk/common.glsl.js
Expand Up @@ -55,7 +55,7 @@ struct GeometricContext {
vec3 position;
vec3 normal;
vec3 viewDir;
#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT
vec3 clearcoatNormal;
#endif
};
Expand Down
Expand Up @@ -20,7 +20,7 @@ geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );

#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT

geometry.clearcoatNormal = clearcoatNormal;

Expand Down
Expand Up @@ -28,7 +28,7 @@ export default /* glsl */`

radiance += getIBLRadiance( geometry.viewDir, geometry.normal, material.roughness );

#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT

clearcoatRadiance += getIBLRadiance( geometry.viewDir, geometry.clearcoatNormal, material.clearcoatRoughness );

Expand Down
Expand Up @@ -47,7 +47,7 @@ material.roughness = min( material.roughness, 1.0 );

#endif

#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT

material.clearcoat = clearcoat;
material.clearcoatRoughness = clearcoatRoughness;
Expand Down
Expand Up @@ -6,7 +6,7 @@ struct PhysicalMaterial {
vec3 specularColor;
float specularF90;

#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT
float clearcoat;
float clearcoatRoughness;
vec3 clearcoatF0;
Expand Down Expand Up @@ -120,7 +120,7 @@ void RE_Direct_Physical( const in IncidentLight directLight, const in GeometricC

vec3 irradiance = dotNL * directLight.color;

#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT

float dotNLcc = saturate( dot( geometry.clearcoatNormal, directLight.direction ) );

Expand Down Expand Up @@ -151,7 +151,7 @@ void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricCo

void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradiance, const in vec3 clearcoatRadiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight) {

#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT

clearcoatSpecular += clearcoatRadiance * EnvironmentBRDF( geometry.clearcoatNormal, geometry.viewDir, material.clearcoatF0, material.clearcoatF90, material.clearcoatRoughness );

Expand Down
5 changes: 2 additions & 3 deletions src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js
Expand Up @@ -3,7 +3,6 @@ export default /* glsl */`

#ifdef PHYSICAL
#define IOR
#define CLEARCOAT
#define SPECULAR
#endif
WestLangley marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -30,7 +29,7 @@ uniform float opacity;
#endif
#endif

#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT
uniform float clearcoat;
uniform float clearcoatRoughness;
#endif
Expand Down Expand Up @@ -107,7 +106,7 @@ void main() {

vec3 outgoingLight = totalDiffuse + totalSpecular + totalEmissiveRadiance;

#ifdef CLEARCOAT
#ifdef USE_CLEARCOAT

float dotNVcc = saturate( dot( geometry.clearcoatNormal, geometry.viewDir ) );

Expand Down
7 changes: 7 additions & 0 deletions src/renderers/webgl/WebGLProgram.js
Expand Up @@ -470,13 +470,17 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) {
parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '',
parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '',
parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '',

parameters.displacementMap && parameters.supportsVertexTextures ? '#define USE_DISPLACEMENTMAP' : '',

parameters.specularMap ? '#define USE_SPECULARMAP' : '',
parameters.specularIntensityMap ? '#define USE_SPECULARINTENSITYMAP' : '',
parameters.specularTintMap ? '#define USE_SPECULARTINTMAP' : '',

parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '',
parameters.metalnessMap ? '#define USE_METALNESSMAP' : '',
parameters.alphaMap ? '#define USE_ALPHAMAP' : '',

parameters.transmission ? '#define USE_TRANSMISSION' : '',
parameters.transmissionMap ? '#define USE_TRANSMISSIONMAP' : '',
parameters.thicknessMap ? '#define USE_THICKNESSMAP' : '',
Expand Down Expand Up @@ -611,9 +615,12 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) {
parameters.normalMap ? '#define USE_NORMALMAP' : '',
( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '',
( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '',

parameters.clearcoat ? '#define USE_CLEARCOAT' : '',
parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '',
parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '',
parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '',

parameters.specularMap ? '#define USE_SPECULARMAP' : '',
parameters.specularIntensityMap ? '#define USE_SPECULARINTENSITYMAP' : '',
parameters.specularTintMap ? '#define USE_SPECULARTINTMAP' : '',
Expand Down
13 changes: 9 additions & 4 deletions src/renderers/webgl/WebGLPrograms.js
Expand Up @@ -37,7 +37,9 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
'precision', 'isWebGL2', 'supportsVertexTextures', 'outputEncoding', 'instancing', 'instancingColor',
'map', 'mapEncoding', 'matcap', 'matcapEncoding', 'envMap', 'envMapMode', 'envMapEncoding', 'envMapCubeUV',
'lightMap', 'lightMapEncoding', 'aoMap', 'emissiveMap', 'emissiveMapEncoding', 'bumpMap', 'normalMap',
'objectSpaceNormalMap', 'tangentSpaceNormalMap', 'clearcoatMap', 'clearcoatRoughnessMap', 'clearcoatNormalMap', 'displacementMap',
'objectSpaceNormalMap', 'tangentSpaceNormalMap',
'clearcoat', 'clearcoatMap', 'clearcoatRoughnessMap', 'clearcoatNormalMap',
'displacementMap',
'specularMap', 'specularIntensityMap', 'specularTintMap', 'specularTintMapEncoding', 'roughnessMap', 'metalnessMap', 'gradientMap',
'alphaMap', 'combine', 'vertexColors', 'vertexAlphas', 'vertexTangents', 'vertexUvs', 'uvsVertexOnly', 'fog', 'useFog', 'fogExp2',
'flatShading', 'sizeAttenuation', 'logarithmicDepthBuffer', 'skinning',
Expand Down Expand Up @@ -190,9 +192,12 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
normalMap: !! material.normalMap,
objectSpaceNormalMap: material.normalMapType === ObjectSpaceNormalMap,
tangentSpaceNormalMap: material.normalMapType === TangentSpaceNormalMap,
clearcoatMap: !! material.clearcoatMap,
clearcoatRoughnessMap: !! material.clearcoatRoughnessMap,
clearcoatNormalMap: !! material.clearcoatNormalMap,

clearcoat: material.clearcoat > 0,
clearcoatMap: material.clearcoat > 0 && !! material.clearcoatMap,
clearcoatRoughnessMap: material.clearcoat > 0 && !! material.clearcoatRoughnessMap,
clearcoatNormalMap: material.clearcoat > 0 && !! material.clearcoatNormalMap,

displacementMap: !! material.displacementMap,
roughnessMap: !! material.roughnessMap,
metalnessMap: !! material.metalnessMap,
Expand Down