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