Points.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.Points = function ( geometry, material ) {
  5. THREE.Object3D.call( this );
  6. this.type = 'Points';
  7. this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
  8. this.material = material !== undefined ? material : new THREE.PointsMaterial( { color: Math.random() * 0xffffff } );
  9. };
  10. THREE.Points.prototype = Object.create( THREE.Object3D.prototype );
  11. THREE.Points.prototype.constructor = THREE.Points;
  12. THREE.Points.prototype.raycast = ( function () {
  13. var inverseMatrix = new THREE.Matrix4();
  14. var ray = new THREE.Ray();
  15. return function raycast( raycaster, intersects ) {
  16. var object = this;
  17. var geometry = object.geometry;
  18. var threshold = raycaster.params.Points.threshold;
  19. inverseMatrix.getInverse( this.matrixWorld );
  20. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  21. if ( geometry.boundingBox !== null ) {
  22. if ( ray.intersectsBox( geometry.boundingBox ) === false ) return;
  23. }
  24. var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  25. var localThresholdSq = localThreshold * localThreshold;
  26. var position = new THREE.Vector3();
  27. function testPoint( point, index ) {
  28. var rayPointDistanceSq = ray.distanceSqToPoint( point );
  29. if ( rayPointDistanceSq < localThresholdSq ) {
  30. var intersectPoint = ray.closestPointToPoint( point );
  31. intersectPoint.applyMatrix4( object.matrixWorld );
  32. var distance = raycaster.ray.origin.distanceTo( intersectPoint );
  33. if ( distance < raycaster.near || distance > raycaster.far ) return;
  34. intersects.push( {
  35. distance: distance,
  36. distanceToRay: Math.sqrt( rayPointDistanceSq ),
  37. point: intersectPoint.clone(),
  38. index: index,
  39. face: null,
  40. object: object
  41. } );
  42. }
  43. }
  44. if ( geometry instanceof THREE.BufferGeometry ) {
  45. var index = geometry.index;
  46. var attributes = geometry.attributes;
  47. var positions = attributes.position.array;
  48. if ( index !== null ) {
  49. var indices = index.array;
  50. for ( var i = 0, il = indices.length; i < il; i ++ ) {
  51. var a = indices[ i ];
  52. position.fromArray( positions, a * 3 );
  53. testPoint( position, a );
  54. }
  55. } else {
  56. for ( var i = 0, l = positions.length / 3; i < l; i ++ ) {
  57. position.fromArray( positions, i * 3 );
  58. testPoint( position, i );
  59. }
  60. }
  61. } else {
  62. var vertices = geometry.vertices;
  63. for ( var i = 0, l = vertices.length; i < l; i ++ ) {
  64. testPoint( vertices[ i ], i );
  65. }
  66. }
  67. };
  68. }() );
  69. THREE.Points.prototype.clone = function () {
  70. return new this.constructor( this.geometry, this.material ).copy( this );
  71. };
  72. // Backwards compatibility
  73. THREE.PointCloud = function ( geometry, material ) {
  74. console.warn( 'THREE.PointCloud has been renamed to THREE.Points.' );
  75. return new THREE.Points( geometry, material );
  76. };
  77. THREE.ParticleSystem = function ( geometry, material ) {
  78. console.warn( 'THREE.ParticleSystem has been renamed to THREE.Points.' );
  79. return new THREE.Points( geometry, material );
  80. };
粤ICP备19079148号