CSMFrustum.js 4.5 KB

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