Skip to content

Commit 61ad2ba

Browse files
authored
Merge pull request #17226 from Mugen87/dev31
Points: Avoid object creation in raycast().
2 parents e6ccda3 + 6e1b8e4 commit 61ad2ba

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/objects/Points.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { BufferGeometry } from '../core/BufferGeometry.js';
1010
* @author alteredq / http://alteredqualia.com/
1111
*/
1212

13-
var _inverseMatrix, _ray, _sphere;
13+
var _inverseMatrix, _ray, _sphere, _position;
1414

1515
function Points( geometry, material ) {
1616

@@ -38,10 +38,10 @@ Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
3838
_inverseMatrix = new Matrix4();
3939
_ray = new Ray();
4040
_sphere = new Sphere();
41+
_position = new Vector3();
4142

4243
}
4344

44-
var object = this;
4545
var geometry = this.geometry;
4646
var matrixWorld = this.matrixWorld;
4747
var threshold = raycaster.params.Points.threshold;
@@ -63,36 +63,6 @@ Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
6363

6464
var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
6565
var localThresholdSq = localThreshold * localThreshold;
66-
var position = new Vector3();
67-
var intersectPoint = new Vector3();
68-
69-
function testPoint( point, index ) {
70-
71-
var rayPointDistanceSq = _ray.distanceSqToPoint( point );
72-
73-
if ( rayPointDistanceSq < localThresholdSq ) {
74-
75-
_ray.closestPointToPoint( point, intersectPoint );
76-
intersectPoint.applyMatrix4( matrixWorld );
77-
78-
var distance = raycaster.ray.origin.distanceTo( intersectPoint );
79-
80-
if ( distance < raycaster.near || distance > raycaster.far ) return;
81-
82-
intersects.push( {
83-
84-
distance: distance,
85-
distanceToRay: Math.sqrt( rayPointDistanceSq ),
86-
point: intersectPoint.clone(),
87-
index: index,
88-
face: null,
89-
object: object
90-
91-
} );
92-
93-
}
94-
95-
}
9666

9767
if ( geometry.isBufferGeometry ) {
9868

@@ -108,19 +78,19 @@ Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
10878

10979
var a = indices[ i ];
11080

111-
position.fromArray( positions, a * 3 );
81+
_position.fromArray( positions, a * 3 );
11282

113-
testPoint( position, a );
83+
testPoint( _position, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
11484

11585
}
11686

11787
} else {
11888

11989
for ( var i = 0, l = positions.length / 3; i < l; i ++ ) {
12090

121-
position.fromArray( positions, i * 3 );
91+
_position.fromArray( positions, i * 3 );
12292

123-
testPoint( position, i );
93+
testPoint( _position, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
12494

12595
}
12696

@@ -132,7 +102,7 @@ Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
132102

133103
for ( var i = 0, l = vertices.length; i < l; i ++ ) {
134104

135-
testPoint( vertices[ i ], i );
105+
testPoint( vertices[ i ], i, localThresholdSq, matrixWorld, raycaster, intersects, this );
136106

137107
}
138108

@@ -194,4 +164,34 @@ Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
194164

195165
} );
196166

167+
function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) {
168+
169+
var rayPointDistanceSq = _ray.distanceSqToPoint( point );
170+
171+
if ( rayPointDistanceSq < localThresholdSq ) {
172+
173+
var intersectPoint = new Vector3();
174+
175+
_ray.closestPointToPoint( point, intersectPoint );
176+
intersectPoint.applyMatrix4( matrixWorld );
177+
178+
var distance = raycaster.ray.origin.distanceTo( intersectPoint );
179+
180+
if ( distance < raycaster.near || distance > raycaster.far ) return;
181+
182+
intersects.push( {
183+
184+
distance: distance,
185+
distanceToRay: Math.sqrt( rayPointDistanceSq ),
186+
point: intersectPoint,
187+
index: index,
188+
face: null,
189+
object: object
190+
191+
} );
192+
193+
}
194+
195+
}
196+
197197
export { Points };

0 commit comments

Comments
 (0)