From 867baa0601f9ac08aa6c9e31d419100ed0d87d53 Mon Sep 17 00:00:00 2001 From: "Mr.doob" Date: Thu, 17 Mar 2016 02:35:35 +0000 Subject: [PATCH] WebGLRenderer: More shadow collection refactoring. See #8379. --- examples/webgl_shadowmap_viewer.html | 2 +- src/renderers/WebGLRenderer.js | 41 ++++++++++++++++++--------- src/renderers/webgl/WebGLProgram.js | 2 -- src/renderers/webgl/WebGLPrograms.js | 7 ++--- src/renderers/webgl/WebGLShadowMap.js | 22 ++------------ 5 files changed, 32 insertions(+), 42 deletions(-) diff --git a/examples/webgl_shadowmap_viewer.html b/examples/webgl_shadowmap_viewer.html index 2c6846d09f..0ec12307a6 100644 --- a/examples/webgl_shadowmap_viewer.html +++ b/examples/webgl_shadowmap_viewer.html @@ -106,7 +106,7 @@ color: 0xff0000, shininess: 150, specular: 0x222222, - shading: THREE.SmoothShading, + shading: THREE.SmoothShading } ); torusKnot = new THREE.Mesh( geometry, material ); diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 81c99c84ba..86316a1497 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -139,8 +139,7 @@ THREE.WebGLRenderer = function ( parameters ) { pointShadowMatrix: [], hemi: [], - shadows: 0, - shadowsPointLight: 0 + shadows: [] }, @@ -303,7 +302,7 @@ THREE.WebGLRenderer = function ( parameters ) { // shadow map - var shadowMap = new THREE.WebGLShadowMap( this, lights, objects ); + var shadowMap = new THREE.WebGLShadowMap( this, _lights, objects ); this.shadowMap = shadowMap; @@ -1160,6 +1159,8 @@ THREE.WebGLRenderer = function ( parameters ) { // + setupShadows( lights ); + shadowMap.render( scene, camera ); setupLights( lights, camera ); @@ -1625,7 +1626,8 @@ THREE.WebGLRenderer = function ( parameters ) { } - if ( materialProperties.lightsHash !== _lights.hash ) { + if ( materialProperties.lightsHash !== undefined && + materialProperties.lightsHash !== _lights.hash ) { material.needsUpdate = true; @@ -2588,6 +2590,26 @@ THREE.WebGLRenderer = function ( parameters ) { } + function setupShadows ( lights ) { + + var lightShadowsLength = 0; + + for ( var i = 0, l = lights.length; i < l; i ++ ) { + + var light = lights[ i ]; + + if ( light.castShadow ) { + + _lights.shadows[ lightShadowsLength ++ ] = light; + + } + + } + + _lights.shadows.length = lightShadowsLength; + + } + function setupLights ( lights, camera ) { var l, ll, light, @@ -2603,9 +2625,6 @@ THREE.WebGLRenderer = function ( parameters ) { spotLength = 0, hemiLength = 0; - _lights.shadows = 0; - _lights.shadowsPointLight = 0; - for ( l = 0, ll = lights.length; l < ll; l ++ ) { light = lights[ l ]; @@ -2638,8 +2657,6 @@ THREE.WebGLRenderer = function ( parameters ) { uniforms.shadowRadius = light.shadow.radius; uniforms.shadowMapSize = light.shadow.mapSize; - _lights.shadows ++; - } _lights.directionalShadowMap[ directionalLength ] = light.shadow.map; @@ -2673,8 +2690,6 @@ THREE.WebGLRenderer = function ( parameters ) { uniforms.shadowRadius = light.shadow.radius; uniforms.shadowMapSize = light.shadow.mapSize; - _lights.shadows ++; - } _lights.spotShadowMap[ spotLength ] = light.shadow.map; @@ -2700,8 +2715,6 @@ THREE.WebGLRenderer = function ( parameters ) { uniforms.shadowRadius = light.shadow.radius; uniforms.shadowMapSize = light.shadow.mapSize; - _lights.shadows ++; - } _lights.pointShadowMap[ pointLength ] = light.shadow.map; @@ -2745,7 +2758,7 @@ THREE.WebGLRenderer = function ( parameters ) { _lights.point.length = pointLength; _lights.hemi.length = hemiLength; - _lights.hash = directionalLength + ',' + pointLength + ',' + spotLength + ',' + hemiLength + ',' + _lights.shadows; + _lights.hash = directionalLength + ',' + pointLength + ',' + spotLength + ',' + hemiLength + ',' + _lights.shadows.length; } diff --git a/src/renderers/webgl/WebGLProgram.js b/src/renderers/webgl/WebGLProgram.js index ebce85fd2d..0bb1186ba0 100644 --- a/src/renderers/webgl/WebGLProgram.js +++ b/src/renderers/webgl/WebGLProgram.js @@ -418,7 +418,6 @@ THREE.WebGLProgram = ( function () { parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '', parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '', - parameters.pointLightShadows > 0 ? '#define POINT_LIGHT_SHADOWS' : '', parameters.sizeAttenuation ? '#define USE_SIZEATTENUATION' : '', @@ -520,7 +519,6 @@ THREE.WebGLProgram = ( function () { parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '', parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '', - parameters.pointLightShadows > 0 ? '#define POINT_LIGHT_SHADOWS' : '', parameters.premultipliedAlpha ? "#define PREMULTIPLIED_ALPHA" : '', diff --git a/src/renderers/webgl/WebGLPrograms.js b/src/renderers/webgl/WebGLPrograms.js index dc671c2026..95da31e0af 100644 --- a/src/renderers/webgl/WebGLPrograms.js +++ b/src/renderers/webgl/WebGLPrograms.js @@ -23,8 +23,7 @@ THREE.WebGLPrograms = function ( renderer, capabilities ) { "maxBones", "useVertexTexture", "morphTargets", "morphNormals", "maxMorphTargets", "maxMorphNormals", "premultipliedAlpha", "numDirLights", "numPointLights", "numSpotLights", "numHemiLights", - "shadowMapEnabled", "pointLightShadows", "toneMapping", 'physicallyCorrectLights', - "shadowMapType", + "shadowMapEnabled", "shadowMapType", "toneMapping", 'physicallyCorrectLights', "alphaTest", "doubleSided", "flipSided" ]; @@ -170,9 +169,7 @@ THREE.WebGLPrograms = function ( renderer, capabilities ) { numSpotLights: lights.spot.length, numHemiLights: lights.hemi.length, - pointLightShadows: lights.shadowsPointLight, - - shadowMapEnabled: renderer.shadowMap.enabled && object.receiveShadow && lights.shadows > 0, + shadowMapEnabled: renderer.shadowMap.enabled && object.receiveShadow && lights.shadows.length > 0, shadowMapType: renderer.shadowMap.type, toneMapping: renderer.toneMapping, diff --git a/src/renderers/webgl/WebGLShadowMap.js b/src/renderers/webgl/WebGLShadowMap.js index 329015dd45..44945596bd 100644 --- a/src/renderers/webgl/WebGLShadowMap.js +++ b/src/renderers/webgl/WebGLShadowMap.js @@ -10,7 +10,7 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) { _frustum = new THREE.Frustum(), _projScreenMatrix = new THREE.Matrix4(), - _lightShadows = [], + _lightShadows = _lights.shadows, _shadowMapSize = new THREE.Vector2(), @@ -97,25 +97,7 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) { if ( scope.enabled === false ) return; if ( scope.autoUpdate === false && scope.needsUpdate === false ) return; - // Collect lights with shadows - - var lightShadowsLength = 0; - - for ( var i = 0, l = _lights.length; i < l; i ++ ) { - - var light = _lights[ i ]; - - if ( light.castShadow ) { - - _lightShadows[ lightShadowsLength ++ ] = light; - - } - - } - - if ( lightShadowsLength === 0 ) return; - - _lightShadows.length = lightShadowsLength; + if ( _lightShadows.length === 0 ) return; // Set GL state for depth map. _state.clearColor( 1, 1, 1, 1 ); -- GitLab