Plane.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { Matrix3 } from './Matrix3.js';
  2. import { Vector3 } from './Vector3.js';
  3. const _vector1 = /*@__PURE__*/ new Vector3();
  4. const _vector2 = /*@__PURE__*/ new Vector3();
  5. const _normalMatrix = /*@__PURE__*/ new Matrix3();
  6. class Plane {
  7. constructor( normal = new Vector3( 1, 0, 0 ), constant = 0 ) {
  8. // normal is assumed to be normalized
  9. this.normal = normal;
  10. this.constant = constant;
  11. }
  12. set( normal, constant ) {
  13. this.normal.copy( normal );
  14. this.constant = constant;
  15. return this;
  16. }
  17. setComponents( x, y, z, w ) {
  18. this.normal.set( x, y, z );
  19. this.constant = w;
  20. return this;
  21. }
  22. setFromNormalAndCoplanarPoint( normal, point ) {
  23. this.normal.copy( normal );
  24. this.constant = - point.dot( this.normal );
  25. return this;
  26. }
  27. setFromCoplanarPoints( a, b, c ) {
  28. const normal = _vector1.subVectors( c, b ).cross( _vector2.subVectors( a, b ) ).normalize();
  29. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  30. this.setFromNormalAndCoplanarPoint( normal, a );
  31. return this;
  32. }
  33. copy( plane ) {
  34. this.normal.copy( plane.normal );
  35. this.constant = plane.constant;
  36. return this;
  37. }
  38. normalize() {
  39. // Note: will lead to a divide by zero if the plane is invalid.
  40. const inverseNormalLength = 1.0 / this.normal.length();
  41. this.normal.multiplyScalar( inverseNormalLength );
  42. this.constant *= inverseNormalLength;
  43. return this;
  44. }
  45. negate() {
  46. this.constant *= - 1;
  47. this.normal.negate();
  48. return this;
  49. }
  50. distanceToPoint( point ) {
  51. return this.normal.dot( point ) + this.constant;
  52. }
  53. distanceToSphere( sphere ) {
  54. return this.distanceToPoint( sphere.center ) - sphere.radius;
  55. }
  56. projectPoint( point, target ) {
  57. return target.copy( this.normal ).multiplyScalar( - this.distanceToPoint( point ) ).add( point );
  58. }
  59. intersectLine( line, target ) {
  60. const direction = line.delta( _vector1 );
  61. const denominator = this.normal.dot( direction );
  62. if ( denominator === 0 ) {
  63. // line is coplanar, return origin
  64. if ( this.distanceToPoint( line.start ) === 0 ) {
  65. return target.copy( line.start );
  66. }
  67. // Unsure if this is the correct method to handle this case.
  68. return null;
  69. }
  70. const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  71. if ( t < 0 || t > 1 ) {
  72. return null;
  73. }
  74. return target.copy( direction ).multiplyScalar( t ).add( line.start );
  75. }
  76. intersectsLine( line ) {
  77. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  78. const startSign = this.distanceToPoint( line.start );
  79. const endSign = this.distanceToPoint( line.end );
  80. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  81. }
  82. intersectsBox( box ) {
  83. return box.intersectsPlane( this );
  84. }
  85. intersectsSphere( sphere ) {
  86. return sphere.intersectsPlane( this );
  87. }
  88. coplanarPoint( target ) {
  89. return target.copy( this.normal ).multiplyScalar( - this.constant );
  90. }
  91. applyMatrix4( matrix, optionalNormalMatrix ) {
  92. const normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix );
  93. const referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix );
  94. const normal = this.normal.applyMatrix3( normalMatrix ).normalize();
  95. this.constant = - referencePoint.dot( normal );
  96. return this;
  97. }
  98. translate( offset ) {
  99. this.constant -= offset.dot( this.normal );
  100. return this;
  101. }
  102. equals( plane ) {
  103. return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
  104. }
  105. clone() {
  106. return new this.constructor().copy( this );
  107. }
  108. }
  109. Plane.prototype.isPlane = true;
  110. export { Plane };
粤ICP备19079148号