Frustum.js 3.0 KB

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