PointCloud.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. };
  10. THREE.PointCloud.prototype = Object.create( THREE.Object3D.prototype );
  11. THREE.PointCloud.prototype.constructor = THREE.PointCloud;
  12. THREE.PointCloud.prototype.raycast = ( function () {
  13. var inverseMatrix = new THREE.Matrix4();
  14. var ray = new THREE.Ray();
  15. return function raycast( raycaster, intersects ) {
  16. var object = this;
  17. var geometry = object.geometry;
  18. var threshold = raycaster.params.PointCloud.threshold;
  19. inverseMatrix.getInverse( this.matrixWorld );
  20. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  21. if ( geometry.boundingBox !== null ) {
  22. if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
  23. return;
  24. }
  25. }
  26. var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  27. var localThresholdSq = localThreshold * localThreshold;
  28. var position = new THREE.Vector3();
  29. var testPoint = function ( point, index ) {
  30. var rayPointDistanceSq = ray.distanceSqToPoint( point );
  31. if ( rayPointDistanceSq < localThresholdSq ) {
  32. var intersectPoint = ray.closestPointToPoint( point );
  33. intersectPoint.applyMatrix4( object.matrixWorld );
  34. var distance = raycaster.ray.origin.distanceTo( intersectPoint );
  35. if ( distance < raycaster.near || distance > raycaster.far ) return;
  36. intersects.push( {
  37. distance: distance,
  38. distanceToRay: Math.sqrt( rayPointDistanceSq ),
  39. point: intersectPoint.clone(),
  40. index: index,
  41. face: null,
  42. object: object
  43. } );
  44. }
  45. };
  46. if ( geometry instanceof THREE.BufferGeometry ) {
  47. var attributes = geometry.attributes;
  48. var positions = attributes.position.array;
  49. if ( attributes.index !== undefined ) {
  50. var indices = attributes.index.array;
  51. var offsets = geometry.drawcalls;
  52. if ( offsets.length === 0 ) {
  53. offsets = [ { start: 0, count: indices.length, index: 0 } ];
  54. }
  55. for ( var oi = 0, ol = offsets.length; oi < ol; ++ oi ) {
  56. var offset = offsets[ oi ];
  57. var start = offset.start;
  58. var count = offset.count;
  59. var index = offset.index;
  60. for ( var i = start, il = start + count; i < il; i ++ ) {
  61. var a = index + indices[ i ];
  62. position.fromArray( positions, a * 3 );
  63. testPoint( position, a );
  64. }
  65. }
  66. } else {
  67. for ( var i = 0, l = positions.length / 3; i < l; i ++ ) {
  68. position.fromArray( positions, i * 3 );
  69. testPoint( position, i );
  70. }
  71. }
  72. } else {
  73. var vertices = geometry.vertices;
  74. for ( var i = 0, l = vertices.length; i < l; i ++ ) {
  75. testPoint( vertices[ i ], i );
  76. }
  77. }
  78. };
  79. }() );
  80. THREE.PointCloud.prototype.clone = function () {
  81. var pointCloud = new THREE.PointCloud( this.geometry, this.material );
  82. return pointCloud.copy( this );
  83. };
  84. THREE.PointCloud.prototype.copy = function ( source ) {
  85. THREE.Object3D.prototype.copy.call( this, source );
  86. return this;
  87. };
  88. THREE.PointCloud.prototype.toJSON = function ( meta ) {
  89. var data = THREE.Object3D.prototype.toJSON.call( this, meta );
  90. // only serialize if not in meta geometries cache
  91. if ( meta.geometries[ this.geometry.uuid ] === undefined ) {
  92. meta.geometries[ this.geometry.uuid ] = this.geometry.toJSON();
  93. }
  94. // only serialize if not in meta materials cache
  95. if ( meta.materials[ this.material.uuid ] === undefined ) {
  96. meta.materials[ this.material.uuid ] = this.material.toJSON();
  97. }
  98. data.object.geometry = this.geometry.uuid;
  99. data.object.material = this.material.uuid;
  100. return data;
  101. };
  102. // Backwards compatibility
  103. THREE.ParticleSystem = function ( geometry, material ) {
  104. console.warn( 'THREE.ParticleSystem has been renamed to THREE.PointCloud.' );
  105. return new THREE.PointCloud( geometry, material );
  106. };
粤ICP备19079148号