WebGLShadowMap.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { FrontSide, BackSide, DoubleSide, NearestFilter, PCFShadowMap, VSMShadowMap, RGBADepthPacking, NoBlending } from '../../constants.js';
  2. import { WebGLRenderTarget } from '../WebGLRenderTarget.js';
  3. import { MeshDepthMaterial } from '../../materials/MeshDepthMaterial.js';
  4. import { MeshDistanceMaterial } from '../../materials/MeshDistanceMaterial.js';
  5. import { ShaderMaterial } from '../../materials/ShaderMaterial.js';
  6. import { BufferAttribute } from '../../core/BufferAttribute.js';
  7. import { BufferGeometry } from '../../core/BufferGeometry.js';
  8. import { Mesh } from '../../objects/Mesh.js';
  9. import { Vector4 } from '../../math/Vector4.js';
  10. import { Vector2 } from '../../math/Vector2.js';
  11. import { Frustum } from '../../math/Frustum.js';
  12. import * as vsm from '../shaders/ShaderLib/vsm.glsl.js';
  13. function WebGLShadowMap( _renderer, _objects, _capabilities ) {
  14. let _frustum = new Frustum();
  15. const _shadowMapSize = new Vector2(),
  16. _viewportSize = new Vector2(),
  17. _viewport = new Vector4(),
  18. _depthMaterial = new MeshDepthMaterial( { depthPacking: RGBADepthPacking } ),
  19. _distanceMaterial = new MeshDistanceMaterial(),
  20. _materialCache = {},
  21. _maxTextureSize = _capabilities.maxTextureSize;
  22. const shadowSide = { 0: BackSide, 1: FrontSide, 2: DoubleSide };
  23. const shadowMaterialVertical = new ShaderMaterial( {
  24. defines: {
  25. VSM_SAMPLES: 8
  26. },
  27. uniforms: {
  28. shadow_pass: { value: null },
  29. resolution: { value: new Vector2() },
  30. radius: { value: 4.0 }
  31. },
  32. vertexShader: vsm.vertex,
  33. fragmentShader: vsm.fragment
  34. } );
  35. const shadowMaterialHorizontal = shadowMaterialVertical.clone();
  36. shadowMaterialHorizontal.defines.HORIZONTAL_PASS = 1;
  37. const fullScreenTri = new BufferGeometry();
  38. fullScreenTri.setAttribute(
  39. 'position',
  40. new BufferAttribute(
  41. new Float32Array( [ - 1, - 1, 0.5, 3, - 1, 0.5, - 1, 3, 0.5 ] ),
  42. 3
  43. )
  44. );
  45. const fullScreenMesh = new Mesh( fullScreenTri, shadowMaterialVertical );
  46. const scope = this;
  47. this.enabled = false;
  48. this.autoUpdate = true;
  49. this.needsUpdate = false;
  50. this.type = PCFShadowMap;
  51. this.render = function ( lights, scene, camera ) {
  52. if ( scope.enabled === false ) return;
  53. if ( scope.autoUpdate === false && scope.needsUpdate === false ) return;
  54. if ( lights.length === 0 ) return;
  55. const currentRenderTarget = _renderer.getRenderTarget();
  56. const activeCubeFace = _renderer.getActiveCubeFace();
  57. const activeMipmapLevel = _renderer.getActiveMipmapLevel();
  58. const _state = _renderer.state;
  59. // Set GL state for depth map.
  60. _state.setBlending( NoBlending );
  61. _state.buffers.color.setClear( 1, 1, 1, 1 );
  62. _state.buffers.depth.setTest( true );
  63. _state.setScissorTest( false );
  64. // render depth map
  65. for ( let i = 0, il = lights.length; i < il; i ++ ) {
  66. const light = lights[ i ];
  67. const shadow = light.shadow;
  68. if ( shadow === undefined ) {
  69. console.warn( 'THREE.WebGLShadowMap:', light, 'has no shadow.' );
  70. continue;
  71. }
  72. if ( shadow.autoUpdate === false && shadow.needsUpdate === false ) continue;
  73. _shadowMapSize.copy( shadow.mapSize );
  74. const shadowFrameExtents = shadow.getFrameExtents();
  75. _shadowMapSize.multiply( shadowFrameExtents );
  76. _viewportSize.copy( shadow.mapSize );
  77. if ( _shadowMapSize.x > _maxTextureSize || _shadowMapSize.y > _maxTextureSize ) {
  78. if ( _shadowMapSize.x > _maxTextureSize ) {
  79. _viewportSize.x = Math.floor( _maxTextureSize / shadowFrameExtents.x );
  80. _shadowMapSize.x = _viewportSize.x * shadowFrameExtents.x;
  81. shadow.mapSize.x = _viewportSize.x;
  82. }
  83. if ( _shadowMapSize.y > _maxTextureSize ) {
  84. _viewportSize.y = Math.floor( _maxTextureSize / shadowFrameExtents.y );
  85. _shadowMapSize.y = _viewportSize.y * shadowFrameExtents.y;
  86. shadow.mapSize.y = _viewportSize.y;
  87. }
  88. }
  89. if ( shadow.map === null ) {
  90. const pars = ( this.type !== VSMShadowMap ) ? { minFilter: NearestFilter, magFilter: NearestFilter } : {};
  91. shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
  92. shadow.map.texture.name = light.name + '.shadowMap';
  93. shadow.camera.updateProjectionMatrix();
  94. }
  95. _renderer.setRenderTarget( shadow.map );
  96. _renderer.clear();
  97. const viewportCount = shadow.getViewportCount();
  98. for ( let vp = 0; vp < viewportCount; vp ++ ) {
  99. const viewport = shadow.getViewport( vp );
  100. _viewport.set(
  101. _viewportSize.x * viewport.x,
  102. _viewportSize.y * viewport.y,
  103. _viewportSize.x * viewport.z,
  104. _viewportSize.y * viewport.w
  105. );
  106. _state.viewport( _viewport );
  107. shadow.updateMatrices( light, vp );
  108. _frustum = shadow.getFrustum();
  109. renderObject( scene, camera, shadow.camera, light, this.type );
  110. }
  111. // do blur pass for VSM
  112. if ( shadow.isPointLightShadow !== true && this.type === VSMShadowMap ) {
  113. VSMPass( shadow, camera );
  114. }
  115. shadow.needsUpdate = false;
  116. }
  117. scope.needsUpdate = false;
  118. _renderer.setRenderTarget( currentRenderTarget, activeCubeFace, activeMipmapLevel );
  119. };
  120. function VSMPass( shadow, camera ) {
  121. const geometry = _objects.update( fullScreenMesh );
  122. if ( shadowMaterialVertical.defines.VSM_SAMPLES !== shadow.blurSamples ) {
  123. shadowMaterialVertical.defines.VSM_SAMPLES = shadow.blurSamples;
  124. shadowMaterialHorizontal.defines.VSM_SAMPLES = shadow.blurSamples;
  125. shadowMaterialVertical.needsUpdate = true;
  126. shadowMaterialHorizontal.needsUpdate = true;
  127. }
  128. if ( shadow.mapPass === null ) {
  129. shadow.mapPass = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y );
  130. }
  131. // vertical pass
  132. shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.texture;
  133. shadowMaterialVertical.uniforms.resolution.value = shadow.mapSize;
  134. shadowMaterialVertical.uniforms.radius.value = shadow.radius;
  135. _renderer.setRenderTarget( shadow.mapPass );
  136. _renderer.clear();
  137. _renderer.renderBufferDirect( camera, null, geometry, shadowMaterialVertical, fullScreenMesh, null );
  138. // horizontal pass
  139. shadowMaterialHorizontal.uniforms.shadow_pass.value = shadow.mapPass.texture;
  140. shadowMaterialHorizontal.uniforms.resolution.value = shadow.mapSize;
  141. shadowMaterialHorizontal.uniforms.radius.value = shadow.radius;
  142. _renderer.setRenderTarget( shadow.map );
  143. _renderer.clear();
  144. _renderer.renderBufferDirect( camera, null, geometry, shadowMaterialHorizontal, fullScreenMesh, null );
  145. }
  146. function getDepthMaterial( object, material, light, shadowCameraNear, shadowCameraFar, type ) {
  147. let result = null;
  148. const customMaterial = ( light.isPointLight === true ) ? object.customDistanceMaterial : object.customDepthMaterial;
  149. if ( customMaterial !== undefined ) {
  150. result = customMaterial;
  151. } else {
  152. result = ( light.isPointLight === true ) ? _distanceMaterial : _depthMaterial;
  153. }
  154. if ( ( _renderer.localClippingEnabled && material.clipShadows === true && Array.isArray( material.clippingPlanes ) && material.clippingPlanes.length !== 0 ) ||
  155. ( material.displacementMap && material.displacementScale !== 0 ) ||
  156. ( material.alphaMap && material.alphaTest > 0 ) ) {
  157. // in this case we need a unique material instance reflecting the
  158. // appropriate state
  159. const keyA = result.uuid, keyB = material.uuid;
  160. let materialsForVariant = _materialCache[ keyA ];
  161. if ( materialsForVariant === undefined ) {
  162. materialsForVariant = {};
  163. _materialCache[ keyA ] = materialsForVariant;
  164. }
  165. let cachedMaterial = materialsForVariant[ keyB ];
  166. if ( cachedMaterial === undefined ) {
  167. cachedMaterial = result.clone();
  168. materialsForVariant[ keyB ] = cachedMaterial;
  169. }
  170. result = cachedMaterial;
  171. }
  172. result.visible = material.visible;
  173. result.wireframe = material.wireframe;
  174. if ( type === VSMShadowMap ) {
  175. result.side = ( material.shadowSide !== null ) ? material.shadowSide : material.side;
  176. } else {
  177. result.side = ( material.shadowSide !== null ) ? material.shadowSide : shadowSide[ material.side ];
  178. }
  179. result.alphaMap = material.alphaMap;
  180. result.alphaTest = material.alphaTest;
  181. result.clipShadows = material.clipShadows;
  182. result.clippingPlanes = material.clippingPlanes;
  183. result.clipIntersection = material.clipIntersection;
  184. result.displacementMap = material.displacementMap;
  185. result.displacementScale = material.displacementScale;
  186. result.displacementBias = material.displacementBias;
  187. result.wireframeLinewidth = material.wireframeLinewidth;
  188. result.linewidth = material.linewidth;
  189. if ( light.isPointLight === true && result.isMeshDistanceMaterial === true ) {
  190. result.referencePosition.setFromMatrixPosition( light.matrixWorld );
  191. result.nearDistance = shadowCameraNear;
  192. result.farDistance = shadowCameraFar;
  193. }
  194. return result;
  195. }
  196. function renderObject( object, camera, shadowCamera, light, type ) {
  197. if ( object.visible === false ) return;
  198. const visible = object.layers.test( camera.layers );
  199. if ( visible && ( object.isMesh || object.isLine || object.isPoints ) ) {
  200. if ( ( object.castShadow || ( object.receiveShadow && type === VSMShadowMap ) ) && ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) ) {
  201. object.modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
  202. const geometry = _objects.update( object );
  203. const material = object.material;
  204. if ( Array.isArray( material ) ) {
  205. const groups = geometry.groups;
  206. for ( let k = 0, kl = groups.length; k < kl; k ++ ) {
  207. const group = groups[ k ];
  208. const groupMaterial = material[ group.materialIndex ];
  209. if ( groupMaterial && groupMaterial.visible ) {
  210. const depthMaterial = getDepthMaterial( object, groupMaterial, light, shadowCamera.near, shadowCamera.far, type );
  211. _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group );
  212. }
  213. }
  214. } else if ( material.visible ) {
  215. const depthMaterial = getDepthMaterial( object, material, light, shadowCamera.near, shadowCamera.far, type );
  216. _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null );
  217. }
  218. }
  219. }
  220. const children = object.children;
  221. for ( let i = 0, l = children.length; i < l; i ++ ) {
  222. renderObject( children[ i ], camera, shadowCamera, light, type );
  223. }
  224. }
  225. }
  226. export { WebGLShadowMap };