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.isPoints = true;
  16. this.type = 'Points';
  17. this.geometry = geometry;
  18. this.material = material;
  19. this.updateMorphTargets();
  20. }
  21. copy( source ) {
  22. super.copy( source );
  23. this.material = 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. if ( geometry.isBufferGeometry ) {
  44. const index = geometry.index;
  45. const attributes = geometry.attributes;
  46. const positionAttribute = attributes.position;
  47. if ( index !== null ) {
  48. const start = Math.max( 0, drawRange.start );
  49. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  50. for ( let i = start, il = end; i < il; i ++ ) {
  51. const a = index.getX( i );
  52. _position.fromBufferAttribute( positionAttribute, a );
  53. testPoint( _position, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
  54. }
  55. } else {
  56. const start = Math.max( 0, drawRange.start );
  57. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  58. for ( let i = start, l = end; i < l; i ++ ) {
  59. _position.fromBufferAttribute( positionAttribute, i );
  60. testPoint( _position, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
  61. }
  62. }
  63. } else {
  64. console.error( 'THREE.Points.raycast() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  65. }
  66. }
  67. updateMorphTargets() {
  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 };
粤ICP备19079148号