Points.js 3.1 KB

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