PointCloud.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.PointCloud = function ( geometry, material ) {
  5. THREE.Object3D.call( this );
  6. this.type = 'PointCloud';
  7. this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
  8. this.material = material !== undefined ? material : new THREE.PointCloudMaterial( { color: Math.random() * 0xffffff } );
  9. this.sortParticles = false;
  10. };
  11. THREE.PointCloud.prototype = Object.create( THREE.Object3D.prototype );
  12. THREE.PointCloud.prototype.constructor = THREE.PointCloud;
  13. THREE.PointCloud.prototype.raycast = ( function () {
  14. var inverseMatrix = new THREE.Matrix4();
  15. var ray = new THREE.Ray();
  16. return function ( raycaster, intersects ) {
  17. var object = this;
  18. var geometry = object.geometry;
  19. var threshold = raycaster.params.PointCloud.threshold;
  20. inverseMatrix.getInverse( this.matrixWorld );
  21. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  22. if ( geometry.boundingBox !== null ) {
  23. if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
  24. return;
  25. }
  26. }
  27. var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  28. var position = new THREE.Vector3();
  29. var testPoint = function ( point, index ) {
  30. var rayPointDistance = ray.distanceToPoint( point );
  31. if ( rayPointDistance < localThreshold ) {
  32. var intersectPoint = ray.closestPointToPoint( point );
  33. intersectPoint.applyMatrix4( object.matrixWorld );
  34. var distance = raycaster.ray.origin.distanceTo( intersectPoint );
  35. intersects.push( {
  36. distance: distance,
  37. distanceToRay: rayPointDistance,
  38. point: intersectPoint.clone(),
  39. index: index,
  40. face: null,
  41. object: object
  42. } );
  43. }
  44. };
  45. if ( geometry instanceof THREE.BufferGeometry ) {
  46. var attributes = geometry.attributes;
  47. var positions = attributes.position.array;
  48. if ( attributes.index !== undefined ) {
  49. var indices = attributes.index.array;
  50. var offsets = geometry.offsets;
  51. if ( offsets.length === 0 ) {
  52. var offset = {
  53. start: 0,
  54. count: indices.length,
  55. index: 0
  56. };
  57. offsets = [ offset ];
  58. }
  59. for ( var oi = 0, ol = offsets.length; oi < ol; ++oi ) {
  60. var start = offsets[ oi ].start;
  61. var count = offsets[ oi ].count;
  62. var index = offsets[ oi ].index;
  63. for ( var i = start, il = start + count; i < il; i ++ ) {
  64. var a = index + indices[ i ];
  65. position.fromArray( positions, a * 3 );
  66. testPoint( position, a );
  67. }
  68. }
  69. } else {
  70. var pointCount = positions.length / 3;
  71. for ( var i = 0; i < pointCount; i ++ ) {
  72. position.set(
  73. positions[ 3 * i ],
  74. positions[ 3 * i + 1 ],
  75. positions[ 3 * i + 2 ]
  76. );
  77. testPoint( position, i );
  78. }
  79. }
  80. } else {
  81. var vertices = this.geometry.vertices;
  82. for ( var i = 0; i < vertices.length; i ++ ) {
  83. testPoint( vertices[ i ], i );
  84. }
  85. }
  86. };
  87. }() );
  88. THREE.PointCloud.prototype.clone = function ( object ) {
  89. if ( object === undefined ) object = new THREE.PointCloud( this.geometry, this.material );
  90. object.sortParticles = this.sortParticles;
  91. THREE.Object3D.prototype.clone.call( this, object );
  92. return object;
  93. };
  94. // Backwards compatibility
  95. THREE.ParticleSystem = function ( geometry, material ) {
  96. console.warn( 'THREE.ParticleSystem has been renamed to THREE.PointCloud.' );
  97. return new THREE.PointCloud( geometry, material );
  98. };