Skip to content

Commit c697059

Browse files
authored
Merge pull request #19980 from ianpurvis/ray-es6-class
Ray: Convert to es6 class
2 parents 9fc532f + 56041e6 commit c697059

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/math/Ray.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,40 @@ const _edge1 = new Vector3();
99
const _edge2 = new Vector3();
1010
const _normal = new Vector3();
1111

12-
function Ray( origin, direction ) {
12+
class Ray {
1313

14-
this.origin = ( origin !== undefined ) ? origin : new Vector3();
15-
this.direction = ( direction !== undefined ) ? direction : new Vector3( 0, 0, - 1 );
14+
constructor( origin, direction ) {
1615

17-
}
16+
this.origin = ( origin !== undefined ) ? origin : new Vector3();
17+
this.direction = ( direction !== undefined ) ? direction : new Vector3( 0, 0, - 1 );
1818

19-
Object.assign( Ray.prototype, {
19+
}
2020

21-
set: function ( origin, direction ) {
21+
set( origin, direction ) {
2222

2323
this.origin.copy( origin );
2424
this.direction.copy( direction );
2525

2626
return this;
2727

28-
},
28+
}
2929

30-
clone: function () {
30+
clone() {
3131

3232
return new this.constructor().copy( this );
3333

34-
},
34+
}
3535

36-
copy: function ( ray ) {
36+
copy( ray ) {
3737

3838
this.origin.copy( ray.origin );
3939
this.direction.copy( ray.direction );
4040

4141
return this;
4242

43-
},
43+
}
4444

45-
at: function ( t, target ) {
45+
at( t, target ) {
4646

4747
if ( target === undefined ) {
4848

@@ -53,25 +53,25 @@ Object.assign( Ray.prototype, {
5353

5454
return target.copy( this.direction ).multiplyScalar( t ).add( this.origin );
5555

56-
},
56+
}
5757

58-
lookAt: function ( v ) {
58+
lookAt( v ) {
5959

6060
this.direction.copy( v ).sub( this.origin ).normalize();
6161

6262
return this;
6363

64-
},
64+
}
6565

66-
recast: function ( t ) {
66+
recast( t ) {
6767

6868
this.origin.copy( this.at( t, _vector ) );
6969

7070
return this;
7171

72-
},
72+
}
7373

74-
closestPointToPoint: function ( point, target ) {
74+
closestPointToPoint( point, target ) {
7575

7676
if ( target === undefined ) {
7777

@@ -92,15 +92,15 @@ Object.assign( Ray.prototype, {
9292

9393
return target.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
9494

95-
},
95+
}
9696

97-
distanceToPoint: function ( point ) {
97+
distanceToPoint( point ) {
9898

9999
return Math.sqrt( this.distanceSqToPoint( point ) );
100100

101-
},
101+
}
102102

103-
distanceSqToPoint: function ( point ) {
103+
distanceSqToPoint( point ) {
104104

105105
const directionDistance = _vector.subVectors( point, this.origin ).dot( this.direction );
106106

@@ -116,9 +116,9 @@ Object.assign( Ray.prototype, {
116116

117117
return _vector.distanceToSquared( point );
118118

119-
},
119+
}
120120

121-
distanceSqToSegment: function ( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
121+
distanceSqToSegment( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
122122

123123
// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistRaySegment.h
124124
// It returns the min distance between the ray and the segment
@@ -235,9 +235,9 @@ Object.assign( Ray.prototype, {
235235

236236
return sqrDist;
237237

238-
},
238+
}
239239

240-
intersectSphere: function ( sphere, target ) {
240+
intersectSphere( sphere, target ) {
241241

242242
_vector.subVectors( sphere.center, this.origin );
243243
const tca = _vector.dot( this.direction );
@@ -265,15 +265,15 @@ Object.assign( Ray.prototype, {
265265
// else t0 is in front of the ray, so return the first collision point scaled by t0
266266
return this.at( t0, target );
267267

268-
},
268+
}
269269

270-
intersectsSphere: function ( sphere ) {
270+
intersectsSphere( sphere ) {
271271

272272
return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius );
273273

274-
},
274+
}
275275

276-
distanceToPlane: function ( plane ) {
276+
distanceToPlane( plane ) {
277277

278278
const denominator = plane.normal.dot( this.direction );
279279

@@ -298,9 +298,9 @@ Object.assign( Ray.prototype, {
298298

299299
return t >= 0 ? t : null;
300300

301-
},
301+
}
302302

303-
intersectPlane: function ( plane, target ) {
303+
intersectPlane( plane, target ) {
304304

305305
const t = this.distanceToPlane( plane );
306306

@@ -312,9 +312,9 @@ Object.assign( Ray.prototype, {
312312

313313
return this.at( t, target );
314314

315-
},
315+
}
316316

317-
intersectsPlane: function ( plane ) {
317+
intersectsPlane( plane ) {
318318

319319
// check if the ray lies on the plane first
320320

@@ -338,9 +338,9 @@ Object.assign( Ray.prototype, {
338338

339339
return false;
340340

341-
},
341+
}
342342

343-
intersectBox: function ( box, target ) {
343+
intersectBox( box, target ) {
344344

345345
let tmin, tmax, tymin, tymax, tzmin, tzmax;
346346

@@ -407,15 +407,15 @@ Object.assign( Ray.prototype, {
407407

408408
return this.at( tmin >= 0 ? tmin : tmax, target );
409409

410-
},
410+
}
411411

412-
intersectsBox: function ( box ) {
412+
intersectsBox( box ) {
413413

414414
return this.intersectBox( box, _vector ) !== null;
415415

416-
},
416+
}
417417

418-
intersectTriangle: function ( a, b, c, backfaceCulling, target ) {
418+
intersectTriangle( a, b, c, backfaceCulling, target ) {
419419

420420
// Compute the offset origin, edges, and normal.
421421

@@ -488,24 +488,24 @@ Object.assign( Ray.prototype, {
488488
// Ray intersects triangle.
489489
return this.at( QdN / DdN, target );
490490

491-
},
491+
}
492492

493-
applyMatrix4: function ( matrix4 ) {
493+
applyMatrix4( matrix4 ) {
494494

495495
this.origin.applyMatrix4( matrix4 );
496496
this.direction.transformDirection( matrix4 );
497497

498498
return this;
499499

500-
},
500+
}
501501

502-
equals: function ( ray ) {
502+
equals( ray ) {
503503

504504
return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
505505

506506
}
507507

508-
} );
508+
}
509509

510510

511511
export { Ray };

0 commit comments

Comments
 (0)