Skip to content

Commit e56746c

Browse files
committed
camera ray intersector > screen ray intersector with screen point
1 parent 7983223 commit e56746c

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

packages/pointer-events/src/forward.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Mesh, Object3D, OrthographicCamera, PerspectiveCamera, Scene, Vector2 } from 'three'
1+
import { Object3D, OrthographicCamera, PerspectiveCamera, Scene, Vector2 } from 'three'
22
import { GetCamera, Pointer, PointerOptions } from './pointer.js'
33
import { NativeEvent, NativeWheelEvent, PointerEvent } from './event.js'
4-
import { CameraRayIntersector } from './intersections/ray.js'
4+
import { ScreenRayIntersector } from './intersections/ray.js'
55
import { generateUniquePointerId } from './pointer/index.js'
66
import { IntersectionOptions } from './intersections/index.js'
77

@@ -122,7 +122,7 @@ function forwardEvents(
122122
generateUniquePointerId(),
123123
`${pointerTypePrefix}${event.pointerType}`,
124124
event.pointerState,
125-
new CameraRayIntersector((nativeEvent, coords) => {
125+
new ScreenRayIntersector((nativeEvent, coords) => {
126126
toCoords(nativeEvent, coords)
127127
return getCamera()
128128
}, options),

packages/pointer-events/src/intersections/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Intersection as ThreeIntersection, Quaternion, Vector3, Line3 } from 'three'
1+
import { Intersection as ThreeIntersection, Quaternion, Vector3, Line3, Vector2 } from 'three'
22

33
export type Intersection = ThreeIntersection & {
44
pointerPosition: Vector3
@@ -16,8 +16,15 @@ export type Intersection = ThreeIntersection & {
1616
lineIndex: number
1717
}
1818
| {
19-
type: 'camera-ray'
19+
type: 'screen-ray'
20+
/**
21+
* distance to the near plane of the camera of the screen
22+
*/
2023
distanceViewPlane: number
24+
/**
25+
* point on the screen for x and y from -1 to 1
26+
*/
27+
screenPoint: Vector2
2128
}
2229
| {
2330
type: 'ray'

packages/pointer-events/src/intersections/ray.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class RayIntersector implements Intersector {
143143
}
144144
}
145145

146-
export class CameraRayIntersector implements Intersector {
146+
export class ScreenRayIntersector implements Intersector {
147147
private readonly raycaster = new Raycaster()
148148
private readonly fromPosition = new Vector3()
149149
private readonly fromQuaternion = new Quaternion()
@@ -165,7 +165,7 @@ export class CameraRayIntersector implements Intersector {
165165

166166
public intersectPointerCapture({ intersection, object }: PointerCapture, nativeEvent: unknown): Intersection {
167167
const details = intersection.details
168-
if (details.type != 'camera-ray') {
168+
if (details.type != 'screen-ray') {
169169
throw new Error(
170170
`unable to process a pointer capture of type "${intersection.details.type}" with a camera ray intersector`,
171171
)
@@ -230,7 +230,7 @@ export class CameraRayIntersector implements Intersector {
230230
return voidObjectIntersectionFromRay(
231231
scene,
232232
this.raycaster.ray,
233-
(distance) => ({ type: 'camera-ray', distanceViewPlane: distance }),
233+
(distance) => ({ type: 'screen-ray', distanceViewPlane: distance, screenPoint: this.coords.clone() }),
234234
pointerPosition,
235235
pointerQuaternion,
236236
)
@@ -241,8 +241,9 @@ export class CameraRayIntersector implements Intersector {
241241

242242
return Object.assign(intersection, {
243243
details: {
244-
type: 'camera-ray' as const,
244+
type: 'screen-ray' as const,
245245
distanceViewPlane: this.viewPlane.distanceToPoint(intersection.point),
246+
screenPoint: this.coords.clone(),
246247
},
247248
pointOnFace: intersection.point,
248249
pointerPosition,

0 commit comments

Comments
 (0)