123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { Sphere } from '../math/Sphere';
- import { Ray } from '../math/Ray';
- import { Matrix4 } from '../math/Matrix4';
- import { Object3D } from '../core/Object3D';
- import { Vector3 } from '../math/Vector3';
- import { PointsMaterial } from '../materials/PointsMaterial';
- import { BufferGeometry } from '../core/BufferGeometry';
- /**
- * @author alteredq / http://alteredqualia.com/
- */
- function Points( geometry, material ) {
- Object3D.call( this );
- this.type = 'Points';
- this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
- this.material = material !== undefined ? material : new PointsMaterial( { color: Math.random() * 0xffffff } );
- }
- Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
- constructor: Points,
- isPoints: true,
- raycast: ( function () {
- var inverseMatrix = new Matrix4();
- var ray = new Ray();
- var sphere = new Sphere();
- return function raycast( raycaster, intersects ) {
- var object = this;
- var geometry = this.geometry;
- var matrixWorld = this.matrixWorld;
- var threshold = raycaster.params.Points.threshold;
- // Checking boundingSphere distance to ray
- if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
- sphere.copy( geometry.boundingSphere );
- sphere.applyMatrix4( matrixWorld );
- if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
- //
- inverseMatrix.getInverse( matrixWorld );
- ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
- var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
- var localThresholdSq = localThreshold * localThreshold;
- var position = new Vector3();
- function testPoint( point, index ) {
- var rayPointDistanceSq = ray.distanceSqToPoint( point );
- if ( rayPointDistanceSq < localThresholdSq ) {
- var intersectPoint = ray.closestPointToPoint( point );
- intersectPoint.applyMatrix4( matrixWorld );
- var distance = raycaster.ray.origin.distanceTo( intersectPoint );
- if ( distance < raycaster.near || distance > raycaster.far ) return;
- intersects.push( {
- distance: distance,
- distanceToRay: Math.sqrt( rayPointDistanceSq ),
- point: intersectPoint.clone(),
- index: index,
- face: null,
- object: object
- } );
- }
- }
- if ( (geometry && geometry.isBufferGeometry) ) {
- var index = geometry.index;
- var attributes = geometry.attributes;
- var positions = attributes.position.array;
- if ( index !== null ) {
- var indices = index.array;
- for ( var i = 0, il = indices.length; i < il; i ++ ) {
- var a = indices[ i ];
- position.fromArray( positions, a * 3 );
- testPoint( position, a );
- }
- } else {
- for ( var i = 0, l = positions.length / 3; i < l; i ++ ) {
- position.fromArray( positions, i * 3 );
- testPoint( position, i );
- }
- }
- } else {
- var vertices = geometry.vertices;
- for ( var i = 0, l = vertices.length; i < l; i ++ ) {
- testPoint( vertices[ i ], i );
- }
- }
- };
- }() ),
- clone: function () {
- return new this.constructor( this.geometry, this.material ).copy( this );
- }
- } );
- export { Points };
|