123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- /**
- * @author alteredq / http://alteredqualia.com/
- */
- THREE.ShadowMapPlugin = function () {
- var _gl,
- _renderer,
- _depthMaterial, _depthMaterialMorph, _depthMaterialSkin, _depthMaterialMorphSkin,
- _frustum = new THREE.Frustum(),
- _projScreenMatrix = new THREE.Matrix4(),
- _min = new THREE.Vector3(),
- _max = new THREE.Vector3(),
- _matrixPosition = new THREE.Vector3();
- this.init = function ( renderer ) {
- _gl = renderer.context;
- _renderer = renderer;
- var depthShader = THREE.ShaderLib[ "depthRGBA" ];
- var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );
- _depthMaterial = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms } );
- _depthMaterialMorph = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true } );
- _depthMaterialSkin = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, skinning: true } );
- _depthMaterialMorphSkin = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true, skinning: true } );
- _depthMaterial._shadowPass = true;
- _depthMaterialMorph._shadowPass = true;
- _depthMaterialSkin._shadowPass = true;
- _depthMaterialMorphSkin._shadowPass = true;
- };
- this.render = function ( scene, camera ) {
- if ( ! ( _renderer.shadowMapEnabled && _renderer.shadowMapAutoUpdate ) ) return;
- this.update( scene, camera );
- };
- this.update = function ( scene, camera ) {
- var i, il, j, jl, n,
- shadowMap, shadowMatrix, shadowCamera,
- program, buffer, material,
- webglObject, object, light,
- renderList,
- lights = [],
- k = 0,
- fog = null;
- // set GL state for depth map
- _gl.clearColor( 1, 1, 1, 1 );
- _gl.disable( _gl.BLEND );
- _gl.enable( _gl.CULL_FACE );
- _gl.frontFace( _gl.CCW );
- if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) {
- _gl.cullFace( _gl.FRONT );
- } else {
- _gl.cullFace( _gl.BACK );
- }
- _renderer.setDepthTest( true );
- // preprocess lights
- // - skip lights that are not casting shadows
- // - create virtual lights for cascaded shadow maps
- for ( i = 0, il = scene.__lights.length; i < il; i ++ ) {
- light = scene.__lights[ i ];
- if ( ! light.castShadow ) continue;
- if ( ( light instanceof THREE.DirectionalLight ) && light.shadowCascade ) {
- for ( n = 0; n < light.shadowCascadeCount; n ++ ) {
- var virtualLight;
- if ( ! light.shadowCascadeArray[ n ] ) {
- virtualLight = createVirtualLight( light, n );
- virtualLight.originalCamera = camera;
- var gyro = new THREE.Gyroscope();
- gyro.position = light.shadowCascadeOffset;
- gyro.add( virtualLight );
- gyro.add( virtualLight.target );
- camera.add( gyro );
- light.shadowCascadeArray[ n ] = virtualLight;
- console.log( "Created virtualLight", virtualLight );
- } else {
- virtualLight = light.shadowCascadeArray[ n ];
- }
- updateVirtualLight( light, n );
- lights[ k ] = virtualLight;
- k ++;
- }
- } else {
- lights[ k ] = light;
- k ++;
- }
- }
- // render depth map
- for ( i = 0, il = lights.length; i < il; i ++ ) {
- light = lights[ i ];
- if ( ! light.shadowMap ) {
- var shadowFilter = THREE.LinearFilter;
- if ( _renderer.shadowMapType === THREE.PCFSoftShadowMap ) {
- shadowFilter = THREE.NearestFilter;
- }
- var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
- light.shadowMap = new THREE.WebGLRenderTarget( light.shadowMapWidth, light.shadowMapHeight, pars );
- light.shadowMapSize = new THREE.Vector2( light.shadowMapWidth, light.shadowMapHeight );
- light.shadowMatrix = new THREE.Matrix4();
- }
- if ( ! light.shadowCamera ) {
- if ( light instanceof THREE.SpotLight ) {
- light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, light.shadowMapWidth / light.shadowMapHeight, light.shadowCameraNear, light.shadowCameraFar );
- } else if ( light instanceof THREE.DirectionalLight ) {
- light.shadowCamera = new THREE.OrthographicCamera( light.shadowCameraLeft, light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom, light.shadowCameraNear, light.shadowCameraFar );
- } else {
- console.error( "Unsupported light type for shadow" );
- continue;
- }
- scene.add( light.shadowCamera );
- if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
- }
- if ( light.shadowCameraVisible && ! light.cameraHelper ) {
- light.cameraHelper = new THREE.CameraHelper( light.shadowCamera );
- light.shadowCamera.add( light.cameraHelper );
- }
- if ( light.isVirtual && virtualLight.originalCamera == camera ) {
- updateShadowCamera( camera, light );
- }
- shadowMap = light.shadowMap;
- shadowMatrix = light.shadowMatrix;
- shadowCamera = light.shadowCamera;
- shadowCamera.position.setFromMatrixPosition( light.matrixWorld );
- _matrixPosition.setFromMatrixPosition( light.target.matrixWorld );
- shadowCamera.lookAt( _matrixPosition );
- shadowCamera.updateMatrixWorld();
- shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
- if ( light.cameraHelper ) light.cameraHelper.visible = light.shadowCameraVisible;
- if ( light.shadowCameraVisible ) light.cameraHelper.update();
- // compute shadow matrix
- 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 );
- shadowMatrix.multiply( shadowCamera.projectionMatrix );
- shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
- // update camera matrices and frustum
- _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
- _frustum.setFromMatrix( _projScreenMatrix );
- // render shadow map
- _renderer.setRenderTarget( shadowMap );
- _renderer.clear();
- // set object matrices & frustum culling
- renderList = scene.__webglObjects;
- for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
- webglObject = renderList[ j ];
- object = webglObject.object;
- webglObject.render = false;
- if ( object.visible && object.castShadow ) {
- if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.intersectsObject( object ) ) {
- object._modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
- webglObject.render = true;
- }
- }
- }
- // render regular objects
- var objectMaterial, useMorphing, useSkinning;
- for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
- webglObject = renderList[ j ];
- if ( webglObject.render ) {
- object = webglObject.object;
- buffer = webglObject.buffer;
- // culling is overriden globally for all objects
- // while rendering depth map
- // need to deal with MeshFaceMaterial somehow
- // in that case just use the first of material.materials for now
- // (proper solution would require to break objects by materials
- // similarly to regular rendering and then set corresponding
- // depth materials per each chunk instead of just once per object)
- objectMaterial = getObjectMaterial( object );
- useMorphing = object.geometry.morphTargets.length > 0 && objectMaterial.morphTargets;
- useSkinning = object instanceof THREE.SkinnedMesh && objectMaterial.skinning;
- if ( object.customDepthMaterial ) {
- material = object.customDepthMaterial;
- } else if ( useSkinning ) {
- material = useMorphing ? _depthMaterialMorphSkin : _depthMaterialSkin;
- } else if ( useMorphing ) {
- material = _depthMaterialMorph;
- } else {
- material = _depthMaterial;
- }
- if ( buffer instanceof THREE.BufferGeometry ) {
- _renderer.renderBufferDirect( shadowCamera, scene.__lights, fog, material, buffer, object );
- } else {
- _renderer.renderBuffer( shadowCamera, scene.__lights, fog, material, buffer, object );
- }
- }
- }
- // set matrices and render immediate objects
- renderList = scene.__webglObjectsImmediate;
- for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
- webglObject = renderList[ j ];
- object = webglObject.object;
- if ( object.visible && object.castShadow ) {
- object._modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
- _renderer.renderImmediateObject( shadowCamera, scene.__lights, fog, _depthMaterial, object );
- }
- }
- }
- // restore GL state
- var clearColor = _renderer.getClearColor(),
- clearAlpha = _renderer.getClearAlpha();
- _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
- _gl.enable( _gl.BLEND );
- if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) {
- _gl.cullFace( _gl.BACK );
- }
- };
- function createVirtualLight( light, cascade ) {
- var virtualLight = new THREE.DirectionalLight();
- virtualLight.isVirtual = true;
- virtualLight.onlyShadow = true;
- virtualLight.castShadow = true;
- virtualLight.shadowCameraNear = light.shadowCameraNear;
- virtualLight.shadowCameraFar = light.shadowCameraFar;
- virtualLight.shadowCameraLeft = light.shadowCameraLeft;
- virtualLight.shadowCameraRight = light.shadowCameraRight;
- virtualLight.shadowCameraBottom = light.shadowCameraBottom;
- virtualLight.shadowCameraTop = light.shadowCameraTop;
- virtualLight.shadowCameraVisible = light.shadowCameraVisible;
- virtualLight.shadowDarkness = light.shadowDarkness;
- virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
- virtualLight.shadowMapWidth = light.shadowCascadeWidth[ cascade ];
- virtualLight.shadowMapHeight = light.shadowCascadeHeight[ cascade ];
- virtualLight.pointsWorld = [];
- virtualLight.pointsFrustum = [];
- var pointsWorld = virtualLight.pointsWorld,
- pointsFrustum = virtualLight.pointsFrustum;
- for ( var i = 0; i < 8; i ++ ) {
- pointsWorld[ i ] = new THREE.Vector3();
- pointsFrustum[ i ] = new THREE.Vector3();
- }
- var nearZ = light.shadowCascadeNearZ[ cascade ];
- var farZ = light.shadowCascadeFarZ[ cascade ];
- pointsFrustum[ 0 ].set( -1, -1, nearZ );
- pointsFrustum[ 1 ].set( 1, -1, nearZ );
- pointsFrustum[ 2 ].set( -1, 1, nearZ );
- pointsFrustum[ 3 ].set( 1, 1, nearZ );
- pointsFrustum[ 4 ].set( -1, -1, farZ );
- pointsFrustum[ 5 ].set( 1, -1, farZ );
- pointsFrustum[ 6 ].set( -1, 1, farZ );
- pointsFrustum[ 7 ].set( 1, 1, farZ );
- return virtualLight;
- }
- // Synchronize virtual light with the original light
- function updateVirtualLight( light, cascade ) {
- var virtualLight = light.shadowCascadeArray[ cascade ];
- virtualLight.position.copy( light.position );
- virtualLight.target.position.copy( light.target.position );
- virtualLight.lookAt( virtualLight.target );
- virtualLight.shadowCameraVisible = light.shadowCameraVisible;
- virtualLight.shadowDarkness = light.shadowDarkness;
- virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
- var nearZ = light.shadowCascadeNearZ[ cascade ];
- var farZ = light.shadowCascadeFarZ[ cascade ];
- var pointsFrustum = virtualLight.pointsFrustum;
- pointsFrustum[ 0 ].z = nearZ;
- pointsFrustum[ 1 ].z = nearZ;
- pointsFrustum[ 2 ].z = nearZ;
- pointsFrustum[ 3 ].z = nearZ;
- pointsFrustum[ 4 ].z = farZ;
- pointsFrustum[ 5 ].z = farZ;
- pointsFrustum[ 6 ].z = farZ;
- pointsFrustum[ 7 ].z = farZ;
- }
- // Fit shadow camera's ortho frustum to camera frustum
- function updateShadowCamera( camera, light ) {
- var shadowCamera = light.shadowCamera,
- pointsFrustum = light.pointsFrustum,
- pointsWorld = light.pointsWorld;
- _min.set( Infinity, Infinity, Infinity );
- _max.set( -Infinity, -Infinity, -Infinity );
- for ( var i = 0; i < 8; i ++ ) {
- var p = pointsWorld[ i ];
- p.copy( pointsFrustum[ i ] );
- THREE.ShadowMapPlugin.__projector.unprojectVector( p, camera );
- p.applyMatrix4( shadowCamera.matrixWorldInverse );
- if ( p.x < _min.x ) _min.x = p.x;
- if ( p.x > _max.x ) _max.x = p.x;
- if ( p.y < _min.y ) _min.y = p.y;
- if ( p.y > _max.y ) _max.y = p.y;
- if ( p.z < _min.z ) _min.z = p.z;
- if ( p.z > _max.z ) _max.z = p.z;
- }
- shadowCamera.left = _min.x;
- shadowCamera.right = _max.x;
- shadowCamera.top = _max.y;
- shadowCamera.bottom = _min.y;
- // can't really fit near/far
- //shadowCamera.near = _min.z;
- //shadowCamera.far = _max.z;
- shadowCamera.updateProjectionMatrix();
- }
- // For the moment just ignore objects that have multiple materials with different animation methods
- // Only the first material will be taken into account for deciding which depth material to use for shadow maps
- function getObjectMaterial( object ) {
- return object.material instanceof THREE.MeshFaceMaterial
- ? object.material.materials[ 0 ]
- : object.material;
- };
- };
- THREE.ShadowMapPlugin.__projector = new THREE.Projector();
|