Преглед изворни кода

Merge pull request #17767 from sciecode/dev-ortho-light

ShaderChunks: Fix orthographic view direction
Mr.doob пре 5 година
родитељ
комит
a5584f2a6a

+ 10 - 0
src/renderers/WebGLRenderer.js

@@ -1781,6 +1781,16 @@ function WebGLRenderer( parameters ) {
 
 			}
 
+			if ( material.isMeshPhongMaterial ||
+				material.isMeshLambertMaterial ||
+				material.isMeshBasicMaterial ||
+				material.isMeshStandardMaterial ||
+				material.isShaderMaterial ) {
+
+				p_uniforms.setValue( _gl, 'isOrthographic', camera.isOrthographicCamera === true );
+
+			}
+
 			if ( material.isMeshPhongMaterial ||
 				material.isMeshLambertMaterial ||
 				material.isMeshBasicMaterial ||

+ 13 - 3
src/renderers/shaders/ShaderChunk/envmap_fragment.glsl.js

@@ -3,18 +3,28 @@ export default /* glsl */`
 
 	#ifdef ENV_WORLDPOS
 
-		vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );
+		vec3 cameraToFrag;
+		
+		if ( isOrthographic ) {
+
+			cameraToFrag = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );
+
+		}  else {
+
+			cameraToFrag = normalize( vWorldPosition - cameraPosition );
+
+		}
 
 		// Transforming Normal Vectors with the Inverse Transformation
 		vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
 
 		#ifdef ENVMAP_MODE_REFLECTION
 
-			vec3 reflectVec = reflect( cameraToVertex, worldNormal );
+			vec3 reflectVec = reflect( cameraToFrag, worldNormal );
 
 		#else
 
-			vec3 reflectVec = refract( cameraToVertex, worldNormal, refractionRatio );
+			vec3 reflectVec = refract( cameraToFrag, worldNormal, refractionRatio );
 
 		#endif
 

+ 11 - 1
src/renderers/shaders/ShaderChunk/envmap_vertex.glsl.js

@@ -7,7 +7,17 @@ export default /* glsl */`
 
 	#else
 
-		vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );
+		vec3 cameraToVertex;
+
+		if ( isOrthographic ) { 
+
+			cameraToVertex = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );
+
+		} else {
+
+			cameraToVertex = normalize( worldPosition.xyz - cameraPosition );
+
+		}
 
 		vec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );
 

+ 1 - 1
src/renderers/shaders/ShaderChunk/lights_fragment_begin.glsl.js

@@ -18,7 +18,7 @@ GeometricContext geometry;
 
 geometry.position = - vViewPosition;
 geometry.normal = normal;
-geometry.viewDir = normalize( vViewPosition );
+geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
 
 #ifdef CLEARCOAT
 

+ 1 - 1
src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl.js

@@ -4,7 +4,7 @@ vec3 diffuse = vec3( 1.0 );
 GeometricContext geometry;
 geometry.position = mvPosition.xyz;
 geometry.normal = normalize( transformedNormal );
-geometry.viewDir = normalize( -mvPosition.xyz );
+geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );
 
 GeometricContext backGeometry;
 backGeometry.position = geometry.position;

+ 2 - 0
src/renderers/webgl/WebGLProgram.js

@@ -496,6 +496,7 @@ function WebGLProgram( renderer, extensions, cacheKey, material, shader, paramet
 			'uniform mat4 viewMatrix;',
 			'uniform mat3 normalMatrix;',
 			'uniform vec3 cameraPosition;',
+			'uniform bool isOrthographic;',
 
 			'#ifdef USE_INSTANCING',
 
@@ -619,6 +620,7 @@ function WebGLProgram( renderer, extensions, cacheKey, material, shader, paramet
 
 			'uniform mat4 viewMatrix;',
 			'uniform vec3 cameraPosition;',
+			'uniform bool isOrthographic;',
 
 			( parameters.toneMapping !== NoToneMapping ) ? '#define TONE_MAPPING' : '',
 			( parameters.toneMapping !== NoToneMapping ) ? ShaderChunk[ 'tonemapping_pars_fragment' ] : '', // this code is required here because it is used by the toneMapping() function defined below