Frustum.js 2.7 KB

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