WebGLShadowMap.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
  6. var _gl = _renderer.context,
  7. _state = _renderer.state,
  8. _frustum = new THREE.Frustum(),
  9. _projScreenMatrix = new THREE.Matrix4(),
  10. _min = new THREE.Vector3(),
  11. _max = new THREE.Vector3(),
  12. _lookTarget = new THREE.Vector3(),
  13. _lightPositionWorld = new THREE.Vector3(),
  14. _renderList = [],
  15. _MorphingFlag = 1,
  16. _SkinningFlag = 2,
  17. _NumberOfMaterialVariants = _MorphingFlag | _SkinningFlag,
  18. _depthMaterials = new Array( _NumberOfMaterialVariants ),
  19. _distanceMaterials = new Array( _NumberOfMaterialVariants );
  20. var cubeDirections = [ new THREE.Vector3( 1, 0, 0 ), new THREE.Vector3( - 1, 0, 0 ), new THREE.Vector3( 0, 0, 1 ),
  21. new THREE.Vector3( 0, 0, - 1 ), new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, - 1, 0 ) ];
  22. var cubeUps = [ new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 1, 0 ),
  23. new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 0, 0, - 1 ) ];
  24. var cube2DViewPorts = [ new THREE.Vector4(), new THREE.Vector4(), new THREE.Vector4(),
  25. new THREE.Vector4(), new THREE.Vector4(), new THREE.Vector4() ];
  26. var _vector4 = new THREE.Vector4();
  27. // init
  28. var depthShader = THREE.ShaderLib[ "depthRGBA" ];
  29. var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );
  30. var distanceShader = THREE.ShaderLib[ "distanceRGBA" ];
  31. var distanceUniforms = THREE.UniformsUtils.clone( distanceShader.uniforms );
  32. for ( var i = 0; i !== _NumberOfMaterialVariants; ++ i ) {
  33. var useMorphing = ( i & _MorphingFlag ) !== 0;
  34. var useSkinning = ( i & _SkinningFlag ) !== 0;
  35. var depthMaterial = new THREE.ShaderMaterial( {
  36. uniforms: depthUniforms,
  37. vertexShader: depthShader.vertexShader,
  38. fragmentShader: depthShader.fragmentShader,
  39. morphTargets: useMorphing,
  40. skinning: useSkinning
  41. } );
  42. depthMaterial._shadowPass = true;
  43. _depthMaterials[ i ] = depthMaterial;
  44. var distanceMaterial = new THREE.ShaderMaterial( {
  45. uniforms: distanceUniforms,
  46. vertexShader: distanceShader.vertexShader,
  47. fragmentShader: distanceShader.fragmentShader,
  48. morphTargets: useMorphing,
  49. skinning: useSkinning
  50. } );
  51. distanceMaterial._shadowPass = true;
  52. _distanceMaterials[ i ] = distanceMaterial;
  53. }
  54. //
  55. var scope = this;
  56. this.enabled = false;
  57. this.autoUpdate = true;
  58. this.needsUpdate = false;
  59. this.type = THREE.PCFShadowMap;
  60. this.cullFace = THREE.CullFaceFront;
  61. this.render = function ( scene ) {
  62. var faceCount, isPointLight;
  63. if ( scope.enabled === false ) return;
  64. if ( scope.autoUpdate === false && scope.needsUpdate === false ) return;
  65. // set GL state for depth map
  66. _gl.clearColor( 1, 1, 1, 1 );
  67. _state.disable( _gl.BLEND );
  68. _state.enable( _gl.CULL_FACE );
  69. _gl.frontFace( _gl.CCW );
  70. if ( scope.cullFace === THREE.CullFaceFront ) {
  71. _gl.cullFace( _gl.FRONT );
  72. } else {
  73. _gl.cullFace( _gl.BACK );
  74. }
  75. _state.setDepthTest( true );
  76. // render depth map
  77. for ( var i = 0, il = _lights.length; i < il; i ++ ) {
  78. var light = _lights[ i ];
  79. if ( light instanceof THREE.PointLight ) {
  80. faceCount = 6;
  81. isPointLight = true;
  82. var vpWidth = light.shadowMapWidth / 4.0;
  83. var vpHeight = light.shadowMapHeight / 2.0;
  84. // These viewports map a cube-map onto a 2D texture with the
  85. // following orientation:
  86. //
  87. // xzXZ
  88. // y Y
  89. //
  90. // X - Positive x direction
  91. // x - Negative x direction
  92. // Y - Positive y direction
  93. // y - Negative y direction
  94. // Z - Positive z direction
  95. // z - Negative z direction
  96. // positive X
  97. cube2DViewPorts[ 0 ].set( vpWidth * 2, vpHeight, vpWidth, vpHeight );
  98. // negative X
  99. cube2DViewPorts[ 1 ].set( 0, vpHeight, vpWidth, vpHeight );
  100. // positive Z
  101. cube2DViewPorts[ 2 ].set( vpWidth * 3, vpHeight, vpWidth, vpHeight );
  102. // negative Z
  103. cube2DViewPorts[ 3 ].set( vpWidth, vpHeight, vpWidth, vpHeight );
  104. // positive Y
  105. cube2DViewPorts[ 4 ].set( vpWidth * 3, 0, vpWidth, vpHeight );
  106. // negative Y
  107. cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
  108. } else {
  109. faceCount = 1;
  110. isPointLight = false;
  111. }
  112. if ( ! light.castShadow ) continue;
  113. if ( ! light.shadowMap ) {
  114. var shadowFilter = THREE.LinearFilter;
  115. if ( scope.type === THREE.PCFSoftShadowMap ) {
  116. shadowFilter = THREE.NearestFilter;
  117. }
  118. var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
  119. light.shadowMap = new THREE.WebGLRenderTarget( light.shadowMapWidth, light.shadowMapHeight, pars );
  120. light.shadowMapSize = new THREE.Vector2( light.shadowMapWidth, light.shadowMapHeight );
  121. light.shadowMatrix = new THREE.Matrix4();
  122. }
  123. if ( ! light.shadowCamera ) {
  124. if ( light instanceof THREE.SpotLight ) {
  125. light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, light.shadowMapWidth / light.shadowMapHeight, light.shadowCameraNear, light.shadowCameraFar );
  126. } else if ( light instanceof THREE.DirectionalLight ) {
  127. light.shadowCamera = new THREE.OrthographicCamera( light.shadowCameraLeft, light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom, light.shadowCameraNear, light.shadowCameraFar );
  128. } else {
  129. light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, 1.0, light.shadowCameraNear, light.shadowCameraFar );
  130. }
  131. scene.add( light.shadowCamera );
  132. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  133. }
  134. if ( light.shadowCameraVisible && ! light.cameraHelper ) {
  135. light.cameraHelper = new THREE.CameraHelper( light.shadowCamera );
  136. scene.add( light.cameraHelper );
  137. }
  138. var shadowMap = light.shadowMap;
  139. var shadowMatrix = light.shadowMatrix;
  140. var shadowCamera = light.shadowCamera;
  141. _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  142. shadowCamera.position.copy( _lightPositionWorld );
  143. // save the existing viewport so it can be restored later
  144. _renderer.getViewport( _vector4 );
  145. _renderer.setRenderTarget( shadowMap );
  146. _renderer.clear();
  147. // render shadow map for each cube face (if omni-directional) or
  148. // run a single pass if not
  149. for ( var face = 0; face < faceCount; face ++ ) {
  150. if ( isPointLight ) {
  151. _lookTarget.copy( shadowCamera.position );
  152. _lookTarget.add( cubeDirections[ face ] );
  153. shadowCamera.up.copy( cubeUps[ face ] );
  154. shadowCamera.lookAt( _lookTarget );
  155. var vpDimensions = cube2DViewPorts[ face ];
  156. _renderer.setViewport( vpDimensions.x, vpDimensions.y, vpDimensions.z, vpDimensions.w );
  157. } else {
  158. _lookTarget.setFromMatrixPosition( light.target.matrixWorld );
  159. shadowCamera.lookAt( _lookTarget );
  160. }
  161. shadowCamera.updateMatrixWorld();
  162. shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
  163. if ( light.cameraHelper ) light.cameraHelper.visible = light.shadowCameraVisible;
  164. if ( light.shadowCameraVisible ) light.cameraHelper.update();
  165. // compute shadow matrix
  166. shadowMatrix.set(
  167. 0.5, 0.0, 0.0, 0.5,
  168. 0.0, 0.5, 0.0, 0.5,
  169. 0.0, 0.0, 0.5, 0.5,
  170. 0.0, 0.0, 0.0, 1.0
  171. );
  172. shadowMatrix.multiply( shadowCamera.projectionMatrix );
  173. shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
  174. // update camera matrices and frustum
  175. _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  176. _frustum.setFromMatrix( _projScreenMatrix );
  177. // set object matrices & frustum culling
  178. _renderList.length = 0;
  179. projectObject( scene, shadowCamera );
  180. // render shadow map
  181. // render regular objects
  182. for ( var j = 0, jl = _renderList.length; j < jl; j ++ ) {
  183. var object = _renderList[ j ];
  184. var geometry = _objects.update( object );
  185. var material = object.material;
  186. if ( material instanceof THREE.MeshFaceMaterial ) {
  187. var groups = geometry.groups;
  188. var materials = material.materials;
  189. for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
  190. var group = groups[ k ];
  191. var groupMaterial = materials[ group.materialIndex ];
  192. if ( groupMaterial.visible === true ) {
  193. var depthMaterial = getDepthMaterial( object, groupMaterial, isPointLight, _lightPositionWorld );
  194. _renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, group );
  195. }
  196. }
  197. } else {
  198. var depthMaterial = getDepthMaterial( object, material, isPointLight, _lightPositionWorld );
  199. _renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, null );
  200. }
  201. }
  202. }
  203. }
  204. // restore GL state
  205. var clearColor = _renderer.getClearColor(),
  206. clearAlpha = _renderer.getClearAlpha();
  207. _renderer.setClearColor( clearColor, clearAlpha );
  208. _state.enable( _gl.BLEND );
  209. if ( scope.cullFace === THREE.CullFaceFront ) {
  210. _gl.cullFace( _gl.BACK );
  211. }
  212. _renderer.setViewport( _vector4.x, _vector4.y, _vector4.z, _vector4.w );
  213. _renderer.resetGLState();
  214. scope.needsUpdate = false;
  215. };
  216. function getDepthMaterial( object, material, isPointLight, lightPositionWorld ) {
  217. var geometry = object.geometry;
  218. var newMaterial = null;
  219. var materialVariants = _depthMaterials;
  220. var customMaterial = object.customDepthMaterial;
  221. if ( isPointLight ) {
  222. materialVariants = _distanceMaterials;
  223. customMaterial = object.customDistanceMaterial;
  224. }
  225. if ( ! customMaterial ) {
  226. var useMorphing = geometry.morphTargets !== undefined &&
  227. geometry.morphTargets.length > 0 && material.morphTargets;
  228. var useSkinning = object instanceof THREE.SkinnedMesh && material.skinning;
  229. var variantIndex = 0;
  230. if ( useMorphing ) variantIndex |= _MorphingFlag;
  231. if ( useSkinning ) variantIndex |= _SkinningFlag;
  232. newMaterial = materialVariants[ variantIndex ];
  233. } else {
  234. newMaterial = customMaterial;
  235. }
  236. newMaterial.visible = material.visible;
  237. newMaterial.wireframe = material.wireframe;
  238. newMaterial.wireframeLinewidth = material.wireframeLinewidth;
  239. if ( isPointLight && newMaterial.uniforms.lightPos !== undefined ) {
  240. newMaterial.uniforms.lightPos.value.copy( lightPositionWorld );
  241. }
  242. return newMaterial;
  243. }
  244. function projectObject( object, camera ) {
  245. if ( object.visible === false ) return;
  246. if ( object instanceof THREE.Mesh || object instanceof THREE.Line || object instanceof THREE.Points ) {
  247. if ( object.castShadow && ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) ) {
  248. var material = object.material;
  249. if ( material.visible === true ) {
  250. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  251. _renderList.push( object );
  252. }
  253. }
  254. }
  255. var children = object.children;
  256. for ( var i = 0, l = children.length; i < l; i ++ ) {
  257. projectObject( children[ i ], camera );
  258. }
  259. }
  260. };
粤ICP备19079148号