Frustum.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.Frustum = function ( ) {
  6. this.planes = [
  7. new THREE.Vector4(),
  8. new THREE.Vector4(),
  9. new THREE.Vector4(),
  10. new THREE.Vector4(),
  11. new THREE.Vector4(),
  12. new THREE.Vector4()
  13. ];
  14. };
  15. THREE.Frustum.prototype.setFromMatrix = function ( m ) {
  16. var i, plane,
  17. planes = this.planes;
  18. planes[ 0 ].set( m.n41 - m.n11, m.n42 - m.n12, m.n43 - m.n13, m.n44 - m.n14 );
  19. planes[ 1 ].set( m.n41 + m.n11, m.n42 + m.n12, m.n43 + m.n13, m.n44 + m.n14 );
  20. planes[ 2 ].set( m.n41 + m.n21, m.n42 + m.n22, m.n43 + m.n23, m.n44 + m.n24 );
  21. planes[ 3 ].set( m.n41 - m.n21, m.n42 - m.n22, m.n43 - m.n23, m.n44 - m.n24 );
  22. planes[ 4 ].set( m.n41 - m.n31, m.n42 - m.n32, m.n43 - m.n33, m.n44 - m.n34 );
  23. planes[ 5 ].set( m.n41 + m.n31, m.n42 + m.n32, m.n43 + m.n33, m.n44 + m.n34 );
  24. for ( i = 0; i < 6; i ++ ) {
  25. plane = planes[ i ];
  26. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  27. }
  28. };
  29. THREE.Frustum.prototype.contains = function ( object ) {
  30. var distance,
  31. planes = this.planes,
  32. matrix = object.matrixWorld,
  33. scale = THREE.Frustum.__v1.set( matrix.getColumnX().length(), matrix.getColumnY().length(), matrix.getColumnZ().length() ),
  34. radius = - object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) );
  35. for ( var i = 0; i < 6; i ++ ) {
  36. distance = planes[ i ].x * matrix.n14 + planes[ i ].y * matrix.n24 + planes[ i ].z * matrix.n34 + planes[ i ].w;
  37. if ( distance <= radius ) return false;
  38. }
  39. return true;
  40. };
  41. THREE.Frustum.__v1 = new THREE.Vector3();