WebGLShadowMap.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. import { FrontSide, BackSide, DoubleSide, RGBAFormat, NearestFilter, PCFShadowMap, RGBADepthPacking, NoBlending } from '../../constants.js';
  6. import { WebGLRenderTarget } from '../WebGLRenderTarget.js';
  7. import { MeshDepthMaterial } from '../../materials/MeshDepthMaterial.js';
  8. import { MeshDistanceMaterial } from '../../materials/MeshDistanceMaterial.js';
  9. import { Vector4 } from '../../math/Vector4.js';
  10. import { Vector3 } from '../../math/Vector3.js';
  11. import { Vector2 } from '../../math/Vector2.js';
  12. import { Matrix4 } from '../../math/Matrix4.js';
  13. import { Frustum } from '../../math/Frustum.js';
  14. function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
  15. var _frustum = new Frustum(),
  16. _projScreenMatrix = new Matrix4(),
  17. _shadowMapSize = new Vector2(),
  18. _maxShadowMapSize = new Vector2( maxTextureSize, maxTextureSize ),
  19. _lookTarget = new Vector3(),
  20. _lightPositionWorld = new Vector3(),
  21. _MorphingFlag = 1,
  22. _SkinningFlag = 2,
  23. _NumberOfMaterialVariants = ( _MorphingFlag | _SkinningFlag ) + 1,
  24. _depthMaterials = new Array( _NumberOfMaterialVariants ),
  25. _distanceMaterials = new Array( _NumberOfMaterialVariants ),
  26. _materialCache = {};
  27. var shadowSide = { 0: BackSide, 1: FrontSide, 2: DoubleSide };
  28. var cubeDirections = [
  29. new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
  30. new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
  31. ];
  32. var cubeUps = [
  33. new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
  34. new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 )
  35. ];
  36. var cube2DViewPorts = [
  37. new Vector4(), new Vector4(), new Vector4(),
  38. new Vector4(), new Vector4(), new Vector4()
  39. ];
  40. // init
  41. for ( var i = 0; i !== _NumberOfMaterialVariants; ++ i ) {
  42. var useMorphing = ( i & _MorphingFlag ) !== 0;
  43. var useSkinning = ( i & _SkinningFlag ) !== 0;
  44. var depthMaterial = new MeshDepthMaterial( {
  45. depthPacking: RGBADepthPacking,
  46. morphTargets: useMorphing,
  47. skinning: useSkinning
  48. } );
  49. _depthMaterials[ i ] = depthMaterial;
  50. //
  51. var distanceMaterial = new MeshDistanceMaterial( {
  52. morphTargets: useMorphing,
  53. skinning: useSkinning
  54. } );
  55. _distanceMaterials[ i ] = distanceMaterial;
  56. }
  57. //
  58. var scope = this;
  59. this.enabled = false;
  60. this.autoUpdate = true;
  61. this.needsUpdate = false;
  62. this.type = PCFShadowMap;
  63. this.render = function ( lights, scene, camera ) {
  64. if ( scope.enabled === false ) return;
  65. if ( scope.autoUpdate === false && scope.needsUpdate === false ) return;
  66. if ( lights.length === 0 ) return;
  67. var _state = _renderer.state;
  68. // Set GL state for depth map.
  69. _state.setBlending( NoBlending );
  70. _state.buffers.color.setClear( 1, 1, 1, 1 );
  71. _state.buffers.depth.setTest( true );
  72. _state.setScissorTest( false );
  73. // render depth map
  74. var faceCount;
  75. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  76. var light = lights[ i ];
  77. var shadow = light.shadow;
  78. var isPointLight = light && light.isPointLight;
  79. if ( shadow === undefined ) {
  80. console.warn( 'THREE.WebGLShadowMap:', light, 'has no shadow.' );
  81. continue;
  82. }
  83. var shadowCamera = shadow.camera;
  84. _shadowMapSize.copy( shadow.mapSize );
  85. _shadowMapSize.min( _maxShadowMapSize );
  86. if ( isPointLight ) {
  87. var vpWidth = _shadowMapSize.x;
  88. var vpHeight = _shadowMapSize.y;
  89. // These viewports map a cube-map onto a 2D texture with the
  90. // following orientation:
  91. //
  92. // xzXZ
  93. // y Y
  94. //
  95. // X - Positive x direction
  96. // x - Negative x direction
  97. // Y - Positive y direction
  98. // y - Negative y direction
  99. // Z - Positive z direction
  100. // z - Negative z direction
  101. // positive X
  102. cube2DViewPorts[ 0 ].set( vpWidth * 2, vpHeight, vpWidth, vpHeight );
  103. // negative X
  104. cube2DViewPorts[ 1 ].set( 0, vpHeight, vpWidth, vpHeight );
  105. // positive Z
  106. cube2DViewPorts[ 2 ].set( vpWidth * 3, vpHeight, vpWidth, vpHeight );
  107. // negative Z
  108. cube2DViewPorts[ 3 ].set( vpWidth, vpHeight, vpWidth, vpHeight );
  109. // positive Y
  110. cube2DViewPorts[ 4 ].set( vpWidth * 3, 0, vpWidth, vpHeight );
  111. // negative Y
  112. cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
  113. _shadowMapSize.x *= 4.0;
  114. _shadowMapSize.y *= 2.0;
  115. }
  116. if ( shadow.map === null ) {
  117. var pars = { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat };
  118. shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
  119. shadow.map.texture.name = light.name + ".shadowMap";
  120. shadowCamera.updateProjectionMatrix();
  121. }
  122. if ( shadow.isSpotLightShadow ) {
  123. shadow.update( light );
  124. }
  125. var shadowMap = shadow.map;
  126. var shadowMatrix = shadow.matrix;
  127. _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  128. shadowCamera.position.copy( _lightPositionWorld );
  129. if ( isPointLight ) {
  130. faceCount = 6;
  131. // for point lights we set the shadow matrix to be a translation-only matrix
  132. // equal to inverse of the light's position
  133. shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z );
  134. } else {
  135. faceCount = 1;
  136. _lookTarget.setFromMatrixPosition( light.target.matrixWorld );
  137. shadowCamera.lookAt( _lookTarget );
  138. shadowCamera.updateMatrixWorld();
  139. // compute shadow matrix
  140. shadowMatrix.set(
  141. 0.5, 0.0, 0.0, 0.5,
  142. 0.0, 0.5, 0.0, 0.5,
  143. 0.0, 0.0, 0.5, 0.5,
  144. 0.0, 0.0, 0.0, 1.0
  145. );
  146. shadowMatrix.multiply( shadowCamera.projectionMatrix );
  147. shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
  148. }
  149. _renderer.setRenderTarget( shadowMap );
  150. _renderer.clear();
  151. // render shadow map for each cube face (if omni-directional) or
  152. // run a single pass if not
  153. for ( var face = 0; face < faceCount; face ++ ) {
  154. if ( isPointLight ) {
  155. _lookTarget.copy( shadowCamera.position );
  156. _lookTarget.add( cubeDirections[ face ] );
  157. shadowCamera.up.copy( cubeUps[ face ] );
  158. shadowCamera.lookAt( _lookTarget );
  159. shadowCamera.updateMatrixWorld();
  160. var vpDimensions = cube2DViewPorts[ face ];
  161. _state.viewport( vpDimensions );
  162. }
  163. // update camera matrices and frustum
  164. _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  165. _frustum.setFromMatrix( _projScreenMatrix );
  166. // set object matrices & frustum culling
  167. renderObject( scene, camera, shadowCamera, isPointLight );
  168. }
  169. }
  170. scope.needsUpdate = false;
  171. };
  172. function getDepthMaterial( object, material, isPointLight, lightPositionWorld, shadowCameraNear, shadowCameraFar ) {
  173. var geometry = object.geometry;
  174. var result = null;
  175. var materialVariants = _depthMaterials;
  176. var customMaterial = object.customDepthMaterial;
  177. if ( isPointLight ) {
  178. materialVariants = _distanceMaterials;
  179. customMaterial = object.customDistanceMaterial;
  180. }
  181. if ( ! customMaterial ) {
  182. var useMorphing = false;
  183. if ( material.morphTargets ) {
  184. if ( geometry && geometry.isBufferGeometry ) {
  185. useMorphing = geometry.morphAttributes && geometry.morphAttributes.position && geometry.morphAttributes.position.length > 0;
  186. } else if ( geometry && geometry.isGeometry ) {
  187. useMorphing = geometry.morphTargets && geometry.morphTargets.length > 0;
  188. }
  189. }
  190. if ( object.isSkinnedMesh && material.skinning === false ) {
  191. console.warn( 'THREE.WebGLShadowMap: THREE.SkinnedMesh with material.skinning set to false:', object );
  192. }
  193. var useSkinning = object.isSkinnedMesh && material.skinning;
  194. var variantIndex = 0;
  195. if ( useMorphing ) variantIndex |= _MorphingFlag;
  196. if ( useSkinning ) variantIndex |= _SkinningFlag;
  197. result = materialVariants[ variantIndex ];
  198. } else {
  199. result = customMaterial;
  200. }
  201. if ( _renderer.localClippingEnabled &&
  202. material.clipShadows === true &&
  203. material.clippingPlanes.length !== 0 ) {
  204. // in this case we need a unique material instance reflecting the
  205. // appropriate state
  206. var keyA = result.uuid, keyB = material.uuid;
  207. var materialsForVariant = _materialCache[ keyA ];
  208. if ( materialsForVariant === undefined ) {
  209. materialsForVariant = {};
  210. _materialCache[ keyA ] = materialsForVariant;
  211. }
  212. var cachedMaterial = materialsForVariant[ keyB ];
  213. if ( cachedMaterial === undefined ) {
  214. cachedMaterial = result.clone();
  215. materialsForVariant[ keyB ] = cachedMaterial;
  216. }
  217. result = cachedMaterial;
  218. }
  219. result.visible = material.visible;
  220. result.wireframe = material.wireframe;
  221. result.side = ( material.shadowSide != null ) ? material.shadowSide : shadowSide[ material.side ];
  222. result.clipShadows = material.clipShadows;
  223. result.clippingPlanes = material.clippingPlanes;
  224. result.clipIntersection = material.clipIntersection;
  225. result.wireframeLinewidth = material.wireframeLinewidth;
  226. result.linewidth = material.linewidth;
  227. if ( isPointLight && result.isMeshDistanceMaterial ) {
  228. result.referencePosition.copy( lightPositionWorld );
  229. result.nearDistance = shadowCameraNear;
  230. result.farDistance = shadowCameraFar;
  231. }
  232. return result;
  233. }
  234. function renderObject( object, camera, shadowCamera, isPointLight ) {
  235. if ( object.visible === false ) return;
  236. var visible = object.layers.test( camera.layers );
  237. if ( visible && ( object.isMesh || object.isLine || object.isPoints ) ) {
  238. if ( object.castShadow && ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) ) {
  239. object.modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
  240. var geometry = _objects.update( object );
  241. var material = object.material;
  242. if ( Array.isArray( material ) ) {
  243. var groups = geometry.groups;
  244. for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
  245. var group = groups[ k ];
  246. var groupMaterial = material[ group.materialIndex ];
  247. if ( groupMaterial && groupMaterial.visible ) {
  248. var depthMaterial = getDepthMaterial( object, groupMaterial, isPointLight, _lightPositionWorld, shadowCamera.near, shadowCamera.far );
  249. _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group );
  250. }
  251. }
  252. } else if ( material.visible ) {
  253. var depthMaterial = getDepthMaterial( object, material, isPointLight, _lightPositionWorld, shadowCamera.near, shadowCamera.far );
  254. _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null );
  255. }
  256. }
  257. }
  258. var children = object.children;
  259. for ( var i = 0, l = children.length; i < l; i ++ ) {
  260. renderObject( children[ i ], camera, shadowCamera, isPointLight );
  261. }
  262. }
  263. }
  264. export { WebGLShadowMap };