Skip to content

Commit 5e43534

Browse files
committed
WebGLShadowMap: Automatically compute final pointlight shadow texture size.
1 parent 3b8faeb commit 5e43534

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

examples/webgl_shadowmap_pointlight.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@
7373
pointLight.shadowCameraNear = 1;
7474
pointLight.shadowCameraFar = 30;
7575
// pointLight.shadowCameraVisible = true;
76-
pointLight.shadowMapWidth = 2048;
77-
pointLight.shadowMapHeight = 1024;
7876
pointLight.shadowBias = 0.01;
7977

8078
var geometry = new THREE.SphereGeometry( 0.3, 32, 32 );

src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200

201201
float getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {
202202

203-
vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
203+
vec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );
204204

205205
// for point lights, the uniform @vShadowCoord is re-purposed to hold
206206
// the distance from the light to the world-space position of the fragment.
@@ -213,7 +213,7 @@
213213

214214
#if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT )
215215

216-
vec2 offset = vec2( - 1, 1 ) * shadowRadius * 2.0 * texelSize.y;
216+
vec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;
217217

218218
return (
219219
texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +

src/renderers/webgl/WebGLShadowMap.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
1010
_frustum = new THREE.Frustum(),
1111
_projScreenMatrix = new THREE.Matrix4(),
1212

13+
_shadowMapSize = new THREE.Vector2(),
14+
1315
_lookTarget = new THREE.Vector3(),
1416
_lightPositionWorld = new THREE.Vector3(),
1517

@@ -115,15 +117,16 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
115117

116118
var shadow = light.shadow;
117119
var shadowCamera = shadow.camera;
118-
var shadowMapSize = shadow.mapSize;
120+
121+
_shadowMapSize.copy( shadow.mapSize );
119122

120123
if ( light instanceof THREE.PointLight ) {
121124

122125
faceCount = 6;
123126
isPointLight = true;
124127

125-
var vpWidth = shadowMapSize.x / 4.0;
126-
var vpHeight = shadowMapSize.y / 2.0;
128+
var vpWidth = _shadowMapSize.x;
129+
var vpHeight = _shadowMapSize.y;
127130

128131
// These viewports map a cube-map onto a 2D texture with the
129132
// following orientation:
@@ -151,6 +154,9 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
151154
// negative Y
152155
cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
153156

157+
_shadowMapSize.x *= 4.0;
158+
_shadowMapSize.y *= 2.0;
159+
154160
} else {
155161

156162
faceCount = 1;
@@ -162,13 +168,13 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
162168

163169
var pars = { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
164170

165-
shadow.map = new THREE.WebGLRenderTarget( shadowMapSize.x, shadowMapSize.y, pars );
171+
shadow.map = new THREE.WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
166172

167173
//
168174

169175
if ( light instanceof THREE.SpotLight ) {
170176

171-
shadowCamera.aspect = shadowMapSize.x / shadowMapSize.y;
177+
shadowCamera.aspect = _shadowMapSize.x / _shadowMapSize.y;
172178

173179
}
174180

0 commit comments

Comments
 (0)