Points.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 = /*@__PURE__*/ new Matrix4();
  9. const _ray = /*@__PURE__*/ new Ray();
  10. const _sphere = /*@__PURE__*/ new Sphere();
  11. const _position = /*@__PURE__*/ new Vector3();
  12. class Points extends Object3D {
  13. constructor( geometry = new BufferGeometry(), material = new PointsMaterial() ) {
  14. super();
  15. this.type = 'Points';
  16. this.geometry = geometry;
  17. this.material = material;
  18. this.updateMorphTargets();
  19. }
  20. copy( source ) {
  21. super.copy( source );
  22. this.material = source.material;
  23. this.geometry = source.geometry;
  24. return this;
  25. }
  26. raycast( raycaster, intersects ) {
  27. const geometry = this.geometry;
  28. const matrixWorld = this.matrixWorld;
  29. const threshold = raycaster.params.Points.threshold;
  30. const drawRange = geometry.drawRange;
  31. // Checking boundingSphere distance to ray
  32. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  33. _sphere.copy( geometry.boundingSphere );
  34. _sphere.applyMatrix4( matrixWorld );
  35. _sphere.radius += threshold;
  36. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  37. //
  38. _inverseMatrix.copy( matrixWorld ).invert();
  39. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  40. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  41. const localThresholdSq = localThreshold * localThreshold;
  42. if ( geometry.isBufferGeometry ) {
  43. const index = geometry.index;
  44. const attributes = geometry.attributes;
  45. const positionAttribute = attributes.position;
  46. if ( index !== null ) {
  47. const start = Math.max( 0, drawRange.start );
  48. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  49. for ( let i = start, il = end; i < il; i ++ ) {
  50. const a = index.getX( i );
  51. _position.fromBufferAttribute( positionAttribute, a );
  52. testPoint( _position, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
  53. }
  54. } else {
  55. const start = Math.max( 0, drawRange.start );
  56. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  57. for ( let i = start, l = end; i < l; i ++ ) {
  58. _position.fromBufferAttribute( positionAttribute, i );
  59. testPoint( _position, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
  60. }
  61. }
  62. } else {
  63. console.error( 'THREE.Points.raycast() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  64. }
  65. }
  66. updateMorphTargets() {
  67. const geometry = this.geometry;
  68. if ( geometry.isBufferGeometry ) {
  69. const morphAttributes = geometry.morphAttributes;
  70. const keys = Object.keys( morphAttributes );
  71. if ( keys.length > 0 ) {
  72. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  73. if ( morphAttribute !== undefined ) {
  74. this.morphTargetInfluences = [];
  75. this.morphTargetDictionary = {};
  76. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  77. const name = morphAttribute[ m ].name || String( m );
  78. this.morphTargetInfluences.push( 0 );
  79. this.morphTargetDictionary[ name ] = m;
  80. }
  81. }
  82. }
  83. } else {
  84. const morphTargets = geometry.morphTargets;
  85. if ( morphTargets !== undefined && morphTargets.length > 0 ) {
  86. console.error( 'THREE.Points.updateMorphTargets() does not support THREE.Geometry. Use THREE.BufferGeometry instead.' );
  87. }
  88. }
  89. }
  90. }
  91. Points.prototype.isPoints = true;
  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 };