CSM.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. import {
  2. Vector2,
  3. Vector3,
  4. DirectionalLight,
  5. MathUtils,
  6. ShaderChunk,
  7. Matrix4,
  8. Box3
  9. } from 'three';
  10. import { CSMFrustum } from './CSMFrustum.js';
  11. import { CSMShader } from './CSMShader.js';
  12. const _cameraToLightMatrix = new Matrix4();
  13. const _lightSpaceFrustum = new CSMFrustum( { webGL: true } );
  14. const _center = new Vector3();
  15. const _bbox = new Box3();
  16. const _uniformArray = [];
  17. const _logArray = [];
  18. const _lightOrientationMatrix = new Matrix4();
  19. const _lightOrientationMatrixInverse = new Matrix4();
  20. const _up = new Vector3( 0, 1, 0 );
  21. /**
  22. * An implementation of Cascade Shadow Maps (CSM).
  23. *
  24. * This module can only be used with {@link WebGLRenderer}. When using {@link WebGPURenderer},
  25. * use {@link CSMShadowNode} instead.
  26. */
  27. export class CSM {
  28. /**
  29. * Constructs a new CSM instance.
  30. *
  31. * @param {CSM~Data} data - The CSM data.
  32. */
  33. constructor( data ) {
  34. /**
  35. * The scene's camera.
  36. *
  37. * @type {Camera}
  38. */
  39. this.camera = data.camera;
  40. /**
  41. * The parent object, usually the scene.
  42. *
  43. * @type {Object3D}
  44. */
  45. this.parent = data.parent;
  46. /**
  47. * The number of cascades.
  48. *
  49. * @type {number}
  50. * @default 3
  51. */
  52. this.cascades = data.cascades || 3;
  53. /**
  54. * The maximum far value.
  55. *
  56. * @type {number}
  57. * @default 100000
  58. */
  59. this.maxFar = data.maxFar || 100000;
  60. /**
  61. * The frustum split mode.
  62. *
  63. * @type {('practical'|'uniform'|'logarithmic'|'custom')}
  64. * @default 'practical'
  65. */
  66. this.mode = data.mode || 'practical';
  67. /**
  68. * The shadow map size.
  69. *
  70. * @type {number}
  71. * @default 2048
  72. */
  73. this.shadowMapSize = data.shadowMapSize || 2048;
  74. /**
  75. * The shadow bias.
  76. *
  77. * @type {number}
  78. * @default 0.000001
  79. */
  80. this.shadowBias = data.shadowBias || 0.000001;
  81. /**
  82. * The light direction.
  83. *
  84. * @type {Vector3}
  85. */
  86. this.lightDirection = data.lightDirection || new Vector3( 1, - 1, 1 ).normalize();
  87. /**
  88. * The light intensity.
  89. *
  90. * @type {number}
  91. * @default 3
  92. */
  93. this.lightIntensity = data.lightIntensity || 3;
  94. /**
  95. * The light near value.
  96. *
  97. * @type {number}
  98. * @default 1
  99. */
  100. this.lightNear = data.lightNear || 1;
  101. /**
  102. * The light far value.
  103. *
  104. * @type {number}
  105. * @default 2000
  106. */
  107. this.lightFar = data.lightFar || 2000;
  108. /**
  109. * The light margin.
  110. *
  111. * @type {number}
  112. * @default 200
  113. */
  114. this.lightMargin = data.lightMargin || 200;
  115. /**
  116. * Custom split callback when using `mode='custom'`.
  117. *
  118. * @type {Function}
  119. */
  120. this.customSplitsCallback = data.customSplitsCallback;
  121. /**
  122. * Whether to fade between cascades or not.
  123. *
  124. * @type {boolean}
  125. * @default false
  126. */
  127. this.fade = false;
  128. /**
  129. * The main frustum.
  130. *
  131. * @type {CSMFrustum}
  132. */
  133. this.mainFrustum = new CSMFrustum( { webGL: true } );
  134. /**
  135. * An array of frustums representing the cascades.
  136. *
  137. * @type {Array<CSMFrustum>}
  138. */
  139. this.frustums = [];
  140. /**
  141. * An array of numbers in the range `[0,1]` the defines how the
  142. * mainCSM frustum should be split up.
  143. *
  144. * @type {Array<number>}
  145. */
  146. this.breaks = [];
  147. /**
  148. * An array of directional lights which cast the shadows for
  149. * the different cascades. There is one directional light for each
  150. * cascade.
  151. *
  152. * @type {Array<DirectionalLight>}
  153. */
  154. this.lights = [];
  155. /**
  156. * A Map holding enhanced material shaders.
  157. *
  158. * @type {Map<Material,Object>}
  159. */
  160. this.shaders = new Map();
  161. this._createLights();
  162. this.updateFrustums();
  163. this._injectInclude();
  164. }
  165. /**
  166. * Creates the directional lights of this CSM instance.
  167. *
  168. * @private
  169. */
  170. _createLights() {
  171. for ( let i = 0; i < this.cascades; i ++ ) {
  172. const light = new DirectionalLight( 0xffffff, this.lightIntensity );
  173. light.castShadow = true;
  174. light.shadow.mapSize.width = this.shadowMapSize;
  175. light.shadow.mapSize.height = this.shadowMapSize;
  176. light.shadow.camera.near = this.lightNear;
  177. light.shadow.camera.far = this.lightFar;
  178. light.shadow.bias = this.shadowBias;
  179. this.parent.add( light );
  180. this.parent.add( light.target );
  181. this.lights.push( light );
  182. }
  183. }
  184. /**
  185. * Inits the cascades according to the scene's camera and breaks configuration.
  186. *
  187. * @private
  188. */
  189. _initCascades() {
  190. const camera = this.camera;
  191. camera.updateProjectionMatrix();
  192. this.mainFrustum.setFromProjectionMatrix( camera.projectionMatrix, this.maxFar );
  193. this.mainFrustum.split( this.breaks, this.frustums );
  194. }
  195. /**
  196. * Updates the shadow bounds of this CSM instance.
  197. *
  198. * @private
  199. */
  200. _updateShadowBounds() {
  201. const frustums = this.frustums;
  202. for ( let i = 0; i < frustums.length; i ++ ) {
  203. const light = this.lights[ i ];
  204. const shadowCam = light.shadow.camera;
  205. const frustum = this.frustums[ i ];
  206. // Get the two points that represent that furthest points on the frustum assuming
  207. // that's either the diagonal across the far plane or the diagonal across the whole
  208. // frustum itself.
  209. const nearVerts = frustum.vertices.near;
  210. const farVerts = frustum.vertices.far;
  211. const point1 = farVerts[ 0 ];
  212. let point2;
  213. if ( point1.distanceTo( farVerts[ 2 ] ) > point1.distanceTo( nearVerts[ 2 ] ) ) {
  214. point2 = farVerts[ 2 ];
  215. } else {
  216. point2 = nearVerts[ 2 ];
  217. }
  218. let squaredBBWidth = point1.distanceTo( point2 );
  219. if ( this.fade ) {
  220. // expand the shadow extents by the fade margin if fade is enabled.
  221. const camera = this.camera;
  222. const far = Math.max( camera.far, this.maxFar );
  223. const linearDepth = frustum.vertices.far[ 0 ].z / ( far - camera.near );
  224. const margin = 0.25 * Math.pow( linearDepth, 2.0 ) * ( far - camera.near );
  225. squaredBBWidth += margin;
  226. }
  227. shadowCam.left = - squaredBBWidth / 2;
  228. shadowCam.right = squaredBBWidth / 2;
  229. shadowCam.top = squaredBBWidth / 2;
  230. shadowCam.bottom = - squaredBBWidth / 2;
  231. shadowCam.updateProjectionMatrix();
  232. }
  233. }
  234. /**
  235. * Computes the breaks of this CSM instance based on the scene's camera, number of cascades
  236. * and the selected split mode.
  237. *
  238. * @private
  239. */
  240. _getBreaks() {
  241. const camera = this.camera;
  242. const far = Math.min( camera.far, this.maxFar );
  243. this.breaks.length = 0;
  244. switch ( this.mode ) {
  245. case 'uniform':
  246. uniformSplit( this.cascades, camera.near, far, this.breaks );
  247. break;
  248. case 'logarithmic':
  249. logarithmicSplit( this.cascades, camera.near, far, this.breaks );
  250. break;
  251. case 'practical':
  252. practicalSplit( this.cascades, camera.near, far, 0.5, this.breaks );
  253. break;
  254. case 'custom':
  255. if ( this.customSplitsCallback === undefined ) console.error( 'CSM: Custom split scheme callback not defined.' );
  256. this.customSplitsCallback( this.cascades, camera.near, far, this.breaks );
  257. break;
  258. }
  259. function uniformSplit( amount, near, far, target ) {
  260. for ( let i = 1; i < amount; i ++ ) {
  261. target.push( ( near + ( far - near ) * i / amount ) / far );
  262. }
  263. target.push( 1 );
  264. }
  265. function logarithmicSplit( amount, near, far, target ) {
  266. for ( let i = 1; i < amount; i ++ ) {
  267. target.push( ( near * ( far / near ) ** ( i / amount ) ) / far );
  268. }
  269. target.push( 1 );
  270. }
  271. function practicalSplit( amount, near, far, lambda, target ) {
  272. _uniformArray.length = 0;
  273. _logArray.length = 0;
  274. logarithmicSplit( amount, near, far, _logArray );
  275. uniformSplit( amount, near, far, _uniformArray );
  276. for ( let i = 1; i < amount; i ++ ) {
  277. target.push( MathUtils.lerp( _uniformArray[ i - 1 ], _logArray[ i - 1 ], lambda ) );
  278. }
  279. target.push( 1 );
  280. }
  281. }
  282. /**
  283. * Updates the CSM. This method must be called in your animation loop before
  284. * calling `renderer.render()`.
  285. */
  286. update() {
  287. const camera = this.camera;
  288. const frustums = this.frustums;
  289. // for each frustum we need to find its min-max box aligned with the light orientation
  290. // the position in _lightOrientationMatrix does not matter, as we transform there and back
  291. _lightOrientationMatrix.lookAt( new Vector3(), this.lightDirection, _up );
  292. _lightOrientationMatrixInverse.copy( _lightOrientationMatrix ).invert();
  293. for ( let i = 0; i < frustums.length; i ++ ) {
  294. const light = this.lights[ i ];
  295. const shadowCam = light.shadow.camera;
  296. const texelWidth = ( shadowCam.right - shadowCam.left ) / this.shadowMapSize;
  297. const texelHeight = ( shadowCam.top - shadowCam.bottom ) / this.shadowMapSize;
  298. _cameraToLightMatrix.multiplyMatrices( _lightOrientationMatrixInverse, camera.matrixWorld );
  299. frustums[ i ].toSpace( _cameraToLightMatrix, _lightSpaceFrustum );
  300. const nearVerts = _lightSpaceFrustum.vertices.near;
  301. const farVerts = _lightSpaceFrustum.vertices.far;
  302. _bbox.makeEmpty();
  303. for ( let j = 0; j < 4; j ++ ) {
  304. _bbox.expandByPoint( nearVerts[ j ] );
  305. _bbox.expandByPoint( farVerts[ j ] );
  306. }
  307. _bbox.getCenter( _center );
  308. _center.z = _bbox.max.z + this.lightMargin;
  309. _center.x = Math.floor( _center.x / texelWidth ) * texelWidth;
  310. _center.y = Math.floor( _center.y / texelHeight ) * texelHeight;
  311. _center.applyMatrix4( _lightOrientationMatrix );
  312. light.position.copy( _center );
  313. light.target.position.copy( _center );
  314. light.target.position.x += this.lightDirection.x;
  315. light.target.position.y += this.lightDirection.y;
  316. light.target.position.z += this.lightDirection.z;
  317. }
  318. }
  319. /**
  320. * Injects the CSM shader enhancements into the built-in materials.
  321. *
  322. * @private
  323. */
  324. _injectInclude() {
  325. ShaderChunk.lights_fragment_begin = CSMShader.lights_fragment_begin;
  326. ShaderChunk.lights_pars_begin = CSMShader.lights_pars_begin;
  327. }
  328. /**
  329. * Applications must call this method for all materials that should be affected by CSM.
  330. *
  331. * @param {Material} material - The material to setup for CSM support.
  332. */
  333. setupMaterial( material ) {
  334. material.defines = material.defines || {};
  335. material.defines.USE_CSM = 1;
  336. material.defines.CSM_CASCADES = this.cascades;
  337. if ( this.fade ) {
  338. material.defines.CSM_FADE = '';
  339. }
  340. const breaksVec2 = [];
  341. const scope = this;
  342. const shaders = this.shaders;
  343. material.onBeforeCompile = function ( shader ) {
  344. const far = Math.min( scope.camera.far, scope.maxFar );
  345. scope._getExtendedBreaks( breaksVec2 );
  346. shader.uniforms.CSM_cascades = { value: breaksVec2 };
  347. shader.uniforms.cameraNear = { value: scope.camera.near };
  348. shader.uniforms.shadowFar = { value: far };
  349. shaders.set( material, shader );
  350. };
  351. shaders.set( material, null );
  352. }
  353. /**
  354. * Updates the CSM uniforms.
  355. *
  356. * @private
  357. */
  358. _updateUniforms() {
  359. const far = Math.min( this.camera.far, this.maxFar );
  360. const shaders = this.shaders;
  361. shaders.forEach( function ( shader, material ) {
  362. if ( shader !== null ) {
  363. const uniforms = shader.uniforms;
  364. this._getExtendedBreaks( uniforms.CSM_cascades.value );
  365. uniforms.cameraNear.value = this.camera.near;
  366. uniforms.shadowFar.value = far;
  367. }
  368. if ( ! this.fade && 'CSM_FADE' in material.defines ) {
  369. delete material.defines.CSM_FADE;
  370. material.needsUpdate = true;
  371. } else if ( this.fade && ! ( 'CSM_FADE' in material.defines ) ) {
  372. material.defines.CSM_FADE = '';
  373. material.needsUpdate = true;
  374. }
  375. }, this );
  376. }
  377. /**
  378. * Computes the extended breaks for the CSM uniforms.
  379. *
  380. * @private
  381. * @param {Array<Vector2>} target - The target array that holds the extended breaks.
  382. */
  383. _getExtendedBreaks( target ) {
  384. while ( target.length < this.breaks.length ) {
  385. target.push( new Vector2() );
  386. }
  387. target.length = this.breaks.length;
  388. for ( let i = 0; i < this.cascades; i ++ ) {
  389. const amount = this.breaks[ i ];
  390. const prev = this.breaks[ i - 1 ] || 0;
  391. target[ i ].x = prev;
  392. target[ i ].y = amount;
  393. }
  394. }
  395. /**
  396. * Applications must call this method every time they change camera or CSM settings.
  397. */
  398. updateFrustums() {
  399. this._getBreaks();
  400. this._initCascades();
  401. this._updateShadowBounds();
  402. this._updateUniforms();
  403. }
  404. /**
  405. * Applications must call this method when they remove the CSM usage from their scene.
  406. */
  407. remove() {
  408. for ( let i = 0; i < this.lights.length; i ++ ) {
  409. this.parent.remove( this.lights[ i ].target );
  410. this.parent.remove( this.lights[ i ] );
  411. }
  412. }
  413. /**
  414. * Frees the GPU-related resources allocated by this instance. Call this
  415. * method whenever this instance is no longer used in your app.
  416. */
  417. dispose() {
  418. const shaders = this.shaders;
  419. shaders.forEach( function ( shader, material ) {
  420. delete material.onBeforeCompile;
  421. delete material.defines.USE_CSM;
  422. delete material.defines.CSM_CASCADES;
  423. delete material.defines.CSM_FADE;
  424. if ( shader !== null ) {
  425. delete shader.uniforms.CSM_cascades;
  426. delete shader.uniforms.cameraNear;
  427. delete shader.uniforms.shadowFar;
  428. }
  429. material.needsUpdate = true;
  430. } );
  431. shaders.clear();
  432. }
  433. }
  434. /**
  435. * Constructor data of `CSM`.
  436. *
  437. * @typedef {Object} CSM~Data
  438. * @property {Camera} camera - The scene's camera.
  439. * @property {Object3D} parent - The parent object, usually the scene.
  440. * @property {number} [cascades=3] - The number of cascades.
  441. * @property {number} [maxFar=100000] - The maximum far value.
  442. * @property {('practical'|'uniform'|'logarithmic'|'custom')} [mode='practical'] - The frustum split mode.
  443. * @property {Function} [customSplitsCallback] - Custom split callback when using `mode='custom'`.
  444. * @property {number} [shadowMapSize=2048] - The shadow map size.
  445. * @property {number} [shadowBias=0.000001] - The shadow bias.
  446. * @property {Vector3} [lightDirection] - The light direction.
  447. * @property {number} [lightIntensity=3] - The light intensity.
  448. * @property {number} [lightNear=1] - The light near value.
  449. * @property {number} [lightNear=2000] - The light far value.
  450. * @property {number} [lightMargin=200] - The light margin.
  451. **/
粤ICP备19079148号