From 1cdc0957319dfd203f314b132fd8ea2178c478bd Mon Sep 17 00:00:00 2001 From: "Mr.doob" Date: Wed, 28 Oct 2015 08:40:36 -0400 Subject: [PATCH] WebGLRenderer: Create lights hash so material remembers the light setup it was created for. See #7440. --- src/renderers/WebGLRenderer.js | 37 ++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 571e7bdc2c..8947902397 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -108,6 +108,8 @@ THREE.WebGLRenderer = function ( parameters ) { _lights = { + hash: '', + ambient: [ 0, 0, 0 ], directional: [], point: [], @@ -1496,6 +1498,8 @@ THREE.WebGLRenderer = function ( parameters ) { material instanceof THREE.MeshPhysicalMaterial || material.lights ) { + materialProperties.lightsHash = _lights.hash; + // wire up the material to this renderer's lighting state uniforms.ambientLightColor.value = _lights.ambient; @@ -1543,7 +1547,20 @@ THREE.WebGLRenderer = function ( parameters ) { var materialProperties = materialsCache.get( material ); - if ( material.needsUpdate || ! materialProperties.program ) { + if ( materialProperties.program === undefined ) { + + material.needsUpdate = true; + + } + + if ( materialProperties.lightsHash !== undefined && + materialProperties.lightsHash !== _lights.hash ) { + + material.needsUpdate = true; + + } + + if ( material.needsUpdate ) { initMaterial( material, lights, fog, object ); material.needsUpdate = false; @@ -2564,11 +2581,6 @@ THREE.WebGLRenderer = function ( parameters ) { viewMatrix = camera.matrixWorldInverse, - directionalCurrent = _lights.directional.length, - pointCurrent = _lights.point.length, - spotCurrent = _lights.spot.length, - hemiCurrent = _lights.hemi.length, - directionalLength = 0, pointLength = 0, spotLength = 0, @@ -2691,22 +2703,13 @@ THREE.WebGLRenderer = function ( parameters ) { _lights.ambient[ 1 ] = g; _lights.ambient[ 2 ] = b; - // Reset materials if light setup changes - - if ( directionalCurrent !== directionalLength || - pointCurrent !== pointLength || - spotCurrent !== spotLength || - hemiCurrent !== hemiLength ) { - - materialsCache.clear(); - - } - _lights.directional.length = directionalLength; _lights.point.length = pointLength; _lights.spot.length = spotLength; _lights.hemi.length = hemiLength; + _lights.hash = directionalLength + ',' + pointLength + ',' + spotLength + ',' + hemiLength; + } // GL state setting -- GitLab