Points.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { Sphere } from '../math/Sphere.js';
  2. import { Ray } from '../math/Ray.js';
  3. import { Matrix4 } from '../math/Matrix4.js';
  4. import { Object3D } from '../core/Object3D.js';
  5. import { Vector3 } from '../math/Vector3.js';
  6. import { PointsMaterial } from '../materials/PointsMaterial.js';
  7. import { BufferGeometry } from '../core/BufferGeometry.js';
  8. const _inverseMatrix = new Matrix4();
  9. const _ray = new Ray();
  10. const _sphere = new Sphere();
  11. const _position = new Vector3();
  12. function Points( geometry = new BufferGeometry(), material = new PointsMaterial() ) {
  13. Object3D.call( this );
  14. this.type = 'Points';
  15. this.geometry = geometry;
  16. this.material = material;
  17. this.updateMorphTargets();
  18. }
  19. Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
  20. constructor: Points,
  21. isPoints: true,
  22. copy: function ( source ) {
  23. Object3D.prototype.copy.call( this, source );
  24. this.material = source.material;
  25. this.geometry = source.geometry;
  26. return this;
  27. },
  28. raycast: function ( raycaster, intersects ) {
  29. const geometry = this.geometry;
  30. const matrixWorld = this.matrixWorld;
  31. const threshold = raycaster.params.Points.threshold;
  32. // Checking boundingSphere distance to ray
  33. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  34. _sphere.copy( geometry.boundingSphere );
  35. _sphere.applyMatrix4( matrixWorld );
  36. _sphere.radius += threshold;
  37. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  38. //
  39. _inverseMatrix.copy( matrixWorld ).invert();
  40. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  41. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  42. const localThresholdSq = localThreshold * localThreshold;
  43. if ( geometry.isBufferGeometry ) {
  44. const index = geometry.index;
  45. const attributes = geometry.attributes;
  46. const positionAttribute = attributes.position;
  47. if ( index !== null ) {
  48. const indices = index.array;
  49. for ( let i = 0, il = indices.length; i < il; i ++ ) {
  50. const a = indices[ i ];
  51. _position.fromBufferAttribute( positionAttribute, a );
  52. testPoint( _position, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
  53. }
  54. } else {
  55. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  56. _position.fromBufferAttribute( positionAttribute, i );
  57. testPoint( _position, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
  58. }
  59. }
  60. } else {
  61. const vertices = geometry.vertices;
  62. for ( let i = 0, l = vertices.length; i < l; i ++ ) {
  63. testPoint( vertices[ i ], i, localThresholdSq, matrixWorld, raycaster, intersects, this );
  64. }
  65. }
  66. },
  67. updateMorphTargets: function () {
  68. const geometry = this.geometry;
  69. if ( geometry.isBufferGeometry ) {
  70. const morphAttributes = geometry.morphAttributes;
  71. const keys = Object.keys( morphAttributes );
  72. if ( keys.length > 0 ) {
  73. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  74. if ( morphAttribute !== undefined ) {
  75. this.morphTargetInfluences = [];
  76. this.morphTargetDictionary = {};
  77. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  78. const name = morphAttribute[ m ].name || String( m );
  79. this.morphTargetInfluences.push( 0 );
  80. this.morphTargetDictionary[ name ] = m;
  81. }
  82. }
  83. }
  84. } else {
  85. const morphTargets = geometry.morphTargets;
  86. if ( morphTargets !== undefined && morphTargets.length > 0 ) {
  87. console.error( 'THREE.Points.updateMorphTargets() does not support THREE.Geometry. Use THREE.BufferGeometry instead.' );
  88. }
  89. }
  90. }
  91. } );
  92. function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) {
  93. const rayPointDistanceSq = _ray.distanceSqToPoint( point );
  94. if ( rayPointDistanceSq < localThresholdSq ) {
  95. const intersectPoint = new Vector3();
  96. _ray.closestPointToPoint( point, intersectPoint );
  97. intersectPoint.applyMatrix4( matrixWorld );
  98. const distance = raycaster.ray.origin.distanceTo( intersectPoint );
  99. if ( distance < raycaster.near || distance > raycaster.far ) return;
  100. intersects.push( {
  101. distance: distance,
  102. distanceToRay: Math.sqrt( rayPointDistanceSq ),
  103. point: intersectPoint,
  104. index: index,
  105. face: null,
  106. object: object
  107. } );
  108. }
  109. }
  110. export { Points };