Points.js 4.1 KB

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