2
0
Эх сурвалжийг харах

WebGLShadowMap: Automatically compute final pointlight shadow texture size.

Mr.doob 9 жил өмнө
parent
commit
5e4353429a

+ 0 - 2
examples/webgl_shadowmap_pointlight.html

@@ -73,8 +73,6 @@
 					pointLight.shadowCameraNear = 1;
 					pointLight.shadowCameraFar = 30;
 					// pointLight.shadowCameraVisible = true;
-					pointLight.shadowMapWidth = 2048;
-					pointLight.shadowMapHeight = 1024;
 					pointLight.shadowBias = 0.01;
 
 					var geometry = new THREE.SphereGeometry( 0.3, 32, 32 );

+ 2 - 2
src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl

@@ -200,7 +200,7 @@
 
 	float getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {
 
-		vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
+		vec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );
 
 		// for point lights, the uniform @vShadowCoord is re-purposed to hold
 		// the distance from the light to the world-space position of the fragment.
@@ -213,7 +213,7 @@
 
 		#if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT )
 
-			vec2 offset = vec2( - 1, 1 ) * shadowRadius * 2.0 * texelSize.y;
+			vec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;
 
 			return (
 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +

+ 11 - 5
src/renderers/webgl/WebGLShadowMap.js

@@ -10,6 +10,8 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 	_frustum = new THREE.Frustum(),
 	_projScreenMatrix = new THREE.Matrix4(),
 
+	_shadowMapSize = new THREE.Vector2(),
+
 	_lookTarget = new THREE.Vector3(),
 	_lightPositionWorld = new THREE.Vector3(),
 
@@ -115,15 +117,16 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 
 			var shadow = light.shadow;
 			var shadowCamera = shadow.camera;
-			var shadowMapSize = shadow.mapSize;
+
+			_shadowMapSize.copy( shadow.mapSize );
 
 			if ( light instanceof THREE.PointLight ) {
 
 				faceCount = 6;
 				isPointLight = true;
 
-				var vpWidth = shadowMapSize.x / 4.0;
-				var vpHeight = shadowMapSize.y / 2.0;
+				var vpWidth = _shadowMapSize.x;
+				var vpHeight = _shadowMapSize.y;
 
 				// These viewports map a cube-map onto a 2D texture with the
 				// following orientation:
@@ -151,6 +154,9 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 				// negative Y
 				cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
 
+				_shadowMapSize.x *= 4.0;
+				_shadowMapSize.y *= 2.0;
+
 			} else {
 
 				faceCount = 1;
@@ -162,13 +168,13 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 
 				var pars = { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
 
-				shadow.map = new THREE.WebGLRenderTarget( shadowMapSize.x, shadowMapSize.y, pars );
+				shadow.map = new THREE.WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
 
 				//
 
 				if ( light instanceof THREE.SpotLight ) {
 
-					shadowCamera.aspect = shadowMapSize.x / shadowMapSize.y;
+					shadowCamera.aspect = _shadowMapSize.x / _shadowMapSize.y;
 
 				}