Skip to content

Instantly share code, notes, and snippets.

@Taremin
Last active December 31, 2024 11:02
Show Gist options
  • Save Taremin/7c6d55eb95cefa85428b75bd0e283ab1 to your computer and use it in GitHub Desktop.
Save Taremin/7c6d55eb95cefa85428b75bd0e283ab1 to your computer and use it in GitHub Desktop.
Anisotropic Reflection
/*
* https://en.wikibooks.org/wiki/GLSL_Programming/Unity/Brushed_Metal
*/
float anisotropy(
vector varyingNormalDirection,
vector varyingTangentDirection,
vector lightDirection,
vector viewDirection,
float alphaX,
float alphaY
) {
vector normalDirection = normalize(varyingNormalDirection);
vector tangentDirection = normalize(varyingTangentDirection);
float attenuation = 1.0;
vector halfwayVector = normalize(lightDirection + viewDirection);
vector binormalDirection = cross(normalDirection, tangentDirection);
float dotLN = dot(lightDirection, normalDirection);
float dotHN = dot(halfwayVector, normalDirection);
float dotVN = dot(viewDirection, normalDirection);
float dotHTAlphaX = dot(halfwayVector, tangentDirection) / alphaX;
float dotHBAlphaY = dot(halfwayVector, binormalDirection) / alphaY;
float specularReflection = attenuation
* sqrt(max(0.0, dotLN / dotVN))
* exp(-2.0 * (dotHTAlphaX * dotHTAlphaX + dotHBAlphaY * dotHBAlphaY) / (1.0 + dotHN));
return (1.0 - abs((specularReflection - 1.0))) / 2.0;
}
shader anisotropy(
normal n = (0.0),
vector tangent = (0.0),
vector viewDir = (0.0),
vector lightDir = (0.0),
float alphaX = 0.0,
float alphaY = 0.0,
output float anisotropic_reflection = (0.0),
) {
anisotropic_reflection = anisotropy(n, tangent, lightDir, viewDir, alphaX, alphaY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment