Frustum.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @author vHawk / https://github.com/vHawk/
  3. */
  4. import { MathUtils, Vector3 } from '../../../build/three.module.js';
  5. export default class Frustum {
  6. constructor( data ) {
  7. data = data || {};
  8. this.fov = data.fov || 70;
  9. this.near = data.near || 0.1;
  10. this.far = data.far || 1000;
  11. this.aspect = data.aspect || 1;
  12. this.vertices = {
  13. near: [],
  14. far: []
  15. };
  16. }
  17. getViewSpaceVertices() {
  18. this.nearPlaneY = this.near * Math.tan( MathUtils.degToRad( this.fov / 2 ) );
  19. this.nearPlaneX = this.aspect * this.nearPlaneY;
  20. this.farPlaneY = this.far * Math.tan( MathUtils.degToRad( this.fov / 2 ) );
  21. this.farPlaneX = this.aspect * this.farPlaneY;
  22. // 3 --- 0 vertices.near/far order
  23. // | |
  24. // 2 --- 1
  25. this.vertices.near.push(
  26. new Vector3( this.nearPlaneX, this.nearPlaneY, - this.near ),
  27. new Vector3( this.nearPlaneX, - this.nearPlaneY, - this.near ),
  28. new Vector3( - this.nearPlaneX, - this.nearPlaneY, - this.near ),
  29. new Vector3( - this.nearPlaneX, this.nearPlaneY, - this.near )
  30. );
  31. this.vertices.far.push(
  32. new Vector3( this.farPlaneX, this.farPlaneY, - this.far ),
  33. new Vector3( this.farPlaneX, - this.farPlaneY, - this.far ),
  34. new Vector3( - this.farPlaneX, - this.farPlaneY, - this.far ),
  35. new Vector3( - this.farPlaneX, this.farPlaneY, - this.far )
  36. );
  37. return this.vertices;
  38. }
  39. split( breaks ) {
  40. const result = [];
  41. for ( let i = 0; i < breaks.length; i ++ ) {
  42. const cascade = new Frustum();
  43. if ( i === 0 ) {
  44. cascade.vertices.near = this.vertices.near;
  45. } else {
  46. for ( let j = 0; j < 4; j ++ ) {
  47. cascade.vertices.near.push( new Vector3().lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i - 1 ] ) );
  48. }
  49. }
  50. if ( i === breaks - 1 ) {
  51. cascade.vertices.far = this.vertices.far;
  52. } else {
  53. for ( let j = 0; j < 4; j ++ ) {
  54. cascade.vertices.far.push( new Vector3().lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i ] ) );
  55. }
  56. }
  57. result.push( cascade );
  58. }
  59. return result;
  60. }
  61. toSpace( cameraMatrix ) {
  62. const result = new Frustum();
  63. const point = new Vector3();
  64. for ( var i = 0; i < 4; i ++ ) {
  65. point.set( this.vertices.near[ i ].x, this.vertices.near[ i ].y, this.vertices.near[ i ].z );
  66. point.applyMatrix4( cameraMatrix );
  67. result.vertices.near.push( point.clone() );
  68. point.set( this.vertices.far[ i ].x, this.vertices.far[ i ].y, this.vertices.far[ i ].z );
  69. point.applyMatrix4( cameraMatrix );
  70. result.vertices.far.push( point.clone() );
  71. }
  72. return result;
  73. }
  74. }