CSMFrustum.js 2.7 KB

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