diff options
Diffstat (limited to 'res/effectlib/calculateRoughness.glsllib')
-rw-r--r-- | res/effectlib/calculateRoughness.glsllib | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/res/effectlib/calculateRoughness.glsllib b/res/effectlib/calculateRoughness.glsllib new file mode 100644 index 0000000..13ca823 --- /dev/null +++ b/res/effectlib/calculateRoughness.glsllib @@ -0,0 +1,32 @@ +float calculateRoughness( in vec3 N, in float roughnessU, in float roughnessV, in vec3 tangentU ) +{ + float roughness = roughnessU; + if ( abs(roughnessU - roughnessV) > 0.00001) + { + // determine major and minor radii a and b, and the vector along the major axis + float a = roughnessU; + float b = roughnessV; + + // we need the angle between the major axis and the projection of viewDir on the tangential plane + // the major axis is the orthonormalization of tangentU with respect to N + // the projection of viewDir is the orthonormalization of viewDir with respect to N + // as both vectors would be calculated by orthonormalize, we can as well leave the second cross + // product in those calculations away, as they don't change the angular relation. + vec3 minorAxis = normalize( cross( tangentU, N ) ); // crossing this with N would give the major axis + // which is equivalent to orthonormalizing tangentU with respect to N + if ( roughnessU < roughnessV ) + { + a = roughnessV; + b = roughnessU; + minorAxis = cross( N, minorAxis ); + } + + vec3 po = normalize( cross( viewDir, N ) ); + float cosPhi = dot( po, minorAxis ); + + // determine the polar coordinate of viewDir, take that radius as the roughness + float excentricitySquare = 1.0f - square( b / a ); + roughness = b / sqrt( 1.0f - excentricitySquare * square( cosPhi ) ); + } + return( roughness ); +} |