WebGLShadowMap.js 10 KB

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