Points.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.isPoints = true;
  16. this.type = 'Points';
  17. this.geometry = geometry;
  18. this.material = material;
  19. this.updateMorphTargets();
  20. }
  21. copy( source, recursive ) {
  22. super.copy( source, recursive );
  23. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  24. this.geometry = source.geometry;
  25. return this;
  26. }
  27. raycast( raycaster, intersects ) {
  28. const geometry = this.geometry;
  29. const matrixWorld = this.matrixWorld;
  30. const threshold = raycaster.params.Points.threshold;
  31. const drawRange = geometry.drawRange;
  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. 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. }
  63. updateMorphTargets() {
  64. const geometry = this.geometry;
  65. const morphAttributes = geometry.morphAttributes;
  66. const keys = Object.keys( morphAttributes );
  67. if ( keys.length > 0 ) {
  68. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  69. if ( morphAttribute !== undefined ) {
  70. this.morphTargetInfluences = [];
  71. this.morphTargetDictionary = {};
  72. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  73. const name = morphAttribute[ m ].name || String( m );
  74. this.morphTargetInfluences.push( 0 );
  75. this.morphTargetDictionary[ name ] = m;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) {
  82. const rayPointDistanceSq = _ray.distanceSqToPoint( point );
  83. if ( rayPointDistanceSq < localThresholdSq ) {
  84. const intersectPoint = new Vector3();
  85. _ray.closestPointToPoint( point, intersectPoint );
  86. intersectPoint.applyMatrix4( matrixWorld );
  87. const distance = raycaster.ray.origin.distanceTo( intersectPoint );
  88. if ( distance < raycaster.near || distance > raycaster.far ) return;
  89. intersects.push( {
  90. distance: distance,
  91. distanceToRay: Math.sqrt( rayPointDistanceSq ),
  92. point: intersectPoint,
  93. index: index,
  94. face: null,
  95. object: object
  96. } );
  97. }
  98. }
  99. export { Points };
粤ICP备19079148号