Skip to content

Commit 00a427c

Browse files
authored
SkinnedMesh: Add raycast() method back. (#25957)
1 parent f15c944 commit 00a427c

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/objects/Mesh.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,15 @@ class Mesh extends Object3D {
155155

156156
if ( material === undefined ) return;
157157

158-
// Checking boundingSphere distance to ray
158+
// test with bounding sphere in world space
159159

160160
if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
161161

162162
_sphere.copy( geometry.boundingSphere );
163163
_sphere.applyMatrix4( matrixWorld );
164164

165+
// check distance from ray origin to bounding sphere
166+
165167
_ray.copy( raycaster.ray ).recast( raycaster.near );
166168

167169
if ( _sphere.containsPoint( _ray.origin ) === false ) {
@@ -172,24 +174,26 @@ class Mesh extends Object3D {
172174

173175
}
174176

175-
//
177+
// convert ray to local space of mesh
176178

177179
_inverseMatrix.copy( matrixWorld ).invert();
178180
_ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
179181

180-
// Check boundingBox before continuing
182+
// test with bounding box in local space
181183

182184
if ( geometry.boundingBox !== null ) {
183185

184186
if ( _ray.intersectsBox( geometry.boundingBox ) === false ) return;
185187

186188
}
187189

188-
this._computeIntersections( raycaster, intersects );
190+
// test for intersections with geometry
191+
192+
this._computeIntersections( raycaster, intersects, _ray );
189193

190194
}
191195

192-
_computeIntersections( raycaster, intersects ) {
196+
_computeIntersections( raycaster, intersects, rayLocalSpace ) {
193197

194198
let intersection;
195199

@@ -224,7 +228,7 @@ class Mesh extends Object3D {
224228
const b = index.getX( j + 1 );
225229
const c = index.getX( j + 2 );
226230

227-
intersection = checkGeometryIntersection( this, groupMaterial, raycaster, _ray, uv, uv1, normal, a, b, c );
231+
intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
228232

229233
if ( intersection ) {
230234

@@ -249,7 +253,7 @@ class Mesh extends Object3D {
249253
const b = index.getX( i + 1 );
250254
const c = index.getX( i + 2 );
251255

252-
intersection = checkGeometryIntersection( this, material, raycaster, _ray, uv, uv1, normal, a, b, c );
256+
intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
253257

254258
if ( intersection ) {
255259

@@ -282,7 +286,7 @@ class Mesh extends Object3D {
282286
const b = j + 1;
283287
const c = j + 2;
284288

285-
intersection = checkGeometryIntersection( this, groupMaterial, raycaster, _ray, uv, uv1, normal, a, b, c );
289+
intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
286290

287291
if ( intersection ) {
288292

@@ -307,7 +311,7 @@ class Mesh extends Object3D {
307311
const b = i + 1;
308312
const c = i + 2;
309313

310-
intersection = checkGeometryIntersection( this, material, raycaster, _ray, uv, uv1, normal, a, b, c );
314+
intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
311315

312316
if ( intersection ) {
313317

src/objects/SkinnedMesh.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Matrix4 } from '../math/Matrix4.js';
44
import { Sphere } from '../math/Sphere.js';
55
import { Vector3 } from '../math/Vector3.js';
66
import { Vector4 } from '../math/Vector4.js';
7+
import { Ray } from '../math/Ray.js';
78

89
const _basePosition = /*@__PURE__*/ new Vector3();
910

@@ -14,7 +15,9 @@ const _vector3 = /*@__PURE__*/ new Vector3();
1415
const _matrix4 = /*@__PURE__*/ new Matrix4();
1516
const _vertex = /*@__PURE__*/ new Vector3();
1617

17-
//const _sphere = /*@__PURE__*/ new Sphere();
18+
const _sphere = /*@__PURE__*/ new Sphere();
19+
const _inverseMatrix = /*@__PURE__*/ new Matrix4();
20+
const _ray = /*@__PURE__*/ new Ray();
1821

1922
class SkinnedMesh extends Mesh {
2023

@@ -97,18 +100,40 @@ class SkinnedMesh extends Mesh {
97100

98101
}
99102

100-
// raycast( raycaster, intersects ) {
103+
raycast( raycaster, intersects ) {
101104

102-
// if ( this.boundingSphere === null ) this.computeBoundingSphere();
105+
const material = this.material;
106+
const matrixWorld = this.matrixWorld;
103107

104-
// _sphere.copy( this.boundingSphere );
105-
// _sphere.applyMatrix4( this.matrixWorld );
108+
if ( material === undefined ) return;
106109

107-
// if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
110+
// test with bounding sphere in world space
108111

109-
// this._computeIntersections( raycaster, intersects );
112+
if ( this.boundingSphere === null ) this.computeBoundingSphere();
110113

111-
// }
114+
_sphere.copy( this.boundingSphere );
115+
_sphere.applyMatrix4( matrixWorld );
116+
117+
if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
118+
119+
// convert ray to local space of skinned mesh
120+
121+
_inverseMatrix.copy( matrixWorld ).invert();
122+
_ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
123+
124+
// test with bounding box in local space
125+
126+
if ( this.boundingBox !== null ) {
127+
128+
if ( _ray.intersectsBox( this.boundingBox ) === false ) return;
129+
130+
}
131+
132+
// test for intersections with geometry
133+
134+
this._computeIntersections( raycaster, intersects, _ray );
135+
136+
}
112137

113138
getVertexPosition( index, target ) {
114139

0 commit comments

Comments
 (0)