Skip to content

Commit

Permalink
feat: add support for clearCoatMap and clearCoatRoughnessMap
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnsgn committed Jun 6, 2022
1 parent a78ed9b commit 80153ee
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
50 changes: 48 additions & 2 deletions shaders/chunks/clear-coat.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@ export default /* glsl */ `
uniform float uClearCoat;
uniform float uClearCoatRoughness;
#ifdef USE_CLEAR_COAT_MAP
uniform sampler2D uClearCoatMap;
uniform float uClearCoatMapScale;
#ifdef USE_CLEAR_COAT_MAP_TEX_COORD_TRANSFORM
uniform mat3 uClearCoatMapTexCoordTransform;
#endif
void getClearCoat(inout PBRData data) {
#ifdef USE_CLEAR_COAT_MAP_TEX_COORD_TRANSFORM
vec2 texCoord = getTextureCoordinates(data, CLEAR_COAT_MAP_TEX_COORD_INDEX, uClearCoatMapTexCoordTransform);
#else
vec2 texCoord = getTextureCoordinates(data, CLEAR_COAT_MAP_TEX_COORD_INDEX);
#endif
data.clearCoat = uClearCoat * texture2D(uClearCoatMap, texCoord).r;
}
#else
void getClearCoat(inout PBRData data) {
data.clearCoat = uClearCoat;
}
#endif
#ifdef USE_CLEAR_COAT_ROUGHNESS_MAP
uniform sampler2D uClearCoatRoughnessMap;
uniform float uClearCoatRoughnessMapScale;
#ifdef USE_CLEAR_COAT_ROUGHNESS_MAP_TEX_COORD_TRANSFORM
uniform mat3 uClearCoatRoughnessMapTexCoordTransform;
#endif
void getClearCoatRoughness(inout PBRData data) {
#ifdef USE_CLEAR_COAT_ROUGHNESS_MAP_TEX_COORD_TRANSFORM
vec2 texCoord = getTextureCoordinates(data, CLEAR_COAT_ROUGHNESS_MAP_TEX_COORD_INDEX, uClearCoatRoughnessMapTexCoordTransform);
#else
vec2 texCoord = getTextureCoordinates(data, CLEAR_COAT_ROUGHNESS_MAP_TEX_COORD_INDEX);
#endif
data.clearCoatRoughness = uClearCoatRoughness * texture2D(uClearCoatRoughnessMap, texCoord).g;
}
#else
void getClearCoatRoughness(inout PBRData data) {
data.clearCoatRoughness = uClearCoatRoughness;
}
#endif
#ifdef USE_CLEAR_COAT_NORMAL_MAP
uniform sampler2D uClearCoatNormalMap;
uniform float uClearCoatNormalMapScale;
Expand Down Expand Up @@ -50,7 +96,7 @@ export default /* glsl */ `
// IOR = 1.5, F0 = 0.04
// as material is no longer in contact with air we calculate new IOR on the
// as material is no longer in contact with air we calculate new IOR on the
// clear coat and material interface
vec3 f0ClearCoatToSurface(const vec3 f0) {
return saturate(f0 * (f0 * (0.941892 - 0.263008 * f0) + 0.346479) - 0.0285998);
Expand All @@ -65,7 +111,7 @@ export default /* glsl */ `
float D = D_GGX(data.clearCoatLinearRoughness, clearCoatNoH, h, data.normalWorld);
float V = V_Kelemen(LoH);
// air-polyurethane interface has IOR = 1.5 -> F0 = 0.04
float F = F_Schlick(0.04, 1.0, LoH) * uClearCoat;
float F = F_Schlick(0.04, 1.0, LoH) * data.clearCoat;
Fcc = F;
return D * V * F;
Expand Down
4 changes: 2 additions & 2 deletions shaders/chunks/indirect.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export default /* glsl */ `
vec3 clearCoatR = data.reflectionWorld;
#endif
// The clear coat layer assumes an IOR of 1.5 (4% reflectance)
float Fc = F_Schlick(0.04, 1.0, clearCoatNoV) * uClearCoat;
float Fc = F_Schlick(0.04, 1.0, clearCoatNoV) * data.clearCoat;
float attenuation = 1.0 - Fc;
Fr *= (attenuation * attenuation);
Fr += getPrefilteredReflection(clearCoatR, uClearCoatRoughness) * (ao * Fc);
Fr += getPrefilteredReflection(clearCoatR, data.clearCoatRoughness) * (ao * Fc);
Fd *= attenuation;
}
#endif
Expand Down
17 changes: 11 additions & 6 deletions shaders/pipeline/material.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ struct PBRData {
float metallic; // metallic value at the surface
float linearRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])
vec3 f0; // Reflectance at normal incidence, specular color
float clearCoat;
float clearCoatRoughness;
float clearCoatLinearRoughness;
vec3 sheenColor;
float sheenRoughness;
vec3 sheen;
vec3 clearCoatNormal;
vec3 reflectionWorld;
vec3 directColor;
vec3 diffuseColor; // color contribution from diffuse lighting
vec3 indirectDiffuse; // contribution from IBL light probe and Ambient Light
vec3 indirectSpecular; // contribution from IBL light probe and Area Light
vec3 sheenColor;
float sheenRoughness;
vec3 sheen;
};
// Includes
Expand Down Expand Up @@ -187,9 +189,12 @@ void main() {
data.linearRoughness = data.roughness * data.roughness;
#ifdef USE_CLEAR_COAT
data.clearCoatLinearRoughness = uClearCoatRoughness * uClearCoatRoughness;
data.f0 = mix(data.f0, f0ClearCoatToSurface(data.f0), uClearCoat);
data.roughness = max(data.roughness, uClearCoatRoughness);
getClearCoat(data);
getClearCoatRoughness(data);
data.clearCoatLinearRoughness = data.clearCoatRoughness * data.clearCoatRoughness;
data.f0 = mix(data.f0, f0ClearCoatToSurface(data.f0), data.clearCoat);
data.roughness = max(data.roughness, data.clearCoatRoughness);
getClearCoatNormal(data);
#endif
Expand Down

0 comments on commit 80153ee

Please sign in to comment.