forked from vrmeup/threejs-webxr-hands-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxrInput.js
More file actions
141 lines (123 loc) · 5.32 KB
/
Copy pathxrInput.js
File metadata and controls
141 lines (123 loc) · 5.32 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use strict";
import * as THREE from 'three';
import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
import { XRHandModelFactory } from 'three/addons/webxr/XRHandModelFactory.js';
import { XrMechanicalControllerInput } from './xrMechanicalControllerInput.js' ;
import { XrHandControllerInput } from './xrHandControllerInput.js' ;
import { XrHead } from './xrHead.js' ;
import { Pointer } from './pointer.js';
const PointerActiveColor = new THREE.Color("gray") ;
const PointerPressedColor = new THREE.Color("yellow") ;
/**
* Create the WebXR grip controllers and hand controllers and respond
* to the event to manage the corresponding handler classes.
*/
export class XrInput {
constructor(context)
{
this.context = context ;
this._controllerModelFactory = new XRControllerModelFactory();
this._handModelFactory = new XRHandModelFactory();
this._leftHandController = undefined ;
this._rightHandController = undefined ;
this._head = new XrHead(this.context) ;
const xr = context.renderer.xr
const profile = 'mesh' // 'spheres' | 'boxes' | 'mesh'
this.setupController(0, xr, profile);
this.setupController(1, xr, profile);
this._leftPointer = new Pointer() ;
this.context.scene.add(this._leftPointer) ;
this._rightPointer = new Pointer() ;
this.context.scene.add(this._rightPointer) ;
}
onAnimate() {
this._head.update();
this._leftHandController?.onAnimate();
this._rightHandController?.onAnimate();
this.updateDebugPointers(this._leftPointer, this._leftHandController);
this.updateDebugPointers(this._rightPointer, this._rightHandController);
}
updateDebugPointers(pointer, controller) {
if (!controller || !controller.pointerActive) {
pointer.visible = false ;
return ;
}
pointer.visible = true ;
if(controller.select) {
pointer.material.color = PointerPressedColor ;
} else {
pointer.material.color = PointerActiveColor ;
}
pointer.setFromDir(controller.pointerWOrigin, controller.pointerWDirection);
}
setupController(index, xr, handProfile) {
// Controller
const controllerGrip = xr.getControllerGrip(index);
const controllerModel = this._controllerModelFactory.createControllerModel(controllerGrip);
controllerGrip.add(controllerModel);
const axis = new THREE.AxesHelper(0.2)
controllerModel.add(axis);
this.context.scene.add(controllerGrip);
// Hand
const controllerHand = xr.getHand(index);
const handModel = this._handModelFactory.createHandModel(controllerHand, handProfile) ;
controllerHand.add(handModel);
this.context.scene.add(controllerHand);
// Events
controllerGrip.addEventListener('connected', (event) => this.onControllerConnect(event, controllerGrip, controllerHand));
controllerGrip.addEventListener('disconnected', (event) => this.onControllerDisconnect(event, controllerGrip, controllerHand));
}
onControllerConnect(event, controllerGrip, hand){
const data = event.data ;
this.logData(data) ;
let gamepad = event.data.gamepad ;
if (data.handedness == "right") {
if(data.hand) {
this._rightHandController = new XrHandControllerInput(this.context, hand, gamepad, 'right', this._head);
} else {
this._rightHandController = new XrMechanicalControllerInput(this.context, controllerGrip, gamepad, 'right');
}
this.addEvents(controllerGrip, this._rightHandController);
this._rightHandController.onConnect();
}
if (data.handedness == "left") {
if(data.hand) {
this._leftHandController = new XrHandControllerInput(this.context, hand, gamepad, 'left', this._head);
} else {
this._leftHandController = new XrMechanicalControllerInput(this.context, controllerGrip, gamepad, 'left');
}
this.addEvents(controllerGrip, this._leftHandController);
this._leftHandController.onConnect();
}
}
onControllerDisconnect(event, controllerGrip, hand) {
const data = event.data ;
this.logData(data) ;
if (data.handedness == "right") {
this._rightHandController?.onDisconnect();
this._rightHandController = undefined ;
}
if (data.handedness == "left") {
this._leftHandController?.onDisconnect();
this._leftHandController = undefined;
}
}
logData(data) {
console.info(`Controller ${data.handedness} connected gamepad${data.gamepad?"✔":"❌"} grip${data.gripSpace?"✔":"❌"} hand${data.hand?"✔":"❌"}`)
}
addEvents(controller, hand)
{
controller.addEventListener('selectstart', () => {
hand.select = true;
});
controller.addEventListener('selectend', () => {
hand.select = false;
});
controller.addEventListener('squeezestart', () => {
hand.squeeze = true;
});
controller.addEventListener('squeezeend', () => {
hand.squeeze = false;
});
}
}