Frustum.js 2.6 KB

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