WebGLShadowMap.js 11 KB

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