提交 8c5bc528 编写于 作者: B Ben Houston

cleaner and more GC efficient.

上级 7985c8b0
...@@ -2422,7 +2422,7 @@ THREE.WebGLRenderer = function ( parameters ) { ...@@ -2422,7 +2422,7 @@ THREE.WebGLRenderer = function ( parameters ) {
var l, ll, light, var l, ll, light,
r = 0, g = 0, b = 0, r = 0, g = 0, b = 0,
color, skyColor, groundColor, color,
intensity, intensity,
distance, distance,
...@@ -2430,17 +2430,10 @@ THREE.WebGLRenderer = function ( parameters ) { ...@@ -2430,17 +2430,10 @@ THREE.WebGLRenderer = function ( parameters ) {
viewMatrix = camera.matrixWorldInverse, viewMatrix = camera.matrixWorldInverse,
function adjustArray( array, newLength ) { zlights.directional = [];
if( array.length !== newLength ) { zlights.point = [];
return Array( newLength ); zlights.spot = [];
} zlights.hemi = [];
return array;
}
var directionalLights = adjustArray( zlights.directional );
var pointLights = adjustArray( zlights.point );
var spotLights = adjustArray( zlights.spot );
var hemisphereLights = adjustArray( zlights.hemi );
for ( l = 0, ll = lights.length; l < ll; l ++ ) { for ( l = 0, ll = lights.length; l < ll; l ++ ) {
...@@ -2462,10 +2455,18 @@ THREE.WebGLRenderer = function ( parameters ) { ...@@ -2462,10 +2455,18 @@ THREE.WebGLRenderer = function ( parameters ) {
} else if ( light instanceof THREE.DirectionalLight ) { } else if ( light instanceof THREE.DirectionalLight ) {
var directionalLight = directionalLights[i]; if( ! light.__webglUniforms ) {
light.__webglUniforms = {
position: new THREE.Vector3(),
color: new THREE.Color(),
distance: 0
}
}
var lightUniforms = directionalLights[i] = light.__webglUniforms;
if ( ! light.visible ) { if ( ! light.visible ) {
directionlLight.color = new THREE.Color( 0x000000 ); lightUniforms.color.setRGB( 0, 0, 0 );
continue; continue;
} }
...@@ -2474,77 +2475,100 @@ THREE.WebGLRenderer = function ( parameters ) { ...@@ -2474,77 +2475,100 @@ THREE.WebGLRenderer = function ( parameters ) {
_direction.sub( _vector3 ); _direction.sub( _vector3 );
_direction.transformDirection( viewMatrix ); _direction.transformDirection( viewMatrix );
directionlLight.position = _direction.clone(); lightUniforms.position.copy( _direction );
directionlLight.color = color.clone().multiplyScalar( intensity ); lightUniforms.color.copy( color ).multiplyScalar( intensity );
directionlLight.distance = distance; lightUniforms.distance = distance;
} else if ( light instanceof THREE.PointLight ) { } else if ( light instanceof THREE.PointLight ) {
if ( ! light.visible ) continue; if( ! light.__webglUniforms ) {
light.__webglUniforms = {
position: new THREE.Vector3(),
color: new THREE.Color(),
distance: 0,
decay: 0
}
}
var lightUniforms = pointLights[i] = light.__webglUniforms;
if ( ! light.visible ) {
lightUniforms.color.setRGB( 0, 0, 0 );
continue;
}
_vector3.setFromMatrixPosition( light.matrixWorld ); _vector3.setFromMatrixPosition( light.matrixWorld );
_vector3.applyMatrix4( viewMatrix ); _vector3.applyMatrix4( viewMatrix );
var pointLight = pointLights[i]; lightUniforms.position.copy( _vector3 );
lightUniforms.color.copy( color ).multiplyScalar( intensity );
pointLight.position = _vector3.clone(); lightUniforms.distance = distance;
pointLight.color = color.clone().multiplyScalar( intensity ); lightUniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;
pointLight.distance = distance;
pointLight.decay = ( light.distance === 0 ) ? 0.0 : light.decay;
} else if ( light instanceof THREE.SpotLight ) { } else if ( light instanceof THREE.SpotLight ) {
if ( ! light.visible ) continue; if( ! light.__webglUniforms ) {
light.__webglUniforms = {
position: new THREE.Vector3(),
direction: new THREE.Vector3(),
color: new THREE.Color(),
distance: 0,
decay: 0,
angleCos: 0
}
}
var lightUniforms = spotLights[i] = light.__webglUniforms;
var spotLight = spotLights[i]; if ( ! light.visible ) {
lightUniforms.color.setRGB( 0, 0, 0 );
continue;
}
_direction.setFromMatrixPosition( light.matrixWorld ); _direction.setFromMatrixPosition( light.matrixWorld );
_vector3.copy( _direction ).applyMatrix4( viewMatrix ); _vector3.copy( _direction ).applyMatrix4( viewMatrix );
spotLight.position = _vector3.clone(); lightUniforms.position.copy( _vector3 );
spotLight.color = color.clone().multiplyScalar( intensity ); lightUniforms.color.copy( color ).multiplyScalar( intensity );
spotLight.distance = distance; lightUniforms.distance = distance;
_vector3.setFromMatrixPosition( light.target.matrixWorld ); _vector3.setFromMatrixPosition( light.target.matrixWorld );
_direction.sub( _vector3 ); _direction.sub( _vector3 );
_direction.transformDirection( viewMatrix ); _direction.transformDirection( viewMatrix );
spotLight.direction = _direction.clone(); lightUniforms.direction.copy( _direction);
spotLight.angleCos = Math.cos( light.angle ); lightUniforms.angleCos = Math.cos( light.angle );
spotLight.exponent = light.exponent; lightUniforms.exponent = light.exponent;
spotLight.decay = ( light.distance === 0 ) ? 0.0 : light.decay; lightUniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;
} else if ( light instanceof THREE.HemisphereLight ) { } else if ( light instanceof THREE.HemisphereLight ) {
if ( ! light.visible ) continue; if( ! light.__webglUniforms ) {
light.__webglUniforms = {
position: new THREE.Vector3(),
skyColor: new THREE.Color(),
groundColor: new THREE.Color()
}
}
var lightUniforms = hemisphereLights[i] = light.__webglUniforms;
if ( ! light.visible ) {
lightUniforms.skyColor.setRGB( 0, 0, 0 );
continue;
}
_direction.setFromMatrixPosition( light.matrixWorld ); _direction.setFromMatrixPosition( light.matrixWorld );
_direction.transformDirection( viewMatrix ); _direction.transformDirection( viewMatrix );
var hemisphereLight = hemisphereLights[i]; lightUniforms.position.copy( _direction);
lightUniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
spotLight.position = _direction.clone(); lightUniforms.groundColor.copy( groundColor ).multiplyScalar( intensity );
spotLight.skyColor = light.color.clone().multiplyScalar( intensity );
spotLight.groundColor = light.groundColor.clone().multiplyScalar( intensity );
} }
} }
// null eventual remains from removed lights
// (this is to avoid if in shader)
for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0;
for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0;
for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0;
for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0;
for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0;
zlights.directional.length = dirLength;
zlights.point.length = pointLength;
zlights.spot.length = spotLength;
zlights.hemi.length = hemiLength;
zlights.ambient[ 0 ] = r; zlights.ambient[ 0 ] = r;
zlights.ambient[ 1 ] = g; zlights.ambient[ 1 ] = g;
zlights.ambient[ 2 ] = b; zlights.ambient[ 2 ] = b;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册