Frustum.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 plane;
  17. var planes = this.planes;
  18. var me = m.elements;
  19. var me0 = me[0], me1 = me[1], me2 = me[2], me3 = me[3];
  20. var me4 = me[4], me5 = me[5], me6 = me[6], me7 = me[7];
  21. var me8 = me[8], me9 = me[9], me10 = me[10], me11 = me[11];
  22. var me12 = me[12], me13 = me[13], me14 = me[14], me15 = me[15];
  23. planes[ 0 ].set( me3 - me0, me7 - me4, me11 - me8, me15 - me12 );
  24. planes[ 1 ].set( me3 + me0, me7 + me4, me11 + me8, me15 + me12 );
  25. planes[ 2 ].set( me3 + me1, me7 + me5, me11 + me9, me15 + me13 );
  26. planes[ 3 ].set( me3 - me1, me7 - me5, me11 - me9, me15 - me13 );
  27. planes[ 4 ].set( me3 - me2, me7 - me6, me11 - me10, me15 - me14 );
  28. planes[ 5 ].set( me3 + me2, me7 + me6, me11 + me10, me15 + me14 );
  29. for ( var i = 0; i < 6; i ++ ) {
  30. plane = planes[ i ];
  31. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  32. }
  33. };
  34. THREE.Frustum.prototype.contains = function ( object ) {
  35. var distance = 0.0;
  36. var planes = this.planes;
  37. var matrix = object.matrixWorld;
  38. var me = matrix.elements;
  39. var radius = - object.geometry.boundingSphere.radius * matrix.getMaxScaleOnAxis();
  40. for ( var i = 0; i < 6; i ++ ) {
  41. distance = planes[ i ].x * me[12] + planes[ i ].y * me[13] + planes[ i ].z * me[14] + planes[ i ].w;
  42. if ( distance <= radius ) return false;
  43. }
  44. return true;
  45. };
  46. THREE.Frustum.__v1 = new THREE.Vector3();