WebGLShadowMap.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. _MorphingFlag = 1,
  24. _SkinningFlag = 2,
  25. _NumberOfMaterialVariants = ( _MorphingFlag | _SkinningFlag ) + 1,
  26. _depthMaterials = new Array( _NumberOfMaterialVariants ),
  27. _distanceMaterials = new Array( _NumberOfMaterialVariants ),
  28. _materialCache = {};
  29. var shadowSide = { 0: BackSide, 1: FrontSide, 2: DoubleSide };
  30. var shadowMaterialVertical = new ShaderMaterial( {
  31. defines: {
  32. SAMPLE_RATE: 2.0 / 8.0,
  33. HALF_SAMPLE_RATE: 1.0 / 8.0
  34. },
  35. uniforms: {
  36. shadow_pass: { value: null },
  37. resolution: { value: new Vector2() },
  38. radius: { value: 4.0 }
  39. },
  40. vertexShader: vsm_vert,
  41. fragmentShader: vsm_frag
  42. } );
  43. var shadowMaterialHorizonal = shadowMaterialVertical.clone();
  44. shadowMaterialHorizonal.defines.HORIZONAL_PASS = 1;
  45. var fullScreenTri = new BufferGeometry();
  46. fullScreenTri.addAttribute(
  47. "position",
  48. new BufferAttribute(
  49. new Float32Array( [ - 1, - 1, 0.5, 3, - 1, 0.5, - 1, 3, 0.5 ] ),
  50. 3
  51. )
  52. );
  53. var fullScreenMesh = new Mesh( fullScreenTri, shadowMaterialVertical );
  54. // init
  55. for ( var i = 0; i !== _NumberOfMaterialVariants; ++ i ) {
  56. var useMorphing = ( i & _MorphingFlag ) !== 0;
  57. var useSkinning = ( i & _SkinningFlag ) !== 0;
  58. var depthMaterial = new MeshDepthMaterial( {
  59. depthPacking: RGBADepthPacking,
  60. morphTargets: useMorphing,
  61. skinning: useSkinning
  62. } );
  63. _depthMaterials[ i ] = depthMaterial;
  64. var distanceMaterial = new MeshDistanceMaterial( {
  65. morphTargets: useMorphing,
  66. skinning: useSkinning
  67. } );
  68. _distanceMaterials[ i ] = distanceMaterial;
  69. }
  70. var scope = this;
  71. this.enabled = false;
  72. this.autoUpdate = true;
  73. this.needsUpdate = false;
  74. this.type = PCFShadowMap;
  75. this.render = function ( lights, scene, camera ) {
  76. if ( scope.enabled === false ) return;
  77. if ( scope.autoUpdate === false && scope.needsUpdate === false ) return;
  78. if ( lights.length === 0 ) return;
  79. var currentRenderTarget = _renderer.getRenderTarget();
  80. var activeCubeFace = _renderer.getActiveCubeFace();
  81. var activeMipmapLevel = _renderer.getActiveMipmapLevel();
  82. var _state = _renderer.state;
  83. // Set GL state for depth map.
  84. _state.setBlending( NoBlending );
  85. _state.buffers.color.setClear( 1, 1, 1, 1 );
  86. _state.buffers.depth.setTest( true );
  87. _state.setScissorTest( false );
  88. // render depth map
  89. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  90. var light = lights[ i ];
  91. var shadow = light.shadow;
  92. if ( shadow === undefined ) {
  93. console.warn( 'THREE.WebGLShadowMap:', light, 'has no shadow.' );
  94. continue;
  95. }
  96. _shadowMapSize.copy( shadow.mapSize );
  97. var shadowFrameExtents = shadow.getFrameExtents();
  98. _shadowMapSize.multiply( shadowFrameExtents );
  99. _viewportSize.copy( shadow.mapSize );
  100. if ( _shadowMapSize.x > maxTextureSize || _shadowMapSize.y > maxTextureSize ) {
  101. console.warn( 'THREE.WebGLShadowMap:', light, 'has shadow exceeding max texture size, reducing' );
  102. if ( _shadowMapSize.x > maxTextureSize ) {
  103. _viewportSize.x = Math.floor( maxTextureSize / shadowFrameExtents.x );
  104. _shadowMapSize.x = _viewportSize.x * shadowFrameExtents.x;
  105. shadow.mapSize.x = _viewportSize.x;
  106. }
  107. if ( _shadowMapSize.y > maxTextureSize ) {
  108. _viewportSize.y = Math.floor( maxTextureSize / shadowFrameExtents.y );
  109. _shadowMapSize.y = _viewportSize.y * shadowFrameExtents.y;
  110. shadow.mapSize.y = _viewportSize.y;
  111. }
  112. }
  113. if ( shadow.map === null && ! shadow.isPointLightShadow && this.type === VSMShadowMap ) {
  114. var pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat };
  115. shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
  116. shadow.map.texture.name = light.name + ".shadowMap";
  117. shadow.mapPass = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
  118. shadow.camera.updateProjectionMatrix();
  119. }
  120. if ( shadow.map === null ) {
  121. var pars = { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat };
  122. shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
  123. shadow.map.texture.name = light.name + ".shadowMap";
  124. shadow.camera.updateProjectionMatrix();
  125. }
  126. _renderer.setRenderTarget( shadow.map );
  127. _renderer.clear();
  128. var viewportCount = shadow.getViewportCount();
  129. for ( var vp = 0; vp < viewportCount; vp ++ ) {
  130. var viewport = shadow.getViewport( vp );
  131. _viewport.set(
  132. _viewportSize.x * viewport.x,
  133. _viewportSize.y * viewport.y,
  134. _viewportSize.x * viewport.z,
  135. _viewportSize.y * viewport.w
  136. );
  137. _state.viewport( _viewport );
  138. shadow.updateMatrices( light, camera, vp );
  139. _frustum = shadow.getFrustum();
  140. renderObject( scene, camera, shadow.camera, light, this.type );
  141. }
  142. // do blur pass for VSM
  143. if ( ! shadow.isPointLightShadow && this.type === VSMShadowMap ) {
  144. VSMPass( shadow, camera );
  145. }
  146. }
  147. scope.needsUpdate = false;
  148. _renderer.setRenderTarget( currentRenderTarget, activeCubeFace, activeMipmapLevel );
  149. };
  150. function VSMPass( shadow, camera ) {
  151. var geometry = _objects.update( fullScreenMesh );
  152. // vertical pass
  153. shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.texture;
  154. shadowMaterialVertical.uniforms.resolution.value = shadow.mapSize;
  155. shadowMaterialVertical.uniforms.radius.value = shadow.radius;
  156. _renderer.setRenderTarget( shadow.mapPass );
  157. _renderer.clear();
  158. _renderer.renderBufferDirect( camera, null, geometry, shadowMaterialVertical, fullScreenMesh, null );
  159. // horizonal pass
  160. shadowMaterialHorizonal.uniforms.shadow_pass.value = shadow.mapPass.texture;
  161. shadowMaterialHorizonal.uniforms.resolution.value = shadow.mapSize;
  162. shadowMaterialHorizonal.uniforms.radius.value = shadow.radius;
  163. _renderer.setRenderTarget( shadow.map );
  164. _renderer.clear();
  165. _renderer.renderBufferDirect( camera, null, geometry, shadowMaterialHorizonal, fullScreenMesh, null );
  166. }
  167. function getDepthMaterial( object, material, light, shadowCameraNear, shadowCameraFar, type ) {
  168. var geometry = object.geometry;
  169. var result = null;
  170. var materialVariants = _depthMaterials;
  171. var customMaterial = object.customDepthMaterial;
  172. if ( light.isPointLight ) {
  173. materialVariants = _distanceMaterials;
  174. customMaterial = object.customDistanceMaterial;
  175. }
  176. if ( ! customMaterial ) {
  177. var useMorphing = false;
  178. if ( material.morphTargets ) {
  179. if ( geometry && geometry.isBufferGeometry ) {
  180. useMorphing = geometry.morphAttributes && geometry.morphAttributes.position && geometry.morphAttributes.position.length > 0;
  181. } else if ( geometry && geometry.isGeometry ) {
  182. useMorphing = geometry.morphTargets && geometry.morphTargets.length > 0;
  183. }
  184. }
  185. if ( object.isSkinnedMesh && material.skinning === false ) {
  186. console.warn( 'THREE.WebGLShadowMap: THREE.SkinnedMesh with material.skinning set to false:', object );
  187. }
  188. var useSkinning = object.isSkinnedMesh && material.skinning;
  189. var variantIndex = 0;
  190. if ( useMorphing ) variantIndex |= _MorphingFlag;
  191. if ( useSkinning ) variantIndex |= _SkinningFlag;
  192. result = materialVariants[ variantIndex ];
  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. var keyA = result.uuid, keyB = material.uuid;
  202. var materialsForVariant = _materialCache[ keyA ];
  203. if ( materialsForVariant === undefined ) {
  204. materialsForVariant = {};
  205. _materialCache[ keyA ] = materialsForVariant;
  206. }
  207. var 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 && result.isMeshDistanceMaterial ) {
  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. var 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. var geometry = _objects.update( object );
  240. var material = object.material;
  241. if ( Array.isArray( material ) ) {
  242. var groups = geometry.groups;
  243. for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
  244. var group = groups[ k ];
  245. var groupMaterial = material[ group.materialIndex ];
  246. if ( groupMaterial && groupMaterial.visible ) {
  247. var depthMaterial = getDepthMaterial( object, groupMaterial, light, shadowCamera.near, shadowCamera.far, type );
  248. _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group );
  249. }
  250. }
  251. } else if ( material.visible ) {
  252. var depthMaterial = getDepthMaterial( object, material, light, shadowCamera.near, shadowCamera.far, type );
  253. _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null );
  254. }
  255. }
  256. }
  257. var children = object.children;
  258. for ( var i = 0, l = children.length; i < l; i ++ ) {
  259. renderObject( children[ i ], camera, shadowCamera, light, type );
  260. }
  261. }
  262. }
  263. export { WebGLShadowMap };
粤ICP备19079148号