PointCloud.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 ( 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 position = new THREE.Vector3();
  28. var testPoint = function ( point, index ) {
  29. var rayPointDistance = ray.distanceToPoint( point );
  30. if ( rayPointDistance < localThreshold ) {
  31. var intersectPoint = ray.closestPointToPoint( point );
  32. intersectPoint.applyMatrix4( object.matrixWorld );
  33. var distance = raycaster.ray.origin.distanceTo( intersectPoint );
  34. if ( distance < raycaster.near || distance > raycaster.far ) return;
  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. offsets.push( {
  53. start: 0,
  54. count: indices.length,
  55. index: 0
  56. } );
  57. }
  58. for ( var oi = 0, ol = offsets.length; oi < ol; ++ oi ) {
  59. var offset = offsets[ oi ];
  60. var start = offset.start;
  61. var count = offset.count;
  62. var index = offset.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. for ( var i = 0, l = positions.length / 3; i < l; i ++ ) {
  71. position.fromArray( positions, i * 3 );
  72. testPoint( position, i );
  73. }
  74. }
  75. } else {
  76. var vertices = geometry.vertices;
  77. for ( var i = 0, l = vertices.length; i < l; i ++ ) {
  78. testPoint( vertices[ i ], i );
  79. }
  80. }
  81. };
  82. }() );
  83. THREE.PointCloud.prototype.clone = function ( object ) {
  84. if ( object === undefined ) object = new THREE.PointCloud( this.geometry, this.material );
  85. THREE.Object3D.prototype.clone.call( this, object );
  86. return object;
  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号