Frustum.js 2.6 KB

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