1
1
import { Vector2 , Vector3 } from 'three'
2
- import type { Intersection , Object3D , Object3DEventMap } from 'three'
2
+ import type { Intersection , Object3D } from 'three'
3
3
import type { Ref , ShallowRef } from 'vue'
4
4
import { computed , onUnmounted , shallowRef } from 'vue'
5
5
import type { EventHook } from '@vueuse/core'
6
6
import { createEventHook , useElementBounding , usePointer } from '@vueuse/core'
7
7
8
+ import type { DomEvent , TresCamera , TresEvent } from 'src/types'
8
9
import type { TresContext } from '../useTresContextProvider'
9
10
10
- export type Intersects = Intersection < Object3D < Object3DEventMap > > [ ]
11
- interface PointerMoveEventPayload {
12
- intersects ?: Intersects
13
- event : PointerEvent
14
- }
15
-
16
- interface PointerClickEventPayload {
17
- intersects : Intersects
18
- event : PointerEvent
19
- }
20
-
21
- interface WheelEventPayload {
22
- intersects : Intersects
23
- event : WheelEvent
24
- }
25
-
26
11
export const useRaycaster = (
27
12
objects : Ref < Object3D [ ] > ,
28
13
ctx : TresContext ,
29
14
) => {
30
15
// having a separate computed makes useElementBounding work
31
16
const canvas = computed ( ( ) => ctx . renderer . value . domElement as HTMLCanvasElement )
32
- const intersects : ShallowRef < Intersects [ ] > = shallowRef ( [ ] )
17
+ const intersects : ShallowRef < Intersection [ ] > = shallowRef ( [ ] )
33
18
const { x, y } = usePointer ( { target : canvas } )
34
19
let delta = 0
35
20
@@ -53,7 +38,7 @@ export const useRaycaster = (
53
38
return intersects . value
54
39
}
55
40
56
- const getIntersects = ( event ?: PointerEvent | MouseEvent | WheelEvent ) => {
41
+ const getIntersects = ( event ?: DomEvent ) => {
57
42
const pointerPosition = getRelativePointerPosition ( {
58
43
x : event ?. clientX ?? x . value ,
59
44
y : event ?. clientY ?? y . value ,
@@ -63,14 +48,14 @@ export const useRaycaster = (
63
48
return getIntersectsByRelativePointerPosition ( pointerPosition ) || [ ]
64
49
}
65
50
66
- const eventHookClick = createEventHook < PointerClickEventPayload > ( )
67
- const eventHookDblClick = createEventHook < PointerClickEventPayload > ( )
68
- const eventHookPointerMove = createEventHook < PointerMoveEventPayload > ( )
69
- const eventHookPointerUp = createEventHook < PointerMoveEventPayload > ( )
70
- const eventHookPointerDown = createEventHook < PointerMoveEventPayload > ( )
71
- const eventHookPointerMissed = createEventHook < PointerClickEventPayload > ( )
72
- const eventHookContextMenu = createEventHook < PointerClickEventPayload > ( )
73
- const eventHookWheel = createEventHook < WheelEventPayload > ( )
51
+ const eventHookClick = createEventHook < TresEvent > ( )
52
+ const eventHookDblClick = createEventHook < TresEvent > ( )
53
+ const eventHookPointerMove = createEventHook < TresEvent > ( )
54
+ const eventHookPointerUp = createEventHook < TresEvent > ( )
55
+ const eventHookPointerDown = createEventHook < TresEvent > ( )
56
+ const eventHookPointerMissed = createEventHook < TresEvent > ( )
57
+ const eventHookContextMenu = createEventHook < TresEvent > ( )
58
+ const eventHookWheel = createEventHook < TresEvent > ( )
74
59
75
60
/* ({
76
61
...DomEvent // All the original event data
@@ -92,19 +77,19 @@ export const useRaycaster = (
92
77
93
78
for ( const property in event ) {
94
79
// Copy all non-function properties
95
- if ( typeof property !== 'function' ) { mouseEventProperties [ property ] = event [ property ] }
80
+ if ( typeof property !== 'function' ) { mouseEventProperties [ property ] = ( event as Record < string , any > ) [ property ] }
96
81
}
97
82
return mouseEventProperties
98
83
}
99
84
100
85
const triggerEventHook = ( eventHook : EventHook , event : PointerEvent | MouseEvent | WheelEvent ) => {
101
86
const eventProperties = copyMouseEventProperties ( event )
102
-
87
+ const unprojectedPoint = new Vector3 ( event ?. clientX , event ?. clientY , 0 ) . unproject ( ctx . camera ?. value as TresCamera )
103
88
eventHook . trigger ( {
104
89
...eventProperties ,
105
90
intersections : intersects . value ,
106
91
// The unprojectedPoint is wrong, math needs to be fixed
107
- unprojectedPoint : new Vector3 ( event ?. clientX , event ?. clientY , 0 ) . unproject ( ctx . camera ?. value ) ,
92
+ unprojectedPoint,
108
93
ray : ctx . raycaster ?. value . ray ,
109
94
camera : ctx . camera ?. value ,
110
95
sourceEvent : event ,
@@ -127,8 +112,8 @@ export const useRaycaster = (
127
112
128
113
// a click event is fired whenever a pointerdown happened after pointerup on the same object
129
114
let mouseDownObject : Object3D | undefined
130
- let mouseDownPosition
131
- let mouseUpPosition
115
+ let mouseDownPosition : Vector2
116
+ let mouseUpPosition : Vector2
132
117
133
118
const onPointerDown = ( event : PointerEvent ) => {
134
119
mouseDownObject = intersects . value [ 0 ] ?. object
@@ -160,7 +145,7 @@ export const useRaycaster = (
160
145
)
161
146
162
147
// Compute the distance between the mouse down and mouse up events
163
- delta = mouseDownPosition . distanceTo ( mouseUpPosition )
148
+ delta = mouseDownPosition ? .distanceTo ( mouseUpPosition )
164
149
165
150
if ( event . button === 0 ) {
166
151
// Left click
@@ -214,14 +199,14 @@ export const useRaycaster = (
214
199
215
200
return {
216
201
intersects,
217
- onClick : ( fn : ( value : PointerClickEventPayload ) => void ) => eventHookClick . on ( fn ) . off ,
218
- onDblClick : ( fn : ( value : PointerClickEventPayload ) => void ) => eventHookDblClick . on ( fn ) . off ,
219
- onContextMenu : ( fn : ( value : PointerClickEventPayload ) => void ) => eventHookContextMenu . on ( fn ) . off ,
220
- onPointerMove : ( fn : ( value : PointerMoveEventPayload ) => void ) => eventHookPointerMove . on ( fn ) . off ,
221
- onPointerUp : ( fn : ( value : PointerMoveEventPayload ) => void ) => eventHookPointerUp . on ( fn ) . off ,
222
- onPointerDown : ( fn : ( value : PointerMoveEventPayload ) => void ) => eventHookPointerDown . on ( fn ) . off ,
223
- onPointerMissed : ( fn : ( value : PointerClickEventPayload ) => void ) => eventHookPointerMissed . on ( fn ) . off ,
224
- onWheel : ( fn : ( value : WheelEventPayload ) => void ) => eventHookWheel . on ( fn ) . off ,
202
+ onClick : ( fn : ( value : TresEvent ) => void ) => eventHookClick . on ( fn ) . off ,
203
+ onDblClick : ( fn : ( value : TresEvent ) => void ) => eventHookDblClick . on ( fn ) . off ,
204
+ onContextMenu : ( fn : ( value : TresEvent ) => void ) => eventHookContextMenu . on ( fn ) . off ,
205
+ onPointerMove : ( fn : ( value : TresEvent ) => void ) => eventHookPointerMove . on ( fn ) . off ,
206
+ onPointerUp : ( fn : ( value : TresEvent ) => void ) => eventHookPointerUp . on ( fn ) . off ,
207
+ onPointerDown : ( fn : ( value : TresEvent ) => void ) => eventHookPointerDown . on ( fn ) . off ,
208
+ onPointerMissed : ( fn : ( value : TresEvent ) => void ) => eventHookPointerMissed . on ( fn ) . off ,
209
+ onWheel : ( fn : ( value : TresEvent ) => void ) => eventHookWheel . on ( fn ) . off ,
225
210
forceUpdate,
226
211
}
227
212
}
0 commit comments