Skip to content

Commit

Permalink
fix: anisotropy and normal maps are now no longer rotated 90 degrees.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 4, 2020
1 parent 838130e commit 1e49f5f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/examples/brdfs/03_specularAnisotropic/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() {
vec3 albedo = vec3( 1. );
vec3 specular = vec3( 1. );
float specularRoughness = 0.25;
vec2 specularAnisotropicFlow = specularAnisotropicFlowModulator * decodeAnisotropyFlowMap( texture2D( specularAnisotropicFlowMap, v_uv0 ) );
vec2 specularAnisotropicFlow = specularAnisotropicFlowModulator * decodeAnisotropyFlowMap( texture2D( specularAnisotropicFlowMap, v_uv0 ).rg );
vec3 specularF0 = specularIntensityToF0( specular );

Surface surface;
Expand Down
13 changes: 9 additions & 4 deletions src/lib/shaders/includes/brdfs/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ vec3 specularIntensityToF0( in vec3 specularIntensity ) {
mat3 surfaceToNormalMatrix( in Surface surface ) {
return mat3(surface.tangent, surface.bitangent, surface.normal);
}

vec3 flatSurfaceNormal( vec3 position ) {
return normalize(cross(dFdx(position), dFdy(position)));
}

// Get normal, tangent and bitangent vectors.
// based on the glTF reference viewer
void uvToTangentFrame( inout Surface surface, in vec2 uv ) {
vec3 tempTangent = dFdx( uv.y ) * dFdy(surface.position) - dFdy( uv.y ) * dFdx(surface.position);
vec3 tempTangent = dFdy( uv.y ) * dFdx(surface.position) - dFdx( uv.y ) * dFdy(surface.position);

surface.normal = normalize(surface.normal);
surface.tangent = normalize(tempTangent - surface.normal * dot(surface.normal, tempTangent));
surface.bitangent = cross(surface.normal, surface.bitangent);
surface.bitangent = -cross(surface.normal, surface.tangent);
}

void rotateTangentFrame( inout Surface surface, in vec2 anisotropicDirection ) {
// Due to anisoptry, the tangent can be further rotated around the geometric normal.
// NOTE: The null anisotropic direction value is (1, 0, 0)
mat3 normalMatrix = surfaceToNormalMatrix( surface );
surface.tangent = normalMatrix * vec3( anisotropicDirection, 0. );
surface.bitangent = normalMatrix * vec3( anisotropicDirection.y, -anisotropicDirection.x, 0. );
surface.tangent = normalMatrix * vec3( anisotropicDirection.yx, 0. );
surface.bitangent = normalMatrix * vec3( anisotropicDirection.x, -anisotropicDirection.y, 0. );
}

// this should be done after rotating the tangent frame
Expand Down
4 changes: 2 additions & 2 deletions src/lib/shaders/includes/brdfs/specular/anisotropy.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

vec2 decodeAnisotropyFlowMap( vec4 value ) {
return value.rg * 2. - vec2(1.);
vec2 decodeAnisotropyFlowMap( vec2 value ) {
return value * 2. - 1.;
}

// based on filament
Expand Down

0 comments on commit 1e49f5f

Please sign in to comment.