Points.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.isIntersectionBox( geometry.boundingBox ) === false ) {
  23. return;
  24. }
  25. }
  26. var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  27. var localThresholdSq = localThreshold * localThreshold;
  28. var position = new THREE.Vector3();
  29. function testPoint( point, index ) {
  30. var rayPointDistanceSq = ray.distanceSqToPoint( point );
  31. if ( rayPointDistanceSq < localThresholdSq ) {
  32. var intersectPoint = ray.closestPointToPoint( point );
  33. intersectPoint.applyMatrix4( object.matrixWorld );
  34. var distance = raycaster.ray.origin.distanceTo( intersectPoint );
  35. if ( distance < raycaster.near || distance > raycaster.far ) return;
  36. intersects.push( {
  37. distance: distance,
  38. distanceToRay: Math.sqrt( rayPointDistanceSq ),
  39. point: intersectPoint.clone(),
  40. index: index,
  41. face: null,
  42. object: object
  43. } );
  44. }
  45. }
  46. if ( geometry instanceof THREE.BufferGeometry ) {
  47. var index = geometry.index;
  48. var attributes = geometry.attributes;
  49. var positions = attributes.position.array;
  50. if ( index !== null ) {
  51. var indices = index.array;
  52. for ( var i = 0, il = indices.length; i < il; i ++ ) {
  53. var a = indices[ i ];
  54. position.fromArray( positions, a * 3 );
  55. testPoint( position, a );
  56. }
  57. } else {
  58. for ( var i = 0, l = positions.length / 3; i < l; i ++ ) {
  59. position.fromArray( positions, i * 3 );
  60. testPoint( position, i );
  61. }
  62. }
  63. } else {
  64. var vertices = geometry.vertices;
  65. for ( var i = 0, l = vertices.length; i < l; i ++ ) {
  66. testPoint( vertices[ i ], i );
  67. }
  68. }
  69. };
  70. }() );
  71. THREE.Points.prototype.clone = function () {
  72. return new this.constructor( this.geometry, this.material ).copy( this );
  73. };
  74. THREE.Points.prototype.toJSON = function ( meta ) {
  75. var data = THREE.Object3D.prototype.toJSON.call( this, meta );
  76. data.object.geometry = this.geometry.uuid;
  77. data.object.material = this.material.uuid;
  78. return data;
  79. };
  80. // Backwards compatibility
  81. THREE.PointCloud = function ( geometry, material ) {
  82. console.warn( 'THREE.PointCloud has been renamed to THREE.Points.' );
  83. return new THREE.Points( geometry, material );
  84. };
  85. THREE.ParticleSystem = function ( geometry, material ) {
  86. console.warn( 'THREE.ParticleSystem has been renamed to THREE.Points.' );
  87. return new THREE.Points( geometry, material );
  88. };
粤ICP备19079148号