-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViveController.js
119 lines (82 loc) · 3 KB
/
ViveController.js
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
/**
* @author mrdoob / http://mrdoob.com
* @author stewdio / http://stewd.io
*/
class ViveController extends THREE.Object3D{
constructor(id){
super();
this.scope = this;
this.gamepad;
this.id_ = id;
this.axes = [ 0, 0 ];
this.thumbpadIsPressed = false;
this.triggerIsPressed = false;
this.gripsArePressed = false;
this.menuIsPressed = false;
this.matrixAutoUpdate = false;
this.standingMatrix = new THREE.Matrix4();
}
/**
* Iterate across gamepads as Vive Controllers may not be
* in position 0 and 1.
*/
findGamepad( id ) {
var gamepads = navigator.getGamepads && navigator.getGamepads();
for ( var i = 0, j = 0; i < gamepads.length; i ++ ) {
var gamepad = gamepads[ i ];
if ( gamepad && ( gamepad.id === 'OpenVR Gamepad' || gamepad.id.startsWith( 'Oculus Touch' ) || gamepad.id.startsWith( 'Spatial Controller' ) ) ) {
if ( j === this.id_) return gamepad;
j ++;
}
}
}
getGamepad (){
return gamepad;
};
getButtonState( button ) {
if ( button === 'thumbpad' ) return thumbpadIsPressed;
if ( button === 'trigger' ) return triggerIsPressed;
if ( button === 'grips' ) return gripsArePressed;
if ( button === 'menu' ) return menuIsPressed;
};
update (){
var gamepad = this.gamepad;
var scope = this.scope;
gamepad = this.findGamepad( this.id_ );
if ( gamepad !== undefined && gamepad.pose !== undefined ) {
if ( gamepad.pose === null ) return; // No user action yet
// Position and orientation.
var pose = gamepad.pose;
if ( pose.position !== null ) scope.position.fromArray( pose.position );
if ( pose.orientation !== null ) scope.quaternion.fromArray( pose.orientation );
scope.matrix.compose( scope.position, scope.quaternion, scope.scale );
scope.matrix.premultiply( scope.standingMatrix );
scope.matrixWorldNeedsUpdate = true;
scope.visible = true;
// Thumbpad and Buttons.
if ( axes[ 0 ] !== gamepad.axes[ 0 ] || axes[ 1 ] !== gamepad.axes[ 1 ] ) {
axes[ 0 ] = gamepad.axes[ 0 ]; // X axis: -1 = Left, +1 = Right.
axes[ 1 ] = gamepad.axes[ 1 ]; // Y axis: -1 = Bottom, +1 = Top.
scope.dispatchEvent( { type: 'axischanged', axes: axes } );
}
if ( thumbpadIsPressed !== gamepad.buttons[ 0 ].pressed ) {
thumbpadIsPressed = gamepad.buttons[ 0 ].pressed;
scope.dispatchEvent( { type: thumbpadIsPressed ? 'thumbpaddown' : 'thumbpadup', axes: axes } );
}
if ( triggerIsPressed !== gamepad.buttons[ 1 ].pressed ) {
triggerIsPressed = gamepad.buttons[ 1 ].pressed;
scope.dispatchEvent( { type: triggerIsPressed ? 'triggerdown' : 'triggerup' } );
}
if ( gripsArePressed !== gamepad.buttons[ 2 ].pressed ) {
gripsArePressed = gamepad.buttons[ 2 ].pressed;
scope.dispatchEvent( { type: gripsArePressed ? 'gripsdown' : 'gripsup' } );
}
if ( menuIsPressed !== gamepad.buttons[ 3 ].pressed ) {
menuIsPressed = gamepad.buttons[ 3 ].pressed;
scope.dispatchEvent( { type: menuIsPressed ? 'menudown' : 'menuup' } );
}
} else {
scope.visible = false;
}
};
}