CSMFrustum.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. * The zFar value.
  26. *
  27. * @type {number}
  28. */
  29. this.zFar = 1;
  30. /**
  31. * An object representing the vertices of the near and
  32. * far plane in view space.
  33. *
  34. * @type {Object}
  35. */
  36. this.vertices = {
  37. near: [
  38. new Vector3(),
  39. new Vector3(),
  40. new Vector3(),
  41. new Vector3()
  42. ],
  43. far: [
  44. new Vector3(),
  45. new Vector3(),
  46. new Vector3(),
  47. new Vector3()
  48. ]
  49. };
  50. if ( data.projectionMatrix !== undefined ) {
  51. this.setFromProjectionMatrix( data.projectionMatrix, data.maxFar || 10000 );
  52. }
  53. // In case of reversed depth buffer, zNear and zFar must be 1 and 0
  54. // respectively regardless of the coordinate system.
  55. if ( data.reversedDepth === true ) {
  56. this.zNear = 1;
  57. this.zFar = 0;
  58. }
  59. }
  60. /**
  61. * Setups this CSM frustum from the given projection matrix and max far value.
  62. *
  63. * @param {Matrix4} projectionMatrix - The projection matrix, usually of the scene's camera.
  64. * @param {number} maxFar - The maximum far value.
  65. * @returns {Object} An object representing the vertices of the near and far plane in view space.
  66. */
  67. setFromProjectionMatrix( projectionMatrix, maxFar ) {
  68. const zNear = this.zNear;
  69. const zFar = this.zFar;
  70. const isOrthographic = projectionMatrix.elements[ 2 * 4 + 3 ] === 0;
  71. inverseProjectionMatrix.copy( projectionMatrix ).invert();
  72. // 3 --- 0 vertices.near/far order
  73. // | |
  74. // 2 --- 1
  75. // clip space spans from [-1, 1]
  76. this.vertices.near[ 0 ].set( 1, 1, zNear );
  77. this.vertices.near[ 1 ].set( 1, - 1, zNear );
  78. this.vertices.near[ 2 ].set( - 1, - 1, zNear );
  79. this.vertices.near[ 3 ].set( - 1, 1, zNear );
  80. this.vertices.near.forEach( function ( v ) {
  81. v.applyMatrix4( inverseProjectionMatrix );
  82. } );
  83. this.vertices.far[ 0 ].set( 1, 1, zFar );
  84. this.vertices.far[ 1 ].set( 1, - 1, zFar );
  85. this.vertices.far[ 2 ].set( - 1, - 1, zFar );
  86. this.vertices.far[ 3 ].set( - 1, 1, zFar );
  87. this.vertices.far.forEach( function ( v ) {
  88. v.applyMatrix4( inverseProjectionMatrix );
  89. const absZ = Math.abs( v.z );
  90. if ( isOrthographic ) {
  91. v.z *= Math.min( maxFar / absZ, 1.0 );
  92. } else {
  93. v.multiplyScalar( Math.min( maxFar / absZ, 1.0 ) );
  94. }
  95. } );
  96. return this.vertices;
  97. }
  98. /**
  99. * Splits the CSM frustum by the given array. The new CSM frustum are pushed into the given
  100. * target array.
  101. *
  102. * @param {Array<number>} breaks - An array of numbers in the range `[0,1]` the defines how the
  103. * CSM frustum should be split up.
  104. * @param {Array<CSMFrustum>} target - The target array that holds the new CSM frustums.
  105. */
  106. split( breaks, target ) {
  107. while ( breaks.length > target.length ) {
  108. target.push( new CSMFrustum() );
  109. }
  110. target.length = breaks.length;
  111. const near = this.vertices.near[ 0 ].z;
  112. const far = this.vertices.far[ 0 ].z;
  113. for ( let i = 0; i < breaks.length; i ++ ) {
  114. const cascade = target[ i ];
  115. if ( i === 0 ) {
  116. for ( let j = 0; j < 4; j ++ ) {
  117. cascade.vertices.near[ j ].copy( this.vertices.near[ j ] );
  118. }
  119. } else {
  120. const alpha = ( breaks[ i - 1 ] * far - near ) / ( far - near );
  121. for ( let j = 0; j < 4; j ++ ) {
  122. cascade.vertices.near[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], alpha );
  123. }
  124. }
  125. if ( i === breaks.length - 1 ) {
  126. for ( let j = 0; j < 4; j ++ ) {
  127. cascade.vertices.far[ j ].copy( this.vertices.far[ j ] );
  128. }
  129. } else {
  130. const alpha = ( breaks[ i ] * far - near ) / ( far - near );
  131. for ( let j = 0; j < 4; j ++ ) {
  132. cascade.vertices.far[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], alpha );
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Transforms the given target CSM frustum into the different coordinate system defined by the
  139. * given camera matrix.
  140. *
  141. * @param {Matrix4} cameraMatrix - The matrix that defines the new coordinate system.
  142. * @param {CSMFrustum} target - The CSM to convert.
  143. */
  144. toSpace( cameraMatrix, target ) {
  145. for ( let i = 0; i < 4; i ++ ) {
  146. target.vertices.near[ i ]
  147. .copy( this.vertices.near[ i ] )
  148. .applyMatrix4( cameraMatrix );
  149. target.vertices.far[ i ]
  150. .copy( this.vertices.far[ i ] )
  151. .applyMatrix4( cameraMatrix );
  152. }
  153. }
  154. }
  155. /**
  156. * Constructor data of `CSMFrustum`.
  157. *
  158. * @typedef {Object} CSMFrustum~Data
  159. * @property {boolean} [webGL] - Whether this CSM frustum is used with WebGL or WebGPU.
  160. * @property {boolean} [reversedDepth] - Whether reversed depth buffer is enabled.
  161. * @property {Matrix4} [projectionMatrix] - A projection matrix usually of the scene's camera.
  162. * @property {number} [maxFar] - The maximum far value.
  163. **/
  164. export { CSMFrustum };
粤ICP备19079148号