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

Clear coat fresnel fix #4657

Merged
merged 4 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/graphics/program-lib/chunks/lit/frag/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ vec3 combineColor() {
ret = ret * sheenScaling + sSpecularLight + sReflection.rgb * sReflection.a;
#endif
#ifdef LIT_CLEARCOAT
float clearCoatScaling = 1.0 - ccFresnel * ccReflection.a;
ret = ret * clearCoatScaling + ccSpecularLight + ccReflection.rgb * ccReflection.a;
float clearCoatScaling = 1.0 - ccFresnel * ccSpecularity;
ret = ret * clearCoatScaling + (ccSpecularLight + ccReflection.rgb) * ccSpecularity;
#endif

return ret;
Expand Down
5 changes: 5 additions & 0 deletions src/graphics/program-lib/chunks/lit/frag/fresnelSchlick.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ vec3 getFresnel(float cosTheta, vec3 f0) {
return ret;
#endif
}

float getFresnelCC(float cosTheta) {
float fresnel = pow(1.0 - max(cosTheta, 0.0), 5.0);
return 0.04 + (1.0 - 0.04) * fresnel;
}
`;
4 changes: 1 addition & 3 deletions src/graphics/program-lib/chunks/lit/frag/reflectionCC.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export default /* glsl */`
#ifdef LIT_CLEARCOAT
uniform float material_clearCoatReflectivity;

void addReflectionCC() {
ccReflection += vec4(calcReflection(ccReflDirW, ccGlossiness), material_clearCoatReflectivity);
ccReflection += calcReflection(ccReflDirW, ccGlossiness);
}
#endif
`;
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/lit/frag/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ void main(void) {

#ifdef LIT_CLEARCOAT
ccSpecularLight = vec3(0);
ccReflection = vec4(0);
ccReflection = vec3(0);
#endif
`;
8 changes: 4 additions & 4 deletions src/graphics/program-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,8 @@ class LitShader {
if (options.clearCoat) {
code += " addReflectionCC();\n";
if (options.fresnelModel > 0) {
code += " ccFresnel = dot(dViewDirW, ccNormalW);\n";
code += " ccReflection.rgb *= getFresnel(ccFresnel, vec3(ccSpecularity));\n";
code += " ccFresnel = getFresnelCC(dot(dViewDirW, ccNormalW));\n";
code += " ccReflection.rgb *= ccFresnel;\n";
} else {
code += " ccFresnel = 0.0;\n";
code += " ccReflection.rgb *= ccSpecularity;\n";
Expand Down Expand Up @@ -1311,7 +1311,7 @@ class LitShader {
if (options.opacityFadesSpecular === false) {
if (options.blendType === BLEND_NORMAL || options.blendType === BLEND_PREMULTIPLIED) {
code += "float specLum = dot((dSpecularLight + dReflection.rgb * dReflection.a), vec3( 0.2126, 0.7152, 0.0722 ));\n";
code += "#ifdef LIT_CLEARCOAT\n specLum += dot(ccSpecularLight * ccSpecularity + ccReflection.rgb * ccReflection.a * ccSpecularity, vec3( 0.2126, 0.7152, 0.0722 ));\n#endif\n";
code += "#ifdef LIT_CLEARCOAT\n specLum += dot(ccSpecularLight * ccSpecularity + ccReflection.rgb * ccSpecularity, vec3( 0.2126, 0.7152, 0.0722 ));\n#endif\n";
code += "dAlpha = clamp(dAlpha + gammaCorrectInput(specLum), 0.0, 1.0);\n";
}
code += "dAlpha *= material_alphaFade;\n";
Expand Down Expand Up @@ -1369,7 +1369,7 @@ class LitShader {
if (code.includes("dAtten3")) structCode += "vec3 dAtten3;\n";
if (code.includes("dMsdf")) structCode += "vec4 dMsdf;\n";
if (code.includes("ccFresnel")) structCode += "float ccFresnel;\n";
if (code.includes("ccReflection")) structCode += "vec4 ccReflection;\n";
if (code.includes("ccReflection")) structCode += "vec3 ccReflection;\n";
if (code.includes("ccReflDirW")) structCode += "vec3 ccReflDirW;\n";
if (code.includes("ccSpecularLight")) structCode += "vec3 ccSpecularLight;\n";
if (code.includes("ccSpecularityNoFres")) structCode += "float ccSpecularityNoFres;\n";
Expand Down
4 changes: 2 additions & 2 deletions src/scene/materials/standard-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class StandardMaterialOptionsBuilder {
(stdMat.specularTint || (!stdMat.specularMap && !stdMat.specularVertexColor)) &&
notWhite(stdMat.specular);

const specularityFactorTint = useSpecular && stdMat.useMetalness &&
const specularityFactorTint = useSpecular && stdMat.useMetalnessSpecularColor &&
(stdMat.specularityFactorTint || (stdMat.specularityFactor < 1 && !stdMat.specularityFactorMap));

const emissiveTintColor = !stdMat.emissiveMap || (notWhite(stdMat.emissive) && stdMat.emissiveTint);
Expand All @@ -145,7 +145,7 @@ class StandardMaterialOptionsBuilder {
options.diffuseTint = diffuseTint ? 2 : 0;
options.specularTint = specularTint ? 2 : 0;
options.specularityFactorTint = specularityFactorTint ? 1 : 0;
options.useSpecularityFactor = specularityFactorTint || !!stdMat.specularityFactorMap;
options.useSpecularityFactor = (specularityFactorTint || !!stdMat.specularityFactorMap) && stdMat.useMetalnessSpecularColor;
options.useSpecularColor = useSpecularColor;
options.metalnessTint = (stdMat.useMetalness && stdMat.metalness < 1) ? 1 : 0;
options.glossTint = 1;
Expand Down
1 change: 0 additions & 1 deletion src/scene/materials/standard-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ class StandardMaterial extends Material {
if (this.clearCoat > 0) {
this._setParameter('material_clearCoat', this.clearCoat);
this._setParameter('material_clearCoatGlossiness', this.clearCoatGlossiness);
this._setParameter('material_clearCoatReflectivity', this.clearCoat); // for now don't separate this
this._setParameter('material_clearCoatBumpiness', this.clearCoatBumpiness);
}

Expand Down