forked from pmndrs/xr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXRController.tsx
43 lines (39 loc) · 1.19 KB
/
XRController.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { WebGLRenderer, Group, XRInputSource } from 'three'
export interface XRController {
inputSource: XRInputSource
/**
* Group with orientation that should be used to render virtual
* objects such that they appear to be held in the user’s hand
*/
grip: Group
/** Group with orientation of the preferred pointing ray */
controller: Group
/** Group with hand */
hand: Group
}
export const XRController = {
make: (id: number, gl: WebGLRenderer, onConnected: (c: XRController) => any, onDisconnected: (c: XRController) => any) => {
const controller = gl.xr.getController(id)
const grip = gl.xr.getControllerGrip(id)
const hand = gl.xr.getHand(id)
const xrController: XRController = {
inputSource: undefined as any,
grip,
controller,
hand
}
grip.userData.name = 'grip'
controller.userData.name = 'controller'
hand.userData.name = 'hand'
controller.addEventListener('connected', (event) => {
if (event.fake) {
return
}
xrController.inputSource = event.data
onConnected(xrController)
})
controller.addEventListener('disconnected', (_) => {
onDisconnected(xrController)
})
}
}