Skip to content

Commit

Permalink
Fixes sheen intensity (#4697)
Browse files Browse the repository at this point in the history
* Fix double clearcoat spec multiplication.

* Use CC fresnel for lights

* Use CC fresnel for lights

* Add reflection sample with base mip offset for materials

* Defer multiplication of sheen specular color to combine step.

* We don't use reciprocal PI for energy preservation, so doing it for sheen was a mistake.

Also revert the mip offset reflection sampling.
Also, sheen should not be a factor of the materials reflectivity.
  • Loading branch information
GSterbrant authored and Martin Valigursky committed Oct 6, 2022
1 parent 48a2a47 commit 18b0168
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/graphics/program-lib/chunks/lit/frag/clusteredLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,14 @@ void evaluateLight(ClusterLightData light) {
#ifdef LIT_CLEARCOAT
#ifdef LIT_SPECULAR_FRESNEL
ccSpecularLight += getLightSpecularCC(halfDir) * dAtten * light.color * dAtten3 * getFresnel(dot(dViewDirW, halfDir), vec3(ccSpecularity));
ccSpecularLight += getLightSpecularCC(halfDir) * dAtten * light.color * dAtten3 * getFresnelCC(dot(dViewDirW, halfDir));
#else
ccSpecularLight += getLightSpecularCC(halfDir) * dAtten * light.color * dAtten3 * vec3(ccSpecularity);
ccSpecularLight += getLightSpecularCC(halfDir) * dAtten * light.color * dAtten3;
#endif
#endif
#ifdef LIT_SHEEN
sSpecularLight += getLightSpecularSheen(halfDir) * dAtten * light.color * dAtten3 * sSpecularity;
sSpecularLight += getLightSpecularSheen(halfDir) * dAtten * light.color * dAtten3;
#endif
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/graphics/program-lib/chunks/lit/frag/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ vec3 combineColor() {
#ifdef LIT_REFLECTIONS
ret += dReflection.rgb * dReflection.a;
#endif
#ifdef LIT_SHEEN
float sheenScaling = 1.0 - max(max(sSpecularity.r, sSpecularity.g), sSpecularity.b) * 0.157;
ret = ret * sheenScaling + sSpecularLight + sReflection.rgb * sReflection.a;
ret = ret * sheenScaling + (sSpecularLight + sReflection.rgb) * sSpecularity;
#endif
#ifdef LIT_CLEARCOAT
float clearCoatScaling = 1.0 - ccFresnel * ccSpecularity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ void addReflectionSheen() {
float a = sGlossiness < 0.25 ? -339.2 * alphaG + 161.4 * sGlossiness - 25.9 : -8.48 * alphaG + 14.3 * sGlossiness - 9.95;
float b = sGlossiness < 0.25 ? 44.0 * alphaG - 23.7 * sGlossiness + 3.26 : 1.97 * alphaG - 3.27 * sGlossiness + 0.72;
float DG = exp( a * NoV + b ) + ( sGlossiness < 0.25 ? 0.0 : 0.1 * ( sGlossiness - 0.25 ) );
sReflection += vec4(calcReflection(dReflDirW, sGlossiness), saturate(DG * 1.0/PI));
sReflection += calcReflection(dNormalW, 0.0) * saturate(DG);
}
`;
11 changes: 5 additions & 6 deletions src/graphics/program-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,6 @@ class LitShader {
code += " ccReflection.rgb *= ccFresnel;\n";
} else {
code += " ccFresnel = 0.0;\n";
code += " ccReflection.rgb *= ccSpecularity;\n";
}
}
if (options.useSpecularityFactor) {
Expand All @@ -1051,11 +1050,11 @@ class LitShader {

if (options.sheen) {
code += " addReflectionSheen();\n";
code += " sReflection.rgb *= sSpecularity;\n";
}

// Fresnel has to be applied to reflections
code += " addReflection();\n";

if (options.fresnelModel > 0) {
code += " dReflection.rgb *= getFresnel(dot(dViewDirW, dNormalW), dSpecularity);\n";
} else {
Expand Down Expand Up @@ -1248,18 +1247,18 @@ class LitShader {
if (options.clearCoat) {
code += " ccSpecularLight += getLightSpecularCC(dHalfDirW) * dAtten * light" + i + "_color";
code += usesCookieNow ? " * dAtten3" : "";
code += calcFresnel ? " * getFresnel(dot(dViewDirW, dHalfDirW), vec3(ccSpecularity))" : " * vec3(ccSpecularity)";
code += calcFresnel ? " * getFresnelCC(dot(dViewDirW, dHalfDirW))" : "";
code += ";\n";
}
if (options.sheen) {
code += " sSpecularLight += getLightSpecularSheen(dHalfDirW) * dAtten * light" + i + "_color * sSpecularity";
code += " sSpecularLight += getLightSpecularSheen(dHalfDirW) * dAtten * light" + i + "_color";
code += usesCookieNow ? " * dAtten3" : "";
code += ";\n";
}
if (options.useSpecular) {
code += " dSpecularLight += getLightSpecular(dHalfDirW) * dAtten * light" + i + "_color";
code += usesCookieNow ? " * dAtten3" : "";
code += calcFresnel ? " * getFresnel(dot(dViewDirW, dHalfDirW), dSpecularity)" : " * dSpecularity";
code += calcFresnel ? " * getFresnel(dot(dViewDirW, dHalfDirW), dSpecularity)" : "";
code += ";\n";
}
}
Expand Down Expand Up @@ -1374,7 +1373,7 @@ class LitShader {
if (code.includes("ccSpecularLight")) structCode += "vec3 ccSpecularLight;\n";
if (code.includes("ccSpecularityNoFres")) structCode += "float ccSpecularityNoFres;\n";
if (code.includes("sSpecularLight")) structCode += "vec3 sSpecularLight;\n";
if (code.includes("sReflection")) structCode += "vec4 sReflection;\n";
if (code.includes("sReflection")) structCode += "vec3 sReflection;\n";

const result = this._fsGetBeginCode() +
this.varyings +
Expand Down

0 comments on commit 18b0168

Please sign in to comment.