Frustum.js 2.9 KB

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