Points.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /**
  13. * A class for displaying points or point clouds.
  14. *
  15. * @augments Object3D
  16. */
  17. class Points extends Object3D {
  18. /**
  19. * Constructs a new point cloud.
  20. *
  21. * @param {BufferGeometry} [geometry] - The points geometry.
  22. * @param {Material|Array<Material>} [material] - The points material.
  23. */
  24. constructor( geometry = new BufferGeometry(), material = new PointsMaterial() ) {
  25. super();
  26. /**
  27. * This flag can be used for type testing.
  28. *
  29. * @type {boolean}
  30. * @readonly
  31. * @default true
  32. */
  33. this.isPoints = true;
  34. this.type = 'Points';
  35. /**
  36. * The points geometry.
  37. *
  38. * @type {BufferGeometry}
  39. */
  40. this.geometry = geometry;
  41. /**
  42. * The line material.
  43. *
  44. * @type {Material|Array<Material>}
  45. * @default PointsMaterial
  46. */
  47. this.material = material;
  48. /**
  49. * A dictionary representing the morph targets in the geometry. The key is the
  50. * morph targets name, the value its attribute index. This member is `undefined`
  51. * by default and only set when morph targets are detected in the geometry.
  52. *
  53. * @type {Object<String,number>|undefined}
  54. * @default undefined
  55. */
  56. this.morphTargetDictionary = undefined;
  57. /**
  58. * An array of weights typically in the range `[0,1]` that specify how much of the morph
  59. * is applied. This member is `undefined` by default and only set when morph targets are
  60. * detected in the geometry.
  61. *
  62. * @type {Array<number>|undefined}
  63. * @default undefined
  64. */
  65. this.morphTargetInfluences = undefined;
  66. this.updateMorphTargets();
  67. }
  68. copy( source, recursive ) {
  69. super.copy( source, recursive );
  70. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  71. this.geometry = source.geometry;
  72. return this;
  73. }
  74. /**
  75. * Computes intersection points between a casted ray and this point cloud.
  76. *
  77. * @param {Raycaster} raycaster - The raycaster.
  78. * @param {Array<Object>} intersects - The target array that holds the intersection points.
  79. */
  80. raycast( raycaster, intersects ) {
  81. const geometry = this.geometry;
  82. const matrixWorld = this.matrixWorld;
  83. const threshold = raycaster.params.Points.threshold;
  84. const drawRange = geometry.drawRange;
  85. // Checking boundingSphere distance to ray
  86. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  87. _sphere.copy( geometry.boundingSphere );
  88. _sphere.applyMatrix4( matrixWorld );
  89. _sphere.radius += threshold;
  90. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  91. //
  92. _inverseMatrix.copy( matrixWorld ).invert();
  93. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  94. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  95. const localThresholdSq = localThreshold * localThreshold;
  96. const index = geometry.index;
  97. const attributes = geometry.attributes;
  98. const positionAttribute = attributes.position;
  99. if ( index !== null ) {
  100. const start = Math.max( 0, drawRange.start );
  101. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  102. for ( let i = start, il = end; i < il; i ++ ) {
  103. const a = index.getX( i );
  104. _position.fromBufferAttribute( positionAttribute, a );
  105. testPoint( _position, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
  106. }
  107. } else {
  108. const start = Math.max( 0, drawRange.start );
  109. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  110. for ( let i = start, l = end; i < l; i ++ ) {
  111. _position.fromBufferAttribute( positionAttribute, i );
  112. testPoint( _position, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
  113. }
  114. }
  115. }
  116. /**
  117. * Sets the values of {@link Points#morphTargetDictionary} and {@link Points#morphTargetInfluences}
  118. * to make sure existing morph targets can influence this 3D object.
  119. */
  120. updateMorphTargets() {
  121. const geometry = this.geometry;
  122. const morphAttributes = geometry.morphAttributes;
  123. const keys = Object.keys( morphAttributes );
  124. if ( keys.length > 0 ) {
  125. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  126. if ( morphAttribute !== undefined ) {
  127. this.morphTargetInfluences = [];
  128. this.morphTargetDictionary = {};
  129. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  130. const name = morphAttribute[ m ].name || String( m );
  131. this.morphTargetInfluences.push( 0 );
  132. this.morphTargetDictionary[ name ] = m;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) {
  139. const rayPointDistanceSq = _ray.distanceSqToPoint( point );
  140. if ( rayPointDistanceSq < localThresholdSq ) {
  141. const intersectPoint = new Vector3();
  142. _ray.closestPointToPoint( point, intersectPoint );
  143. intersectPoint.applyMatrix4( matrixWorld );
  144. const distance = raycaster.ray.origin.distanceTo( intersectPoint );
  145. if ( distance < raycaster.near || distance > raycaster.far ) return;
  146. intersects.push( {
  147. distance: distance,
  148. distanceToRay: Math.sqrt( rayPointDistanceSq ),
  149. point: intersectPoint,
  150. index: index,
  151. face: null,
  152. faceIndex: null,
  153. barycoord: null,
  154. object: object
  155. } );
  156. }
  157. }
  158. export { Points };
粤ICP备19079148号