update method
void
update()
Implementation
void update() {
final camera = data.camera;
final frustums = this.frustums;
// for each frustum we need to find its min-max box aligned with the light orientation
// the position in _lightOrientationMatrix does not matter, as we transform there and back
_lightOrientationMatrix.lookAt( Vector3(), data.lightDirection, _up );
_lightOrientationMatrixInverse.setFrom( _lightOrientationMatrix ).invert();
for (int i = 0; i < frustums.length; i ++ ) {
final light = lights[ i ];
final shadowCam = light.shadow?.camera;
final texelWidth = ( shadowCam!.right - shadowCam.left ) / data.shadowMapSize;
final texelHeight = ( shadowCam.top - shadowCam.bottom ) / data.shadowMapSize;
_cameraToLightMatrix.multiply2( _lightOrientationMatrixInverse, camera.matrixWorld );
frustums[ i ].toSpace( _cameraToLightMatrix, _lightSpaceFrustum );
final nearVerts = _lightSpaceFrustum.vertices.near;
final farVerts = _lightSpaceFrustum.vertices.far;
_bbox.empty();
for (int j = 0; j < 4; j ++ ) {
_bbox.expandByPoint( nearVerts[ j ] );
_bbox.expandByPoint( farVerts[ j ] );
}
_bbox.getCenter( _center );
_center.z = _bbox.max.z + data.lightMargin;
_center.x = ( _center.x / texelWidth ).floor() * texelWidth;
_center.y = ( _center.y / texelHeight ).floor() * texelHeight;
_center.applyMatrix4( _lightOrientationMatrix );
light.position.setFrom( _center );
light.target?.position.setFrom( _center );
light.target?.position.x += data.lightDirection.x;
light.target?.position.y += data.lightDirection.y;
light.target?.position.z += data.lightDirection.z;
}
}