CSMFrustum.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { Vector3, Matrix4 } from 'three';
  2. const inverseProjectionMatrix = new Matrix4();
  3. /**
  4. * Represents the frustum of a CSM instance.
  5. *
  6. * @three_import import { CSMFrustum } from 'three/addons/csm/CSMFrustum.js';
  7. */
  8. class CSMFrustum {
  9. /**
  10. * Constructs a new CSM frustum.
  11. *
  12. * @param {CSMFrustum~Data} [data] - The CSM data.
  13. */
  14. constructor( data ) {
  15. data = data || {};
  16. /**
  17. * The zNear value. This value depends on whether the CSM
  18. * is used with WebGL or WebGPU. Both API use different
  19. * conventions for their projection matrices.
  20. *
  21. * @type {number}
  22. */
  23. this.zNear = data.webGL === true ? - 1 : 0;
  24. /**
  25. * An object representing the vertices of the near and
  26. * far plane in view space.
  27. *
  28. * @type {Object}
  29. */
  30. this.vertices = {
  31. near: [
  32. new Vector3(),
  33. new Vector3(),
  34. new Vector3(),
  35. new Vector3()
  36. ],
  37. far: [
  38. new Vector3(),
  39. new Vector3(),
  40. new Vector3(),
  41. new Vector3()
  42. ]
  43. };
  44. if ( data.projectionMatrix !== undefined ) {
  45. this.setFromProjectionMatrix( data.projectionMatrix, data.maxFar || 10000 );
  46. }
  47. }
  48. /**
  49. * Setups this CSM frustum from the given projection matrix and max far value.
  50. *
  51. * @param {Matrix4} projectionMatrix - The projection matrix, usually of the scene's camera.
  52. * @param {number} maxFar - The maximum far value.
  53. * @returns {Object} An object representing the vertices of the near and far plane in view space.
  54. */
  55. setFromProjectionMatrix( projectionMatrix, maxFar ) {
  56. const zNear = this.zNear;
  57. const isOrthographic = projectionMatrix.elements[ 2 * 4 + 3 ] === 0;
  58. inverseProjectionMatrix.copy( projectionMatrix ).invert();
  59. // 3 --- 0 vertices.near/far order
  60. // | |
  61. // 2 --- 1
  62. // clip space spans from [-1, 1]
  63. this.vertices.near[ 0 ].set( 1, 1, zNear );
  64. this.vertices.near[ 1 ].set( 1, - 1, zNear );
  65. this.vertices.near[ 2 ].set( - 1, - 1, zNear );
  66. this.vertices.near[ 3 ].set( - 1, 1, zNear );
  67. this.vertices.near.forEach( function ( v ) {
  68. v.applyMatrix4( inverseProjectionMatrix );
  69. } );
  70. this.vertices.far[ 0 ].set( 1, 1, 1 );
  71. this.vertices.far[ 1 ].set( 1, - 1, 1 );
  72. this.vertices.far[ 2 ].set( - 1, - 1, 1 );
  73. this.vertices.far[ 3 ].set( - 1, 1, 1 );
  74. this.vertices.far.forEach( function ( v ) {
  75. v.applyMatrix4( inverseProjectionMatrix );
  76. const absZ = Math.abs( v.z );
  77. if ( isOrthographic ) {
  78. v.z *= Math.min( maxFar / absZ, 1.0 );
  79. } else {
  80. v.multiplyScalar( Math.min( maxFar / absZ, 1.0 ) );
  81. }
  82. } );
  83. return this.vertices;
  84. }
  85. /**
  86. * Splits the CSM frustum by the given array. The new CSM frustum are pushed into the given
  87. * target array.
  88. *
  89. * @param {Array<number>} breaks - An array of numbers in the range `[0,1]` the defines how the
  90. * CSM frustum should be split up.
  91. * @param {Array<CSMFrustum>} target - The target array that holds the new CSM frustums.
  92. */
  93. split( breaks, target ) {
  94. while ( breaks.length > target.length ) {
  95. target.push( new CSMFrustum() );
  96. }
  97. target.length = breaks.length;
  98. for ( let i = 0; i < breaks.length; i ++ ) {
  99. const cascade = target[ i ];
  100. if ( i === 0 ) {
  101. for ( let j = 0; j < 4; j ++ ) {
  102. cascade.vertices.near[ j ].copy( this.vertices.near[ j ] );
  103. }
  104. } else {
  105. for ( let j = 0; j < 4; j ++ ) {
  106. cascade.vertices.near[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i - 1 ] );
  107. }
  108. }
  109. if ( i === breaks.length - 1 ) {
  110. for ( let j = 0; j < 4; j ++ ) {
  111. cascade.vertices.far[ j ].copy( this.vertices.far[ j ] );
  112. }
  113. } else {
  114. for ( let j = 0; j < 4; j ++ ) {
  115. cascade.vertices.far[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i ] );
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Transforms the given target CSM frustum into the different coordinate system defined by the
  122. * given camera matrix.
  123. *
  124. * @param {Matrix4} cameraMatrix - The matrix that defines the new coordinate system.
  125. * @param {CSMFrustum} target - The CSM to convert.
  126. */
  127. toSpace( cameraMatrix, target ) {
  128. for ( let i = 0; i < 4; i ++ ) {
  129. target.vertices.near[ i ]
  130. .copy( this.vertices.near[ i ] )
  131. .applyMatrix4( cameraMatrix );
  132. target.vertices.far[ i ]
  133. .copy( this.vertices.far[ i ] )
  134. .applyMatrix4( cameraMatrix );
  135. }
  136. }
  137. }
  138. /**
  139. * Constructor data of `CSMFrustum`.
  140. *
  141. * @typedef {Object} CSMFrustum~Data
  142. * @property {boolean} [webGL] - Whether this CSM frustum is used with WebGL or WebGPU.
  143. * @property {Matrix4} [projectionMatrix] - A projection matrix usually of the scene's camera.
  144. * @property {number} [maxFar] - The maximum far value.
  145. **/
  146. export { CSMFrustum };