CSMFrustum.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { Vector3, Matrix4 } from '../../../build/three.module.js';
  2. const inverseProjectionMatrix = new Matrix4();
  3. class CSMFrustum {
  4. constructor( data ) {
  5. data = data || {};
  6. this.vertices = {
  7. near: [
  8. new Vector3(),
  9. new Vector3(),
  10. new Vector3(),
  11. new Vector3()
  12. ],
  13. far: [
  14. new Vector3(),
  15. new Vector3(),
  16. new Vector3(),
  17. new Vector3()
  18. ]
  19. };
  20. if ( data.projectionMatrix !== undefined ) {
  21. this.setFromProjectionMatrix( data.projectionMatrix, data.maxFar || 10000 );
  22. }
  23. }
  24. setFromProjectionMatrix( projectionMatrix, maxFar ) {
  25. const isOrthographic = projectionMatrix.elements[ 2 * 4 + 3 ] === 0;
  26. inverseProjectionMatrix.copy( projectionMatrix ).invert();
  27. // 3 --- 0 vertices.near/far order
  28. // | |
  29. // 2 --- 1
  30. // clip space spans from [-1, 1]
  31. this.vertices.near[ 0 ].set( 1, 1, - 1 );
  32. this.vertices.near[ 1 ].set( 1, - 1, - 1 );
  33. this.vertices.near[ 2 ].set( - 1, - 1, - 1 );
  34. this.vertices.near[ 3 ].set( - 1, 1, - 1 );
  35. this.vertices.near.forEach( function ( v ) {
  36. v.applyMatrix4( inverseProjectionMatrix );
  37. } );
  38. this.vertices.far[ 0 ].set( 1, 1, 1 );
  39. this.vertices.far[ 1 ].set( 1, - 1, 1 );
  40. this.vertices.far[ 2 ].set( - 1, - 1, 1 );
  41. this.vertices.far[ 3 ].set( - 1, 1, 1 );
  42. this.vertices.far.forEach( function ( v ) {
  43. v.applyMatrix4( inverseProjectionMatrix );
  44. const absZ = Math.abs( v.z );
  45. if ( isOrthographic ) {
  46. v.z *= Math.min( maxFar / absZ, 1.0 );
  47. } else {
  48. v.multiplyScalar( Math.min( maxFar / absZ, 1.0 ) );
  49. }
  50. } );
  51. return this.vertices;
  52. }
  53. split( breaks, target ) {
  54. while ( breaks.length > target.length ) {
  55. target.push( new CSMFrustum() );
  56. }
  57. target.length = breaks.length;
  58. for ( let i = 0; i < breaks.length; i ++ ) {
  59. const cascade = target[ i ];
  60. if ( i === 0 ) {
  61. for ( let j = 0; j < 4; j ++ ) {
  62. cascade.vertices.near[ j ].copy( this.vertices.near[ j ] );
  63. }
  64. } else {
  65. for ( let j = 0; j < 4; j ++ ) {
  66. cascade.vertices.near[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i - 1 ] );
  67. }
  68. }
  69. if ( i === breaks.length - 1 ) {
  70. for ( let j = 0; j < 4; j ++ ) {
  71. cascade.vertices.far[ j ].copy( this.vertices.far[ j ] );
  72. }
  73. } else {
  74. for ( let j = 0; j < 4; j ++ ) {
  75. cascade.vertices.far[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i ] );
  76. }
  77. }
  78. }
  79. }
  80. toSpace( cameraMatrix, target ) {
  81. for ( var i = 0; i < 4; i ++ ) {
  82. target.vertices.near[ i ]
  83. .copy( this.vertices.near[ i ] )
  84. .applyMatrix4( cameraMatrix );
  85. target.vertices.far[ i ]
  86. .copy( this.vertices.far[ i ] )
  87. .applyMatrix4( cameraMatrix );
  88. }
  89. }
  90. }
  91. export { CSMFrustum };