Skip to content

Commit b81a6e4

Browse files
WestLangleyMugen87
andauthored
WebGLRenderer Shaders: introduce transformNormalByInverseViewMatrix() (#33351)
Co-authored-by: Michael Herzog <michael.herzog@human-interactive.org>
1 parent 4e24967 commit b81a6e4

8 files changed

Lines changed: 25 additions & 15 deletions

File tree

examples/jsm/helpers/LightProbeHelper.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ class LightProbeHelper extends Mesh {
5858
5959
#define RECIPROCAL_PI 0.318309886
6060
61-
vec3 inverseTransformDirection( in vec3 normal, in mat4 matrix ) {
6261
63-
// matrix is assumed to be orthogonal
62+
vec3 transformNormalByInverseViewMatrix( in vec3 normal, in mat4 viewMatrix ) {
6463
65-
return normalize( ( vec4( normal, 0.0 ) * matrix ).xyz );
64+
// upper-left 3x3 of view matrix is assumed to be orthogonal
65+
66+
return normalize( ( vec4( normal, 0.0 ) * viewMatrix ).xyz );
6667
6768
}
6869
@@ -101,7 +102,7 @@ class LightProbeHelper extends Mesh {
101102
102103
vec3 normal = normalize( vNormal );
103104
104-
vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
105+
vec3 worldNormal = transformNormalByInverseViewMatrix( normal, viewMatrix );
105106
106107
vec3 irradiance = shGetIrradianceAt( worldNormal, sh );
107108

src/renderers/shaders/ShaderChunk/common.glsl.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,21 @@ vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
6464
6565
}
6666
67-
vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
67+
#define inverseTransformDirection transformNormalByInverseViewMatrix // keeping this for backwards compatibility
6868
69-
// dir can be either a direction vector or a normal vector
70-
// upper-left 3x3 of matrix is assumed to be orthogonal
69+
vec3 transformNormalByInverseViewMatrix( in vec3 normal, in mat4 viewMatrix ) {
7170
72-
return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
71+
// upper-left 3x3 of view matrix is assumed to be orthogonal
72+
73+
return normalize( ( vec4( normal, 0.0 ) * viewMatrix ).xyz );
74+
75+
}
76+
77+
vec3 transformDirectionByInverseViewMatrix( in vec3 dir, in mat4 viewMatrix ) {
78+
79+
// upper-left 3x3 of view matrix is assumed to be orthogonal
80+
81+
return normalize( ( vec4( dir, 0.0 ) * viewMatrix ).xyz );
7382
7483
}
7584

src/renderers/shaders/ShaderChunk/envmap_fragment.glsl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default /* glsl */`
1616
}
1717
1818
// Transforming Normal Vectors with the Inverse Transformation
19-
vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
19+
vec3 worldNormal = transformNormalByInverseViewMatrix( normal, viewMatrix );
2020
2121
#ifdef ENVMAP_MODE_REFLECTION
2222

src/renderers/shaders/ShaderChunk/envmap_physical_pars_fragment.glsl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default /* glsl */`
55
66
#ifdef ENVMAP_TYPE_CUBE_UV
77
8-
vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
8+
vec3 worldNormal = transformNormalByInverseViewMatrix( normal, viewMatrix );
99
1010
vec4 envMapColor = textureCubeUV( envMap, envMapRotation * worldNormal, 1.0 );
1111
@@ -28,7 +28,7 @@ export default /* glsl */`
2828
// Mixing the reflection with the normal is more accurate and keeps rough objects from gathering light from behind their tangent plane.
2929
reflectVec = normalize( mix( reflectVec, normal, pow4( roughness ) ) );
3030
31-
reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
31+
reflectVec = transformDirectionByInverseViewMatrix( reflectVec, viewMatrix );
3232
3333
vec4 envMapColor = textureCubeUV( envMap, envMapRotation * reflectVec, roughness );
3434

src/renderers/shaders/ShaderChunk/envmap_vertex.glsl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default /* glsl */`
1919
2020
}
2121
22-
vec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );
22+
vec3 worldNormal = transformNormalByInverseViewMatrix( transformedNormal, viewMatrix );
2323
2424
#ifdef ENVMAP_MODE_REFLECTION
2525

src/renderers/shaders/ShaderChunk/lights_pars_begin.glsl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ vec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) {
3737
3838
vec3 getLightProbeIrradiance( const in vec3 lightProbe[ 9 ], const in vec3 normal ) {
3939
40-
vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
40+
vec3 worldNormal = transformNormalByInverseViewMatrix( normal, viewMatrix );
4141
4242
vec3 irradiance = shGetIrradianceAt( worldNormal, lightProbe );
4343

src/renderers/shaders/ShaderChunk/shadowmap_vertex.glsl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default /* glsl */`
66
77
// Offsetting the position used for querying occlusion along the world normal can be used to reduce shadow acne.
88
9-
vec3 shadowWorldNormal = inverseTransformDirection( transformedNormal, viewMatrix );
9+
vec3 shadowWorldNormal = transformNormalByInverseViewMatrix( transformedNormal, viewMatrix );
1010
1111
#else
1212

src/renderers/shaders/ShaderChunk/transmission_fragment.glsl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default /* glsl */`
2121
2222
vec3 pos = vWorldPosition;
2323
vec3 v = normalize( cameraPosition - pos );
24-
vec3 n = inverseTransformDirection( normal, viewMatrix );
24+
vec3 n = transformNormalByInverseViewMatrix( normal, viewMatrix );
2525
2626
vec4 transmitted = getIBLVolumeRefraction(
2727
n, v, material.roughness, material.diffuseContribution, material.specularColorBlended, material.specularF90,

0 commit comments

Comments
 (0)