Skip to content

Commit a5584f2

Browse files
authored
Merge pull request #17767 from sciecode/dev-ortho-light
ShaderChunks: Fix orthographic view direction
2 parents 3230bfa + c804a1b commit a5584f2

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

src/renderers/WebGLRenderer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,16 @@ function WebGLRenderer( parameters ) {
17811781

17821782
}
17831783

1784+
if ( material.isMeshPhongMaterial ||
1785+
material.isMeshLambertMaterial ||
1786+
material.isMeshBasicMaterial ||
1787+
material.isMeshStandardMaterial ||
1788+
material.isShaderMaterial ) {
1789+
1790+
p_uniforms.setValue( _gl, 'isOrthographic', camera.isOrthographicCamera === true );
1791+
1792+
}
1793+
17841794
if ( material.isMeshPhongMaterial ||
17851795
material.isMeshLambertMaterial ||
17861796
material.isMeshBasicMaterial ||

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@ export default /* glsl */`
33
44
#ifdef ENV_WORLDPOS
55
6-
vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );
6+
vec3 cameraToFrag;
7+
8+
if ( isOrthographic ) {
9+
10+
cameraToFrag = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );
11+
12+
} else {
13+
14+
cameraToFrag = normalize( vWorldPosition - cameraPosition );
15+
16+
}
717
818
// Transforming Normal Vectors with the Inverse Transformation
919
vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
1020
1121
#ifdef ENVMAP_MODE_REFLECTION
1222
13-
vec3 reflectVec = reflect( cameraToVertex, worldNormal );
23+
vec3 reflectVec = reflect( cameraToFrag, worldNormal );
1424
1525
#else
1626
17-
vec3 reflectVec = refract( cameraToVertex, worldNormal, refractionRatio );
27+
vec3 reflectVec = refract( cameraToFrag, worldNormal, refractionRatio );
1828
1929
#endif
2030

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ export default /* glsl */`
77
88
#else
99
10-
vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );
10+
vec3 cameraToVertex;
11+
12+
if ( isOrthographic ) {
13+
14+
cameraToVertex = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );
15+
16+
} else {
17+
18+
cameraToVertex = normalize( worldPosition.xyz - cameraPosition );
19+
20+
}
1121
1222
vec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );
1323

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ GeometricContext geometry;
1818
1919
geometry.position = - vViewPosition;
2020
geometry.normal = normal;
21-
geometry.viewDir = normalize( vViewPosition );
21+
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
2222
2323
#ifdef CLEARCOAT
2424

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ vec3 diffuse = vec3( 1.0 );
44
GeometricContext geometry;
55
geometry.position = mvPosition.xyz;
66
geometry.normal = normalize( transformedNormal );
7-
geometry.viewDir = normalize( -mvPosition.xyz );
7+
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );
88
99
GeometricContext backGeometry;
1010
backGeometry.position = geometry.position;

src/renderers/webgl/WebGLProgram.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ function WebGLProgram( renderer, extensions, cacheKey, material, shader, paramet
496496
'uniform mat4 viewMatrix;',
497497
'uniform mat3 normalMatrix;',
498498
'uniform vec3 cameraPosition;',
499+
'uniform bool isOrthographic;',
499500

500501
'#ifdef USE_INSTANCING',
501502

@@ -619,6 +620,7 @@ function WebGLProgram( renderer, extensions, cacheKey, material, shader, paramet
619620

620621
'uniform mat4 viewMatrix;',
621622
'uniform vec3 cameraPosition;',
623+
'uniform bool isOrthographic;',
622624

623625
( parameters.toneMapping !== NoToneMapping ) ? '#define TONE_MAPPING' : '',
624626
( parameters.toneMapping !== NoToneMapping ) ? ShaderChunk[ 'tonemapping_pars_fragment' ] : '', // this code is required here because it is used by the toneMapping() function defined below

0 commit comments

Comments
 (0)