1
0

CSM.js 14 KB

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