CSMFrustum.js 2.7 KB

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