WebGLShadowMap.js 11 KB

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