WebGLShadowMap.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 * 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 && ! shadow.isPointLightShadow && this.type === VSMShadowMap ) {
  90. const pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat };
  91. shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
  92. shadow.map.texture.name = light.name + '.shadowMap';
  93. shadow.mapPass = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
  94. shadow.camera.updateProjectionMatrix();
  95. }
  96. if ( shadow.map === null ) {
  97. const pars = { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat };
  98. shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
  99. shadow.map.texture.name = light.name + '.shadowMap';
  100. shadow.camera.updateProjectionMatrix();
  101. }
  102. _renderer.setRenderTarget( shadow.map );
  103. _renderer.clear();
  104. const viewportCount = shadow.getViewportCount();
  105. for ( let vp = 0; vp < viewportCount; vp ++ ) {
  106. const viewport = shadow.getViewport( vp );
  107. _viewport.set(
  108. _viewportSize.x * viewport.x,
  109. _viewportSize.y * viewport.y,
  110. _viewportSize.x * viewport.z,
  111. _viewportSize.y * viewport.w
  112. );
  113. _state.viewport( _viewport );
  114. shadow.updateMatrices( light, vp );
  115. _frustum = shadow.getFrustum();
  116. renderObject( scene, camera, shadow.camera, light, this.type );
  117. }
  118. // do blur pass for VSM
  119. if ( ! shadow.isPointLightShadow && this.type === VSMShadowMap ) {
  120. VSMPass( shadow, camera );
  121. }
  122. shadow.needsUpdate = false;
  123. }
  124. scope.needsUpdate = false;
  125. _renderer.setRenderTarget( currentRenderTarget, activeCubeFace, activeMipmapLevel );
  126. };
  127. function VSMPass( shadow, camera ) {
  128. const geometry = _objects.update( fullScreenMesh );
  129. if ( shadowMaterialVertical.defines.VSM_SAMPLES !== shadow.blurSamples ) {
  130. shadowMaterialVertical.defines.VSM_SAMPLES = shadow.blurSamples;
  131. shadowMaterialHorizontal.defines.VSM_SAMPLES = shadow.blurSamples;
  132. shadowMaterialVertical.needsUpdate = true;
  133. shadowMaterialHorizontal.needsUpdate = true;
  134. }
  135. // vertical pass
  136. shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.texture;
  137. shadowMaterialVertical.uniforms.resolution.value = shadow.mapSize;
  138. shadowMaterialVertical.uniforms.radius.value = shadow.radius;
  139. _renderer.setRenderTarget( shadow.mapPass );
  140. _renderer.clear();
  141. _renderer.renderBufferDirect( camera, null, geometry, shadowMaterialVertical, fullScreenMesh, null );
  142. // horizontal pass
  143. shadowMaterialHorizontal.uniforms.shadow_pass.value = shadow.mapPass.texture;
  144. shadowMaterialHorizontal.uniforms.resolution.value = shadow.mapSize;
  145. shadowMaterialHorizontal.uniforms.radius.value = shadow.radius;
  146. _renderer.setRenderTarget( shadow.map );
  147. _renderer.clear();
  148. _renderer.renderBufferDirect( camera, null, geometry, shadowMaterialHorizontal, fullScreenMesh, null );
  149. }
  150. function getDepthMaterial( object, geometry, material, light, shadowCameraNear, shadowCameraFar, type ) {
  151. let result = null;
  152. const customMaterial = ( light.isPointLight === true ) ? object.customDistanceMaterial : object.customDepthMaterial;
  153. if ( customMaterial !== undefined ) {
  154. result = customMaterial;
  155. } else {
  156. result = ( light.isPointLight === true ) ? _distanceMaterial : _depthMaterial;
  157. }
  158. if ( ( _renderer.localClippingEnabled && material.clipShadows === true && material.clippingPlanes.length !== 0 ) ||
  159. ( material.displacementMap && material.displacementScale !== 0 ) ||
  160. ( material.alphaMap && material.alphaTest > 0 ) ) {
  161. // in this case we need a unique material instance reflecting the
  162. // appropriate state
  163. const keyA = result.uuid, keyB = material.uuid;
  164. let materialsForVariant = _materialCache[ keyA ];
  165. if ( materialsForVariant === undefined ) {
  166. materialsForVariant = {};
  167. _materialCache[ keyA ] = materialsForVariant;
  168. }
  169. let cachedMaterial = materialsForVariant[ keyB ];
  170. if ( cachedMaterial === undefined ) {
  171. cachedMaterial = result.clone();
  172. materialsForVariant[ keyB ] = cachedMaterial;
  173. }
  174. result = cachedMaterial;
  175. }
  176. result.visible = material.visible;
  177. result.wireframe = material.wireframe;
  178. if ( type === VSMShadowMap ) {
  179. result.side = ( material.shadowSide !== null ) ? material.shadowSide : material.side;
  180. } else {
  181. result.side = ( material.shadowSide !== null ) ? material.shadowSide : shadowSide[ material.side ];
  182. }
  183. result.alphaMap = material.alphaMap;
  184. result.alphaTest = material.alphaTest;
  185. result.clipShadows = material.clipShadows;
  186. result.clippingPlanes = material.clippingPlanes;
  187. result.clipIntersection = material.clipIntersection;
  188. result.displacementMap = material.displacementMap;
  189. result.displacementScale = material.displacementScale;
  190. result.displacementBias = material.displacementBias;
  191. result.wireframeLinewidth = material.wireframeLinewidth;
  192. result.linewidth = material.linewidth;
  193. if ( light.isPointLight === true && result.isMeshDistanceMaterial === true ) {
  194. result.referencePosition.setFromMatrixPosition( light.matrixWorld );
  195. result.nearDistance = shadowCameraNear;
  196. result.farDistance = shadowCameraFar;
  197. }
  198. return result;
  199. }
  200. function renderObject( object, camera, shadowCamera, light, type ) {
  201. if ( object.visible === false ) return;
  202. const visible = object.layers.test( camera.layers );
  203. if ( visible && ( object.isMesh || object.isLine || object.isPoints ) ) {
  204. if ( ( object.castShadow || ( object.receiveShadow && type === VSMShadowMap ) ) && ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) ) {
  205. object.modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
  206. const geometry = _objects.update( object );
  207. const material = object.material;
  208. if ( Array.isArray( material ) ) {
  209. const groups = geometry.groups;
  210. for ( let k = 0, kl = groups.length; k < kl; k ++ ) {
  211. const group = groups[ k ];
  212. const groupMaterial = material[ group.materialIndex ];
  213. if ( groupMaterial && groupMaterial.visible ) {
  214. const depthMaterial = getDepthMaterial( object, geometry, groupMaterial, light, shadowCamera.near, shadowCamera.far, type );
  215. _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group );
  216. }
  217. }
  218. } else if ( material.visible ) {
  219. const depthMaterial = getDepthMaterial( object, geometry, material, light, shadowCamera.near, shadowCamera.far, type );
  220. _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null );
  221. }
  222. }
  223. }
  224. const children = object.children;
  225. for ( let i = 0, l = children.length; i < l; i ++ ) {
  226. renderObject( children[ i ], camera, shadowCamera, light, type );
  227. }
  228. }
  229. }
  230. export { WebGLShadowMap };
粤ICP备19079148号