Skip to content

Commit

Permalink
fix: fix incorrect rotation of tangent frame
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 4, 2020
1 parent 50cf10f commit 838130e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/examples/brdfs/04_normals/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
float specularRoughness = 0.25;
vec3 specularF0 = specularIntensityToF0( specular );

vec3 normal = vec3( normalModulator, 1. ) * rgbToNormal( texture2D( normalMap, v_uv0 ).grb );
vec3 normal = vec3( normalModulator, 1. ) * rgbToNormal( texture2D( normalMap, v_uv0 ).rgb );

Surface surface;
surface.position = v_viewSurfacePosition;
Expand Down
9 changes: 4 additions & 5 deletions src/examples/brdfs/08_displacement/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ uniform vec3 pointLightColor;
uniform float pointLightRange;

uniform sampler2D normalMap;
uniform float normalScale;
uniform vec2 normalScale;
uniform float displacementScale;

#pragma include <brdfs/common>
#pragma include <lighting/punctual>
Expand All @@ -22,21 +23,19 @@ uniform float normalScale;
void main() {

vec3 ambient = vec3(0.);
vec3 albedo = mix( vec3(0.2), vec3( 1., 0., 0. ), normalScale );
vec3 albedo = mix( vec3(0.2), vec3( 1., 0., 0. ), normalScale.x );
vec3 specular = vec3(1.);
float specularRoughness = 0.25;
vec3 specularF0 = specularIntensityToF0( specular );
vec3 normal = rgbToNormal( texture2D( normalMap, vec2(1.0)-v_uv0 ).grb ) * vec3( 1., -1., 1. );
vec3 normal = normalize( rgbToNormal( texture2D( normalMap, vec2(1.0)-v_uv0 ).rgb ) * vec3( normalScale, 1. ) );

Surface surface;
surface.position = v_viewSurfacePosition;
surface.normal = normalize( v_viewSurfaceNormal );
surface.viewDirection = normalize( -v_viewSurfacePosition );

uvToTangentFrame( surface, v_uv0 );
vec3 oldNormal = surface.normal;
perturbSurfaceNormal_TangentSpace( surface, normal );
surface.normal = normalize( mix( oldNormal, surface.normal, normalScale ) );

PunctualLight punctualLight;
punctualLight.position = pointLightViewPosition;
Expand Down
9 changes: 6 additions & 3 deletions src/examples/brdfs/08_displacement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
makeMatrix4Scale,
makeMatrix4Translation,
} from "../../../lib/math/Matrix4.Functions";
import { Vector2 } from "../../../lib/math/Vector2";
import { Vector3 } from "../../../lib/math/Vector3";
import { makeBufferGeometryFromGeometry } from "../../../lib/renderers/webgl/buffers/BufferGeometry";
import { ClearState } from "../../../lib/renderers/webgl/ClearState";
Expand Down Expand Up @@ -58,7 +59,7 @@ async function init(): Promise<null> {

// materials
normalMap: normalMap,
normalScale: 1.0,
normalScale: new Vector2(1.0, -1.0),
displacementMap: displacementMap,
displacementScale: 1.0,
};
Expand All @@ -74,8 +75,10 @@ async function init(): Promise<null> {
new Euler(0.15 * Math.PI, now * 0.0002, 0, EulerOrder.XZY),
uniforms.localToWorld,
);
uniforms.normalScale = Math.cos(now * 0.0008) * 0.5 + 0.5;
uniforms.displacementScale = uniforms.normalScale * 0.1;

const effectScale = Math.cos(now * 0.0008) * 0.5 + 0.5;
uniforms.normalScale = new Vector2(1.0, -1.0).multiplyByScalar(effectScale);
uniforms.displacementScale = effectScale * 0.1;
uniforms.pointLightViewPosition = new Vector3(Math.cos(now * 0.001) * 3.0, 2.0, 0.5);

canvasFramebuffer.clear(AttachmentBits.All);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/shaders/includes/brdfs/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void uvToTangentFrame( inout Surface surface, in vec2 uv ) {
vec3 tempTangent = dFdx( uv.y ) * dFdy(surface.position) - dFdy( uv.y ) * dFdx(surface.position);

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

void rotateTangentFrame( inout Surface surface, in vec2 anisotropicDirection ) {
Expand Down

0 comments on commit 838130e

Please sign in to comment.