|
@@ -8,21 +8,17 @@ import { WebGLRenderTarget } from '../WebGLRenderTarget.js';
|
|
|
import { MeshDepthMaterial } from '../../materials/MeshDepthMaterial.js';
|
|
|
import { MeshDistanceMaterial } from '../../materials/MeshDistanceMaterial.js';
|
|
|
import { Vector4 } from '../../math/Vector4.js';
|
|
|
-import { Vector3 } from '../../math/Vector3.js';
|
|
|
import { Vector2 } from '../../math/Vector2.js';
|
|
|
-import { Matrix4 } from '../../math/Matrix4.js';
|
|
|
import { Frustum } from '../../math/Frustum.js';
|
|
|
|
|
|
function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
var _frustum = new Frustum(),
|
|
|
- _projScreenMatrix = new Matrix4(),
|
|
|
|
|
|
_shadowMapSize = new Vector2(),
|
|
|
- _maxShadowMapSize = new Vector2( maxTextureSize, maxTextureSize ),
|
|
|
+ _viewportSize = new Vector2(),
|
|
|
|
|
|
- _lookTarget = new Vector3(),
|
|
|
- _lightPositionWorld = new Vector3(),
|
|
|
+ _viewport = new Vector4(),
|
|
|
|
|
|
_MorphingFlag = 1,
|
|
|
_SkinningFlag = 2,
|
|
@@ -36,21 +32,6 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
var shadowSide = { 0: BackSide, 1: FrontSide, 2: DoubleSide };
|
|
|
|
|
|
- var cubeDirections = [
|
|
|
- new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
|
|
|
- new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
|
|
|
- ];
|
|
|
-
|
|
|
- var cubeUps = [
|
|
|
- new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
|
|
|
- new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 )
|
|
|
- ];
|
|
|
-
|
|
|
- var cube2DViewPorts = [
|
|
|
- new Vector4(), new Vector4(), new Vector4(),
|
|
|
- new Vector4(), new Vector4(), new Vector4()
|
|
|
- ];
|
|
|
-
|
|
|
// init
|
|
|
|
|
|
for ( var i = 0; i !== _NumberOfMaterialVariants; ++ i ) {
|
|
@@ -69,8 +50,6 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
_depthMaterials[ i ] = depthMaterial;
|
|
|
|
|
|
- //
|
|
|
-
|
|
|
var distanceMaterial = new MeshDistanceMaterial( {
|
|
|
|
|
|
morphTargets: useMorphing,
|
|
@@ -82,8 +61,6 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
- //
|
|
|
-
|
|
|
var scope = this;
|
|
|
|
|
|
this.enabled = false;
|
|
@@ -114,13 +91,10 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
// render depth map
|
|
|
|
|
|
- var faceCount;
|
|
|
-
|
|
|
for ( var i = 0, il = lights.length; i < il; i ++ ) {
|
|
|
|
|
|
var light = lights[ i ];
|
|
|
var shadow = light.shadow;
|
|
|
- var isPointLight = light && light.isPointLight;
|
|
|
|
|
|
if ( shadow === undefined ) {
|
|
|
|
|
@@ -129,130 +103,68 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
- var shadowCamera = shadow.camera;
|
|
|
-
|
|
|
_shadowMapSize.copy( shadow.mapSize );
|
|
|
- _shadowMapSize.min( _maxShadowMapSize );
|
|
|
-
|
|
|
- if ( isPointLight ) {
|
|
|
-
|
|
|
- var vpWidth = _shadowMapSize.x;
|
|
|
- var vpHeight = _shadowMapSize.y;
|
|
|
-
|
|
|
- // These viewports map a cube-map onto a 2D texture with the
|
|
|
- // following orientation:
|
|
|
- //
|
|
|
- // xzXZ
|
|
|
- // y Y
|
|
|
- //
|
|
|
- // X - Positive x direction
|
|
|
- // x - Negative x direction
|
|
|
- // Y - Positive y direction
|
|
|
- // y - Negative y direction
|
|
|
- // Z - Positive z direction
|
|
|
- // z - Negative z direction
|
|
|
-
|
|
|
- // positive X
|
|
|
- cube2DViewPorts[ 0 ].set( vpWidth * 2, vpHeight, vpWidth, vpHeight );
|
|
|
- // negative X
|
|
|
- cube2DViewPorts[ 1 ].set( 0, vpHeight, vpWidth, vpHeight );
|
|
|
- // positive Z
|
|
|
- cube2DViewPorts[ 2 ].set( vpWidth * 3, vpHeight, vpWidth, vpHeight );
|
|
|
- // negative Z
|
|
|
- cube2DViewPorts[ 3 ].set( vpWidth, vpHeight, vpWidth, vpHeight );
|
|
|
- // positive Y
|
|
|
- cube2DViewPorts[ 4 ].set( vpWidth * 3, 0, vpWidth, vpHeight );
|
|
|
- // negative Y
|
|
|
- cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
|
|
|
-
|
|
|
- _shadowMapSize.x *= 4.0;
|
|
|
- _shadowMapSize.y *= 2.0;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if ( shadow.map === null ) {
|
|
|
-
|
|
|
- var pars = { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat };
|
|
|
-
|
|
|
- shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
|
|
|
- shadow.map.texture.name = light.name + ".shadowMap";
|
|
|
|
|
|
- shadowCamera.updateProjectionMatrix();
|
|
|
+ var shadowFrameExtents = shadow.getFrameExtents();
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- if ( shadow.isSpotLightShadow ) {
|
|
|
+ _shadowMapSize.multiply( shadowFrameExtents );
|
|
|
|
|
|
- shadow.update( light );
|
|
|
-
|
|
|
- }
|
|
|
+ _viewportSize.copy( shadow.mapSize );
|
|
|
|
|
|
- var shadowMap = shadow.map;
|
|
|
- var shadowMatrix = shadow.matrix;
|
|
|
+ if ( _shadowMapSize.x > maxTextureSize || _shadowMapSize.y > maxTextureSize ) {
|
|
|
|
|
|
- _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
|
|
|
- shadowCamera.position.copy( _lightPositionWorld );
|
|
|
+ console.warn( 'THREE.WebGLShadowMap:', light, 'has shadow exceeding max texture size, reducing' );
|
|
|
|
|
|
- if ( isPointLight ) {
|
|
|
+ if ( _shadowMapSize.x > maxTextureSize ) {
|
|
|
|
|
|
- faceCount = 6;
|
|
|
+ _viewportSize.x = Math.floor( maxTextureSize / shadowFrameExtents.x );
|
|
|
+ _shadowMapSize.x = _viewportSize.x * shadowFrameExtents.x;
|
|
|
+ shadow.mapSize.x = _viewportSize.x;
|
|
|
|
|
|
- // for point lights we set the shadow matrix to be a translation-only matrix
|
|
|
- // equal to inverse of the light's position
|
|
|
+ }
|
|
|
|
|
|
- shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z );
|
|
|
+ if ( _shadowMapSize.y > maxTextureSize ) {
|
|
|
|
|
|
- } else {
|
|
|
+ _viewportSize.y = Math.floor( maxTextureSize / shadowFrameExtents.y );
|
|
|
+ _shadowMapSize.y = _viewportSize.y * shadowFrameExtents.y;
|
|
|
+ shadow.mapSize.y = _viewportSize.y;
|
|
|
|
|
|
- faceCount = 1;
|
|
|
+ }
|
|
|
|
|
|
- _lookTarget.setFromMatrixPosition( light.target.matrixWorld );
|
|
|
- shadowCamera.lookAt( _lookTarget );
|
|
|
- shadowCamera.updateMatrixWorld();
|
|
|
+ }
|
|
|
|
|
|
- // compute shadow matrix
|
|
|
+ if ( shadow.map === null ) {
|
|
|
|
|
|
- shadowMatrix.set(
|
|
|
- 0.5, 0.0, 0.0, 0.5,
|
|
|
- 0.0, 0.5, 0.0, 0.5,
|
|
|
- 0.0, 0.0, 0.5, 0.5,
|
|
|
- 0.0, 0.0, 0.0, 1.0
|
|
|
- );
|
|
|
+ var pars = { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat };
|
|
|
|
|
|
- shadowMatrix.multiply( shadowCamera.projectionMatrix );
|
|
|
- shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
|
|
|
+ shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
|
|
|
+ shadow.map.texture.name = light.name + ".shadowMap";
|
|
|
|
|
|
}
|
|
|
|
|
|
- _renderer.setRenderTarget( shadowMap );
|
|
|
+ _renderer.setRenderTarget( shadow.map );
|
|
|
_renderer.clear();
|
|
|
|
|
|
- // render shadow map for each cube face (if omni-directional) or
|
|
|
- // run a single pass if not
|
|
|
+ var viewportCount = shadow.getViewportCount();
|
|
|
|
|
|
- for ( var face = 0; face < faceCount; face ++ ) {
|
|
|
+ for ( var vp = 0; vp < viewportCount; vp ++ ) {
|
|
|
|
|
|
- if ( isPointLight ) {
|
|
|
+ var viewport = shadow.getViewport( vp );
|
|
|
|
|
|
- _lookTarget.copy( shadowCamera.position );
|
|
|
- _lookTarget.add( cubeDirections[ face ] );
|
|
|
- shadowCamera.up.copy( cubeUps[ face ] );
|
|
|
- shadowCamera.lookAt( _lookTarget );
|
|
|
- shadowCamera.updateMatrixWorld();
|
|
|
-
|
|
|
- var vpDimensions = cube2DViewPorts[ face ];
|
|
|
- _state.viewport( vpDimensions );
|
|
|
-
|
|
|
- }
|
|
|
+ _viewport.set(
|
|
|
+ _viewportSize.x * viewport.x,
|
|
|
+ _viewportSize.y * viewport.y,
|
|
|
+ _viewportSize.x * viewport.z,
|
|
|
+ _viewportSize.y * viewport.w
|
|
|
+ );
|
|
|
|
|
|
- // update camera matrices and frustum
|
|
|
+ _state.viewport( _viewport );
|
|
|
|
|
|
- _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
|
|
|
- _frustum.setFromMatrix( _projScreenMatrix );
|
|
|
+ shadow.updateMatrices( light, camera, vp );
|
|
|
|
|
|
- // set object matrices & frustum culling
|
|
|
+ _frustum = shadow.getFrustum();
|
|
|
|
|
|
- renderObject( scene, camera, shadowCamera, isPointLight );
|
|
|
+ renderObject( scene, camera, shadow.camera, light );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -264,7 +176,7 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
};
|
|
|
|
|
|
- function getDepthMaterial( object, material, isPointLight, lightPositionWorld, shadowCameraNear, shadowCameraFar ) {
|
|
|
+ function getDepthMaterial( object, material, light, shadowCameraNear, shadowCameraFar ) {
|
|
|
|
|
|
var geometry = object.geometry;
|
|
|
|
|
@@ -273,7 +185,7 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
var materialVariants = _depthMaterials;
|
|
|
var customMaterial = object.customDepthMaterial;
|
|
|
|
|
|
- if ( isPointLight ) {
|
|
|
+ if ( light.isPointLight ) {
|
|
|
|
|
|
materialVariants = _distanceMaterials;
|
|
|
customMaterial = object.customDistanceMaterial;
|
|
@@ -362,9 +274,9 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
result.wireframeLinewidth = material.wireframeLinewidth;
|
|
|
result.linewidth = material.linewidth;
|
|
|
|
|
|
- if ( isPointLight && result.isMeshDistanceMaterial ) {
|
|
|
+ if ( light.isPointLight && result.isMeshDistanceMaterial ) {
|
|
|
|
|
|
- result.referencePosition.copy( lightPositionWorld );
|
|
|
+ result.referencePosition.setFromMatrixPosition( light.matrixWorld );
|
|
|
result.nearDistance = shadowCameraNear;
|
|
|
result.farDistance = shadowCameraFar;
|
|
|
|
|
@@ -374,7 +286,7 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
- function renderObject( object, camera, shadowCamera, isPointLight ) {
|
|
|
+ function renderObject( object, camera, shadowCamera, light ) {
|
|
|
|
|
|
if ( object.visible === false ) return;
|
|
|
|
|
@@ -400,7 +312,7 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
if ( groupMaterial && groupMaterial.visible ) {
|
|
|
|
|
|
- var depthMaterial = getDepthMaterial( object, groupMaterial, isPointLight, _lightPositionWorld, shadowCamera.near, shadowCamera.far );
|
|
|
+ var depthMaterial = getDepthMaterial( object, groupMaterial, light, shadowCamera.near, shadowCamera.far );
|
|
|
_renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group );
|
|
|
|
|
|
}
|
|
@@ -409,7 +321,7 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
} else if ( material.visible ) {
|
|
|
|
|
|
- var depthMaterial = getDepthMaterial( object, material, isPointLight, _lightPositionWorld, shadowCamera.near, shadowCamera.far );
|
|
|
+ var depthMaterial = getDepthMaterial( object, material, light, shadowCamera.near, shadowCamera.far );
|
|
|
_renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null );
|
|
|
|
|
|
}
|
|
@@ -422,7 +334,7 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
|
|
|
|
|
|
for ( var i = 0, l = children.length; i < l; i ++ ) {
|
|
|
|
|
|
- renderObject( children[ i ], camera, shadowCamera, isPointLight );
|
|
|
+ renderObject( children[ i ], camera, shadowCamera, light );
|
|
|
|
|
|
}
|
|
|
|